def _get_panoptic_segmentation_module(self, experiment_name): params = exp_factory.get_exp_config(experiment_name) params.task.model.backbone.resnet.model_id = 18 params.task.model.detection_generator.nms_version = 'batched' input_specs = tf.keras.layers.InputSpec(shape=[1, 128, 128, 3]) model = factory.build_panoptic_maskrcnn(input_specs=input_specs, model_config=params.task.model) panoptic_segmentation_module = panoptic_segmentation.PanopticSegmentationModule( params, model=model, batch_size=1, input_image_size=[128, 128]) return panoptic_segmentation_module
def test_build_model_fail_with_none_batch_size(self): params = exp_factory.get_exp_config('panoptic_fpn_coco') input_specs = tf.keras.layers.InputSpec(shape=[1, 128, 128, 3]) model = factory.build_panoptic_maskrcnn(input_specs=input_specs, model_config=params.task.model) with self.assertRaisesRegex( ValueError, 'batch_size cannot be None for panoptic segmentation model.'): _ = panoptic_segmentation.PanopticSegmentationModule( params, model=model, batch_size=None, input_image_size=[128, 128])
def build_model(self) -> tf.keras.Model: """Build Panoptic Mask R-CNN model.""" input_specs = tf.keras.layers.InputSpec( shape=[None] + self.task_config.model.input_size) l2_weight_decay = self.task_config.losses.l2_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) model = factory.build_panoptic_maskrcnn( input_specs=input_specs, model_config=self.task_config.model, l2_regularizer=l2_regularizer) return model
def test_builder(self, backbone_type, input_size, segmentation_backbone_type, segmentation_decoder_type): num_classes = 2 input_specs = tf.keras.layers.InputSpec( shape=[None, input_size[0], input_size[1], 3]) segmentation_output_stride = 16 level = int(np.math.log2(segmentation_output_stride)) segmentation_model = semantic_segmentation.SemanticSegmentationModel( num_classes=2, backbone=backbones.Backbone(type=segmentation_backbone_type), decoder=decoders.Decoder(type=segmentation_decoder_type), head=semantic_segmentation.SegmentationHead(level=level)) model_config = panoptic_maskrcnn_cfg.PanopticMaskRCNN( num_classes=num_classes, segmentation_model=segmentation_model, backbone=backbones.Backbone(type=backbone_type), shared_backbone=segmentation_backbone_type is None, shared_decoder=segmentation_decoder_type is None) l2_regularizer = tf.keras.regularizers.l2(5e-5) _ = factory.build_panoptic_maskrcnn(input_specs=input_specs, model_config=model_config, l2_regularizer=l2_regularizer)
def main(_): params = exp_factory.get_exp_config(FLAGS.experiment) for config_file in FLAGS.config_file or []: params = hyperparams.override_params_dict(params, config_file, is_strict=True) if FLAGS.params_override: params = hyperparams.override_params_dict(params, FLAGS.params_override, is_strict=True) params.validate() params.lock() input_image_size = [int(x) for x in FLAGS.input_image_size.split(',')] input_specs = tf.keras.layers.InputSpec( shape=[FLAGS.batch_size, *input_image_size, 3]) model = factory.build_panoptic_maskrcnn(input_specs=input_specs, model_config=params.task.model) export_module = panoptic_segmentation.PanopticSegmentationModule( params=params, model=model, batch_size=FLAGS.batch_size, input_image_size=[int(x) for x in FLAGS.input_image_size.split(',')], num_channels=3) export_saved_model_lib.export_inference_graph( input_type=FLAGS.input_type, batch_size=FLAGS.batch_size, input_image_size=input_image_size, params=params, checkpoint_path=FLAGS.checkpoint_path, export_dir=FLAGS.export_dir, export_module=export_module, export_checkpoint_subdir='checkpoint', export_saved_model_subdir='saved_model')