Safemotion Lib
Loading...
Searching...
No Matches
Classes | Functions
fastreid.utils.checkpoint Namespace Reference

Classes

class  Checkpointer
 
class  PeriodicCheckpointer
 

Functions

 get_missing_parameters_message (list keys)
 
 get_unexpected_parameters_message (list keys)
 
 _strip_prefix_if_present (collections.OrderedDict state_dict, str prefix)
 
 _group_checkpoint_keys (list keys)
 
 _group_to_str (list group)
 

Function Documentation

◆ _group_checkpoint_keys()

fastreid.utils.checkpoint._group_checkpoint_keys ( list keys)
protected
Group keys based on common prefixes. A prefix is the string up to the final
"." in each key.
Args:
    keys (list[str]): list of parameter names, i.e. keys in the model
        checkpoint dict.
Returns:
    dict[list]: keys with common prefixes are grouped into lists.

Definition at line 369 of file checkpoint.py.

369def _group_checkpoint_keys(keys: list):
370 """
371 Group keys based on common prefixes. A prefix is the string up to the final
372 "." in each key.
373 Args:
374 keys (list[str]): list of parameter names, i.e. keys in the model
375 checkpoint dict.
376 Returns:
377 dict[list]: keys with common prefixes are grouped into lists.
378 """
379 groups = defaultdict(list)
380 for key in keys:
381 pos = key.rfind(".")
382 if pos >= 0:
383 head, tail = key[:pos], [key[pos + 1:]]
384 else:
385 head, tail = key, []
386 groups[head].extend(tail)
387 return groups
388
389

◆ _group_to_str()

fastreid.utils.checkpoint._group_to_str ( list group)
protected
Format a group of parameter name suffixes into a loggable string.
Args:
    group (list[str]): list of parameter name suffixes.
Returns:
    str: formated string.

Definition at line 390 of file checkpoint.py.

390def _group_to_str(group: list):
391 """
392 Format a group of parameter name suffixes into a loggable string.
393 Args:
394 group (list[str]): list of parameter name suffixes.
395 Returns:
396 str: formated string.
397 """
398 if len(group) == 0:
399 return ""
400
401 if len(group) == 1:
402 return "." + group[0]
403
404 return ".{" + ", ".join(group) + "}"

◆ _strip_prefix_if_present()

fastreid.utils.checkpoint._strip_prefix_if_present ( collections.OrderedDict state_dict,
str prefix )
protected
Strip the prefix in metadata, if any.
Args:
    state_dict (OrderedDict): a state-dict to be loaded to the model.
    prefix (str): prefix.

Definition at line 336 of file checkpoint.py.

336def _strip_prefix_if_present(state_dict: collections.OrderedDict, prefix: str):
337 """
338 Strip the prefix in metadata, if any.
339 Args:
340 state_dict (OrderedDict): a state-dict to be loaded to the model.
341 prefix (str): prefix.
342 """
343 keys = sorted(state_dict.keys())
344 if not all(len(key) == 0 or key.startswith(prefix) for key in keys):
345 return
346
347 for key in keys:
348 newkey = key[len(prefix):]
349 state_dict[newkey] = state_dict.pop(key)
350
351 # also strip the prefix in metadata, if any..
352 try:
353 metadata = state_dict._metadata
354 except AttributeError:
355 pass
356 else:
357 for key in list(metadata.keys()):
358 # for the metadata dict, the key can be:
359 # '': for the DDP module, which we want to remove.
360 # 'module': for the actual model.
361 # 'module.xx.xx': for the rest.
362
363 if len(key) == 0:
364 continue
365 newkey = key[len(prefix):]
366 metadata[newkey] = metadata.pop(key)
367
368

◆ get_missing_parameters_message()

fastreid.utils.checkpoint.get_missing_parameters_message ( list keys)
Get a logging-friendly message to report parameter names (keys) that are in
the model but not found in a checkpoint.
Args:
    keys (list[str]): List of keys that were not found in the checkpoint.
Returns:
    str: message.

Definition at line 301 of file checkpoint.py.

301def get_missing_parameters_message(keys: list):
302 """
303 Get a logging-friendly message to report parameter names (keys) that are in
304 the model but not found in a checkpoint.
305 Args:
306 keys (list[str]): List of keys that were not found in the checkpoint.
307 Returns:
308 str: message.
309 """
310 groups = _group_checkpoint_keys(keys)
311 msg = "Some model parameters are not in the checkpoint:\n"
312 msg += "\n".join(
313 " " + colored(k + _group_to_str(v), "blue") for k, v in groups.items()
314 )
315 return msg
316
317

◆ get_unexpected_parameters_message()

fastreid.utils.checkpoint.get_unexpected_parameters_message ( list keys)
Get a logging-friendly message to report parameter names (keys) that are in
the checkpoint but not found in the model.
Args:
    keys (list[str]): List of keys that were not found in the model.
Returns:
    str: message.

Definition at line 318 of file checkpoint.py.

318def get_unexpected_parameters_message(keys: list):
319 """
320 Get a logging-friendly message to report parameter names (keys) that are in
321 the checkpoint but not found in the model.
322 Args:
323 keys (list[str]): List of keys that were not found in the model.
324 Returns:
325 str: message.
326 """
327 groups = _group_checkpoint_keys(keys)
328 msg = "The checkpoint contains parameters not used by the model:\n"
329 msg += "\n".join(
330 " " + colored(k + _group_to_str(v), "magenta")
331 for k, v in groups.items()
332 )
333 return msg
334
335