def _ssd_continuous_metrics(self, predictions, targets, is_cuda=False): def __to_cuda(obj): if is_cuda: obj = obj.cuda() return obj predicted_boxes = predictions['boxes'] target_boxes = targets['boxes'] assert len(predicted_boxes) == len(target_boxes) total_images = len(target_boxes) image_ground_truths = LongTensor([target.size(0) for target in target_boxes]) image_predictions = LongTensor([prediction.size(0) for prediction in predicted_boxes]) continuous_recalls = torch_zeros(total_images, dtype=torch_float) continuous_precisions = torch_zeros(total_images, dtype=torch_float) image_dimensions = __to_cuda(LongTensor([300, 300, 300, 300])) for image_index in range(total_images): if len(target_boxes[image_index]) == 0: continue image_predicted_boxes = (predicted_boxes[image_index] * image_dimensions).tolist() image_target_boxes = (target_boxes[image_index] * image_dimensions).tolist() image_predicted_boxes = [shapely_box(*box) for box in image_predicted_boxes] image_target_boxes = [shapely_box(*box) for box in image_target_boxes] total_predictions = len(image_predicted_boxes) total_targets = len(image_target_boxes) if total_predictions == 0 or total_targets == 0: continue ground_truth_union = image_target_boxes[0] for image_target_box in image_target_boxes[1:]: ground_truth_union = ground_truth_union.union(image_target_box) prediction_union = image_predicted_boxes[0] for image_predicted_box in image_predicted_boxes[1:]: prediction_union = prediction_union.union(image_predicted_box) prediction_ground_truth_intersection = prediction_union.intersection(ground_truth_union) ground_truth_union = ground_truth_union.area prediction_union = prediction_union.area prediction_ground_truth_intersection = prediction_ground_truth_intersection.area continuous_recalls[image_index] = torch_tensor( prediction_ground_truth_intersection / max(ground_truth_union, 1e-10)) continuous_precisions[image_index] = torch_tensor( prediction_ground_truth_intersection / max(prediction_union, 1e-10)) overall_recall = (image_ground_truths * continuous_recalls).sum() / max( image_ground_truths.sum(), torch_tensor(1e-10)) overall_precision = (image_predictions * continuous_precisions).sum() / max( image_predictions.sum(), torch_tensor(1e-10)) return overall_recall.item(), overall_precision.item(), continuous_recalls.tolist(), \ continuous_precisions.tolist()
def _yolo_continuous_metrics(self, predictions, targets): predicted_boxes = predictions['values'] target_boxes = targets['values'] predicted_boxes = [Tensor([box[2:] for box in image]) for image in predicted_boxes] target_boxes = [Tensor([box[1:] for box in image]) for image in target_boxes] assert len(predicted_boxes) == len(target_boxes) total_images = len(target_boxes) image_ground_truths = LongTensor([target.size(0) for target in target_boxes]) image_predictions = LongTensor([prediction.size(0) for prediction in predicted_boxes]) continuous_recalls = torch_zeros(total_images, dtype=torch_float) continuous_precisions = torch_zeros(total_images, dtype=torch_float) for image_index in range(total_images): image_predicted_boxes = (predicted_boxes[image_index]).tolist() image_target_boxes = (target_boxes[image_index]).tolist() image_predicted_boxes = [shapely_box(*box) for box in image_predicted_boxes] image_target_boxes = [shapely_box(*box) for box in image_target_boxes] total_predictions = len(image_predicted_boxes) total_targets = len(image_target_boxes) if total_predictions == 0 or total_targets == 0: continue ground_truth_union = image_target_boxes[0] for image_target_box in image_target_boxes[1:]: ground_truth_union = ground_truth_union.union(image_target_box) prediction_union = image_predicted_boxes[0] for image_predicted_box in image_predicted_boxes[1:]: prediction_union = prediction_union.union(image_predicted_box) prediction_ground_truth_intersection = prediction_union.intersection(ground_truth_union) ground_truth_union = ground_truth_union.area prediction_union = prediction_union.area prediction_ground_truth_intersection = prediction_ground_truth_intersection.area continuous_recalls[image_index] = torch_tensor( prediction_ground_truth_intersection / max(ground_truth_union, 1e-10)) continuous_precisions[image_index] = torch_tensor( prediction_ground_truth_intersection / max(prediction_union, 1e-10)) overall_recall = (image_ground_truths * continuous_recalls).sum() / max( image_ground_truths.sum(), torch_tensor(1e-10)) overall_precision = (image_predictions * continuous_precisions).sum() / max( image_predictions.sum(), torch_tensor(1e-10)) return overall_recall.item(), overall_precision.item(), continuous_recalls.tolist(), \ continuous_precisions.tolist()
def __call__(self, results): in_img = results['img'] in_chars = results['ann_info']['chars'] in_boxes = results['ann_info'][self.box_type] img_width, img_height = in_img.size rotate_center = [img_width / 2., img_height / 2.] tan_temp_max_angle = rotate_center[1] / rotate_center[0] temp_max_angle = np.arctan(tan_temp_max_angle) * 180. / np.pi random_angle = np.random.uniform(max(self.min_angle, -temp_max_angle), min(self.max_angle, temp_max_angle)) random_angle_radian = random_angle * np.pi / 180. img_box = shapely_box(0, 0, img_width, img_height) out_img = TF.rotate(in_img, random_angle, resample=False, expand=False, center=rotate_center) out_boxes, out_chars = self.rotate_bbox(in_boxes, in_chars, random_angle_radian, rotate_center, img_box) results['img'] = out_img results['ann_info']['chars'] = out_chars results['ann_info'][self.box_type] = out_boxes return results
def _plot(image, box, mask, title): plt.title(title, loc="left") plt.axis('off') plt.imshow(image) plt.plot(*shapely_box(*box).exterior.xy) plt.imshow(np.ma.masked_where(mask == 0, mask), alpha=0.3) plt.tight_layout()
def _shape_of_vehicle(self, vehicle_id): p = self._traci_conn.vehicle.getPosition(vehicle_id) length = self._traci_conn.vehicle.getLength(vehicle_id) width = self._traci_conn.vehicle.getWidth(vehicle_id) heading = Heading.from_sumo(self._traci_conn.vehicle.getAngle(vehicle_id)) poly = shapely_box(p[0] - width * 0.5, p[1] - length, p[0] + width * 0.5, p[1],) return shapely_rotate(poly, heading, use_radians=True)
def _shape_of_vehicle(self, sumo_vehicle_state, vehicle_id): p = sumo_vehicle_state[vehicle_id][tc.VAR_POSITION] length = sumo_vehicle_state[vehicle_id][tc.VAR_LENGTH] width = sumo_vehicle_state[vehicle_id][tc.VAR_WIDTH] heading = Heading.from_sumo(sumo_vehicle_state[vehicle_id][tc.VAR_ANGLE]) poly = shapely_box(p[0] - width * 0.5, p[1] - length, p[0] + width * 0.5, p[1],) return shapely_rotate(poly, heading, use_radians=True)
def to_polygon(self) -> Polygon: p = self.pose.position d = self.dimensions poly = shapely_box( p[0] - d.width * 0.5, p[1] - d.length * 0.5, p[0] + d.width * 0.5, p[1] + d.length * 0.5, ) return shapely_rotate(poly, self.pose.heading, use_radians=True)
def to_polygon(self) -> Polygon: """Convert the chassis to a 2D shape.""" p = self.pose.as_position2d() d = self.dimensions poly = shapely_box( p[0] - d.width * 0.5, p[1] - d.length * 0.5, p[0] + d.width * 0.5, p[1] + d.length * 0.5, ) return shapely_rotate(poly, self.pose.heading, use_radians=True)
def __init__(self, imagery_path, building_path, bands=(5, 3, 2), fraction=1.0): super().__init__() self._raster = rasterio.open(imagery_path) self._bands = bands gdf = gpd.read_file(building_path).sample(frac=fraction) # Filter geometries not fully within the raster extent raster_extent = shapely_box(*self._raster.bounds) self._gdf = gdf.loc[gdf.geometry.buffer(0.0005).within( raster_extent)].reset_index() self._image_transforms = transforms.Compose([ transforms.ToTensor(), ]) self._cache_dir = Path("data/cache/")
def _yolo_discrete_metrics(self, predictions, targets, *unused_args, **unused_kwargs): predictions = np.array(predictions['values']) targets = np.array(targets['values']) average_precisions = [] recalls = [] precisions = [] for class_id in range(self.num_classes): total_predicted_images = len(predictions) class_detections = [list(filter( lambda x: x[0] == class_id, predictions[image_index])) for image_index in range(total_predicted_images)] class_targets = [] for i in range(len(targets)): result = [] for target in targets[i]: if target[0] == class_id: result.append(target) class_targets.append(result) class_targets = np.array(class_targets) have_been_detected = [False] * sum([len(ct) for ct in class_targets]) total_class_ground_truth = len(class_targets) total_class_detection = len(class_detections) true_positives = np.zeros(total_class_detection) false_positives = np.zeros(total_class_detection) for image_index, detection in enumerate(class_detections): if len(detection) == 0: continue if len(class_targets[image_index]) == 0: false_positives[image_index] = 1. continue confidences = np.array([float(x[1]) for x in detection]) bounding_boxes = np.array([[float(coord) for coord in x[2:]] for x in detection]) sorted_indices = np.argsort(-confidences) bounding_boxes = bounding_boxes[sorted_indices, :] detected_bounding_boxes = bounding_boxes.astype(float) ground_truth_bounding_boxes = np.array([class_target[1:] for class_target in class_targets[ image_index]]).astype(float) for detected_bounding_box in detected_bounding_boxes: prediction_box_center = shapely_box(*detected_bounding_box).centroid ground_truth_contains_prediction_center = [ shapely_box(*box).contains(prediction_box_center) for box in ground_truth_bounding_boxes] for ind, contains in enumerate(ground_truth_contains_prediction_center): if contains: if not have_been_detected[ind]: true_positives[image_index] = 1. have_been_detected[ind] = True else: false_positives[image_index] = 1. else: false_positives[image_index] = 1. cumul_true_positives = np.cumsum(true_positives) cumul_false_positives = np.cumsum(false_positives) class_recalls = cumul_true_positives / max(float(total_class_ground_truth), np.finfo(np.float64).eps) class_precisions = cumul_true_positives / np.maximum( cumul_true_positives + cumul_false_positives, np.finfo(np.float64).eps) class_average_precision = 0. recall_thresholds = [x / 10 for x in range(11)] for threshold in recall_thresholds: if np.sum(class_recalls >= threshold) == 0: interpolated_precision = 0 else: interpolated_precision = np.max(class_precisions[class_recalls >= threshold]) class_average_precision += interpolated_precision class_average_precision /= len(recall_thresholds) average_precisions.append(class_average_precision) total_true_positives = np.sum(true_positives) recalls.append(total_true_positives / np.maximum(total_class_ground_truth, np.finfo(np.float64).eps)) precisions.append(total_true_positives / np.maximum(total_true_positives + np.sum(false_positives), np.finfo(np.float64).eps)) return average_precisions, recalls, precisions
def _ssd_discrete_metrics(self, predictions, targets, is_cuda=False, *unused_args, **unused_kwargs): def __to_cuda(obj): if is_cuda: obj = obj.cuda() return obj predicted_boxes = predictions['boxes'] predicted_labels = predictions['labels'] predicted_class_scores = predictions['scores'] target_boxes = targets['boxes'] target_labels = targets['labels'] assert len(predicted_boxes) == len(predicted_labels) == len(predicted_class_scores) == len( target_boxes) == len(target_labels) target_images = list() for i in range(len(target_labels)): target_images.extend([i] * target_labels[i].size(0)) target_images = __to_cuda(LongTensor(target_images)) target_boxes = torch_cat(target_boxes, dim=0) target_labels = torch_cat(target_labels, dim=0) assert target_images.size(0) == target_boxes.size(0) == target_labels.size(0) predicted_images = list() for i in range(len(predicted_labels)): predicted_images.extend([i] * predicted_labels[i].size(0)) predicted_images = __to_cuda(LongTensor(predicted_images)) predicted_boxes = torch_cat(predicted_boxes, dim=0) predicted_labels = torch_cat(predicted_labels, dim=0) predicted_class_scores = torch_cat(predicted_class_scores, dim=0) assert predicted_images.size(0) == predicted_boxes.size(0) == predicted_labels.size( 0) == predicted_class_scores.size(0) average_precisions = torch_zeros(self.num_classes, dtype=torch_float) recalls = torch_zeros(self.num_classes, dtype=torch_float) precisions = torch_zeros(self.num_classes, dtype=torch_float) for c in range(self.num_classes): target_class_images = target_images[target_labels == c] target_class_boxes = target_boxes[target_labels == c] total_objects = target_class_boxes.size(0) target_class_boxes_detected = __to_cuda(torch_zeros(total_objects, dtype=torch_uint8)) class_c_predicted_images = predicted_images[predicted_labels == c] class_c_predicted_boxes = predicted_boxes[predicted_labels == c] class_c_predicted_class_scores = predicted_class_scores[predicted_labels == c] class_c_num_detections = class_c_predicted_boxes.size(0) if class_c_num_detections == 0: continue class_c_predicted_class_scores, sort_ind = torch_sort(class_c_predicted_class_scores, dim=0, descending=True) class_c_predicted_images = class_c_predicted_images[sort_ind] class_c_predicted_boxes = class_c_predicted_boxes[sort_ind] true_positives = __to_cuda(torch_zeros(class_c_num_detections, dtype=torch_float)) false_positives = __to_cuda(torch_zeros(class_c_num_detections, dtype=torch_float)) for d in range(class_c_num_detections): this_detection_box = shapely_box(*class_c_predicted_boxes[d].data) this_image = class_c_predicted_images[d] object_boxes = target_class_boxes[target_class_images == this_image] if object_boxes.size(0) == 0: false_positives[d] = 1 continue ground_truth_contains_prediction_center = [ shapely_box(*box.data).contains(this_detection_box.centroid) for box in object_boxes] for ind, prediction_center_in_ground_truth in enumerate(ground_truth_contains_prediction_center): original_ind = LongTensor(range(target_class_boxes.size(0)))[target_class_images == this_image][ind] if prediction_center_in_ground_truth: if target_class_boxes_detected[original_ind] == 0: true_positives[d] = 1 target_class_boxes_detected[original_ind] = 1 else: false_positives[d] = 1 else: false_positives[d] = 1 cumul_true_positives = torch_cumsum(true_positives, dim=0) cumul_false_positives = torch_cumsum(false_positives, dim=0) cumul_precision = cumul_true_positives / (cumul_true_positives + cumul_false_positives + 1e-10) cumul_recall = cumul_true_positives / total_objects recall_thresholds = [x / 10 for x in range(11)] interpolated_precisions = __to_cuda(torch_zeros((len(recall_thresholds)), dtype=torch_float)) for i, threshold in enumerate(recall_thresholds): recalls_above_threshold = cumul_recall >= threshold if recalls_above_threshold.any(): interpolated_precisions[i] = cumul_precision[recalls_above_threshold].max() else: interpolated_precisions[i] = 0. average_precisions[c] = interpolated_precisions.mean() total_true_positives = torch_sum(true_positives) recalls[c] = total_true_positives / max(float(total_objects), 1e-10) precisions[c] = total_true_positives / max( total_true_positives + torch_sum(false_positives), torch_tensor(1e-10)) return average_precisions.tolist(), recalls.tolist(), precisions.tolist()