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

#378 Feature/sg 281 add kd notebook

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-281-add_kd_notebook
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  1. from typing import Dict, Optional, Union
  2. import torch
  3. from torchmetrics import Metric
  4. import super_gradients
  5. from super_gradients.training.utils import tensor_container_to_device
  6. from super_gradients.training.utils.detection_utils import compute_detection_matching, compute_detection_metrics
  7. from super_gradients.training.utils.detection_utils import DetectionPostPredictionCallback, IouThreshold
  8. from super_gradients.common.abstractions.abstract_logger import get_logger
  9. logger = get_logger(__name__)
  10. class DetectionMetrics(Metric):
  11. """
  12. DetectionMetrics
  13. Metric class for computing F1, Precision, Recall and Mean Average Precision.
  14. Attributes:
  15. num_cls: Number of classes.
  16. post_prediction_callback: DetectionPostPredictionCallback to be applied on net's output prior
  17. to the metric computation (NMS).
  18. normalize_targets: Whether to normalize bbox coordinates by image size (default=False).
  19. iou_thresholds: IoU threshold to compute the mAP (default=torch.linspace(0.5, 0.95, 10)).
  20. recall_thresholds: Recall threshold to compute the mAP (default=torch.linspace(0, 1, 101)).
  21. score_threshold: Score threshold to compute Recall, Precision and F1 (default=0.1)
  22. top_k_predictions: Number of predictions per class used to compute metrics, ordered by confidence score
  23. (default=100)
  24. dist_sync_on_step: Synchronize metric state across processes at each ``forward()``
  25. before returning the value at the step. (default=False)
  26. accumulate_on_cpu: Run on CPU regardless of device used in other parts.
  27. This is to avoid "CUDA out of memory" that might happen on GPU (default False)
  28. """
  29. def __init__(self, num_cls: int,
  30. post_prediction_callback: DetectionPostPredictionCallback = None,
  31. normalize_targets: bool = False,
  32. iou_thres: IouThreshold = IouThreshold.MAP_05_TO_095,
  33. recall_thres: torch.Tensor = None,
  34. score_thres: float = 0.1,
  35. top_k_predictions: int = 100,
  36. dist_sync_on_step: bool = False,
  37. accumulate_on_cpu: bool = True):
  38. super().__init__(dist_sync_on_step=dist_sync_on_step)
  39. self.num_cls = num_cls
  40. self.iou_thres = iou_thres
  41. self.map_str = 'mAP@%.1f' % iou_thres[0] if not iou_thres.is_range() else 'mAP@%.2f:%.2f' % iou_thres
  42. self.component_names = ["Precision", "Recall", self.map_str, "F1"]
  43. self.components = len(self.component_names)
  44. self.post_prediction_callback = post_prediction_callback
  45. self.is_distributed = super_gradients.is_distributed()
  46. self.denormalize_targets = not normalize_targets
  47. self.world_size = None
  48. self.rank = None
  49. self.add_state("matching_info", default=[], dist_reduce_fx=None)
  50. self.iou_thresholds = iou_thres.to_tensor()
  51. self.recall_thresholds = torch.linspace(0, 1, 101) if recall_thres is None else recall_thres
  52. self.score_threshold = score_thres
  53. self.top_k_predictions = top_k_predictions
  54. self.accumulate_on_cpu = accumulate_on_cpu
  55. def update(self, preds, target: torch.Tensor, device: str,
  56. inputs: torch.tensor, crowd_targets: Optional[torch.Tensor] = None):
  57. """
  58. Apply NMS and match all the predictions and targets of a given batch, and update the metric state accordingly.
  59. :param preds : Raw output of the model, the format might change from one model to another, but has to fit
  60. the input format of the post_prediction_callback
  61. :param target: Targets for all images of shape (total_num_targets, 6)
  62. format: (index, x, y, w, h, label) where x,y,w,h are in range [0,1]
  63. :param device: Device to run on
  64. :param inputs: Input image tensor of shape (batch_size, n_img, height, width)
  65. :param crowd_targets: Crowd targets for all images of shape (total_num_targets, 6)
  66. format: (index, x, y, w, h, label) where x,y,w,h are in range [0,1]
  67. """
  68. self.iou_thresholds = self.iou_thresholds.to(device)
  69. _, _, height, width = inputs.shape
  70. targets = target.clone()
  71. crowd_targets = torch.zeros(size=(0, 6), device=device) if crowd_targets is None else crowd_targets.clone()
  72. preds = self.post_prediction_callback(preds, device=device)
  73. new_matching_info = compute_detection_matching(
  74. preds, targets, height, width, self.iou_thresholds, crowd_targets=crowd_targets,
  75. top_k=self.top_k_predictions, denormalize_targets=self.denormalize_targets,
  76. device=self.device, return_on_cpu=self.accumulate_on_cpu)
  77. accumulated_matching_info = getattr(self, "matching_info")
  78. setattr(self, "matching_info", accumulated_matching_info + new_matching_info)
  79. def compute(self) -> Dict[str, Union[float, torch.Tensor]]:
  80. """Compute the metrics for all the accumulated results.
  81. :return: Metrics of interest
  82. """
  83. mean_ap, mean_precision, mean_recall, mean_f1 = -1., -1., -1., -1.
  84. accumulated_matching_info = getattr(self, "matching_info")
  85. if len(accumulated_matching_info):
  86. matching_info_tensors = [torch.cat(x, 0) for x in list(zip(*accumulated_matching_info))]
  87. # shape (n_class, nb_iou_thresh)
  88. ap, precision, recall, f1, unique_classes = compute_detection_metrics(
  89. *matching_info_tensors, recall_thresholds=self.recall_thresholds, score_threshold=self.score_threshold,
  90. device="cpu" if self.accumulate_on_cpu else self.device)
  91. # Precision, recall and f1 are computed for smallest IoU threshold (usually 0.5), averaged over classes
  92. mean_precision, mean_recall, mean_f1 = precision[:, 0].mean(), recall[:, 0].mean(), f1[:, 0].mean()
  93. # MaP is averaged over IoU thresholds and over classes
  94. mean_ap = ap.mean()
  95. return {"Precision": mean_precision, "Recall": mean_recall, self.map_str: mean_ap, "F1": mean_f1}
  96. def _sync_dist(self, dist_sync_fn=None, process_group=None):
  97. """
  98. When in distributed mode, stats are aggregated after each forward pass to the metric state. Since these have all
  99. different sizes we override the synchronization function since it works only for tensors (and use
  100. all_gather_object)
  101. @param dist_sync_fn:
  102. @return:
  103. """
  104. if self.world_size is None:
  105. self.world_size = torch.distributed.get_world_size() if self.is_distributed else -1
  106. if self.rank is None:
  107. self.rank = torch.distributed.get_rank() if self.is_distributed else -1
  108. if self.is_distributed:
  109. local_state_dict = {attr: getattr(self, attr) for attr in self._reductions.keys()}
  110. gathered_state_dicts = [None] * self.world_size
  111. torch.distributed.barrier()
  112. torch.distributed.all_gather_object(gathered_state_dicts, local_state_dict)
  113. matching_info = []
  114. for state_dict in gathered_state_dicts:
  115. matching_info += state_dict["matching_info"]
  116. matching_info = tensor_container_to_device(matching_info, device="cpu" if self.accumulate_on_cpu else self.device)
  117. setattr(self, "matching_info", matching_info)
Discard
Tip!

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