MCPcopy Create free account
hub / github.com/HYUNJS/SGT / _load_model

Method _load_model

projects/SGT/sgt/checkpointer.py:112–156  ·  view source on GitHub ↗

Load weights from a checkpoint. ckpt trained on this detectron2 framework: checkpoint_state_dict['model'] Official COCO pretrained CenterNet ckpt: checkpoint_state_dict['model']['state_dict'] Args: checkpoint (Any): checkpoint contains th

(self, checkpoint: Any)

Source from the content-addressed store, hash-verified

110 return checkpoint
111
112 def _load_model(self, checkpoint: Any) -> _IncompatibleKeys:
113 """
114 Load weights from a checkpoint.
115
116 ckpt trained on this detectron2 framework: checkpoint_state_dict['model']
117 Official COCO pretrained CenterNet ckpt: checkpoint_state_dict['model']['state_dict']
118
119 Args:
120 checkpoint (Any): checkpoint contains the weights.
121
122 Returns:
123 ``NamedTuple`` with ``missing_keys``, ``unexpected_keys``,
124 and ``incorrect_shapes`` fields:
125 * **missing_keys** is a list of str containing the missing keys
126 * **unexpected_keys** is a list of str containing the unexpected keys
127 * **incorrect_shapes** is a list of (key, shape in checkpoint, shape in model)
128
129 This is just like the return value of
130 :func:`torch.nn.Module.load_state_dict`, but with extra support
131 for ``incorrect_shapes``.
132 """
133 checkpoint_state_dict = checkpoint
134 if 'model' in checkpoint_state_dict:
135 checkpoint_state_dict = checkpoint_state_dict['model']
136 if 'state_dict' in checkpoint_state_dict:
137 checkpoint_state_dict = checkpoint_state_dict['state_dict']
138 model_state_dict = self.model.state_dict()
139 converted_checkpoint_state_dict = self.convert_weight_name(checkpoint_state_dict, model_state_dict)
140
141 checkpoint = {'model': converted_checkpoint_state_dict}
142 incompatible = super()._load_model(checkpoint)
143 if incompatible is None: # support older versions of fvcore
144 return None
145
146 model_buffers = dict(self.model.named_buffers(recurse=False))
147 for k in ["pixel_mean", "pixel_std"]:
148 # Ignore missing key message about pixel_mean/std.
149 # Though they may be missing in old checkpoints, they will be correctly
150 # initialized from config anyway.
151 if k in model_buffers:
152 try:
153 incompatible.missing_keys.remove(k)
154 except ValueError:
155 pass
156 return incompatible
157
158 def convert_weight_name(self, checkpoint_dict, model_state_dict):
159 msg = 'If you see this, your model does not fully load the pre-trained weight'

Callers 1

loadMethod · 0.95

Calls 1

convert_weight_nameMethod · 0.95

Tested by

no test coverage detected