Week 3 Assignment 2 : predicting segmentation on own images? (solved)

Just for fun: I would like to test the U-Net model on an image of my own that I have uploaded. Any pointers in how I can do the preprocessing on a single image and run unet.predict() to get the predicted mask?

Edit:

Quick hack below works for me. Just take some pictures from your own street scenery (be careful!) and try it.


def show_own_image(input_image):
    """
    Display input image and predicted mask of image uploaded to
    your notebook directory.
    """
        
    image = tf.io.read_file('{}'.format(input_image))
    image = tf.image.decode_png(image, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, (96, 128), method='nearest')
    image = image / 255.
    
    pred_mask = create_mask(unet.predict(image[tf.newaxis, ...]))
          
    title = ['Input Image', 'Predicted Mask']
    img = [image, pred_mask]
    
    plt.figure(figsize=(15, 15))
       
    for i in range(len(img)):
        plt.subplot(1, 2, i+1)
        plt.title(title[i])
        plt.imshow(tf.keras.preprocessing.image.array_to_img(img[i]))
        plt.axis('off')
    plt.show()
1 Like

Thanks, @jwarmenhoven, for sharing your “use your own image” code! Looks good!

1 Like