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(
9999preprocessor = keras_hub.models.DeepLabV3ImageSegmenterPreprocessor(image_converter)
100100```
101101
102+
102103Let us visualize the results of this pretrained model
103104
104105
105106``` python
106107filepath = 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)
109110image = keras.utils.load_img(filepath)
110- image = keras.utils.img_to_array (image)
111+ image = np.array (image)
111112
112113image = preprocessor(image)
113114image = 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
117118def 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
152162We 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 )
154164and 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
672682def 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
753757how many steps the model will train for. The initial learning rate is
754758proportional to 0.007 and the decay steps are 2124. This means that the learning
755759rate 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)
953959images, masks = next (iter (train_ds.take(1 )))
954960images = ops.convert_to_tensor(images)
955961masks = 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 )
957963masks = ops.expand_dims(ops.argmax(masks, axis = - 1 ), axis = - 1 )
958964
959965plot_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
0 commit comments