KerasHub
Divyasreepat commited on
Commit
815453f
1 Parent(s): 6eb610d

Update README.md with new model card content

Browse files
Files changed (1) hide show
  1. README.md +173 -9
README.md CHANGED
@@ -1,14 +1,178 @@
1
  ---
2
  library_name: keras-hub
3
  ---
4
- This is a [`SAM` model](https://keras.io/api/keras_hub/models/sam) uploaded using the KerasHub library and can be used with JAX, TensorFlow, and PyTorch backends.
5
- This model is related to a `ImageSegmenter` task.
6
 
7
- Model config:
8
- * **name:** sam_backbone
9
- * **trainable:** True
10
- * **image_encoder:** {'module': 'keras_hub.src.models.vit_det.vit_det_backbone', 'class_name': 'ViTDetBackbone', 'config': {'name': 'vi_t_det_backbone', 'trainable': True, 'image_shape': [1024, 1024, 3], 'patch_size': 16, 'hidden_size': 1024, 'num_layers': 24, 'intermediate_dim': 4096, 'num_heads': 16, 'num_output_channels': 256, 'use_bias': True, 'use_abs_pos': True, 'use_rel_pos': True, 'window_size': 14, 'global_attention_layer_indices': [5, 11, 17, 23], 'layer_norm_epsilon': 1e-06}, 'registered_name': 'keras_hub>ViTDetBackbone'}
11
- * **prompt_encoder:** {'module': 'keras_hub.src.models.sam.sam_prompt_encoder', 'class_name': 'SAMPromptEncoder', 'config': {'name': 'sam_prompt_encoder', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}, 'hidden_size': 256, 'image_embedding_size': [64, 64], 'input_image_size': [1024, 1024], 'mask_in_channels': 16, 'activation': 'gelu'}, 'registered_name': 'keras_hub>SAMPromptEncoder'}
12
- * **mask_decoder:** {'module': 'keras_hub.src.models.sam.sam_mask_decoder', 'class_name': 'SAMMaskDecoder', 'config': {'name': 'sam_mask_decoder', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}, 'hidden_size': 256, 'num_layers': 2, 'intermediate_dim': 2048, 'num_heads': 8, 'embedding_dim': 256, 'num_multimask_outputs': 3, 'iou_head_depth': 3, 'iou_head_hidden_dim': 256, 'activation': 'gelu'}, 'registered_name': 'keras_hub>SAMMaskDecoder'}
13
 
