File size: 4,686 Bytes
8aa674c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import torch
from .layer_decay_optimizer import build_vit_optimizer
def build_optimizer(cfg, model, logger):
optimizer_type = cfg["type"]
cfg.pop("type")
if optimizer_type == "LayerDecayAdamW":
return build_vit_optimizer(cfg, model, logger)
# set the backbone's optim_groups: SHOULD ONLY CONTAIN BACKBONE PARAMS
if hasattr(model.module, "backbone"): # if backbone exists
if model.module.backbone.freeze_backbone == False: # not frozen
backbone_cfg = cfg["backbone"]
cfg.pop("backbone")
backbone_optim_groups = get_backbone_optim_groups(backbone_cfg, model, logger)
else: # frozen backbone
backbone_optim_groups = []
logger.info(f"Freeze the backbone...")
else:
backbone_optim_groups = []
# set the detector's optim_groups: SHOULD NOT CONTAIN BACKBONE PARAMS
# here, if each method want their own paramwise config, eg. to specify the learning rate,
# weight decay for a certain layer, the model should have a function called get_optim_groups
if "paramwise" in cfg.keys() and cfg["paramwise"]:
cfg.pop("paramwise")
det_optim_groups = model.module.get_optim_groups(cfg)
else:
# optim_groups that does not contain backbone params
detector_params = []
for name, param in model.module.named_parameters():
# exclude the backbone
if name.startswith("backbone"):
continue
detector_params.append(param)
det_optim_groups = [dict(params=detector_params)]
# merge the optim_groups
optim_groups = backbone_optim_groups + det_optim_groups
if optimizer_type == "AdamW":
optimizer = torch.optim.AdamW(optim_groups, **cfg)
elif optimizer_type == "Adam":
optimizer = torch.optim.Adam(optim_groups, **cfg)
elif optimizer_type == "SGD":
optimizer = torch.optim.SGD(optim_groups, **cfg)
else:
raise f"Optimizer {optimizer_type} is not supported so far."
return optimizer
def get_backbone_optim_groups(cfg, model, logger):
"""Example:
backbone = dict(
lr=1e-5,
weight_decay=1e-4,
custom=[dict(name="residual", lr=1e-3, weight_decay=1e-4)],
exclude=[],
)
"""
# custom_name_list
if "custom" in cfg.keys():
custom_name_list = [d["name"] for d in cfg["custom"]]
custom_params_list = [[]] * len(custom_name_list)
else:
custom_name_list = []
# exclude_name_list
if "exclude" in cfg.keys():
exclude_name_list = cfg["exclude"]
else:
exclude_name_list = []
# rest_params_list
rest_params_list = []
name_list = []
# split the backbone parameters into different groups
for name, param in model.module.backbone.named_parameters():
# loop the exclude_name_list
is_exclude = False
if len(exclude_name_list) > 0:
for exclude_name in exclude_name_list:
if exclude_name in name:
is_exclude = True
break
# loop through the custom_name_list
is_custom = False
if len(custom_name_list) > 0:
for i, custom_name in enumerate(custom_name_list):
if custom_name in name:
custom_params_list[i].append(param)
name_list.append(name)
is_custom = True
break
# if is_custom, we have already appended the param to the custom_params_list
# if is _exclude, we do not need to append the param to the rest_params_list
if is_exclude or is_custom:
continue
# this is a rest parameter without special treatment
if not is_custom:
# this is the rest backbone parameters
rest_params_list.append(param)
name_list.append(name)
for name in name_list:
logger.info(f"Backbone parameter: {name}")
# add params to optim_groups
backbone_optim_groups = []
if len(rest_params_list) > 0:
backbone_optim_groups.append(
dict(
params=rest_params_list,
lr=cfg["lr"],
weight_decay=cfg["weight_decay"],
)
)
if len(custom_name_list) > 0:
for i, custom_name in enumerate(custom_name_list):
backbone_optim_groups.append(
dict(
params=custom_params_list[i],
lr=cfg["custom"][i]["lr"],
weight_decay=cfg["custom"][i]["weight_decay"],
)
)
return backbone_optim_groups
|