Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretrained selection #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions classification/detect_from_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ def test_full_image_network(video_path, model_path, output_path,
face_detector = dlib.get_frontal_face_detector()

# Load model
model, *_ = model_selection(modelname='xception', num_out_classes=2)
pretrained = (model_path is None)
model, *_ = model_selection(modelname='xception', num_out_classes=2, pretrained=pretrained)
if model_path is not None:
model = torch.load(model_path)
print('Model found in {}'.format(model_path))
else:
print('No model found, initializing random model.')
print('No model found, using pretrained model.')
if cuda:
model = model.cuda()

Expand Down Expand Up @@ -221,7 +222,7 @@ def test_full_image_network(video_path, model_path, output_path,
p = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument('--video_path', '-i', type=str)
p.add_argument('--model_path', '-mi', type=str, default=None)
p.add_argument('--model_path', '-m', type=str, default=None)
p.add_argument('--output_path', '-o', type=str,
default='.')
p.add_argument('--start_frame', type=int, default=0)
Expand Down
21 changes: 12 additions & 9 deletions classification/network/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class TransferModel(nn.Module):
Simple transfer learning model that takes an imagenet pretrained model with
a fc layer as base model and retrains a new fc layer for num_out_classes
"""
def __init__(self, modelchoice, num_out_classes=2, dropout=0.0):
def __init__(self, modelchoice, num_out_classes=2, dropout=0.0, pretrained=True):
super(TransferModel, self).__init__()
self.modelchoice = modelchoice
if modelchoice == 'xception':
self.model = return_pytorch04_xception()
self.model = return_pytorch04_xception(pretrained)
# Replace fc
num_ftrs = self.model.last_linear.in_features
if not dropout:
Expand All @@ -55,9 +55,9 @@ def __init__(self, modelchoice, num_out_classes=2, dropout=0.0):
)
elif modelchoice == 'resnet50' or modelchoice == 'resnet18':
if modelchoice == 'resnet50':
self.model = torchvision.models.resnet50(pretrained=True)
self.model = torchvision.models.resnet50(pretrained=pretrained)
if modelchoice == 'resnet18':
self.model = torchvision.models.resnet18(pretrained=True)
self.model = torchvision.models.resnet18(pretrained=pretrained)
# Replace fc
num_ftrs = self.model.fc.in_features
if not dropout:
Expand Down Expand Up @@ -116,18 +116,21 @@ def forward(self, x):


def model_selection(modelname, num_out_classes,
dropout=None):
dropout=None, pretrained=True):
"""
:param modelname:
:return: model, image size, pretraining<yes/no>, input_list
"""
if modelname == 'xception':
return TransferModel(modelchoice='xception',
num_out_classes=num_out_classes), 299, \
True, ['image'], None
num_out_classes=num_out_classes,
pretrained=pretrained), \
299, True, ['image'], None
elif modelname == 'resnet18':
return TransferModel(modelchoice='resnet18', dropout=dropout,
num_out_classes=num_out_classes), \
return TransferModel(modelchoice='resnet18',
dropout=dropout,
num_out_classes=num_out_classes,
pretrained=pretrained), \
224, True, ['image'], None
else:
raise NotImplementedError(modelname)
Expand Down