14
- This model card has been generated automatically and should be completed by the model author. See [Model Cards documentation](https://huggingface.co/docs/hub/model-cards) for more information.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  library_name: keras-hub
3
  ---
4
+ ### Model Overview
5
+ The Segment Anything Model (SAM) produces high quality object masks from input prompts such as points or boxes, and it can be used to generate masks for all objects in an image. It has been trained on a dataset of 11 million images and 1.1 billion masks, and has strong zero-shot performance on a variety of segmentation tasks. This model is supported in both KerasCV and KerasHub. KerasCV will no longer be actively developed, so please try to use KerasHub.
6
 
 
 
 
 
 
 
7
 
8
+
9
+ ## Links
10
+ * [Segment Anything Quickstart Notebook: coming soon]()
11
+ * [Segment Anything API Documentation](https://keras.io/api/keras_hub/models/sam/)
12
+ * [Segment Anything Model Card](https://github.com/facebookresearch/segment-anything)
13
+ * [Segment Anything paper](https://arxiv.org/abs/2304.02643)
14
+
15
+ ## Installation
16
+
17
+ Keras and KerasHub can be installed with:
18
+
19
+ ```
20
+ pip install -U -q keras-Hub
21
+ pip install -U -q keras>=3
22
+ ```
23
+
24
+ Jax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instructions on installing them in another environment see the [Keras Getting Started](https://keras.io/getting_started/) page.
25
+
26
+ ## Presets
27
+
28
+ The following model checkpoints are provided by the Keras team. Weights have been ported from https://dl.fbaipublicfiles.com/segment_anything/. Full code examples for each are available below.
29
+ | Preset name | Parameters | Description |
30
+ |----------------|------------|--------------------------------------------------|
31
+ | sam_base_sa1b | 93.74M | The base SAM model trained on the SA1B dataset. |
32
+ | sam_large_sa1b | 312.34M | The large SAM model trained on the SA1B dataset. |
33
+ | sam_huge_sa1b | 641.09M | The huge SAM model trained on the SA1B dataset. |
34
+
35
+ ### Example Usage
36
+ Load pretrained model using `from_preset`.
37
+
38
+ ```python
39
+ image_size=1024
40
+ batch_size=2
41
+ input_data = {
42
+ "images": np.ones(
43
+ (batch_size, image_size, image_size, 3),
44
+ dtype="float32",
45
+ ),
46
+ "points": np.ones((batch_size, 1, 2), dtype="float32"),
47
+ "labels": np.ones((batch_size, 1), dtype="float32"),
48
+ "boxes": np.ones((batch_size, 1, 2, 2), dtype="float32"),
49
+ "masks": np.zeros(
50
+ (batch_size, 0, image_size, image_size, 1)
51
+ ),
52
+ }
53
+ sam = keras_hub.models.SAMImageSegmenter.from_preset('sam_base_sa1b')
54
+ outputs = sam.predict(input_data)
55
+ masks, iou_pred = outputs["masks"], outputs["iou_pred"]
56
+ ```
57
+
58
+ Load segment anything image segmenter with custom backbone
59
+
60
+ ```python
61
+ image_size = 128
62
+ batch_size = 2
63
+ images = np.ones(
64
+ (batch_size, image_size, image_size, 3),
65
+ dtype="float32",
66
+ )
67
+ image_encoder = keras_hub.models.ViTDetBackbone(
68
+ hidden_size=16,
69
+ num_layers=16,
70
+ intermediate_dim=16 * 4,
71
+ num_heads=16,
72
+ global_attention_layer_indices=[2, 5, 8, 11],
73
+ patch_size=16,
74
+ num_output_channels=8,
75
+ window_size=2,
76
+ image_shape=(image_size, image_size, 3),
77
+ )
78
+ prompt_encoder = keras_hub.layers.SAMPromptEncoder(
79
+ hidden_size=8,
80
+ image_embedding_size=(8, 8),
81
+ input_image_size=(
82
+ image_size,
83
+ image_size,
84
+ ),
85
+ mask_in_channels=16,
86
+ )
87
+ mask_decoder = keras_hub.layers.SAMMaskDecoder(
88
+ num_layers=2,
89
+ hidden_size=8,
90
+ intermediate_dim=32,
91
+ num_heads=8,
92
+ embedding_dim=8,
93
+ num_multimask_outputs=3,
94
+ iou_head_depth=3,
95
+ iou_head_hidden_dim=8,
96
+ )
97
+ backbone = keras_hub.models.SAMBackbone(
98
+ image_encoder=image_encoder,
99
+ prompt_encoder=prompt_encoder,
100
+ mask_decoder=mask_decoder,
101
+ )
102
+ sam = keras_hub.models.SAMImageSegmenter(
103
+ backbone=backbone
104
+ )
105
+ ```
106
+
107
+ ## Example Usage with Hugging Face URI
108
+
109
+ Load pretrained model using `from_preset`.
110
+
111
+ ```python
112
+ image_size=1024
113
+ batch_size=2
114
+ input_data = {
115
+ "images": np.ones(
116
+ (batch_size, image_size, image_size, 3),
117
+ dtype="float32",
118
+ ),
119
+ "points": np.ones((batch_size, 1, 2), dtype="float32"),
120
+ "labels": np.ones((batch_size, 1), dtype="float32"),
121
+ "boxes": np.ones((batch_size, 1, 2, 2), dtype="float32"),
122
+ "masks": np.zeros(
123
+ (batch_size, 0, image_size, image_size, 1)
124
+ ),
125
+ }
126
+ sam = keras_hub.models.SAMImageSegmenter.from_preset('sam_base_sa1b')
127
+ outputs = sam.predict(input_data)
128
+ masks, iou_pred = outputs["masks"], outputs["iou_pred"]
129
+ ```
130
+
131
+ Load segment anything image segmenter with custom backbone
132
+
133
+ ```python
134
+ image_size = 128
135
+ batch_size = 2
136
+ images = np.ones(
137
+ (batch_size, image_size, image_size, 3),
138
+ dtype="float32",
139
+ )
140
+ image_encoder = keras_hub.models.ViTDetBackbone(
141
+ hidden_size=16,
142
+ num_layers=16,
143
+ intermediate_dim=16 * 4,
144
+ num_heads=16,
145
+ global_attention_layer_indices=[2, 5, 8, 11],
146
+ patch_size=16,
147
+ num_output_channels=8,
148
+ window_size=2,
149
+ image_shape=(image_size, image_size, 3),
150
+ )
151
+ prompt_encoder = keras_hub.layers.SAMPromptEncoder(
152
+ hidden_size=8,
153
+ image_embedding_size=(8, 8),
154
+ input_image_size=(
155
+ image_size,
156
+ image_size,
157
+ ),
158
+ mask_in_channels=16,
159
+ )
160
+ mask_decoder = keras_hub.layers.SAMMaskDecoder(
161
+ num_layers=2,
162
+ hidden_size=8,
163
+ intermediate_dim=32,
164
+ num_heads=8,
165
+ embedding_dim=8,
166
+ num_multimask_outputs=3,
167
+ iou_head_depth=3,
168
+ iou_head_hidden_dim=8,
169
+ )
170
+ backbone = keras_hub.models.SAMBackbone(
171
+ image_encoder=image_encoder,
172
+ prompt_encoder=prompt_encoder,
173
+ mask_decoder=mask_decoder,
174
+ )
175
+ sam = keras_hub.models.SAMImageSegmenter(
176
+ backbone=backbone
177
+ )
178
+ ```