<jupyter_start><jupyter_text>Importing all necessary libraries<jupyter_code>#Importing necessary libraries
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as  plt
from tensorflow import keras

from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.layers import Conv2D, MaxPool2D, BatchNormalization
from keras.losses import categorical_crossentropy,sparse_categorical_crossentropy
from keras.optimizers import Adam,RMSprop,SGD
from sklearn.metrics import confusion_matrix,classification_report<jupyter_output><empty_output><jupyter_text>Importing Fashion dataset and splitting it into Train & TestConverting the labels to categorical of float32Reshaping images dataset-> scaling<jupyter_code>#Loading the fashion data set and splitting Splitting MNIST dataset into train,test
(train_images, train_labels), (test_images, test_labels)=tf.keras.datasets.fashion_mnist.load_data()
#converting labels to categorical and with num_class as 10 ( Outputs )
print ('Before scaling',train_images[0])

#Scalling the image dataset
train_images = train_images / 255.0
test_images = test_images / 255.0

print('After scaling',train_images[0])<jupyter_output>Before scaling [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   1   0   0  13  73   0
    0   1   4   0   0   0   0   1   1   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   3   0  36 136 127  62
   54   0   0   0   1   3   4   0   0   3]
 [  0   0   0   0   0   0   0   0   0   0   0   0   6   0 102 204 176 134
  144 123  23   0   0   0   0  12  10   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0 155 236 207 178
  107 156 161 109  64  23  77 130  72  15]
 [  0   0   0   0   0   0   0   0   0   0   0   1   0  69 207 223 218 216
  216 163 127 121 122 146 141  88 172  66]
 [  0   0   0   0   0   0   0   0   0   1   1   1[...]<jupyter_text>Defining Class Plotting first 25 images<jupyter_code>#Defining 10 class name for output
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
n=25
plt.figure(figsize=(20,4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n,i+1)
    lbl=class_names[train_labels[i]]
    plt.title(lbl)
    plt.imshow(train_images[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

# plt.imshow(train_images[i], cmap='gray_r')<jupyter_output><empty_output><jupyter_text>Reshaping the train & test to 28x28x1<jupyter_code>#Reshaping train_images & test_images
print('Before reshaping')
print(train_images.shape)
print(test_images.shape)

train_images=array(train_images).reshape(60000, 28,28,1)
test_images=array(test_images).reshape(10000,28,28,1)

print('After reshaping')
print(train_images.shape)
print(test_images.shape)<jupyter_output>Before reshaping
(60000, 28, 28)
(10000, 28, 28)
After reshaping
(60000, 28, 28, 1)
(10000, 28, 28, 1)<jupyter_text>Building CNN for the fashion MNIST dataset<jupyter_code>model = Sequential()

#Single layer perceptron
model.add(Conv2D(32, (3,3),strides=(1,1), input_shape=[28,28,1]))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))

model.summary()<jupyter_output>Model: "sequential_65"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_65 (Conv2D)           (None, 26, 26, 32)        320       
_________________________________________________________________
activation_184 (Activation)  (None, 26, 26, 32)        0         
_________________________________________________________________
max_pooling2d_62 (MaxPooling (None, 13, 13, 32)        0         
_________________________________________________________________
flatten_62 (Flatten)         (None, 5408)              0         
_________________________________________________________________
dense_124 (Dense)            (None, 128)               692352    
_________________________________________________________________
activation_185 (Activation)  (None, 128)               0         
_____________________________________________________[...]<jupyter_text>Compiling the model with **Sparse Categorical Crossentropy** and fitting it<jupyter_code>OPTIMIZER= Adam()
model.compile(loss='sparse_categorical_crossentropy', optimizer=OPTIMIZER, metrics=['accuracy'])

history=model.fit(train_images,train_labels,batch_size=1000,epochs=30)<jupyter_output>Epoch 1/30
60/60 [==============================] - 0s 7ms/step - loss: 0.7083 - accuracy: 0.7672
Epoch 2/30
60/60 [==============================] - 0s 7ms/step - loss: 0.4027 - accuracy: 0.8605
Epoch 3/30
60/60 [==============================] - 0s 7ms/step - loss: 0.3558 - accuracy: 0.8772
Epoch 4/30
60/60 [==============================] - 0s 7ms/step - loss: 0.3269 - accuracy: 0.8860
Epoch 5/30
60/60 [==============================] - 0s 7ms/step - loss: 0.3053 - accuracy: 0.8935
Epoch 6/30
60/60 [==============================] - 0s 7ms/step - loss: 0.2868 - accuracy: 0.8990
Epoch 7/30
60/60 [==============================] - 0s 7ms/step - loss: 0.2743 - accuracy: 0.9032
Epoch 8/30
60/60 [==============================] - 0s 7ms/step - loss: 0.2649 - accuracy: 0.9056
Epoch 9/30
60/60 [==============================] - 0s 7ms/step - loss: 0.2521 - accuracy: 0.9112
Epoch 10/30
60/60 [==============================] - 0s 7ms/step - loss: 0.2447 - accuracy: 0.9130
Epoch 11/30
60/60 [[...]<jupyter_text>Compiling the model with **Categorical Crossentropy** and fitting it<jupyter_code>train_labels=tf.keras.utils.to_categorical(train_labels, num_classes=10, dtype='float32')
test_labels=tf.keras.utils.to_categorical(test_labels, num_classes=10, dtype='float32')
OPTIMIZER= Adam()
model.compile(loss='categorical_crossentropy', optimizer=OPTIMIZER, metrics=['accuracy'])

history=model.fit(train_images,train_labels,batch_size=1000,epochs=30)<jupyter_output>Epoch 1/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1321 - accuracy: 0.9537
Epoch 2/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1221 - accuracy: 0.9581
Epoch 3/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1181 - accuracy: 0.9598
Epoch 4/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1154 - accuracy: 0.9603
Epoch 5/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1113 - accuracy: 0.9617
Epoch 6/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1085 - accuracy: 0.9638
Epoch 7/30
60/60 [==============================] - 0s 8ms/step - loss: 0.1038 - accuracy: 0.9650
Epoch 8/30
60/60 [==============================] - 0s 8ms/step - loss: 0.0996 - accuracy: 0.9672
Epoch 9/30
60/60 [==============================] - 0s 8ms/step - loss: 0.0987 - accuracy: 0.9671
Epoch 10/30
60/60 [==============================] - 0s 8ms/step - loss: 0.0935 - accuracy: 0.9691
Epoch 11/30
60/60 [[...]<jupyter_text>Saving the model -> Loading it again -> Evaluating to check accuracy<jupyter_code>model.save('my_model.h5')
loaded_model = tf.keras.models.load_model('my_model.h5')
loaded_model.summary()
score = loaded_model.evaluate(train_images, train_labels)
print("Test loss:", score[0])
print('Test accuracy:', score[1])<jupyter_output>1875/1875 [==============================] - 4s 2ms/step - loss: 0.0437 - accuracy: 0.9873
Test loss: 0.043698474764823914
Test accuracy: 0.9873166680335999<jupyter_text>Displaying confusion matrix and its classification report<jupyter_code>Test_labels=np.argmax(test_labels,axis=1)
model_confusion_matrix=confusion_matrix(Test_labels,y_pred)
print(model_confusion_matrix)
print(classification_report(Test_labels, y_pred, target_names=class_names))<jupyter_output>precision    recall  f1-score   support

 T-shirt/top       0.91      0.84      0.87      1000
     Trouser       0.99      0.97      0.98      1000
    Pullover       0.91      0.84      0.87      1000
       Dress       0.91      0.91      0.91      1000
        Coat       0.84      0.91      0.87      1000
      Sandal       0.98      0.98      0.98      1000
       Shirt       0.74      0.81      0.77      1000
     Sneaker       0.95      0.97      0.96      1000
         Bag       0.98      0.97      0.98      1000
  Ankle boot       0.97      0.96      0.97      1000

    accuracy                           0.92     10000
   macro avg       0.92      0.92      0.92     10000
weighted avg       0.92      0.92      0.92     10000<jupyter_text>Counting FP & FN from confusion matrix ie., Misclassification count<jupyter_code>mis_cal_count=0
for i in range(10):
  for j in range(10):
    if i!=j:
      mis_cal_count=miss_cal_count+model_confusion_matrix[i][j]
print('Total count of Misclassification:',mis_cal_count)<jupyter_output>Total count of Misclassification: 836<jupyter_text>Evaluating with different hyperparameters<jupyter_code>epochs=[10,30,50]
batch_size=[500,1000,1500]
Optimizer=['Adam','RMSprop','SGD']
j=0
result={}
for o in range(len(Optimizer)):
  for e in range(len(epochs)):
    for b in range(len(batch_size)):
      
      #Loading data
      (train_images, train_labels), (test_images, test_labels)=tf.keras.datasets.fashion_mnist.load_data()
      
      #Scalling the image dataset
      train_images = train_images / 255.0
      test_images = test_images / 255.0
      train_images=array(train_images).reshape(60000, 28,28,1)
      test_images=array(test_images).reshape(10000,28,28,1)
      
      #Creating Basic CNN
      model = Sequential()
      model.add(Conv2D(32, (3,3),strides=(1,1), input_shape=[28,28,1]))
      model.add(Activation('relu'))
      model.add(MaxPool2D(pool_size=(2,2)))
      model.add(Flatten())
      model.add(Dense(128))
      model.add(Activation('relu'))
      model.add(Dense(10))
      model.add(Activation('softmax'))

      train_labels=tf.keras.utils.to_categorical(train_labels, num_classes=10, dtype='float32')
      test_labels=tf.keras.utils.to_categorical(test_labels, num_classes=10, dtype='float32')
      
      #Model Copilation and fitting
      model.compile(loss='categorical_crossentropy', optimizer=Optimizer[o], metrics=['accuracy'])
      model.fit(train_images,train_labels,batch_size=batch_size[b],epochs=epochs[e])
      score = model.evaluate(train_images, train_labels)
      result2[Optimizer[o]+str(epochs[e])+str(batch_size[b])]=score[1]
      print(Optimizer[o],epochs[e],batch_size[b],':',score[1])
import pandas as pd
pd.DataFrame.from_dict(result2,orient='index')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16701160802663548, 'fraction_numerical': 0.10951138306487897, 'mean_word_length': 3.348200312989045, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '592186', 'license': 'MIT', 'n_tokens_mistral': 4550, 'n_tokens_neox': 3827, 'n_words': 921, 'path': 'Fashion_MNSIT_CNN.ipynb', 'repo_name': 'bvpkr123/github-slideshow', 'size': '11113'}