Beispiel #1
0
  def test_mobilenet_scaling(self, model_id,
                             filter_size_scale):
    """Test for creation of a MobileNet classifier."""
    mobilenet_params = {
        ('MobileNetV1', 1.0): 3228864,
        ('MobileNetV1', 0.75): 1832976,
        ('MobileNetV2', 1.0): 2257984,
        ('MobileNetV2', 0.75): 1382064,
        ('MobileNetV3Large', 1.0): 4226432,
        ('MobileNetV3Large', 0.75): 2731616,
        ('MobileNetV3Small', 1.0): 1529968,
        ('MobileNetV3Small', 0.75): 1026552,
        ('MobileNetV3EdgeTPU', 1.0): 2849312,
        ('MobileNetV3EdgeTPU', 0.75): 1737288,
        ('MobileNetMultiAVG', 1.0): 3700576,
        ('MobileNetMultiAVG', 0.75): 2345864,
        ('MobileNetMultiMAX', 1.0): 3170720,
        ('MobileNetMultiMAX', 0.75): 2041976,
    }

    input_size = 224
    network = mobilenet.MobileNet(model_id=model_id,
                                  filter_size_scale=filter_size_scale)
    self.assertEqual(network.count_params(),
                     mobilenet_params[(model_id, filter_size_scale)])

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)
    _ = network(inputs)
Beispiel #2
0
    def test_serialize_deserialize(self, model_id):
        # Create a network object that sets all of its config options.
        kwargs = dict(model_id=model_id,
                      filter_size_scale=1.0,
                      stochastic_depth_drop_rate=None,
                      use_sync_bn=False,
                      kernel_initializer='VarianceScaling',
                      kernel_regularizer=None,
                      bias_regularizer=None,
                      norm_momentum=0.99,
                      norm_epsilon=0.001,
                      output_stride=None,
                      min_depth=8,
                      divisible_by=8,
                      regularize_depthwise=False,
                      finegrain_classification_mode=True)
        network = mobilenet.MobileNet(**kwargs)

        expected_config = dict(kwargs)
        self.assertEqual(network.get_config(), expected_config)

        # Create another network object from the first object's config.
        new_network = mobilenet.MobileNet.from_config(network.get_config())

        # Validate that the config can be forced to JSON.
        _ = new_network.to_json()

        # If the serialization was successful, the new config should match the old.
        self.assertAllEqual(network.get_config(), new_network.get_config())
Beispiel #3
0
  def test_mobilenet_creation(self, model_id,
                              input_size):
    """Test creation of MobileNet family models."""
    tf.keras.backend.set_image_data_format('channels_last')

    mobilenet_layers = {
        # The stride (relative to input) and number of filters
        # of first few layers for filter_size_scale = 0.75
        'MobileNetV1': [(1, 24), (1, 48), (2, 96), (2, 96)],
        'MobileNetV2': [(1, 24), (1, 16), (2, 24), (2, 24)],
        'MobileNetV3Small': [(1, 16), (2, 16), (3, 24), (3, 24)],
        'MobileNetV3Large': [(1, 16), (1, 16), (2, 24), (2, 24)],
        'MobileNetV3EdgeTPU': [(1, 24), (1, 16), (2, 24), (2, 24)],
        'MobileNetMultiMAX': [(1, 24), (2, 24), (3, 48), (3, 48)],
        'MobileNetMultiAVG': [(1, 24), (2, 24), (2, 24), (3, 48)],
    }

    network = mobilenet.MobileNet(model_id=model_id,
                                  filter_size_scale=0.75)

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)
    endpoints = network(inputs)

    for idx, (stride, num_filter) in enumerate(mobilenet_layers[model_id]):
      self.assertAllEqual(
          [1, input_size / 2 ** stride, input_size / 2 ** stride, num_filter],
          endpoints[idx+1].shape.as_list())
