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35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyOA6q4xuqXb0BaHFu0tHC2h"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "#Pet Image Segementation Using Modified U-Nets built on the Oxford IIIT Pets Dataset"
      ],
      "metadata": {
        "id": "JiICHTic5xBd"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Image Segementation is the process on taking an image as an input and indivually labling if each pixel is part of the object, bording the object, or is not part of the object. The Oxford IIIT Pets Dataset is perfect for this. It is a 37 category database with around 200 images in each category. We can further increase this with data augmentation. All of the images have a corresponding mask which has all the pixels divided into 3 classes: on the pet, bordering the pet, or outside the pet. Using this, we can train a modified U-Net to predict these masks when faced with new images. This model has acheived a 92% accuracy on the validation data, which is very high considering that we can further improve this with more epoches and model tuning."
      ],
      "metadata": {
        "id": "kokOKokS7GY8"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "We must first begin by importing the nessesary libaries into our program. We will be using TensorFlow and Keras to build and train the model, MatPlotLib to show our images and masks, and TensorFlow Datasets to access our dataset."
      ],
      "metadata": {
        "id": "AmBgUS1G96ve"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Preparing Our Data To Be Processed"
      ],
      "metadata": {
        "id": "CZdG-a5yBBlR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install git+https://github.com/tensorflow/examples.git\n",
        "\n",
        "import tensorflow as tf\n",
        "import tensorflow_datasets as tfds\n",
        "from tensorflow_examples.models.pix2pix import pix2pix\n",
        "import matplotlib.pyplot as plt\n"
      ],
      "metadata": {
        "id": "jERRrqR--f1G"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now that we have imported all of our libraries, we can start to load the dataset and get ready for the data to be processed."
      ],
      "metadata": {
        "id": "D6BjANjW_aFS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset, info = tfds.load('oxford_iiit_pet:3.*.*', with_info=True)\n",
        "\n",
        "TRAIN_LENGTH = info.splits['train'].num_examples\n",
        "BATCH_SIZE = 64\n",
        "BUFFER_SIZE = 1000\n",
        "STEPS_PER_EPOCH = TRAIN_LENGTH // BATCH_SIZE"
      ],
      "metadata": {
        "id": "dkemyqs7_q-q"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Next, we will make a load_image function that will resize the image and the mask to (128, 128). We will also make a function to normizlize the image, which will reduce the value range of the image from (0, 255) to (0, 1)."
      ],
      "metadata": {
        "id": "jeN_rXKz_5Qr"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def load_image(datapoint):\n",
        "  input_image = tf.image.resize(datapoint['image'], (128, 128))\n",
        "  input_mask = tf.image.resize(datapoint['segmentation_mask'], (128, 128), method = tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n",
        "  input_image, input_mask = normalize_image(input_image, input_mask)\n",
        "  return input_image, input_mask\n",
        "\n",
        "def normalize_image(input_image, input_mask):\n",
        "  input_image = tf.cast(input_image, tf.float32) / 255.0\n",
        "  input_mask -= 1\n",
        "  return input_image, input_mask"
      ],
      "metadata": {
        "id": "l5NFCx12AqDx"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "We will also now need to add our data augmentation. This will be impletemented in the form of a class and will randomly flip our images and masks using a set seed."
      ],
      "metadata": {
        "id": "K2en1ZvABmMP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "class Augment(tf.keras.layers.Layer):\n",
        "  def __init__(self, seed=42):\n",
        "    super().__init__()\n",
        "    self.augment_inputs = tf.keras.layers.RandomFlip(mode=\"horizontal\", seed=seed)\n",
        "    self.augment_labels = tf.keras.layers.RandomFlip(mode=\"horizontal\", seed=seed)\n",
        "\n",
        "  def call(self, inputs, labels):\n",
        "    inputs = self.augment_inputs(inputs)\n",
        "    labels = self.augment_labels(labels)\n",
        "    return inputs, labels"
      ],
      "metadata": {
        "id": "XBVo-uRzB8Vc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we can group our images into batches to be processed."
      ],
      "metadata": {
        "id": "7YBY0UvfA5El"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "train_images = dataset['train'].map(load_image, num_parallel_calls=tf.data.AUTOTUNE)\n",
        "test_images = dataset['test'].map(load_image, num_parallel_calls=tf.data.AUTOTUNE)\n",
        "\n",
        "train_batches = (train_images.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat().map(Augment()).prefetch(buffer_size=tf.data.AUTOTUNE))\n",
        "test_batches = test_images.batch(BATCH_SIZE)"
      ],
      "metadata": {
        "id": "fAvQHyxABNH3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Creating and Training the Model"
      ],
      "metadata": {
        "id": "GIcff6fzCSKR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "First we will begin by creating our base model. It will be based on the pretrained MobileNetV2 model. We will also create the second model, which will be based on pix2pix.\n"
      ],
      "metadata": {
        "id": "GyY1EnvXCgKY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "base_model = tf.keras.applications.MobileNetV2(input_shape=[128, 128, 3], include_top=False)\n",
        "\n",
        "layer_names = [\n",
        "    'block_1_expand_relu',\n",
        "    'block_3_expand_relu',\n",
        "    'block_6_expand_relu',\n",
        "    'block_13_expand_relu',\n",
        "    'block_16_project',\n",
        "]\n",
        "base_model_outputs = [base_model.get_layer(name).output for name in layer_names]\n",
        "\n",
        "down_stack = tf.keras.Model(inputs=base_model.input, outputs=base_model_outputs)\n",
        "\n",
        "down_stack.trainable = False\n",
        "\n",
        "up_stack = [pix2pix.upsample(512, 3), pix2pix.upsample(256, 3), pix2pix.upsample(128, 3), pix2pix.upsample(64, 3),]\n"
      ],
      "metadata": {
        "id": "KdEkW6IRC0tf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we can define the function that will make our U-Net Model."
      ],
      "metadata": {
        "id": "udgKbAzTDclx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def U_net_model(output_channels:int, down_stack, up_stack):\n",
        "  inputs = tf.keras.layers.Input(shape=[128, 128, 3])\n",
        "  skips = down_stack(inputs)\n",
        "  outputs = skips[-1]\n",
        "  skips = reversed(skips[:-1])\n",
        "\n",
        "  for up, skip in zip(up_stack, skips):\n",
        "    outputs = up(outputs)\n",
        "    concatenate = tf.keras.layers.Concatenate()\n",
        "    outputs = concatenate([outputs, skip])\n",
        "\n",
        "  last = tf.keras.layers.Conv2DTranspose(filters=output_channels, kernel_size=3, strides=2, padding='same')\n",
        "  outputs = last(outputs)\n",
        "  return tf.keras.Model(inputs=inputs, outputs=outputs)"
      ],
      "metadata": {
        "id": "bfkx81pCDjCe"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we can finally create and compile the model."
      ],
      "metadata": {
        "id": "0oSXkajXDsQk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "OUTPUT_CLASSES = 3\n",
        "\n",
        "model = U_net_model(OUTPUT_CLASSES, down_stack, up_stack)\n",
        "model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])"
      ],
      "metadata": {
        "id": "922GC6FhD6cw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "It is now time to train the model we have just built using the train and test batches."
      ],
      "metadata": {
        "id": "0VJaMpmvD9k7"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "EPOCHS = 20\n",
        "VAL_SUBSPLITS = 5\n",
        "VALIDATION_STEPS = info.splits['test'].num_examples//BATCH_SIZE//VAL_SUBSPLITS\n",
        "\n",
        "model.fit(train_batches, epochs=EPOCHS, steps_per_epoch=STEPS_PER_EPOCH, validation_steps=VALIDATION_STEPS, validation_data=test_batches)"
      ],
      "metadata": {
        "id": "He629X5qE4j5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we can save the model in three different formats for later use. We will be saving it in the legacy .h5 format, the new .keras format, and the TensorFlow SavedModel."
      ],
      "metadata": {
        "id": "5jV9jQwyFKeb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.save(\"pets.h5\")\n",
        "model.save(\"pets.keras\")\n",
        "model.save(\"model/dogs\")"
      ],
      "metadata": {
        "id": "4PCorPKVG-4_"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Predict Using the Model"
      ],
      "metadata": {
        "id": "3YSb402_NTwm"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we will use our model to predict a mask against new data. But first, we must load the model off the disk."
      ],
      "metadata": {
        "id": "xmb_EUTQNjEh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def attempt_load(i):\n",
        "  try:\n",
        "    model = tf.keras.models.load_model('pets'+names[i])\n",
        "    return model\n",
        "  except:\n",
        "    attempt_load(i+1)\n",
        "\n",
        "names = ['.keras', '', '.h5']\n",
        "\n",
        "model = attempt_load(0)"
      ],
      "metadata": {
        "id": "jXiX1ghnN2py"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Now we need to create our predicted mask."
      ],
      "metadata": {
        "id": "bdb37QuKRyfG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def create_mask(pred_mask):\n",
        "  pred_mask = tf.math.argmax(pred_mask, axis=-1)\n",
        "  pred_mask = pred_mask[..., tf.newaxis]\n",
        "  return pred_mask[0]"
      ],
      "metadata": {
        "id": "7QUSGR3YSPna"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "We also need a function that can display the image and the predicted mask."
      ],
      "metadata": {
        "id": "ogVrMFQFSS6t"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def display(display_list):\n",
        "  plt.figure(figsize=(15, 15))\n",
        "  titles = ['Input Image', 'Predicted Mask']\n",
        "  for i in range(len(display_list)):\n",
        "    plt.subplot(1, len(display_list), i+1)\n",
        "    plt.title(titles[i])\n",
        "    plt.imshow(tf.keras.utils.array_to_img(display_list[i]))\n",
        "    plt.axis('off')\n",
        "  plt.show()"
      ],
      "metadata": {
        "id": "fyAdwN8KStsb"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Finally, we need a way to get the users' picture and use our model to predict the mask for that picture."
      ],
      "metadata": {
        "id": "ZSoCGr2hSvJN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def show_predictions(image_url, model):\n",
        "  image = tf.keras.utils.get_file(origin=image_url)\n",
        "  image = tf.keras.utils.load_img(image)\n",
        "  image = tf.keras.utils.img_to_array(image)\n",
        "  image = tf.image.resize(image, (128,128))\n",
        "  image = tf.cast(image, tf.float32) / 255.0\n",
        "  image = tf.expand_dims(image, axis=0)\n",
        "  pred_mask = model.predict(image)\n",
        "  display([image[0], create_mask(pred_mask)])"
      ],
      "metadata": {
        "id": "QrApNNhCeRYK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "And now lets wrap it all together!"
      ],
      "metadata": {
        "id": "eyqUFmFfeUT_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "while True:\n",
        "  url = input(\"Please enter an image url:\")\n",
        "  try:\n",
        "    image = tf.keras.utils.get_file(origin=url)\n",
        "    image = tf.keras.utils.load_img(image)\n",
        "    break\n",
        "  except:\n",
        "    print(\"That is not a valid link\")\n",
        "\n",
        "show_predictions(url, model)"
      ],
      "metadata": {
        "id": "is94T8gBeYle"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}