From 2ca922504ac2f23991b1d25b29bc2282a3b30e6b Mon Sep 17 00:00:00 2001 From: Sachin Prasad Date: Fri, 1 Nov 2024 13:50:04 -0700 Subject: [PATCH 1/2] update docstring examples --- .../models/deeplab_v3/deeplab_v3_segmenter.py | 8 +++++--- .../src/models/densenet/densenet_backbone.py | 2 +- keras_hub/src/models/sam/sam_backbone.py | 1 - .../src/models/sam/sam_image_segmenter.py | 19 +++++++++---------- keras_hub/src/models/vgg/vgg_backbone.py | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py b/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py index 7f0f71718c..6146df43f2 100644 --- a/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py +++ b/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py @@ -31,9 +31,9 @@ class DeepLabV3ImageSegmenter(ImageSegmenter): Load a DeepLabV3 preset with all the 21 class, pretrained segmentation head. ```python images = np.ones(shape=(1, 96, 96, 3)) - labels = np.zeros(shape=(1, 96, 96, 1)) + labels = np.zeros(shape=(1, 96, 96, 2)) segmenter = keras_hub.models.DeepLabV3ImageSegmenter.from_preset( - "deeplabv3_resnet50_pascalvoc", + "deeplab_v3_plus_resnet50_pascalvoc", ) segmenter.predict(images) ``` @@ -41,12 +41,14 @@ class DeepLabV3ImageSegmenter(ImageSegmenter): Specify `num_classes` to load randomly initialized segmentation head. ```python segmenter = keras_hub.models.DeepLabV3ImageSegmenter.from_preset( - "deeplabv3_resnet50_pascalvoc", + "deeplab_v3_plus_resnet50_pascalvoc", num_classes=2, ) + segmenter.preprocessor.image_size = (96, 96) segmenter.fit(images, labels, epochs=3) segmenter.predict(images) # Trained 2 class segmentation. ``` + Load DeepLabv3+ presets a extension of DeepLabv3 by adding a simple yet effective decoder module to refine the segmentation results especially along object boundaries. diff --git a/keras_hub/src/models/densenet/densenet_backbone.py b/keras_hub/src/models/densenet/densenet_backbone.py index 103463b699..fa7239696a 100644 --- a/keras_hub/src/models/densenet/densenet_backbone.py +++ b/keras_hub/src/models/densenet/densenet_backbone.py @@ -29,7 +29,7 @@ class DenseNetBackbone(FeaturePyramidBackbone): input_data = np.ones(shape=(8, 224, 224, 3)) # Pretrained backbone - model = keras_hub.models.DenseNetBackbone.from_preset("densenet121_imagenet") + model = keras_hub.models.DenseNetBackbone.from_preset("densenet_121_imagenet") model(input_data) # Randomly initialized backbone with a custom config diff --git a/keras_hub/src/models/sam/sam_backbone.py b/keras_hub/src/models/sam/sam_backbone.py index 6255510105..4198b064e6 100644 --- a/keras_hub/src/models/sam/sam_backbone.py +++ b/keras_hub/src/models/sam/sam_backbone.py @@ -68,7 +68,6 @@ class SAMBackbone(Backbone): image_encoder=image_encoder, prompt_encoder=prompt_encoder, mask_decoder=mask_decoder, - image_shape=(image_size, image_size, 3), ) backbone(input_data) ``` diff --git a/keras_hub/src/models/sam/sam_image_segmenter.py b/keras_hub/src/models/sam/sam_image_segmenter.py index 19b0035cb7..6abf72a0ea 100644 --- a/keras_hub/src/models/sam/sam_image_segmenter.py +++ b/keras_hub/src/models/sam/sam_image_segmenter.py @@ -51,9 +51,9 @@ class SAMImageSegmenter(ImageSegmenter): (batch_size, 0, image_size, image_size, 1) ), } - # todo: update preset name - sam = keras_hub.models.SAMImageSegmenter.from_preset(`sam_base`) - sam(input_data) + sam = keras_hub.models.SAMImageSegmenter.from_preset('sam_base_sa1b') + outputs = sam.predict(input_data) + masks, iou_pred = outputs["masks"], outputs["iou_pred"] ``` Load segment anything image segmenter with custom backbone @@ -65,7 +65,7 @@ class SAMImageSegmenter(ImageSegmenter): (batch_size, image_size, image_size, 3), dtype="float32", ) - image_encoder = ViTDetBackbone( + image_encoder = keras_hub.models.ViTDetBackbone( hidden_size=16, num_layers=16, intermediate_dim=16 * 4, @@ -76,7 +76,7 @@ class SAMImageSegmenter(ImageSegmenter): window_size=2, image_shape=(image_size, image_size, 3), ) - prompt_encoder = SAMPromptEncoder( + prompt_encoder = keras_hub.layers.SAMPromptEncoder( hidden_size=8, image_embedding_size=(8, 8), input_image_size=( @@ -85,7 +85,7 @@ class SAMImageSegmenter(ImageSegmenter): ), mask_in_channels=16, ) - mask_decoder = SAMMaskDecoder( + mask_decoder = keras_hub.layers.SAMMaskDecoder( num_layers=2, hidden_size=8, intermediate_dim=32, @@ -95,13 +95,12 @@ class SAMImageSegmenter(ImageSegmenter): iou_head_depth=3, iou_head_hidden_dim=8, ) - backbone = SAMBackbone( + backbone = keras_hub.models.SAMBackbone( image_encoder=image_encoder, prompt_encoder=prompt_encoder, mask_decoder=mask_decoder, - image_shape=(image_size, image_size, 3), ) - sam = SAMImageSegmenter( + sam = keras_hub.models.SAMImageSegmenter( backbone=backbone ) ``` @@ -115,7 +114,7 @@ class SAMImageSegmenter(ImageSegmenter): labels = np.array([[1., 0.]]) box = np.array([[[[384., 384.], [640., 640.]]]]) input_mask = np.ones((1, 1, 256, 256, 1)) - Prepare an input dictionary: + # Prepare an input dictionary: inputs = { "images": image, "points": points, diff --git a/keras_hub/src/models/vgg/vgg_backbone.py b/keras_hub/src/models/vgg/vgg_backbone.py index ef91c8689d..c70816d78c 100644 --- a/keras_hub/src/models/vgg/vgg_backbone.py +++ b/keras_hub/src/models/vgg/vgg_backbone.py @@ -27,7 +27,7 @@ class VGGBackbone(Backbone): input_data = np.ones((2, 224, 224, 3), dtype="float32") # Pretrained VGG backbone. - model = keras_hub.models.VGGBackbone.from_preset("vgg16") + model = keras_hub.models.VGGBackbone.from_preset("vgg_16_imagenet") model(input_data) # Randomly initialized VGG backbone with a custom config. From f2c1d665170aa86b9079746d94fc32dfebd0beae Mon Sep 17 00:00:00 2001 From: Sachin Prasad Date: Mon, 4 Nov 2024 09:54:48 -0800 Subject: [PATCH 2/2] format fix --- keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py b/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py index 6146df43f2..0758117c6e 100644 --- a/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py +++ b/keras_hub/src/models/deeplab_v3/deeplab_v3_segmenter.py @@ -48,7 +48,7 @@ class DeepLabV3ImageSegmenter(ImageSegmenter): segmenter.fit(images, labels, epochs=3) segmenter.predict(images) # Trained 2 class segmentation. ``` - + Load DeepLabv3+ presets a extension of DeepLabv3 by adding a simple yet effective decoder module to refine the segmentation results especially along object boundaries.