Skip to content

Commit e1a8682

Browse files
Update deeplab image url and fix guides master. (#1977)
* add color mapping * model output * trim output * remove tmp dir
1 parent ca74869 commit e1a8682

10 files changed

+72
-71
lines changed
25.4 KB
Loading
7.8 KB
Loading
5.15 KB
Loading
-6.83 KB
Loading
Binary file not shown.
83.2 KB
Loading

guides/ipynb/keras_hub/semantic_segmentation_deeplab_v3.ipynb

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"\n",
1111
"**Authors:** [Sachin Prasad](https://github.com/sachinprasadhs), [Divyashree Sreepathihalli](https://github.com/divyashreepathihalli), [Ian Stenbit](https://github.com/ianstenbit)<br>\n",
1212
"**Date created:** 2024/10/11<br>\n",
13-
"**Last modified:** 2024/10/11<br>\n",
13+
"**Last modified:** 2024/10/22<br>\n",
1414
"**Description:** DeepLabV3 training and inference with KerasHub."
1515
]
1616
},
@@ -164,28 +164,25 @@
164164
"outputs": [],
165165
"source": [
166166
"filepath = keras.utils.get_file(\n",
167-
" origin=\"https://storage.googleapis.com/keras-cv/pictures/dog.jpeg\"\n",
167+
" origin=\"https://storage.googleapis.com/keras-cv/models/paligemma/cow_beach_1.png\"\n",
168168
")\n",
169169
"image = keras.utils.load_img(filepath)\n",
170-
"image = keras.utils.img_to_array(image)\n",
170+
"image = np.array(image)\n",
171171
"\n",
172172
"image = preprocessor(image)\n",
173173
"image = keras.ops.expand_dims(image, axis=0)\n",
174-
"preds = ops.expand_dims(ops.argmax(model(image), axis=-1), axis=-1)\n",
174+
"preds = ops.expand_dims(ops.argmax(model.predict(image), axis=-1), axis=-1)\n",
175175
"\n",
176176
"\n",
177177
"def plot_segmentation(original_image, predicted_mask):\n",
178-
" original_image = np.squeeze(original_image, axis=0)\n",
179-
" original_image = np.clip(original_image / 255.0, 0, 1)\n",
180-
" predicted_mask = np.squeeze(predicted_mask, axis=0)\n",
181178
" plt.figure(figsize=(5, 5))\n",
182179
"\n",
183180
" plt.subplot(1, 2, 1)\n",
184-
" plt.imshow(original_image)\n",
181+
" plt.imshow(original_image[0] / 255)\n",
185182
" plt.axis(\"off\")\n",
186183
"\n",
187184
" plt.subplot(1, 2, 2)\n",
188-
" plt.imshow(predicted_mask, cmap=\"gray\")\n",
185+
" plt.imshow(predicted_mask[0])\n",
189186
" plt.axis(\"off\")\n",
190187
"\n",
191188
" plt.tight_layout()\n",
@@ -216,7 +213,7 @@
216213
"## Download the data\n",
217214
"\n",
218215
"We download Pascal VOC 2012 dataset with additional annotations provided here\n",
219-
"[Semantic contours from inverse detectors](https://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz)\n",
216+
"[Semantic contours from inverse detectors](https://ieeexplore.ieee.org/document/6126343)\n",
220217
"and split them into train dataset `train_ds` and `eval_ds`."
221218
]
222219
},
@@ -779,28 +776,22 @@
779776
"source": [
780777
"\n",
781778
"def plot_images_masks(images, masks, pred_masks=None):\n",
782-
" images = (images - np.min(images)) / (np.max(images) - np.min(images))\n",
783-
" masks = (masks - np.min(masks)) / (np.max(masks) - np.min(masks))\n",
784-
" if pred_masks is not None:\n",
785-
" pred_masks = (pred_masks - pred_masks.min()) / (\n",
786-
" pred_masks.max() - pred_masks.min()\n",
787-
" )\n",
788779
" num_images = len(images)\n",
789780
" plt.figure(figsize=(8, 4))\n",
790781
" rows = 3 if pred_masks is not None else 2\n",
791782
"\n",
792783
" for i in range(num_images):\n",
793784
" plt.subplot(rows, num_images, i + 1)\n",
794-
" plt.imshow(images[i])\n",
785+
" plt.imshow(images[i] / 255)\n",
795786
" plt.axis(\"off\")\n",
796787
"\n",
797788
" plt.subplot(rows, num_images, num_images + i + 1)\n",
798-
" plt.imshow(masks[i], cmap=\"gray\")\n",
789+
" plt.imshow(masks[i])\n",
799790
" plt.axis(\"off\")\n",
800791
"\n",
801792
" if pred_masks is not None:\n",
802793
" plt.subplot(rows, num_images, i + 1 + 2 * num_images)\n",
803-
" plt.imshow(pred_masks[i, ..., 0], cmap=\"gray\")\n",
794+
" plt.imshow(pred_masks[i])\n",
804795
" plt.axis(\"off\")\n",
805796
"\n",
806797
" plt.show()\n",
@@ -1065,7 +1056,7 @@
10651056
"images, masks = next(iter(train_ds.take(1)))\n",
10661057
"images = ops.convert_to_tensor(images)\n",
10671058
"masks = ops.convert_to_tensor(masks)\n",
1068-
"preds = ops.expand_dims(ops.argmax(model(images), axis=-1), axis=-1)\n",
1059+
"preds = ops.expand_dims(ops.argmax(model.predict(images), axis=-1), axis=-1)\n",
10691060
"masks = ops.expand_dims(ops.argmax(masks, axis=-1), axis=-1)\n",
10701061
"\n",
10711062
"plot_images_masks(images, masks, preds)"

guides/keras_hub/semantic_segmentation_deeplab_v3.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Title: Semantic Segmentation with KerasHub
33
Authors: [Sachin Prasad](https://github.com/sachinprasadhs), [Divyashree Sreepathihalli](https://github.com/divyashreepathihalli), [Ian Stenbit](https://github.com/ianstenbit)
44
Date created: 2024/10/11
5-
Last modified: 2024/10/11
5+
Last modified: 2024/10/22
66
Description: DeepLabV3 training and inference with KerasHub.
77
Accelerator: GPU
88
"""
@@ -98,28 +98,25 @@ class label such as "person", "bike", or "background" to each individual pixel
9898
Let us visualize the results of this pretrained model
9999
"""
100100
filepath = keras.utils.get_file(
101-
origin="https://storage.googleapis.com/keras-cv/pictures/dog.jpeg"
101+
origin="https://storage.googleapis.com/keras-cv/models/paligemma/cow_beach_1.png"
102102
)
103103
image = keras.utils.load_img(filepath)
104-
image = keras.utils.img_to_array(image)
104+
image = np.array(image)
105105

106106
image = preprocessor(image)
107107
image = keras.ops.expand_dims(image, axis=0)
108-
preds = ops.expand_dims(ops.argmax(model(image), axis=-1), axis=-1)
108+
preds = ops.expand_dims(ops.argmax(model.predict(image), axis=-1), axis=-1)
109109

110110

111111
def plot_segmentation(original_image, predicted_mask):
112-
original_image = np.squeeze(original_image, axis=0)
113-
original_image = np.clip(original_image / 255.0, 0, 1)
114-
predicted_mask = np.squeeze(predicted_mask, axis=0)
115112
plt.figure(figsize=(5, 5))
116113

117114
plt.subplot(1, 2, 1)
118-
plt.imshow(original_image)
115+
plt.imshow(original_image[0] / 255)
119116
plt.axis("off")
120117

121118
plt.subplot(1, 2, 2)
122-
plt.imshow(predicted_mask, cmap="gray")
119+
plt.imshow(predicted_mask[0])
123120
plt.axis("off")
124121

125122
plt.tight_layout()
@@ -139,7 +136,7 @@ def plot_segmentation(original_image, predicted_mask):
139136
## Download the data
140137
141138
We download Pascal VOC 2012 dataset with additional annotations provided here
142-
[Semantic contours from inverse detectors](https://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz)
139+
[Semantic contours from inverse detectors](https://ieeexplore.ieee.org/document/6126343)
143140
and split them into train dataset `train_ds` and `eval_ds`.
144141
"""
145142

@@ -652,28 +649,22 @@ def unpackage_inputs(inputs):
652649

653650

654651
def plot_images_masks(images, masks, pred_masks=None):
655-
images = (images - np.min(images)) / (np.max(images) - np.min(images))
656-
masks = (masks - np.min(masks)) / (np.max(masks) - np.min(masks))
657-
if pred_masks is not None:
658-
pred_masks = (pred_masks - pred_masks.min()) / (
659-
pred_masks.max() - pred_masks.min()
660-
)
661652
num_images = len(images)
662653
plt.figure(figsize=(8, 4))
663654
rows = 3 if pred_masks is not None else 2
664655

665656
for i in range(num_images):
666657
plt.subplot(rows, num_images, i + 1)
667-
plt.imshow(images[i])
658+
plt.imshow(images[i] / 255)
668659
plt.axis("off")
669660

670661
plt.subplot(rows, num_images, num_images + i + 1)
671-
plt.imshow(masks[i], cmap="gray")
662+
plt.imshow(masks[i])
672663
plt.axis("off")
673664

674665
if pred_masks is not None:
675666
plt.subplot(rows, num_images, i + 1 + 2 * num_images)
676-
plt.imshow(pred_masks[i, ..., 0], cmap="gray")
667+
plt.imshow(pred_masks[i])
677668
plt.axis("off")
678669

679670
plt.show()
@@ -839,7 +830,7 @@ def dict_to_tuple(x):
839830
images, masks = next(iter(train_ds.take(1)))
840831
images = ops.convert_to_tensor(images)
841832
masks = ops.convert_to_tensor(masks)
842-
preds = ops.expand_dims(ops.argmax(model(images), axis=-1), axis=-1)
833+
preds = ops.expand_dims(ops.argmax(model.predict(images), axis=-1), axis=-1)
843834
masks = ops.expand_dims(ops.argmax(masks, axis=-1), axis=-1)
844835

845836
plot_images_masks(images, masks, preds)

guides/md/keras_hub/semantic_segmentation_deeplab_v3.md

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**Authors:** [Sachin Prasad](https://github.com/sachinprasadhs), [Divyashree Sreepathihalli](https://github.com/divyashreepathihalli), [Ian Stenbit](https://github.com/ianstenbit)<br>
44
**Date created:** 2024/10/11<br>
5-
**Last modified:** 2024/10/11<br>
5+
**Last modified:** 2024/10/22<br>
66
**Description:** DeepLabV3 training and inference with KerasHub.
77

88

@@ -99,33 +99,31 @@ image_converter = keras_hub.layers.DeepLabV3ImageConverter(
9999
preprocessor = keras_hub.models.DeepLabV3ImageSegmenterPreprocessor(image_converter)
100100
```
101101

102+
102103
Let us visualize the results of this pretrained model
103104

104105

105106
```python
106107
filepath = keras.utils.get_file(
107-
origin="https://storage.googleapis.com/keras-cv/pictures/dog.jpeg"
108+
origin="https://storage.googleapis.com/keras-cv/models/paligemma/cow_beach_1.png"
108109
)
109110
image = keras.utils.load_img(filepath)
110-
image = keras.utils.img_to_array(image)
111+
image = np.array(image)
111112

112113
image = preprocessor(image)
113114
image = keras.ops.expand_dims(image, axis=0)
114-
preds = ops.expand_dims(ops.argmax(model(image), axis=-1), axis=-1)
115+
preds = ops.expand_dims(ops.argmax(model.predict(image), axis=-1), axis=-1)
115116

116117

117118
def plot_segmentation(original_image, predicted_mask):
118-
original_image = np.squeeze(original_image, axis=0)
119-
original_image = np.clip(original_image / 255.0, 0, 1)
120-
predicted_mask = np.squeeze(predicted_mask, axis=0)
121119
plt.figure(figsize=(5, 5))
122120

123121
plt.subplot(1, 2, 1)
124-
plt.imshow(original_image)
122+
plt.imshow(original_image[0] / 255)
125123
plt.axis("off")
126124

127125
plt.subplot(1, 2, 2)
128-
plt.imshow(predicted_mask, cmap="gray")
126+
plt.imshow(predicted_mask[0])
129127
plt.axis("off")
130128

131129
plt.tight_layout()
@@ -136,7 +134,19 @@ plot_segmentation(image, preds)
136134
```
137135

138136

139-
![png](/guides/img/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_9_1.png)
137+
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 5s/step
138+
139+
<div class="k-default-codeblock">
140+
```
141+

142+
```
143+
</div>
144+
1/1 ━━━━━━━━━━━━━━━━━━━━ 5s 5s/step
145+
146+
147+
148+
149+
![png](/img/guides/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_9_3.png)
140150

141151

142152

@@ -150,7 +160,7 @@ metric evaluation, and inference!
150160
## Download the data
151161

152162
We download Pascal VOC 2012 dataset with additional annotations provided here
153-
[Semantic contours from inverse detectors](https://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz)
163+
[Semantic contours from inverse detectors](https://ieeexplore.ieee.org/document/6126343)
154164
and split them into train dataset `train_ds` and `eval_ds`.
155165

156166

@@ -670,28 +680,22 @@ segmentation masks and prediction masks as input and displays them in a grid.
670680
```python
671681

672682
def plot_images_masks(images, masks, pred_masks=None):
673-
images = (images - np.min(images)) / (np.max(images) - np.min(images))
674-
masks = (masks - np.min(masks)) / (np.max(masks) - np.min(masks))
675-
if pred_masks is not None:
676-
pred_masks = (pred_masks - pred_masks.min()) / (
677-
pred_masks.max() - pred_masks.min()
678-
)
679683
num_images = len(images)
680684
plt.figure(figsize=(8, 4))
681685
rows = 3 if pred_masks is not None else 2
682686

683687
for i in range(num_images):
684688
plt.subplot(rows, num_images, i + 1)
685-
plt.imshow(images[i])
689+
plt.imshow(images[i] / 255)
686690
plt.axis("off")
687691

688692
plt.subplot(rows, num_images, num_images + i + 1)
689-
plt.imshow(masks[i], cmap="gray")
693+
plt.imshow(masks[i])
690694
plt.axis("off")
691695

692696
if pred_masks is not None:
693697
plt.subplot(rows, num_images, i + 1 + 2 * num_images)
694-
plt.imshow(pred_masks[i, ..., 0], cmap="gray")
698+
plt.imshow(pred_masks[i])
695699
plt.axis("off")
696700

697701
plt.show()
@@ -702,7 +706,7 @@ plot_images_masks(batch["images"], batch["segmentation_masks"])
702706

703707

704708

705-
![png](/guides/img/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_18_0.png)
709+
![png](/img/guides/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_18_0.png)
706710

707711

708712

@@ -732,7 +736,7 @@ plot_images_masks(batch["images"], batch["segmentation_masks"])
732736

733737

734738

735-
![png](/guides/img/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_22_0.png)
739+
![png](/img/guides/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_22_0.png)
736740

737741

738742

@@ -753,7 +757,7 @@ dataset cardinality is important for learning rate decay because it determines
753757
how many steps the model will train for. The initial learning rate is
754758
proportional to 0.007 and the decay steps are 2124. This means that the learning
755759
rate will start at `INITIAL_LR` and then decrease to zero over 2124 steps.
756-
![png](/guides/img/semantic_segmentation_deeplab_v3_plus/learning_rate_schedule.png)
760+
![png](/img/guides/semantic_segmentation_deeplab_v3/learning_rate_schedule.png)
757761

758762

759763
```python
@@ -922,18 +926,20 @@ model.fit(train_ds, validation_data=eval_ds, epochs=EPOCHS)
922926

923927
<div class="k-default-codeblock">
924928
```
925-
1/Unknown 40s 40s/step - categorical_accuracy: 0.0494 - loss: 3.4081 - mean_io_u: 0.0112
929+
1/Unknown 40s 40s/step - categorical_accuracy: 0.1191 - loss: 3.0568 - mean_io_u: 0.0118
930+
931+

926932
```
927933
</div>
928-
2124/2124 ━━━━━━━━━━━━━━━━━━━━ 279s 113ms/step - categorical_accuracy: 0.7188 - loss: 1.1003 - mean_io_u: 0.0934 - val_categorical_accuracy: 0.8222 - val_loss: 0.5761 - val_mean_io_u: 0.3481
934+
2124/2124 ━━━━━━━━━━━━━━━━━━━━ 281s 114ms/step - categorical_accuracy: 0.7286 - loss: 1.0707 - mean_io_u: 0.0926 - val_categorical_accuracy: 0.8199 - val_loss: 0.5900 - val_mean_io_u: 0.3265
929935

930936

931937

932938

933939

934940
<div class="k-default-codeblock">
935941
```
936-
<keras.src.callbacks.history.History at 0x7f6868c16490>
942+
<keras.src.callbacks.history.History at 0x7fd7a897f8d0>
937943
938944
```
939945
</div>
@@ -953,13 +959,26 @@ test_ds = preprocess_inputs(test_ds)
953959
images, masks = next(iter(train_ds.take(1)))
954960
images = ops.convert_to_tensor(images)
955961
masks = ops.convert_to_tensor(masks)
956-
preds = ops.expand_dims(ops.argmax(model(images), axis=-1), axis=-1)
962+
preds = ops.expand_dims(ops.argmax(model.predict(images), axis=-1), axis=-1)
957963
masks = ops.expand_dims(ops.argmax(masks, axis=-1), axis=-1)
958964

959965
plot_images_masks(images, masks, preds)
960966
```
967+
968+
969+
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 3s/step
970+
971+
<div class="k-default-codeblock">
972+
```
973+

974+
```
975+
</div>
976+
1/1 ━━━━━━━━━━━━━━━━━━━━ 3s 3s/step
977+
978+
979+
961980

962-
![png](/guides/img/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_32_2.png)
981+
![png](/img/guides/semantic_segmentation_deeplab_v3/semantic_segmentation_deeplab_v3_32_2.png)
963982

964983

965984

scripts/guides_master.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
"title": "Getting Started with KerasHub",
6565
},
6666
{
67-
"path": "Semantic Segmentation with KerasHub",
68-
"title": "semantic_segmentation_deeplab_v3",
67+
"path": "semantic_segmentation_deeplab_v3",
68+
"title": "Semantic Segmentation with KerasHub",
6969
},
7070
{
71-
"path": "trasnformer_pretraining",
71+
"path": "transformer_pretraining",
7272
"title": "Pretraining a Transformer from scratch with KerasHub",
7373
},
7474
{

0 commit comments

Comments
 (0)