Beispiel #1
0
  def test_resnet3d_network_creation(self, model_id, temporal_size,
                                     spatial_size, activation,
                                     aggregate_endpoints):
    """Test for creation of a ResNet3D-50 classifier."""
    input_specs = tf.keras.layers.InputSpec(
        shape=[None, temporal_size, spatial_size, spatial_size, 3])
    temporal_strides = [1, 1, 1, 1]
    temporal_kernel_sizes = [(3, 3, 3), (3, 1, 3, 1), (3, 1, 3, 1, 3, 1),
                             (1, 3, 1)]

    tf.keras.backend.set_image_data_format('channels_last')

    backbone = backbones.ResNet3D(
        model_id=model_id,
        temporal_strides=temporal_strides,
        temporal_kernel_sizes=temporal_kernel_sizes,
        input_specs=input_specs,
        activation=activation)

    num_classes = 1000
    model = video_classification_model.VideoClassificationModel(
        backbone=backbone,
        num_classes=num_classes,
        input_specs={'image': input_specs},
        dropout_rate=0.2,
        aggregate_endpoints=aggregate_endpoints,
    )

    inputs = np.random.rand(2, temporal_size, spatial_size, spatial_size, 3)
    logits = model(inputs)
    self.assertAllEqual([2, num_classes], logits.numpy().shape)
Beispiel #2
0
  def test_serialize_deserialize(self):
    """Validate the classification network can be serialized and deserialized."""
    model_id = 50
    temporal_strides = [1, 1, 1, 1]
    temporal_kernel_sizes = [(3, 3, 3), (3, 1, 3, 1), (3, 1, 3, 1, 3, 1),
                             (1, 3, 1)]

    backbone = backbones.ResNet3D(
        model_id=model_id,
        temporal_strides=temporal_strides,
        temporal_kernel_sizes=temporal_kernel_sizes)

    model = video_classification_model.VideoClassificationModel(
        backbone=backbone, num_classes=1000)

    config = model.get_config()
    new_model = video_classification_model.VideoClassificationModel.from_config(
        config)

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

    # If the serialization was successful, the new config should match the old.
    self.assertAllEqual(model.get_config(), new_model.get_config())
Beispiel #3
0
def build_video_classification_model(
    input_specs: tf.keras.layers.InputSpec,
    model_config: video_classification_cfg.VideoClassificationModel,
    num_classes: int,
    l2_regularizer: tf.keras.regularizers.Regularizer = None
) -> tf.keras.Model:
    """Builds the video classification model."""
    input_specs_dict = {'image': input_specs}
    norm_activation_config = model_config.norm_activation
    backbone = backbones.factory.build_backbone(
        input_specs=input_specs,
        backbone_config=model_config.backbone,
        norm_activation_config=norm_activation_config,
        l2_regularizer=l2_regularizer)

    model = video_classification_model.VideoClassificationModel(
        backbone=backbone,
        num_classes=num_classes,
        input_specs=input_specs_dict,
        dropout_rate=model_config.dropout_rate,
        aggregate_endpoints=model_config.aggregate_endpoints,
        kernel_regularizer=l2_regularizer,
        require_endpoints=model_config.require_endpoints)
    return model