Load the checkpoint from the given file. If inflation is True, inflate the 2D Conv weights from the checkpoint to 3D Conv. Args: path_to_checkpoint (string): path to the checkpoint to load. model (model): model to load the weights from the checkpoint. loss_scaler
(
path_to_checkpoint,
model,
loss_scaler=None,
data_parallel=True,
optimizer=None,
inflation=False,
convert_from_caffe2=False,
epoch_reset=False,
clear_name_pattern=(),
)
| 189 | |
| 190 | |
| 191 | def load_checkpoint( |
| 192 | path_to_checkpoint, |
| 193 | model, |
| 194 | loss_scaler=None, |
| 195 | data_parallel=True, |
| 196 | optimizer=None, |
| 197 | inflation=False, |
| 198 | convert_from_caffe2=False, |
| 199 | epoch_reset=False, |
| 200 | clear_name_pattern=(), |
| 201 | ): |
| 202 | """ |
| 203 | Load the checkpoint from the given file. If inflation is True, inflate the |
| 204 | 2D Conv weights from the checkpoint to 3D Conv. |
| 205 | Args: |
| 206 | path_to_checkpoint (string): path to the checkpoint to load. |
| 207 | model (model): model to load the weights from the checkpoint. |
| 208 | loss_scaler (scaler): scaler for loss. |
| 209 | data_parallel (bool): if true, model is wrapped by |
| 210 | torch.nn.parallel.DistributedDataParallel. |
| 211 | optimizer (optim): optimizer to load the historical state. |
| 212 | inflation (bool): if True, inflate the weights from the checkpoint. |
| 213 | convert_from_caffe2 (bool): if True, load the model from caffe2 and |
| 214 | convert it to pytorch. |
| 215 | epoch_reset (bool): if True, reset #train iterations from the checkpoint. |
| 216 | clear_name_pattern (string): if given, this (sub)string will be cleared |
| 217 | from a layer name if it can be matched. |
| 218 | Returns: |
| 219 | (int): the number of training epoch of the checkpoint. |
| 220 | """ |
| 221 | assert g_pathmgr.exists( |
| 222 | path_to_checkpoint |
| 223 | ), "Checkpoint '{}' not found".format(path_to_checkpoint) |
| 224 | logger.info("Loading network weights from {}.".format(path_to_checkpoint)) |
| 225 | |
| 226 | # Account for the DDP wrapper in the multi-gpu setting. |
| 227 | ms = model.module if data_parallel else model |
| 228 | if convert_from_caffe2: |
| 229 | with g_pathmgr.open(path_to_checkpoint, "rb") as f: |
| 230 | caffe2_checkpoint = pickle.load(f, encoding="latin1") |
| 231 | state_dict = OrderedDict() |
| 232 | name_convert_func = get_name_convert_func() |
| 233 | for key in caffe2_checkpoint["blobs"].keys(): |
| 234 | converted_key = name_convert_func(key) |
| 235 | converted_key = c2_normal_to_sub_bn(converted_key, ms.state_dict()) |
| 236 | if converted_key in ms.state_dict(): |
| 237 | c2_blob_shape = caffe2_checkpoint["blobs"][key].shape |
| 238 | model_blob_shape = ms.state_dict()[converted_key].shape |
| 239 | |
| 240 | # expand shape dims if they differ (eg for converting linear to conv params) |
| 241 | if len(c2_blob_shape) < len(model_blob_shape): |
| 242 | c2_blob_shape += (1,) * ( |
| 243 | len(model_blob_shape) - len(c2_blob_shape) |
| 244 | ) |
| 245 | caffe2_checkpoint["blobs"][key] = np.reshape( |
| 246 | caffe2_checkpoint["blobs"][key], c2_blob_shape |
| 247 | ) |
| 248 | # Load BN stats to Sub-BN. |
no test coverage detected