Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#578 Feature/sg 516 support head replacement for local pretrained weights unknown dataset

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-516_support_head_replacement_for_local_pretrained_weights_unknown_dataset
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
  1. import torch
  2. from torchmetrics import MetricCollection
  3. from super_gradients.training.utils.utils import AverageMeter
  4. def get_logging_values(loss_loggings: AverageMeter, metrics: MetricCollection, criterion=None):
  5. """
  6. @param loss_loggings: AverageMeter running average for the loss items
  7. @param metrics: MetricCollection object for running user specified metrics
  8. @param criterion the object loss_loggings average meter is monitoring, when set to None- only the metrics values are
  9. computed and returned.
  10. @return: tuple of the computed values
  11. """
  12. if criterion is not None:
  13. loss_loggingg_avg = loss_loggings.average
  14. if not isinstance(loss_loggingg_avg, tuple):
  15. loss_loggingg_avg = tuple([loss_loggingg_avg])
  16. logging_vals = loss_loggingg_avg + get_metrics_results_tuple(metrics)
  17. else:
  18. logging_vals = get_metrics_results_tuple(metrics)
  19. return logging_vals
  20. def get_metrics_titles(metrics_collection: MetricCollection):
  21. """
  22. @param metrics_collection: MetricCollection object for running user specified metrics
  23. @return: list of all the names of the computed values list(str)
  24. """
  25. titles = []
  26. for metric_name, metric in metrics_collection.items():
  27. if metric_name == "additional_items":
  28. continue
  29. elif hasattr(metric, "component_names"):
  30. titles += metric.component_names
  31. else:
  32. titles.append(metric_name)
  33. return titles
  34. def get_metrics_results_tuple(metrics_collection: MetricCollection):
  35. """
  36. @param metrics_collection: metrics collection of the user specified metrics
  37. @type metrics_collection
  38. @return: tuple of metrics values
  39. """
  40. if metrics_collection is None:
  41. results_tuple = ()
  42. else:
  43. results_tuple = tuple(flatten_metrics_dict(metrics_collection.compute()).values())
  44. return results_tuple
  45. def flatten_metrics_dict(metrics_dict: dict):
  46. """
  47. :param metrics_dict - dictionary of metric values where values can also be dictionaries containing subvalues
  48. (in the case of compound metrics)
  49. @return: flattened dict of metric values i.e {metric1_name: metric1_value...}
  50. """
  51. flattened = {}
  52. for metric_name, metric_val in metrics_dict.items():
  53. if metric_name == "additional_items":
  54. continue
  55. # COLLECT ALL OF THE COMPONENTS IN THE CASE OF COMPOUND METRICS
  56. elif isinstance(metric_val, dict):
  57. for sub_metric_name, sub_metric_val in metric_val.items():
  58. flattened[sub_metric_name] = sub_metric_val
  59. else:
  60. flattened[metric_name] = metric_val
  61. return flattened
  62. def get_metrics_dict(metrics_tuple, metrics_collection, loss_logging_item_names):
  63. """
  64. Returns a dictionary with the epoch results as values and their names as keys.
  65. @param metrics_tuple: the result tuple
  66. @param metrics_collection: MetricsCollection
  67. @param loss_logging_item_names: loss component's names.
  68. @return: dict
  69. """
  70. keys = loss_logging_item_names + get_metrics_titles(metrics_collection)
  71. metrics_dict = dict(zip(keys, list(metrics_tuple)))
  72. return metrics_dict
  73. def get_train_loop_description_dict(metrics_tuple, metrics_collection, loss_logging_item_names, **log_items):
  74. """
  75. Returns a dictionary with the epoch's logging items as values and their names as keys, with the purpose of
  76. passing it as a description to tqdm's progress bar.
  77. @param metrics_tuple: the result tuple
  78. @param metrics_collection: MetricsCollection
  79. @param loss_logging_item_names: loss component's names.
  80. @param log_items additional logging items to be rendered.
  81. @return: dict
  82. """
  83. log_items.update(get_metrics_dict(metrics_tuple, metrics_collection, loss_logging_item_names))
  84. for key, value in log_items.items():
  85. if isinstance(value, torch.Tensor):
  86. log_items[key] = value.detach().item()
  87. return log_items
Discard
Tip!

Press p or to see the previous file or, n or to see the next file