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 __init__(self, backbone="resnet50", random_transform=False, batch_size=2, step=100, epoch=40, anchor_settings=None): """ Initializer Args: backbone string indicate which backbone network to use (only for ones defined in keras-retinanet) random_transform boolean indicate whether image is randomly transformed when training batch_size number of images sent for each step during training step number of steps(batches) to run for each epoch during training epoch number of epochs for this training anchor_settings a list of lists indicate anchor box parameters. please reference keras-retinanet's document for how to set up anchor box parameters """ check_keras_version() self._backbone_name = backbone self._backbone = models.backbone(backbone) self._img_preprocessor = self._backbone.preprocess_image self._batch_size = batch_size self._step = step self._epoch = epoch if random_transform: self._transform_generator = random_transform_generator( min_rotation=-0.1, max_rotation=0.1, min_translation=(-0.1, -0.1), max_translation=(0.1, 0.1), min_shear=-0.1, max_shear=0.1, min_scaling=(0.9, 0.9), max_scaling=(1.1, 1.1), flip_x_chance=0.5, flip_y_chance=0.5, ) else: self._transform_generator = random_transform_generator( flip_x_chance=0.5) self._common_args = { 'batch_size': batch_size, 'preprocess_image': self._img_preprocessor } if anchor_settings: self._anchor_params = AnchorParameters(*anchor_settings) else: self._anchor_params = AnchorParameters.default
def parse_anchor_parameters(config): ratios = np.array( list(map(float, config['anchor_parameters']['ratios'].split(' '))), keras.backend.floatx()) scales = np.array( list(map(float, config['anchor_parameters']['scales'].split(' '))), keras.backend.floatx()) sizes = list(map(int, config['anchor_parameters']['sizes'].split(' '))) strides = list(map(int, config['anchor_parameters']['strides'].split(' '))) return AnchorParameters(sizes, strides, ratios, scales)
def calculate_config(values, ratio_count): split_point = int((ratio_count - 1) / 2) ratios = [1] for i in range(split_point): ratios.append(values[i]) ratios.append(1 / values[i]) scales = values[split_point:] return AnchorParameters(SIZES, STRIDES, ratios, scales)
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 calculate_config(values, ratio_count, SIZES=[12, 24, 48, 96, 192], STRIDES=[8, 16, 32, 64, 128]): split_point = int((ratio_count - 1) / 2) ratios = [1] for i in range(split_point): ratios.append(values[i]) ratios.append(1 / values[i]) scales = values[split_point:] return AnchorParameters(SIZES, STRIDES, ratios, scales)
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)