def generate_anchors(self, image_shape): anchor_params = None if self.config and 'anchor_parameters' in self.config: anchor_params = parse_anchor_parameters(self.config) return anchors_for_shape(image_shape, anchor_params=anchor_params, shapes_callback=self.compute_shapes)
def average_overlap(values, entries, state, image_shape, mode='focal', ratio_count=3, include_stride=False): anchor_params = calculate_config(values, ratio_count) if include_stride: anchors = anchors_for_shape(image_shape, anchor_params=anchor_params) else: anchors = base_anchors_for_shape(anchor_params=anchor_params) overlap = compute_overlap(entries, anchors) max_overlap = np.amax(overlap, axis=1) not_matched = len(np.where(max_overlap < 0.5)[0]) if mode == 'avg': result = 1 - np.average(max_overlap) elif mode == 'ce': result = np.average(-np.log(max_overlap)) elif mode == 'focal': result = np.average(-(1 - max_overlap) ** 2 * np.log(max_overlap)) else: raise Exception('Invalid mode.') if result < state['best_result']: state['best_result'] = result print('Current best anchor configuration') print(f'Ratios: {sorted(np.round(anchor_params.ratios, 3))}') print(f'Scales: {sorted(np.round(anchor_params.scales, 3))}') if include_stride: print(f'Average overlap: {np.round(np.average(max_overlap), 3)}') print(f'Number of labels that don\'t have any matching anchor: {not_matched}') print() return result, not_matched
def test_anchors_for_shape_odd_input(): pyramid_levels = [3] image_shape = ( 20, 20 ) # this shape causes rounding errors when downsampling using convolutions sizes = [32] strides = [8] ratios = np.array([1], keras.backend.floatx()) scales = np.array([1], keras.backend.floatx()) anchor_params = AnchorParameters(sizes, strides, ratios, scales) anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params) expected_anchors = np.array([ [-14, -14, 18, 18], [-6, -14, 26, 18], [2, -14, 34, 18], [-14, -6, 18, 26], [-6, -6, 26, 26], [2, -6, 34, 26], [-14, 2, 18, 34], [-6, 2, 26, 34], [2, 2, 34, 34], ]) np.testing.assert_equal(anchors, expected_anchors)
def run(generator, args): """ Main loop. Args generator: The generator to debug. args: parseargs args object. """ # display images, one at a time # for i in np.random.choice(generator.size(), 200): for i in range(generator.size()): # load the data image = generator.load_image(i) annotations = generator.load_annotations(i) # apply random transformations if args.random_transform: image, annotations = generator.random_transform_group_entry( image, annotations) # resize the image and annotations if args.resize: image, image_scale = generator.resize_image(image) annotations[:, :4] *= image_scale image = image[..., image.shape[-2] // 2, 0] image = (image - image.min()) / image.max() * 255.0 image = np.repeat(image.reshape((512, 512, 1)), 3, axis=2) # to rgb image = image.astype(np.uint8).copy() anchors = anchors_for_shape(image.shape) labels_batch, regression_batch, boxes_batch = generator.compute_anchor_targets( anchors, [image], [annotations], generator.num_classes()) anchor_states = labels_batch[0, :, -1] # draw anchors on the image if args.anchors: draw_boxes(image, boxes_batch[0, anchor_states == 1, :], (0, 255, 0), thickness=1) # draw annotations on the image if args.annotations: # draw annotations in red draw_annotations(image, annotations, color=(0, 0, 255), label_to_name=generator.label_to_name) # draw regressed anchors in green to override most red annotations # result is that annotations without anchors are red, with anchors are green draw_boxes(image, boxes_batch[0, anchor_states == 1, :], (0, 255, 0)) # cv2.imshow('Image', image) cv2.imwrite(os.path.join('debug', '{}.png'.format(i)), image) # if cv2.waitKey() == ord('q'): # return False return True
def test_anchors_for_shape_dimensions(): sizes = [32, 64, 128] strides = [8, 16, 32] ratios = np.array([0.5, 1, 2, 3], keras.backend.floatx()) scales = np.array([1, 1.2, 1.6], keras.backend.floatx()) anchor_params = AnchorParameters(sizes, strides, ratios, scales) pyramid_levels = [3, 4, 5] image_shape = (64, 64) all_anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params) assert all_anchors.shape == (1008, 4)
def test_anchors_for_shape_dimensions(): sizes = [32, 64, 128] strides = [8, 16, 32] ratios = np.array([0.5, 1, 2, 3], tensorflow.keras.backend.floatx()) scales = np.array([1, 1.2, 1.6], tensorflow.keras.backend.floatx()) anchor_params = AnchorParameters(sizes, strides, ratios, scales) pyramid_levels = [3, 4, 5] image_shape = (64, 64) all_anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params) assert all_anchors.shape == (1008, 4)
def run(generator, args): """ Main loop. Args generator: The generator to debug. args: parseargs args object. """ # display images, one at a time for i in range(generator.size()): # load the data image = generator.load_image(i) annotations = generator.load_annotations(i) # apply random transformations if args.random_transform: image, annotations = generator.random_transform_group_entry( image, annotations) # resize the image and annotations if args.resize: image, image_scale = generator.resize_image(image) annotations[:, :4] *= image_scale anchors = anchors_for_shape(image.shape) labels_batch, regression_batch, boxes_batch = generator.compute_anchor_targets( anchors, [image], [annotations], generator.num_classes()) anchor_states = labels_batch[0, :, -1] # draw anchors on the image if args.anchors: draw_boxes(image, anchors[anchor_states == 1], (255, 255, 0), thickness=1) # draw annotations on the image if args.annotations: # draw annotations in red draw_annotations(image, annotations, color=(0, 0, 255), label_to_name=generator.label_to_name) # draw regressed anchors in green to override most red annotations # result is that annotations without anchors are red, with anchors are green draw_boxes(image, boxes_batch[0, anchor_states == 1, :], (0, 255, 0)) cv2.imshow('Image', image) if cv2.waitKey() == ord('q'): return False return True
def run(generator, args, anchor_params): # display images, one at a time for i in range(generator.size()): # load the data image = generator.load_image(i) annotations = generator.load_annotations(i) # apply random transformations if args.random_transform: image, annotations = generator.random_transform_group_entry(image, annotations) # resize the image and annotations if args.resize: image, image_scale = generator.resize_image(image) annotations['bboxes'] *= image_scale for m in range(len(annotations['masks'])): annotations['masks'][m], _ = generator.resize_image(annotations['masks'][m]) anchors = anchors_for_shape(image.shape, anchor_params=anchor_params) positive_indices, _, max_indices = compute_gt_annotations(anchors, annotations['bboxes']) # draw anchors on the image if args.anchors: anchors = generator.generate_anchors(image.shape) positive_indices, _, max_indices = compute_gt_annotations(anchors, annotations['bboxes']) draw_boxes(image, anchors[positive_indices], (255, 255, 0), thickness=1) # draw annotations on the image if args.annotations: # draw annotations in red draw_annotations(image, annotations, color=(0, 0, 255), label_to_name=generator.label_to_name) # draw regressed anchors in green to override most red annotations # result is that annotations without anchors are red, with anchors are green draw_boxes(image, annotations['bboxes'][max_indices[positive_indices], :], (0, 255, 0)) # Draw masks over the image with random colours if args.masks: for m in range(len(annotations['masks'])): # crop the mask with the related bbox size, and then draw them box = annotations['bboxes'][m].astype(int) mask = annotations['masks'][m][box[1]:box[3], box[0]:box[2]] draw_mask(image, box, mask, annotations['labels'][m].astype(int)) # add the label caption caption = '{}'.format(generator.label_to_name(annotations['labels'][m])) draw_caption(image, box, caption) cv2.imshow('Image', image) if cv2.waitKey() == ord('q'): return False return True
def average_overlap(values, entries, state, image_shape, mode="focal", ratio_count=3, include_stride=False): anchor_params = calculate_config(values, ratio_count) if include_stride: anchors = anchors_for_shape(image_shape, anchor_params=anchor_params) else: anchors = base_anchors_for_shape(anchor_params=anchor_params) overlap = compute_overlap(entries, anchors) max_overlap = np.amax(overlap, axis=1) not_matched = len(np.where(max_overlap < 0.5)[0]) if mode == "avg": result = 1 - np.average(max_overlap) elif mode == "ce": result = np.average(-np.log(max_overlap)) elif mode == "focal": result = np.average(-(1 - max_overlap)**2 * np.log(max_overlap)) else: raise Exception("Invalid mode.") if "iteration" not in state: state["iteration"] = 0 else: state["iteration"] += 1 if result < state["best_result"]: state["best_result"] = result print(f"Current best anchor configuration {result}", state["iteration"]) print(f"Ratios: {sorted(np.round(anchor_params.ratios, 3))}") print(f"Scales: {sorted(np.round(anchor_params.scales, 3))}") if include_stride: print(f"Average overlap: {np.round(np.average(max_overlap), 3)}") print( f"Number of labels that don't have any matching anchor: {not_matched}" ) print() return result, not_matched
def test_csv_generator_anchors(): anchors_dict = get_anchors_params("tests/test-data/anchors.yaml") train_generator = CSVGenerator("tests/test-data/csv/annotations.csv", "tests/test-data/csv/classes.csv", transform_generator=None, batch_size=1, image_min_side=512, image_max_side=512, **anchors_dict) inputs, targets = train_generator.next() regreession_batch, labels_batch = targets labels = labels_batch[0] image = inputs[0] anchors = anchors_for_shape(image.shape, **anchors_dict) assert len(labels) == len(anchors)
def generate_anchors(self, image_shape): return anchors_for_shape(image_shape, shapes_callback=self.compute_shapes, ratios=self._anchor_parameters.ratios, scales=self._anchor_parameters.scales, strides=self._anchor_parameters.strides, sizes=self._anchor_parameters.sizes)
def test_anchors_for_shape_values(): sizes = [12] strides = [8] ratios = np.array([1, 2], tf.keras.backend.floatx()) scales = np.array([1, 2], tf.keras.backend.floatx()) anchor_params = AnchorParameters(sizes, strides, ratios, scales) pyramid_levels = [3] image_shape = (16, 16) all_anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params) # using almost_equal for floating point imprecisions np.testing.assert_almost_equal(all_anchors[0, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[1, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[2, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[3, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[4, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[5, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[6, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[7, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[8, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[9, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[10, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[11, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[12, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[13, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[14, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[15, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6)
def generate_anchors(self, image_shape): return anchors_for_shape(image_shape, shapes_callback=self.compute_shapes)
def average_overlap(values, entries, image_shape, mode='focal', ratio_count=3, include_stride=False, SIZES=[32, 64, 128, 256, 512], STRIDES=[8, 16, 32, 64, 128], verbose=False, set_state=None, to_tuple=False, threads=1): anchor_params = calculate_config(values, ratio_count, SIZES, STRIDES) if include_stride: anchors = anchors_for_shape(image_shape, anchor_params=anchor_params) else: anchors = base_anchors_for_shape(anchor_params=anchor_params) overlap = compute_overlap(entries, anchors) max_overlap = np.amax(overlap, axis=1) not_matched = len(np.where(max_overlap < 0.5)[0]) if mode == 'avg': result = 1 - np.average(max_overlap) elif mode == 'ce': result = np.average(-np.log(max_overlap)) elif mode == 'focal': result = np.average(-(1 - max_overlap)**2 * np.log(max_overlap)) else: raise Exception('Invalid mode.') if set_state is not None: state = set_state # -------------------------------------------------------------------------------------------------------------------------------- # "scipy.optimize.differential_evolution" utilizes multiprocessing but internally uses "multiprocessing.Pool" and not # "multiprocessing.Process" which is required for sharing state between processes # (see: https://docs.python.org/3/library/multiprocessing.html#sharing-state-between-processes) # # the "state" variable does not affect directly the "scipy.optimize.differential_evolution" process, therefore updates will be # printed out in case of improvement only if a single thread is used # -------------------------------------------------------------------------------------------------------------------------------- if threads == 1: if result < state['best_result']: state['best_result'] = result if verbose: print('Current best anchor configuration') print('State: {}'.format(np.round(state['best_result'], 5))) print('Ratios: {}'.format( sorted(np.round(anchor_params.ratios, 3)))) print('Scales: {}'.format( sorted(np.round(anchor_params.scales, 3)))) if include_stride: if verbose: print('Average overlap: {}'.format( np.round(np.average(max_overlap), 3))) if verbose: print( "Number of labels that don't have any matching anchor: {}". format(not_matched)) print() if to_tuple: # return a tuple, which happens in the last call to the 'average_overlap' function return result, not_matched else: return result
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # optionally choose specific GPU # if args.gpu: # os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # create the generators test_generator = create_test_generator(args) if os.path.exists( '/app/Ant_mcs_detection/output/test_set_all_detections.pkl'): with open('/app/Ant_mcs_detection/output/test_set_all_detections.pkl', 'rb') as f: all_detections = pickle.load(f) else: with tf.device('/device:GPU:3'): config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, device_count={ 'CPU': 1, 'GPU': 1 }) config.gpu_options.allow_growth = True session = tf.Session(config=config) keras.backend.set_session(session) print('Creating model, this may take a second...') model = models.load_model(os.path.abspath(args.weights), backbone_name=args.backbone) model = models.convert_model(model) # print model summary print(model.summary()) score_threshold = 0.05 max_detections = 0 save_path = None eval_batch_size = 16 all_detections = _get_detections(test_generator, model, score_threshold=score_threshold, max_detections=max_detections, save_path=save_path, eval_batch_size=eval_batch_size) with open('/app/Ant_mcs_detection/output/test_set_all_detections.pkl', 'wb') as f: pickle.dump(all_detections, f) all_annotations = _get_annotations( test_generator, predicted_items_count=len(all_detections)) proba_percentiles = np.linspace(10, 99, 90) iou_values_per_example = [] for example_idx in tqdm(range(len(all_annotations)), total=len(all_annotations)): image = np.copy(test_generator.load_image(example_idx)) _, example_mask, _, _ = test_generator.data_manager.get_all_data( test_generator.image_names[example_idx]) example_mask = np.copy(example_mask) image = np.copy(image) image[:, :, 0] = 255 - image[:, :, 0] image[:, :, 1] = 255 - image[:, :, 1] # image = np.ma.array(image) image_mask = np.tile(1 - example_mask, (1, 1, 3)) image = image * image_mask annotations = test_generator.load_annotations(example_idx) anchors = anchors_for_shape(image.shape, anchor_params=None) positive_indices, _, max_indices = compute_gt_annotations( anchors, annotations['bboxes']) curr_detections = all_detections[example_idx][0] # selected_detections = curr_detections[curr_detections[:, -1] >= args.proba_threshold, :] # bboxes = selected_detections[:, :4] # probas = selected_detections[:, -1] bboxes = curr_detections[:, :4] probas = curr_detections[:, -1] # region filter detections bboxes_filtered = np.copy(bboxes) scores_filtered = np.copy(probas) # proba_threshold = np.percentile(scores_filtered, args.shrinking_proba_perc_threshold) proba_threshold = args.proba_threshold filter_indices = np.where(scores_filtered >= proba_threshold)[0] bboxes_filtered = bboxes_filtered[filter_indices, :] scores_filtered = scores_filtered[filter_indices] #region IR_only f = plt.figure(figsize=(6, 6), dpi=300) plt.imshow(image[:, :, 0], cmap='gray', vmin=0, vmax=255) for i in range(annotations['bboxes'].shape[0]): label = annotations['labels'][i] box = annotations['bboxes'][i] x1, y1, x2, y2 = np.array(box).astype(int) plt.plot([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], color='red', linewidth=2) for bbox, proba in zip(bboxes_filtered, scores_filtered): x1, y1, x2, y2 = bbox.astype(int) plt.plot([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], color='lightgreen', linewidth=2, alpha=proba / probas.max()) plt.text(x1, y1, '%.3f' % proba, fontsize=10) plt.axis('off') plt.savefig( os.path.join('/app/Ant_mcs_detection/output/images_IRonly/', 'test_img_IR_withLabels_%05d.png' % example_idx)) plt.close() #endregion IR_only # region IR_WV_SLP f = plt.figure(figsize=(6, 6), dpi=300) plt.imshow(image) for i in range(annotations['bboxes'].shape[0]): label = annotations['labels'][i] box = annotations['bboxes'][i] x1, y1, x2, y2 = np.array(box).astype(int) plt.plot([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], color='red', linewidth=2) for bbox, proba in zip(bboxes_filtered, scores_filtered): x1, y1, x2, y2 = bbox.astype(int) plt.plot([x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1], color='lightgreen', linewidth=2, alpha=proba / probas.max()) plt.text(x1, y1, '%.3f' % proba, fontsize=10) plt.axis('off') plt.savefig( os.path.join( '/app/Ant_mcs_detection/output/images_IR_WV_SLP/', 'test_img_IR_WV_SLP_withLabels_%05d.png' % example_idx)) plt.close()
def test_anchors_for_shape_values(): sizes = [12] strides = [8] ratios = np.array([1, 2], keras.backend.floatx()) scales = np.array([1, 2], keras.backend.floatx()) anchor_params = AnchorParameters(sizes, strides, ratios, scales) pyramid_levels = [3] image_shape = (16, 16) all_anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params) # using almost_equal for floating point imprecisions np.testing.assert_almost_equal(all_anchors[0, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[1, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[2, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[3, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[4, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[5, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[6, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[7, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[8, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[9, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[10, :], [ strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[11, :], [ strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[12, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[13, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[14, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2, ], decimal=6) np.testing.assert_almost_equal(all_anchors[15, :], [ strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2, strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2, ], decimal=6)