реализуй класс на python, который подсчитывают метрику mAP
- User
реализуй класс на python, который подсчитывают метрику mAP
Assistant
Вот пример реализации класса на Python, который подсчитывает метрику mAP (mean Average Precision) для задач объектного обнаружения:
pythonimport numpy as np class MAPMetric: def __init__(self): self.reset() def reset(self): self.true_positives = 0 self.false_positives = 0 self.true_negatives = 0 def update(self, predictions, labels): predictions = np.array(predictions) labels = np.array(labels) # Обработка прогнозов и меток для подсчета TP, FP и TN predicted_boxes = predictions[:, 4:] label_boxes = labels[:, 4:] scores = predictions[:, 0] labels_binary = labels[:, -1].astype(int) # Фильтруем прогнозы по пороговому значению уверенности threshold = 0.5 above_threshold_indices = scores > threshold filtered_predictions = predicted_boxes[above_threshold_indices] # Подсчет TP, FP и TN np.testing.assert_array_less(filtered_predictions, 0) intersection_area = self.compute_iou(filtered_predictions, label_boxes) matches = np.argmax(intersection_area, axis=1) matches = matches[matches != -1] self.true_positives += len(matches) self.false_positives += len(above_threshold_indices) self.true_negatives += np.sum(labels_binary == 0) def compute_iou(self, boxes1, boxes2): # Вычисление IOU (Intersection over Union) между двумя наборами bounding boxes # ... реализации функции compute_iou raise NotImplementedError("Function compute_iou not implemented") def get_map(self): # Подсчет mAP precision = self.precision() recall = self.recall() ap = self.compute_ap(precision, recall) return ap def precision(self): # Подсчет точности if self.true_positives + self.false_positives == 0: return 0.0 return self.true_positives / (self.true_positives + self.false_positives) def recall(self): # Подсчет отдачи if self.true_positives + self.true_negatives == 0: return 0.0 return self.true_positives / (self.true_positives + self.true_negatives) def compute_ap(self, precision, recall): # Вычисление Average Precision m = len(precision) ap = 0.0 recall_copy = recall.copy() recall_copy.append(0) precision_copy = precision.copy() precision_copy.append(0) for i in range(m): ap += (recall_copy[i+1] - recall_copy[i]) * precision_copy[i] return ap / m def __str__(self): return f"mAP: {self.get_map()}"
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.