<jupyter_start><jupyter_text>**CNN Model**<jupyter_code>import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout, BatchNormalization
from keras.utils import to_categorical<jupyter_output><empty_output><jupyter_text>with optimizer = sgd<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# reshape dataset to have a single channel
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))

train_images = train_images.astype('float32') / 255
test_images = test_images.astype('float32') / 255

model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', 
          input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='sgd',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5, batch_size=32)
test_loss, test_acc = model.evaluate(test_images,  test_labels)
print('Test accuracy:', test_acc)<jupyter_output>Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 1.1912 - accuracy: 0.5955
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.5342 - accuracy: 0.8088
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4646 - accuracy: 0.8336
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4289 - accuracy: 0.8494
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4005 - accuracy: 0.8589
313/313 [==============================] - 1s 2ms/step - loss: 0.4067 - accuracy: 0.8570
Test accuracy: 0.8569999933242798<jupyter_text>with optimizer = adam<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255

model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5, batch_size=32)
test_loss, test_acc = model.evaluate(test_images,  test_labels)
print('Test accuracy:', test_acc)<jupyter_output>Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.6470 - accuracy: 0.7758
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3193 - accuracy: 0.8848
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2786 - accuracy: 0.8992
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2475 - accuracy: 0.9089
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2175 - accuracy: 0.9206
313/313 [==============================] - 1s 2ms/step - loss: 0.2841 - accuracy: 0.9021
Test accuracy: 0.9021000266075134<jupyter_text>with padding = same<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', padding="same", input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (5, 5), activation='relu', padding="same"))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5, batch_size=32)
test_loss, test_acc = model.evaluate(test_images,  test_labels)
print('Test accuracy:', test_acc)<jupyter_output>Epoch 1/5
1875/1875 [==============================] - 5s 2ms/step - loss: 0.5849 - accuracy: 0.7910
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2929 - accuracy: 0.8956
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2405 - accuracy: 0.9119
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2143 - accuracy: 0.9229
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.1904 - accuracy: 0.9286
313/313 [==============================] - 1s 2ms/step - loss: 0.2526 - accuracy: 0.9109
Test accuracy: 0.9108999967575073<jupyter_text>with Batchnormalisation<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', input_shape=(28,28,1)))
model.add(BatchNormalization())
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))

model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
    
model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

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

model.fit(train_images, train_labels, epochs=5, batch_size=32)
test_loss, test_acc = model.evaluate(test_images,  test_labels)
print('Test accuracy:', test_acc)<jupyter_output>Epoch 1/5
1875/1875 [==============================] - 12s 6ms/step - loss: 0.7000 - accuracy: 0.7653
Epoch 2/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.3411 - accuracy: 0.8784
Epoch 3/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2800 - accuracy: 0.8997
Epoch 4/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2550 - accuracy: 0.9077
Epoch 5/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2260 - accuracy: 0.9189
313/313 [==============================] - 1s 3ms/step - loss: 0.2006 - accuracy: 0.9267
Test accuracy: 0.9266999959945679<jupyter_text>with leanring rate = 0.001<jupyter_code>learning_rate=0.001
optimizer = tf.optimizers.Adam( learning_rate )

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', input_shape=(28,28,1)))
model.add(BatchNormalization())
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))

model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
    
model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model.add(BatchNormalization())
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

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

model.fit(train_images, train_labels, epochs=5, batch_size=32)
test_loss, test_acc = model.evaluate(test_images,  test_labels)
print('Test accuracy:', test_acc)<jupyter_output>Epoch 1/5
1875/1875 [==============================] - 12s 6ms/step - loss: 0.6883 - accuracy: 0.7680
Epoch 2/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.3295 - accuracy: 0.8831
Epoch 3/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2825 - accuracy: 0.9012
Epoch 4/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2552 - accuracy: 0.9096
Epoch 5/5
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2347 - accuracy: 0.9168
313/313 [==============================] - 1s 3ms/step - loss: 0.2064 - accuracy: 0.9261
Test accuracy: 0.9261000156402588<jupyter_text>with 25 epochs<jupyter_code>learning_rate=0.001
optimizer = tf.keras.optimizers.Adam( learning_rate )

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255

model1 = Sequential()
model1.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', input_shape=(28,28,1)))
model1.add(BatchNormalization())
model1.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model1.add(BatchNormalization())
model1.add(Dropout(0.25))

model1.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model1.add(MaxPooling2D(pool_size=(2, 2)))
model1.add(Dropout(0.25))
    
model1.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', strides=1, padding='same'))
model1.add(BatchNormalization())
model1.add(Dropout(0.25))
model1.add(Flatten())
model1.add(Dense(512, activation='relu'))
model1.add(BatchNormalization())
model1.add(Dropout(0.5))
model1.add(Dense(128, activation='relu'))
model1.add(BatchNormalization())
model1.add(Dropout(0.5))
model1.add(Dense(10, activation='softmax'))

model1.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

