MNIST digit recognization images in Tensorflow

Krunal Kapadiya
3 min readAug 21, 2020

In this article, I will introduce basic knowledge of Tensorflow to the real example of Tensorflow.

I will recommend you to read this article if you haven’t read it.

Above article will help you to learn the basic math operations in Tensorflow. It will be good to have work with session and placeholder.

Let’s move to the

We have 28*28 = 784 pixels of the images. In training.csv we have the correct digit of the number. We will create a model based on the training.csv file and will submit the output of the predicted values on testing.csv file.

# seperating labels and images [X_train = images, Y_train = numbers on respective image]
X_train = train_df.drop(labels = ["label"],axis = 1) # contains values of digits in 255 range
Y_train = train_df['label'] # contains digits
X_train = X_train.values.reshape(-1,28,28,1)/ 255 # reshaping arrays in tensors
# creating common method to display image
def displayImage(image):
plt.imshow(image[:,:,0], cmap=plt.cm.binary)

def displayImageWithPredictedValue(image, prediction):
print('Predicted output image is ', np.argmax(prediction))
plt.imshow(image[:,:,0], cmap=plt.cm.binary)
# displaying first first value
displayImage(X_train[0])

I am using Sequential model and used Flatten layer to convert tensors into arrays. Using relu activation(REctified Linear Units) with different input image parameters, I degraded features vectors to 64. Lastly, I used Softmax function with 10 output entries (0 to 9). I compiled model with adam optimzer and used loss function as sparse_categorical_crossentropy. At the end I trained model using data with 2 epochs. Epoch is training loop (forward and backward) to train model.

model = tf.keras.models.Sequential() # creating Sequential model
model.add(tf.keras.layers.Flatten()) # flattening the input arrays
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) # using relu activation function
model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu)) # using relu activation function
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax)) # activation function to get number of output

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # compiling model

model.fit(X_train, Y_train.values, epochs=5) # training model and fitting data
model.summary()

Now, splitting the sets.

# splitting data to evalueate model
X_train, X_val, Y_train, Y_val = train_test_split(X_train,
Y_train,
test_size=0.20,
random_state=42,
shuffle=True,
stratify=Y_train)

Finding the performance of the model.

val_loss , val_accuracy = model.evaluate(X_val, Y_val) # evaluating performance of the model
print(val_loss, val_accuracy)
predictions = model.predict([X_val])
displayImageWithPredictedValue(X_val[12], predictions[12])

Output:

test_df = test_df.values.reshape(-1,28,28,1)/255

Now finding the performance of the model.

predictions = model.predict([test_df])
displayImageWithPredictedValue(test_df[10], predictions[10])

4. Conclusion:

I created a model that can predict the correct number values from the images with accuracy 0.96614. This dataset can be further explored by applying classification methods such as SVM and K-nearest neighbours.

You can follow me on Kaggle and upvote this kernel to help others to learn it. Thank you :)

You can clap for this blog, share it and also make comments on it.

Thank you for reading it.

--

--

Krunal Kapadiya

Machine Learning | Google Certified Android Developer | Engineer @EInfochips https://krunal3kapadiya-app.web.app/