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

add support for multiple frozen layer specifing #6017

Closed
wants to merge 5 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
12 changes: 9 additions & 3 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
device,
callbacks
):
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze, = \
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze = \
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze

Expand Down Expand Up @@ -124,7 +124,12 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create

# Freeze
freeze = [f'model.{x}.' for x in range(freeze)] # layers to freeze
if len(freeze) >= 2:
freeze = freeze.split(',')
freeze = [x.replace(" ", "") for x in freeze]
freeze = [f'model.{x}.' for x in range(int(freeze[0]), int(freeze[1]) + 1)] # layers to freeze
else:
freeze = [f'model.{x}.' for x in range(freeze)]
for k, v in model.named_parameters():
v.requires_grad = True # train all layers
if any(x in k for x in freeze):
Expand Down Expand Up @@ -469,7 +474,8 @@ def parse_opt(known=False):
parser.add_argument('--linear-lr', action='store_true', help='linear LR')
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
parser.add_argument('--freeze', type=int, default=0, help='Number of layers to freeze. backbone=10, all=24')
parser.add_argument('--freeze', nargs='+', type=str, default='',
help='Specify the layers that you want to freeze in training. (e.g. 10 represents freeze up to 10, or 1,2,4,6 represents freeze layers 1,2,4,6)')
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify')

Expand Down