def test_prediction_size(self): mask_prediction_head = ( keras_mask_head.WeightSharedConvolutionalMaskHead( num_classes=20, num_predictions_per_location=1, conv_hyperparams=self._build_conv_hyperparams(), mask_height=7, mask_width=7)) image_feature = tf.random_uniform([64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) mask_predictions = mask_prediction_head(image_feature) self.assertAllEqual([64, 323, 20, 7, 7], mask_predictions.get_shape().as_list())
def test_class_agnostic_prediction_size(self): mask_prediction_head = ( keras_mask_head.WeightSharedConvolutionalMaskHead( num_classes=20, num_predictions_per_location=1, conv_hyperparams=self._build_conv_hyperparams(), mask_height=7, mask_width=7, masks_are_class_agnostic=True)) def graph_fn(): image_feature = tf.random_uniform( [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) mask_predictions = mask_prediction_head(image_feature) return mask_predictions mask_predictions = self.execute(graph_fn, []) self.assertAllEqual([64, 323, 1, 7, 7], mask_predictions.shape)
def graph_fn(image_features): box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( box_code_size=box_code_size, conv_hyperparams=self._build_conv_hyperparams(), num_predictions_per_location=num_predictions_per_location) class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( num_class_slots=num_classes_without_background + 1, conv_hyperparams=self._build_conv_hyperparams(), num_predictions_per_location=num_predictions_per_location) other_heads = { other_head_name: keras_mask_head.WeightSharedConvolutionalMaskHead( num_classes=num_classes_without_background, conv_hyperparams=self._build_conv_hyperparams(), num_predictions_per_location=num_predictions_per_location, mask_height=mask_height, mask_width=mask_width) } conv_box_predictor = box_predictor.WeightSharedConvolutionalBoxPredictor( is_training=False, num_classes=num_classes_without_background, box_prediction_head=box_prediction_head, class_prediction_head=class_prediction_head, other_heads=other_heads, conv_hyperparams=self._build_conv_hyperparams(), freeze_batchnorm=False, inplace_batchnorm_update=False, depth=32, num_layers_before_predictor=2) box_predictions = conv_box_predictor([image_features]) for key, value in box_predictions.items(): box_predictions[key] = tf.concat(value, axis=1) assert len(box_predictions) == 3 return (box_predictions[box_predictor.BOX_ENCODINGS], box_predictions[ box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], box_predictions[other_head_name])
def test_other_heads_predictions(self): box_code_size = 4 num_classes_without_background = 3 other_head_name = 'Mask' mask_height = 5 mask_width = 5 num_predictions_per_location = 5 box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( box_code_size=box_code_size, conv_hyperparams=self._build_conv_hyperparams(), num_predictions_per_location=num_predictions_per_location) class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( num_class_slots=num_classes_without_background + 1, conv_hyperparams=self._build_conv_hyperparams(), num_predictions_per_location=num_predictions_per_location) other_heads = { other_head_name: keras_mask_head.WeightSharedConvolutionalMaskHead( num_classes=num_classes_without_background, conv_hyperparams=self._build_conv_hyperparams(), num_predictions_per_location=num_predictions_per_location, mask_height=mask_height, mask_width=mask_width) } conv_box_predictor = box_predictor.WeightSharedConvolutionalBoxPredictor( is_training=False, num_classes=num_classes_without_background, box_prediction_head=box_prediction_head, class_prediction_head=class_prediction_head, other_heads=other_heads, conv_hyperparams=self._build_conv_hyperparams(), freeze_batchnorm=False, inplace_batchnorm_update=False, depth=32, num_layers_before_predictor=2) def graph_fn(image_features): box_predictions = conv_box_predictor([image_features]) for key, value in box_predictions.items(): box_predictions[key] = tf.concat(value, axis=1) assert len(box_predictions) == 3 return (box_predictions[box_predictor.BOX_ENCODINGS], box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], box_predictions[other_head_name]) batch_size = 4 feature_ht = 8 feature_wt = 8 image_features = np.random.rand(batch_size, feature_ht, feature_wt, 64).astype(np.float32) (box_encodings, class_predictions, other_head_predictions) = self.execute( graph_fn, [image_features]) num_anchors = feature_ht * feature_wt * num_predictions_per_location self.assertAllEqual(box_encodings.shape, [batch_size, num_anchors, box_code_size]) self.assertAllEqual( class_predictions.shape, [batch_size, num_anchors, num_classes_without_background + 1]) self.assertAllEqual(other_head_predictions.shape, [ batch_size, num_anchors, num_classes_without_background, mask_height, mask_width ])