<jupyter_start><jupyter_text>Fashion MNIST-[<NAME>](rishit.tech) About Me[Twitter](https://twitter.com/rishit_dagli)[GitHub](https://github.com/Rishit-dagli)[Medium](https://medium.com/@rishit.dagli) Note: Please unzip the files with the code in the cell below before you move forward<jupyter_code># !unzip /Fashion MNIST/fashionmnist.zip<jupyter_output><empty_output><jupyter_text>Some imports<jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sns
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Load the data Let's now load the data using `pandas`<jupyter_code>data_train = pd.read_csv('Fashion MNIST/fashion-mnist_train.csv')
data_test = pd.read_csv('Fashion MNIST/fashion-mnist_test.csv')<jupyter_output><empty_output><jupyter_text>Some preprocessing Let's now specify the size of our image<jupyter_code>img_rows, img_cols = 28, 28
input_shape = (img_rows, img_cols, 1)<jupyter_output><empty_output><jupyter_text>Now we will split the feaures and labels<jupyter_code>x_train = np.array(data_train.iloc[:, 1:])
y_train = np.array(data_train.iloc[:, 0])
y_train
x_test = np.array(data_test.iloc[:, 1:])
y_test = np.array(data_test.iloc[:, 0])<jupyter_output><empty_output><jupyter_text>It is importnat to resize our data, we have 60000 train samples and 10000 test samples<jupyter_code>x_train = x_train.reshape(60000, 28, 28, 1)
x_train = x_train / 255.0
x_test = x_test.reshape(10000, 28, 28, 1)
x_test = x_test/255.0<jupyter_output><empty_output><jupyter_text>Model Let's define a few hyper parameters<jupyter_code>num_classes = 10
epochs = 10

img_rows, img_cols = 28, 28

optimizer = 'adam'
loss = 'sparse_categorical_crossentropy'<jupyter_output><empty_output><jupyter_text>And finally the model now<jupyter_code>model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_classes, activation='softmax')
])<jupyter_output><empty_output><jupyter_text>You can try experimenting with different optimizers<jupyter_code>model.compile(optimizer = optimizer, 
              loss = loss, 
              metrics=['accuracy'])
history = model.fit(x_train, 
                    y_train,
                   epochs = epochs)<jupyter_output>Train on 60000 samples
Epoch 1/10
60000/60000 [==============================] - 8s 135us/sample - loss: 0.3940 - accuracy: 0.8583
Epoch 2/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.2685 - accuracy: 0.9017
Epoch 3/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.2241 - accuracy: 0.9175
Epoch 4/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.1913 - accuracy: 0.9294
Epoch 5/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.1631 - accuracy: 0.9395
Epoch 6/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.1403 - accuracy: 0.9481
Epoch 7/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.1188 - accuracy: 0.9565
Epoch 8/10
60000/60000 [==============================] - 8s 126us/sample - loss: 0.0992 - accuracy: 0.9637
Epoch 9/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.0842 - accuracy: 0.9694
Epoch[...]<jupyter_text>Evaluating the model Let's see the test accuracy of our model<jupyter_code>test_loss, test_acc = model.evaluate(x_test, 
                                     y_test)
test_acc<jupyter_output><empty_output><jupyter_text>Seeing inside convolutions Have fun with this code adapted from [Laurence Moroney](http://www.laurencemoroney.com/) which enables us too see an image being processed inside a CNN<jupyter_code>import matplotlib.pyplot as plt
f, axarr = plt.subplots(3,4)
FIRST_IMAGE=0
SECOND_IMAGE=7
THIRD_IMAGE=26
CONVOLUTION_NUMBER = 1
from tensorflow.keras import models
layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)
for x in range(0,4):
  f1 = activation_model.predict(x_test[FIRST_IMAGE].reshape(1, 28, 28, 1))[x]
  axarr[0,x].imshow(f1[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
  axarr[0,x].grid(False)
  f2 = activation_model.predict(x_test[SECOND_IMAGE].reshape(1, 28, 28, 1))[x]
  axarr[1,x].imshow(f2[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
  axarr[1,x].grid(False)
  f3 = activation_model.predict(x_test[THIRD_IMAGE].reshape(1, 28, 28, 1))[x]
  axarr[2,x].imshow(f3[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
  axarr[2,x].grid(False)<jupyter_output><empty_output><jupyter_text>![index.png](attachment:index.png)<jupyter_code>predicted_classes = model.predict_classes(x_test)

y_true = data_test.iloc[:, 0]
correct = np.nonzero(predicted_classes==y_true)
incorrect = np.nonzero(predicted_classes!=y_true)
correct<jupyter_output><empty_output><jupyter_text>A sample image<jupyter_code>plt.imshow(x_test[0].reshape(28,28))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1754082612872238, 'fraction_numerical': 0.07050912584053795, 'mean_word_length': 5.796344647519582, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '21352', 'license': 'Apache-2.0', 'n_tokens_mistral': 2199, 'n_tokens_neox': 1898, 'n_words': 370, 'path': 'Day 6/Fashion MNIST.ipynb', 'repo_name': 'som23ya/10-Days-of-ML', 'size': '5211'}