Creating prediction mask utility
We will generate a function which will show us the real image its true mask and the predicted mask in a single row. It is more of a visualization utility function
Python3
def create_mask(pred_mask): pred_mask = tf.argmax(pred_mask, axis = - 1 ) pred_mask = pred_mask[..., tf.newaxis] return pred_mask[ 0 ] def show_predictions(dataset = None , num = 1 ): if dataset: for image, mask in dataset.take(num): pred_mask = model.predict(image) display_images([image[ 0 ], mask[ 0 ], create_mask(pred_mask)]) else : display_images([sample_image, sample_mask, create_mask(model.predict(sample_image[tf.newaxis, ...]))]) show_predictions() |
Output:
Image Segmentation Using TensorFlow
Image segmentation refers to the task of annotating a single class to different groups of pixels. While the input is an image, the output is a mask that draws the region of the shape in that image. Image segmentation has wide applications in domains such as medical image analysis, self-driving cars, satellite image analysis, etc. There are different types of image segmentation techniques like semantic segmentation, instance segmentation, etc. To summarize the key goal of image segmentation is to recognize and understand what’s in an image at the pixel level.
For the image segmentation task, we will use “The Oxford-IIIT Pet Dataset” which is free to use dataset. They have 37 category pet 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, head ROI, and pixel-level trimap segmentation. Each pixel is classified into one of the three categories:
- Pixel belonging to the pet
- Pixel bordering the pet
- Pixel belongs neither in class 1 nor in class 2
Contact Us