Masking / Inserting Noise

For the needs of this project, we need to simulate a dataset of incomplete digits. So, let's write a function to mask small regions in the original image to form the noised dataset.

The idea is to mask an 8*8 region of the image with the top left corner of the mask falling between 9th and 13th pixel (between index 8 and 12) along both x and y-axis of the image. This is to make sure that we are always masking around the center part of the image.

The bigger the size of the mask, the harder it will be for the MNIST classifier to predict the right digit. 
def noising(image):
array = np.array(image)
i = random.choice(range(8,12)) # x coordinate for the top left corner of the mask
j = random.choice(range(8,12)) # y coordinate for the top left corner of the mask
array[i:i+8, j:j+8]=-1.0 # setting the pixels in the masked region to -1
return array

noised_train_data = np.array([*map(noising, X_train)])
noised_test_data = np.array([*map(noising, X_test)])
print('Noised train data Shape/Dimension : ', noised_train_data.shape)
print('Noised test data Shape/Dimension : ', noised_train_data.shape)
Feel free to experiment with the size of the masked region i.e try smaller/bigger, as well as the location of the mask on the image.
A plot of 9 scaled noised images after upscaling...
# Plot of 9 scaled noised images after upscaling
for i in range(0, 9):
plt.subplot(331+i) # plot of 3 rows and 3 columns
plt.axis('off') # turn off axis
plt.imshow(upscale(noised_train_data[i]), cmap='gray') # gray scale
Figure 14.5: Plot of nine noised/masked MNIST digits 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.254.61