Example #1
0
    def test_mobilenet_creation(self, model_id, filter_size_scale):
        """Test creation of Mobilenet models."""

        network = backbones.MobileNet(model_id=model_id,
                                      filter_size_scale=filter_size_scale,
                                      norm_momentum=0.99,
                                      norm_epsilon=1e-5)

        backbone_config = backbones_cfg.Backbone(
            type='mobilenet',
            mobilenet=backbones_cfg.MobileNet(
                model_id=model_id, filter_size_scale=filter_size_scale))
        norm_activation_config = common_cfg.NormActivation(norm_momentum=0.99,
                                                           norm_epsilon=1e-5,
                                                           use_sync_bn=False)

        factory_network = factory.build_backbone(
            input_specs=tf.keras.layers.InputSpec(shape=[None, None, None, 3]),
            backbone_config=backbone_config,
            norm_activation_config=norm_activation_config)

        network_config = network.get_config()
        factory_network_config = factory_network.get_config()

        self.assertEqual(network_config, factory_network_config)
Example #2
0
    def test_spinenet_creation(self, model_id):
        """Test creation of SpineNet models."""
        input_size = 128
        min_level = 3
        max_level = 7

        input_specs = tf.keras.layers.InputSpec(
            shape=[None, input_size, input_size, 3])
        network = backbones.SpineNet(input_specs=input_specs,
                                     min_level=min_level,
                                     max_level=max_level,
                                     norm_momentum=0.99,
                                     norm_epsilon=1e-5)

        backbone_config = backbones_cfg.Backbone(
            type='spinenet',
            spinenet=backbones_cfg.SpineNet(model_id=model_id))
        norm_activation_config = common_cfg.NormActivation(norm_momentum=0.99,
                                                           norm_epsilon=1e-5,
                                                           use_sync_bn=False)

        factory_network = factory.build_backbone(
            input_specs=tf.keras.layers.InputSpec(
                shape=[None, input_size, input_size, 3]),
            backbone_config=backbone_config,
            norm_activation_config=norm_activation_config)

        network_config = network.get_config()
        factory_network_config = factory_network.get_config()

        self.assertEqual(network_config, factory_network_config)
Example #3
0
    def test_efficientnet_creation(self, model_id, se_ratio):
        """Test creation of EfficientNet models."""

        network = backbones.EfficientNet(model_id=model_id,
                                         se_ratio=se_ratio,
                                         norm_momentum=0.99,
                                         norm_epsilon=1e-5)

        backbone_config = backbones_cfg.Backbone(
            type='efficientnet',
            efficientnet=backbones_cfg.EfficientNet(model_id=model_id,
                                                    se_ratio=se_ratio))
        norm_activation_config = common_cfg.NormActivation(norm_momentum=0.99,
                                                           norm_epsilon=1e-5,
                                                           use_sync_bn=False)

        factory_network = factory.build_backbone(
            input_specs=tf.keras.layers.InputSpec(shape=[None, None, None, 3]),
            backbone_config=backbone_config,
            norm_activation_config=norm_activation_config)

        network_config = network.get_config()
        factory_network_config = factory_network.get_config()

        self.assertEqual(network_config, factory_network_config)
def _create_centernet_model(model_id: int = 52,
                            num_hourglasses: int = 2
                            ) -> centernet_model.CenterNetModel:
  """Create centernet model to load TF1 weights."""
  task_config = centernet.CenterNetTask(
      model=centernet.CenterNetModel(
          backbone=backbones.Backbone(
              type="hourglass",
              hourglass=backbones.Hourglass(
                  model_id=model_id, num_hourglasses=num_hourglasses))))
  model_config = task_config.model

  backbone = factory.build_backbone(
      input_specs=tf.keras.layers.InputSpec(shape=[1, 512, 512, 3]),
      backbone_config=model_config.backbone,
      norm_activation_config=model_config.norm_activation)

  task_outputs = task_config.get_output_length_dict()
  head = centernet_head.CenterNetHead(
      input_specs=backbone.output_specs,
      task_outputs=task_outputs,
      input_levels=model_config.head.input_levels)

  detect_generator_obj = detection_generator.CenterNetDetectionGenerator()

  model = centernet_model.CenterNetModel(
      backbone=backbone, head=head, detection_generator=detect_generator_obj)
  logging.info("Successfully created centernet model.")

  return model
Example #5
0
def build_segmentation_model_3d(
    input_specs: tf.keras.layers.InputSpec,
    model_config: hyperparams.Config,
    l2_regularizer: tf.keras.regularizers.Regularizer = None) -> tf.keras.Model:  # pytype: disable=annotation-type-mismatch  # typed-keras
  """Builds Segmentation model."""
  norm_activation_config = model_config.norm_activation
  backbone = backbone_factory.build_backbone(
      input_specs=input_specs,
      backbone_config=model_config.backbone,
      norm_activation_config=norm_activation_config,
      l2_regularizer=l2_regularizer)

  decoder = decoder_factory.build_decoder(
      input_specs=backbone.output_specs,
      model_config=model_config,
      l2_regularizer=l2_regularizer)

  head_config = model_config.head

  head = segmentation_heads_3d.SegmentationHead3D(
      num_classes=model_config.num_classes,
      level=head_config.level,
      num_convs=head_config.num_convs,
      num_filters=head_config.num_filters,
      upsample_factor=head_config.upsample_factor,
      activation=norm_activation_config.activation,
      use_sync_bn=norm_activation_config.use_sync_bn,
      norm_momentum=norm_activation_config.norm_momentum,
      norm_epsilon=norm_activation_config.norm_epsilon,
      use_batch_normalization=head_config.use_batch_normalization,
      kernel_regularizer=l2_regularizer,
      output_logits=head_config.output_logits)

  model = segmentation_model.SegmentationModel(backbone, decoder, head)
  return model
Example #6
0
def build_yolo(input_specs, model_config, l2_regularization):
    """Builds yolo model."""
    backbone = model_config.backbone.get()
    anchor_dict, _ = model_config.anchor_boxes.get(backbone.min_level,
                                                   backbone.max_level)
    backbone = backbone_factory.build_backbone(input_specs,
                                               model_config.backbone,
                                               model_config.norm_activation,
                                               l2_regularization)
    decoder = decoder_factory.build_decoder(backbone.output_specs,
                                            model_config, l2_regularization)

    head = build_yolo_head(decoder.output_specs, model_config,
                           l2_regularization)
    detection_generator_obj = build_yolo_detection_generator(
        model_config, anchor_dict)

    model = yolo_model.Yolo(backbone=backbone,
                            decoder=decoder,
                            head=head,
                            detection_generator=detection_generator_obj)
    model.build(input_specs.shape)

    model.summary(print_fn=logging.info)

    losses = detection_generator_obj.get_losses()
    return model, losses
    def test_resnet_creation(self, model_id):
        """Test creation of ResNet models."""

        network = backbones.ResNet(model_id=model_id,
                                   se_ratio=0.0,
                                   norm_momentum=0.99,
                                   norm_epsilon=1e-5)

        backbone_config = backbones_cfg.Backbone(type='resnet',
                                                 resnet=backbones_cfg.ResNet(
                                                     model_id=model_id,
                                                     se_ratio=0.0))
        norm_activation_config = common_cfg.NormActivation(norm_momentum=0.99,
                                                           norm_epsilon=1e-5,
                                                           use_sync_bn=False)
        model_config = retinanet_cfg.RetinaNet(
            backbone=backbone_config, norm_activation=norm_activation_config)

        factory_network = factory.build_backbone(
            input_specs=tf.keras.layers.InputSpec(shape=[None, None, None, 3]),
            model_config=model_config)

        network_config = network.get_config()
        factory_network_config = factory_network.get_config()

        self.assertEqual(network_config, factory_network_config)
Example #8
0
    def build_model(self):
        """get an instance of CenterNet."""
        model_config = self.task_config.model
        input_specs = tf.keras.layers.InputSpec(shape=[None] +
                                                model_config.input_size)

        l2_weight_decay = self.task_config.weight_decay
        # Divide weight decay by 2.0 to match the implementation of tf.nn.l2_loss.
        # (https://www.tensorflow.org/api_docs/python/tf/keras/regularizers/l2)
        # (https://www.tensorflow.org/api_docs/python/tf/nn/l2_loss)
        l2_regularizer = (tf.keras.regularizers.l2(l2_weight_decay / 2.0)
                          if l2_weight_decay else None)

        backbone = factory.build_backbone(
            input_specs=input_specs,
            backbone_config=model_config.backbone,
            norm_activation_config=model_config.norm_activation,
            l2_regularizer=l2_regularizer)

        task_outputs = self.task_config.get_output_length_dict()
        head_config = model_config.head
        head = centernet_head.CenterNetHead(
            input_specs=backbone.output_specs,
            task_outputs=task_outputs,
            input_levels=head_config.input_levels,
            heatmap_bias=head_config.heatmap_bias)

        # output_specs is a dict
        backbone_output_spec = backbone.output_specs[
            head_config.input_levels[-1]]
        if len(backbone_output_spec) == 4:
            bb_output_height = backbone_output_spec[1]
        elif len(backbone_output_spec) == 3:
            bb_output_height = backbone_output_spec[0]
        else:
            raise ValueError
        self._net_down_scale = int(model_config.input_size[0] /
                                   bb_output_height)
        dg_config = model_config.detection_generator
        detect_generator_obj = detection_generator.CenterNetDetectionGenerator(
            max_detections=dg_config.max_detections,
            peak_error=dg_config.peak_error,
            peak_extract_kernel_size=dg_config.peak_extract_kernel_size,
            class_offset=dg_config.class_offset,
            net_down_scale=self._net_down_scale,
            input_image_dims=model_config.input_size[0],
            use_nms=dg_config.use_nms,
            nms_pre_thresh=dg_config.nms_pre_thresh,
            nms_thresh=dg_config.nms_thresh)

        model = centernet_model.CenterNetModel(
            backbone=backbone,
            head=head,
            detection_generator=detect_generator_obj)

        return model
Example #9
0
def build_yolo(input_specs, model_config, l2_regularization, masks, xy_scales,
               path_scales):
    print(model_config.as_dict())
    print(input_specs)
    print(l2_regularization)

    backbone = factory.build_backbone(input_specs, model_config,
                                      l2_regularization)
    decoder = build_yolo_decoder(backbone.output_specs, model_config,
                                 l2_regularization)
    head = build_yolo_head(backbone.output_specs, model_config,
                           l2_regularization)
    filter = build_yolo_filter(model_config, head, masks, xy_scales,
                               path_scales)

    model = Yolo(backbone=backbone, decoder=decoder, head=head, filter=filter)
    model.build(input_specs.shape)

    losses = filter.losses
    return model, losses