MCPcopy Create free account
hub / github.com/DanielShalam/BPA / load_weights

Function load_weights

utils.py:191–223  ·  view source on GitHub ↗

Load pretrained weights from given path.

(model: torch.nn.Module, pretrained_path: str)

Source from the content-addressed store, hash-verified

189
190
191def load_weights(model: torch.nn.Module, pretrained_path: str):
192 """
193 Load pretrained weights from given path.
194 """
195 if not pretrained_path:
196 return model
197
198 print(f'Loading weights from {pretrained_path}')
199 state_dict = torch.load(pretrained_path)
200 sd_keys = list(state_dict.keys())
201 if 'state' in sd_keys:
202 state_dict = state_dict['state']
203 for k in list(state_dict.keys()):
204 if k.startswith('module.'):
205 state_dict["{}".format(k[len('module.'):])] = state_dict[k]
206 del state_dict[k]
207
208 model.load_state_dict(state_dict, strict=False)
209
210 elif 'params' in sd_keys:
211 state_dict = state_dict['params']
212 for k in list(state_dict.keys()):
213 if k.startswith('encoder.'):
214 state_dict["{}".format(k[len('encoder.'):])] = state_dict[k]
215
216 del state_dict[k]
217
218 model.load_state_dict(state_dict, strict=True)
219 else:
220 model.load_state_dict(state_dict)
221
222 print("Weights loaded successfully ")
223 return model
224
225
226def get_fs_labels(method: str, num_way: int, num_query: int, num_shot: int):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected