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

Make it easier to do transfer learning by adding --freeze as an option #3685

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion train.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
test_path = data_dict['val']

# Freeze
freeze = [] # parameter names to freeze (full or partial)
freeze = ['model.%s.' % x for x in range(opt.freeze + 1)] if opt.freeze is not None else [] # parameter names to freeze (full or partial)
for k, v in model.named_parameters():
v.requires_grad = True # train all layers
if any(x in k for x in freeze):
print('freezing %s' % k)
v.requires_grad = False
elif freeze:
print('not freezing %s' % k)

# Optimizer
nbs = 64 # nominal batch size
Expand Down Expand Up @@ -495,6 +497,7 @@ def parse_opt(known=False):
parser.add_argument('--notest', action='store_true', help='only test final epoch')
parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check')
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
parser.add_argument('--freeze', type=int, help='freeze layers upto and including layer number')
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
parser.add_argument('--cache-images', action='store_true', help='cache images for faster training')
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
Expand Down