reduce_lr = tf.keras.callbacks.LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)
model1.fit(train_images, train_labels, epochs=25, batch_size=32, callbacks=[reduce_lr])
test_loss, test_acc = model1.evaluate(test_images,  test_labels)
print('Test accuracy:', test_acc)<jupyter_output>Epoch 1/25
1875/1875 [==============================] - 12s 6ms/step - loss: 0.6869 - accuracy: 0.7677
Epoch 2/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.3309 - accuracy: 0.8826
Epoch 3/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2667 - accuracy: 0.9067
Epoch 4/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2348 - accuracy: 0.9157
Epoch 5/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.2120 - accuracy: 0.9253
Epoch 6/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.1941 - accuracy: 0.9301
Epoch 7/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.1666 - accuracy: 0.9396
Epoch 8/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.1510 - accuracy: 0.9458
Epoch 9/25
1875/1875 [==============================] - 11s 6ms/step - loss: 0.1351 - accuracy: 0.9526
Epoch 10/25
1875/1875 [==============================] - 11s 6ms/step - l[...]<jupyter_text>CNN Confusion Matrix<jupyter_code>from scipy.stats import mode
from sklearn.metrics import confusion_matrix, classification_report, roc_auc_score
from sklearn.metrics import accuracy_score
import seaborn as sb
from sklearn.model_selection import ParameterGrid
from sklearn.svm import SVC, LinearSVC
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

y_pred1 = model1.predict(test_images)
y_pred1 = np.argmax(y_pred1,axis=1)
cm = confusion_matrix( y_pred1, test_labels)
plt.subplots(figsize=(10, 6))
sb.heatmap(cm, annot = True, fmt = 'g')
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()<jupyter_output><empty_output><jupyter_text>CNN Classification report<jupyter_code>from sklearn.metrics import roc_auc_score
from sklearn.metrics import log_loss

print (classification_report(y_pred1, test_labels))<jupyter_output>precision    recall  f1-score   support

           0       0.91      0.88      0.90      1034
           1       0.99      1.00      0.99       992
           2       0.93      0.92      0.92      1009
           3       0.94      0.95      0.95       996
           4       0.94      0.89      0.92      1056
           5       0.99      1.00      0.99       992
           6       0.79      0.86      0.82       919
           7       0.98      0.97      0.98      1020
           8       0.99      1.00      0.99       993
           9       0.97      0.98      0.98       989

    accuracy                           0.94     10000
   macro avg       0.94      0.94      0.94     10000
weighted avg       0.95      0.94      0.94     10000<jupyter_text>**SVM**<jupyter_code>from scipy.stats import mode
import numpy as np
from time import time
import pandas as pd
import os
import matplotlib.pyplot as matplot
import matplotlib
%matplotlib inline

import random
matplot.rcdefaults()
from IPython.display import display, HTML
from itertools import chain
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
import seaborn as sb
from sklearn.model_selection import ParameterGrid
from sklearn.svm import SVC, LinearSVC
import sys
import time
import numpy as np
import pickle
from sklearn import model_selection, svm, preprocessing
from sklearn.metrics import accuracy_score,confusion_matrix
# from Fashion_MNIST_Loader.mnist_loader import MNIST
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
fashion_mnist = keras.datasets.fashion_mnist
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.svm import SVC, LinearSVC

def convert(nparray):
    l=[]
    dims=nparray.shape
    for i in range(dims[0]):
        l.append(nparray[i].flatten())
    l=np.array(l)
    return l
train_images_mod = convert(train_images)
test_images_mod  = convert(test_images)

# SVM with rbf kernel.

pipe_svm_1 = Pipeline([('pca', PCA(n_components=25)),('clf',SVC(C=100,kernel='rbf',gamma=0.1))])
pipe_svm_1.fit(train_images_mod,train_labels)
pipe_svm_1_pred_labels = pipe_svm_1.predict(test_images_mod)
pipe_svm_1_acc = accuracy_score(pipe_svm_1_pred_labels,test_labels)
print('The accuracy of SVM classifier with rbf kernel  :' + str(pipe_svm_1_acc))

# SVM with polynomial kernel.

pipe_svm_2 = Pipeline([('pca', PCA(n_components=25)),('clf', SVC(C=100,kernel='poly',degree=5))])
pipe_svm_2.fit(train_images_mod,train_labels)
pipe_svm_2_pred_labels = pipe_svm_2.predict(test_images_mod)
pipe_svm_2_acc = accuracy_score(pipe_svm_2_pred_labels,test_labels)
print('The accuracy of SVM classifier with polynomial kernel  :' + str(pipe_svm_2_acc))


cm1 = confusion_matrix(test_labels, pipe_svm_2_pred_labels)
plt.subplots(figsize=(10, 6))
sb.heatmap(cm1, annot = True, fmt = 'g')
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()<jupyter_output><empty_output><jupyter_text>**Random Forest**<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA


pipe_rf_final = Pipeline([('pca', PCA(n_components=25)),
                        ('rf', RandomForestClassifier(n_estimators=10,bootstrap=True,min_samples_split=100,max_features='sqrt',max_depth=10))])
pipe_rf_final.fit(train_images_mod,train_labels)
new_labels_rf = pipe_rf_final.predict(test_images_mod)
rf_accuracy = accuracy_score(test_labels,new_labels_rf)
print('The accuracy for Random Forest :' + str(rf_accuracy))
cm2 = confusion_matrix(test_labels, new_labels_rf)
plt.subplots(figsize=(10, 6))
sb.heatmap(cm2, annot = True, fmt = 'g')
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20304316665747837, 'fraction_numerical': 0.09195655769336789, 'mean_word_length': 5.287694974003466, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '102871', 'license': 'MIT', 'n_tokens_mistral': 7559, 'n_tokens_neox': 6431, 'n_words': 1285, 'path': 'CaseStudy_TF_SVM_1.ipynb', 'repo_name': 'sumanreddy11/CNN', 'size': '18139'}