Beispiel #4
0
    def test_mobilenet_output_stride(self, model_id, output_stride):
        """Test for creation of a MobileNet with different output strides."""
        tf.keras.backend.set_image_data_format('channels_last')

        mobilenet_layers = {
            # The number of filters of the layers outputs been collected
            # for filter_size_scale = 1.0.
            'MobileNetV1': 1024,
            'MobileNetV2': 320,
            'MobileNetV3Small': 96,
            'MobileNetV3Large': 160,
            'MobileNetV3EdgeTPU': 192,
            'MobileNetMultiMAX': 160,
            'MobileNetMultiAVG': 192,
            'MobileNetMultiAVGSeg': 448,
            'MobileNetMultiMAXSeg': 448,
            'MobileNetV3SmallReducedFilters': 48,
        }

        network = mobilenet.MobileNet(model_id=model_id,
                                      filter_size_scale=1.0,
                                      output_stride=output_stride)
        level = int(math.log2(output_stride))
        input_size = 224

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        endpoints = network(inputs)
        num_filter = mobilenet_layers[model_id]
        self.assertAllEqual([
            1, input_size / output_stride, input_size / output_stride,
            num_filter
        ], endpoints[str(level)].shape.as_list())
Beispiel #5
0
    def test_mobilenet_creation(self, model_id, input_size):
        """Test creation of MobileNet family models."""
        tf.keras.backend.set_image_data_format('channels_last')

        mobilenet_layers = {
            # The number of filters of layers having outputs been collected
            # for filter_size_scale = 1.0
            'MobileNetV1': [128, 256, 512, 1024],
            'MobileNetV2': [24, 32, 96, 320],
            'MobileNetV3Small': [16, 24, 48, 96],
            'MobileNetV3Large': [24, 40, 112, 160],
            'MobileNetV3EdgeTPU': [32, 48, 96, 192],
            'MobileNetMultiMAX': [32, 64, 128, 160],
            'MobileNetMultiAVG': [32, 64, 160, 192],
            'MobileNetMultiAVGSeg': [32, 64, 160, 96],
            'MobileNetMultiMAXSeg': [32, 64, 128, 96],
            'MobileNetV3SmallReducedFilters': [16, 24, 48, 48],
        }

        network = mobilenet.MobileNet(model_id=model_id, filter_size_scale=1.0)

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        endpoints = network(inputs)

        for idx, num_filter in enumerate(mobilenet_layers[model_id]):
            self.assertAllEqual([
                1, input_size / 2**(idx + 2), input_size / 2**(idx + 2),
                num_filter
            ], endpoints[str(idx + 2)].shape.as_list())
Beispiel #6
0
    def test_mobilenet_intermediate_layers(self, model_id, input_size):
        tf.keras.backend.set_image_data_format('channels_last')
        # Tests the mobilenet intermediate depthwise layers.
        mobilenet_depthwise_layers = {
            # The number of filters of depthwise layers having outputs been
            # collected for filter_size_scale = 1.0. Only tests the mobilenet
            # model with inverted bottleneck block using depthwise which excludes
            # MobileNetV1.
            'MobileNetV1': [],
            'MobileNetV2': [144, 192, 576, 960],
            'MobileNetV3Small': [16, 88, 144, 576],
            'MobileNetV3Large': [72, 120, 672, 960],
            'MobileNetV3EdgeTPU': [None, None, 384, 1280],
            'MobileNetMultiMAX': [96, 128, 384, 640],
            'MobileNetMultiAVG': [64, 192, 640, 768],
        }
        network = mobilenet.MobileNet(model_id=model_id,
                                      filter_size_scale=1.0,
                                      output_intermediate_endpoints=True)

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        endpoints = network(inputs)

        for idx, num_filter in enumerate(mobilenet_depthwise_layers[model_id]):
            # Not using depthwise conv in this layer.
            if num_filter is None:
                continue

            self.assertAllEqual([
                1, input_size / 2**(idx + 2), input_size / 2**(idx + 2),
                num_filter
            ], endpoints[str(idx + 2) + '/depthwise'].shape.as_list())
