Path: blob/main/Image Segmentation Tutorial - Unet - Pet Dataset/Image Segmentation Tutorial - UNet - Oxford Pet Dataset.ipynb
578 views
Kernel: Python 3 (ipykernel)
Import Modules
In [ ]:
In [4]:
Load the Dataset
In [5]:
In [6]:
Out[6]:
tfds.core.DatasetInfo(
name='oxford_iiit_pet',
full_name='oxford_iiit_pet/3.2.0',
description="""
The Oxford-IIIT pet dataset is a 37 category pet image dataset with roughly 200
images for each class. The images have large variations in scale, pose and
lighting. All images have an associated ground truth annotation of breed.
""",
homepage='http://www.robots.ox.ac.uk/~vgg/data/pets/',
data_dir='/root/tensorflow_datasets/oxford_iiit_pet/3.2.0',
file_format=tfrecord,
download_size=773.52 MiB,
dataset_size=774.69 MiB,
features=FeaturesDict({
'file_name': Text(shape=(), dtype=string),
'image': Image(shape=(None, None, 3), dtype=uint8),
'label': ClassLabel(shape=(), dtype=int64, num_classes=37),
'segmentation_mask': Image(shape=(None, None, 1), dtype=uint8),
'species': ClassLabel(shape=(), dtype=int64, num_classes=2),
}),
supervised_keys=('image', 'label'),
disable_shuffling=False,
splits={
'test': <SplitInfo num_examples=3669, num_shards=4>,
'train': <SplitInfo num_examples=3680, num_shards=4>,
},
citation="""@InProceedings{parkhi12a,
author = "Parkhi, O. M. and Vedaldi, A. and Zisserman, A. and Jawahar, C.~V.",
title = "Cats and Dogs",
booktitle = "IEEE Conference on Computer Vision and Pattern Recognition",
year = "2012",
}""",
)
In [7]:
Out[7]:
{'train': <_PrefetchDataset element_spec={'file_name': TensorSpec(shape=(), dtype=tf.string, name=None), 'image': TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name=None), 'label': TensorSpec(shape=(), dtype=tf.int64, name=None), 'segmentation_mask': TensorSpec(shape=(None, None, 1), dtype=tf.uint8, name=None), 'species': TensorSpec(shape=(), dtype=tf.int64, name=None)}>,
'test': <_PrefetchDataset element_spec={'file_name': TensorSpec(shape=(), dtype=tf.string, name=None), 'image': TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name=None), 'label': TensorSpec(shape=(), dtype=tf.int64, name=None), 'segmentation_mask': TensorSpec(shape=(None, None, 1), dtype=tf.uint8, name=None), 'species': TensorSpec(shape=(), dtype=tf.int64, name=None)}>}
Preprocessing Steps
In [8]:
In [9]:
In [10]:
Explanatory Data Analysis
In [11]:
In [12]:
Out[12]:
Define U-Net Model
In [13]:
In [24]:
In [25]:
In [26]:
In [27]:
Out[27]:
Train the Model
In [ ]:
Epoch 1/20
57/57 [==============================] - 82s 1s/step - loss: 0.9267 - accuracy: 0.5709 - val_loss: 0.8328 - val_accuracy: 0.5746
Epoch 2/20
57/57 [==============================] - 69s 1s/step - loss: 0.7601 - accuracy: 0.6467 - val_loss: 0.6687 - val_accuracy: 0.7170
Epoch 3/20
57/57 [==============================] - 74s 1s/step - loss: 0.6364 - accuracy: 0.7325 - val_loss: 0.6207 - val_accuracy: 0.7392
Epoch 4/20
57/57 [==============================] - 74s 1s/step - loss: 0.5760 - accuracy: 0.7619 - val_loss: 0.5382 - val_accuracy: 0.7782
Epoch 5/20
57/57 [==============================] - 74s 1s/step - loss: 0.5225 - accuracy: 0.7853 - val_loss: 0.4846 - val_accuracy: 0.8035
Epoch 6/20
57/57 [==============================] - 74s 1s/step - loss: 0.4766 - accuracy: 0.8059 - val_loss: 0.4674 - val_accuracy: 0.8099
Epoch 7/20
57/57 [==============================] - 74s 1s/step - loss: 0.4492 - accuracy: 0.8163 - val_loss: 0.4196 - val_accuracy: 0.8284
Epoch 8/20
57/57 [==============================] - 74s 1s/step - loss: 0.4142 - accuracy: 0.8312 - val_loss: 0.4128 - val_accuracy: 0.8326
Epoch 9/20
57/57 [==============================] - 68s 1s/step - loss: 0.3946 - accuracy: 0.8385 - val_loss: 0.4061 - val_accuracy: 0.8333
Epoch 10/20
57/57 [==============================] - 68s 1s/step - loss: 0.3772 - accuracy: 0.8455 - val_loss: 0.3846 - val_accuracy: 0.8434
Epoch 11/20
57/57 [==============================] - 68s 1s/step - loss: 0.3708 - accuracy: 0.8497 - val_loss: 0.4035 - val_accuracy: 0.8361
Epoch 12/20
57/57 [==============================] - 68s 1s/step - loss: 0.3518 - accuracy: 0.8557 - val_loss: 0.3592 - val_accuracy: 0.8550
Epoch 13/20
57/57 [==============================] - 68s 1s/step - loss: 0.3337 - accuracy: 0.8632 - val_loss: 0.3652 - val_accuracy: 0.8547
Epoch 14/20
57/57 [==============================] - 74s 1s/step - loss: 0.3331 - accuracy: 0.8639 - val_loss: 0.3535 - val_accuracy: 0.8586
Epoch 15/20
57/57 [==============================] - 68s 1s/step - loss: 0.3224 - accuracy: 0.8678 - val_loss: 0.3461 - val_accuracy: 0.8612
Epoch 16/20
57/57 [==============================] - 68s 1s/step - loss: 0.3328 - accuracy: 0.8649 - val_loss: 0.3445 - val_accuracy: 0.8623
Epoch 17/20
57/57 [==============================] - 74s 1s/step - loss: 0.2966 - accuracy: 0.8773 - val_loss: 0.3502 - val_accuracy: 0.8623
Epoch 18/20
57/57 [==============================] - 68s 1s/step - loss: 0.2947 - accuracy: 0.8778 - val_loss: 0.3976 - val_accuracy: 0.8500
Epoch 19/20
57/57 [==============================] - 68s 1s/step - loss: 0.2855 - accuracy: 0.8814 - val_loss: 0.3323 - val_accuracy: 0.8699
Epoch 20/20
57/57 [==============================] - 68s 1s/step - loss: 0.2823 - accuracy: 0.8830 - val_loss: 0.3303 - val_accuracy: 0.8712
Visualize the Results
In [33]:
Out[33]:
Test Predictions
In [46]:
In [47]:
In [48]:
Out[48]:
2/2 [==============================] - 0s 176ms/step
2/2 [==============================] - 0s 123ms/step
2/2 [==============================] - 0s 122ms/step
2/2 [==============================] - 0s 122ms/step
2/2 [==============================] - 0s 124ms/step
2/2 [==============================] - 0s 119ms/step
2/2 [==============================] - 0s 122ms/step
2/2 [==============================] - 0s 120ms/step
2/2 [==============================] - 0s 123ms/step
2/2 [==============================] - 0s 120ms/step
In [16]:
In [16]: