I am rather new to coding and I appreciate any help, but be gentle with me.
I am playing around with the MNIST database for neural networks, since I want to transfer the results to another problem. What I am trying do is to manipulate the MNIST training data set by including a set of images to the image to be classified. Allow me to structure the approach:
- While training the neural network the MNIST db provides an image of a handwritten digit (x_train) and its label/class (y_train)
- However I want to train the neural network not only with a single image input, but also feed the neural network with optional images to choose from
- So if I want the machine to classify the digit "5", I would have the image of the digit "5" and also a set of random images, which should have a random quantity, as the input:
-> Input = Image to classify "5" | Images to refer to "1", "4", "5", the next would be Image to classify "0" | Images to refer to "0", "9", "3", "5", "6" and so on...
-
The "images to refer to" should always include the "digit to classify" but not the "image to classify". Meaning the index of "Image to classify "5"" should not be the same as the index of "Images to refer to ... "5""
-
So far I managed to choose random images (digit_randomizer()) of digits of random quantity (random_with_N_digits()). What I miss is:
- Indices which exclude themselves: Index of "5" to classify is not the index of "5" to choose from
- The images to refer to should not have repeating digits
To 1.: Below you can see my function digit_randomizer(). I currently have no idea how to approach this problem, but with a nested loop checking for "np.where(j != i)"
To 2.: I am considering splitting y_train into 10 different sets of labels (each for one digit). However I don't know what kind of command I should write, since I would need to define a random quantity of images. Pick a random image from the 10 sets at random, while watching out for the index.
Here is my code so far:
import keras as k
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print('')
print('x_train shape:', x_train.shape)
# Reshaping the array to 4-dims so that it can work with the Keras API
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# Making sure that the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255
print('x_train shape reshaped:', x_train.shape)
print('Number of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])
classes = set(y_train)
print('Number of classes =', len(classes),'\n')
print('Classes: \n', classes, '\n')
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)
import random
import warnings
warnings.filterwarnings("ignore")
from random import randint
#select a random image from the training data
def digit_select():
for j in range(1):
j = np.random.choice(np.arange(0, len(y_train)), size = (1,))
digit = (x_train[j] * 255).reshape((28, 28)).astype("uint8")
imgplot = plt.imshow(digit)
plt.title(y_train[j])
imgplot.set_cmap('Greys')
plt.show()
# return between 1 or 10 images
def random_with_N_digits():
range_start = 0
range_end = 9
return randint(range_start, range_end)
# return 1 or 10 random images
def digit_randomizer():
for i in range(random_with_N_digits()):
i = np.random.choice(np.arange(0, len(y_train)), size = (1,))
image = (x_train[i] * 255).reshape((28, 28)).astype("uint8")
imgplot = plt.imshow(image)
imgplot.set_cmap('Greys')
plt.title(y_train[i])
plt.show()
Somehow digit_select should be excluded from digit_randomizer and digit_randomizer should only pick one image per class from y_train.
Any ideas are very much appreciated. Thank you very much.
Aucun commentaire:
Enregistrer un commentaire