Beispiel #7
0
  def test_input_specs(self, input_dim, model_id):
    """Test different input feature dimensions."""
    tf.keras.backend.set_image_data_format('channels_last')

    input_specs = tf.keras.layers.InputSpec(shape=[None, None, None, input_dim])
    network = mobilenet.MobileNet(model_id=model_id, input_specs=input_specs)

    inputs = tf.keras.Input(shape=(128, 128, input_dim), batch_size=1)
    _ = network(inputs)
Beispiel #8
0
    def test_mobilenet_v3_edgetpu_scaling(self, filter_size_scale):
        mobilenet_v3_edgetpu_params = {1.0: 2849312, 0.75: 1737288}

        input_size = 224
        network = mobilenet.MobileNet(model_id='MobileNetV3EdgeTPU',
                                      filter_size_scale=filter_size_scale)
        self.assertEqual(network.count_params(),
                         mobilenet_v3_edgetpu_params[filter_size_scale])

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        _ = network(inputs)
Beispiel #9
0
    def test_mobilenet_v3_small_scaling(self, filter_size_scale):
        mobilenet_v3_small_params = {1.0: 1529968, 0.75: 1026552}

        input_size = 224
        network = mobilenet.MobileNet(model_id='MobileNetV3Small',
                                      filter_size_scale=filter_size_scale)
        self.assertEqual(network.count_params(),
                         mobilenet_v3_small_params[filter_size_scale])

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        _ = network(inputs)
Beispiel #10
0
    def test_mobilenet_v3_large_scaling(self, filter_size_scale):
        mobilenet_v3_large_params = {1.0: 4226432, 0.75: 2731616}

        input_size = 224
        network = mobilenet.MobileNet(model_id='MobileNetV3Large',
                                      filter_size_scale=filter_size_scale)
        self.assertEqual(network.count_params(),
                         mobilenet_v3_large_params[filter_size_scale])

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        _ = network(inputs)
Beispiel #11
0
    def test_mobilenet_v2_scaling(self, filter_size_scale):
        mobilenet_v2_params = {1.0: 2257984, 0.75: 1382064}

        input_size = 224
        network = mobilenet.MobileNet(model_id='MobileNetV2',
                                      filter_size_scale=filter_size_scale)
        self.assertEqual(network.count_params(),
                         mobilenet_v2_params[filter_size_scale])

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        _ = network(inputs)
Beispiel #12
0
    def test_mobilenet_v2_creation(self, input_size):
        """Test creation of EfficientNet family models."""
        tf.keras.backend.set_image_data_format('channels_last')

        network = mobilenet.MobileNet(model_id='MobileNetV2',
                                      filter_size_scale=1.0)

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)
        endpoints = network(inputs)

        self.assertAllEqual([1, input_size / 2**1, input_size / 2**1, 32],
                            endpoints[1].shape.as_list())
        self.assertAllEqual([1, input_size / 2**1, input_size / 2**1, 16],
                            endpoints[2].shape.as_list())
        self.assertAllEqual([1, input_size / 2**2, input_size / 2**2, 24],
                            endpoints[3].shape.as_list())
        self.assertAllEqual([1, input_size / 2**2, input_size / 2**2, 24],
                            endpoints[4].shape.as_list())
Beispiel #13
0
    def test_network_creation_with_mobilenet(self, input_size, min_level,
                                             max_level, use_separable_conv):
        """Test creation of FPN with mobilenet backbone."""
        tf.keras.backend.set_image_data_format('channels_last')

        inputs = tf.keras.Input(shape=(input_size, input_size, 3),
                                batch_size=1)

        backbone = mobilenet.MobileNet(model_id='MobileNetV2')
        network = fpn.FPN(input_specs=backbone.output_specs,
                          min_level=min_level,
                          max_level=max_level,
                          use_separable_conv=use_separable_conv)

        endpoints = backbone(inputs)
        feats = network(endpoints)

        for level in range(min_level, max_level + 1):
            self.assertIn(str(level), feats)
            self.assertAllEqual(
                [1, input_size // 2**level, input_size // 2**level, 256],
                feats[str(level)].shape.as_list())