# -*- coding: utf-8 -*-
"""Fashion_MNIST.ipynb
# Deep Neural Networks
**Fashion MNIST Clothing Classification**

‘Deep Learning’ means using a neural network with several layers of nodes between input and output

It is a dataset comprised of 60,000 small square 28×28 pixel grayscale images of items of 10 types of clothing, such as shoes, t-shirts, dresses, and more. The mapping of all 0-9 integers to class labels is listed below.

- 0: T-shirt/top
- 1: Trouser
- 2: Pullover
- 3: Dress
- 4: Coat
- 5: Sandal
- 6: Shirt
- 7: Sneaker
- 8: Bag
- 9: Ankle boot

It is a more challenging classification problem than MNIST and top results are achieved by deep learning convolutional neural networks with a classification accuracy of about 90% to 95% on the hold out test dataset.

![alt text](https://machinelearningmastery.com/wp-content/uploads/2019/02/Plot-of-a-Subset-of-Images-from-the-Fashion-MNIST-Dataset-1024x768.png)

The Fashion MNIST dataset was developed as a response to the wide use of the MNIST dataset, that has been effectively “solved” given the use of modern convolutional neural networks.
"""

import numpy as np
import random as rn
rn.seed(11)
np.random.seed(11)
import csv
import pandas as pd
from sklearn.datasets import make_blobs,make_moons, load_iris, make_circles, load_boston
import matplotlib.pyplot as plt
from numpy import loadtxt
from sklearn.model_selection import train_test_split
import tensorflow as tf
# first neural network with keras tutorial
from numpy import loadtxt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout, Conv1D, BatchNormalization, Flatten, Conv2D, Input
from tensorflow.keras import backend as K

fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images/255.
test_images = test_images/255.

def plot_history(history):
  plt.plot(history.history['loss'])
  plt.plot(history.history['val_loss'])
  plt.title('model loss')
  plt.ylabel('loss')
  plt.xlabel('epoch')
  plt.legend(['train', 'test'], loc='upper left')
  plt.show()
  return

def plot_acc(acc):
  plt.plot(history.history['accuray'])
  plt.plot(history.history['val_accuray'])
  plt.title('model accuray')
  plt.ylabel('accuray')
  plt.xlabel('epoch')
  plt.legend(['train', 'test'], loc='upper left')
  plt.show()
  return

print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)

# Neural Network Parameters
E=10 # Epocs
BS=100 # Batch size

# K.clear_session()
model = Sequential()

model.add(Input(shape=(28,28)))
model.add(Flatten())
model.add(Dense(25, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam' ,metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

K.clear_session()
model = Sequential()

model.add(Input(shape=(28,28)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam',metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

K.clear_session()
model = Sequential()

model.add(Input(shape=(28,28)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam',metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

K.clear_session()
model = Sequential()

model.add(Input(shape=(28,28)))
model.add(Flatten())
for i in range(5):
  model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam',metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

K.clear_session()
model = Sequential()

model.add(Input(shape=(28,28)))
model.add(Flatten())
for i in range(10):
  model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam',metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

K.clear_session()
model = Sequential()

model.add(Input(shape=(28,28)))
model.add(Flatten())
for i in range(20):
  model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam',metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

train_images = train_images.reshape((train_images.shape[0], 28, 28, 1))
test_images = test_images.reshape((test_images.shape[0], 28, 28, 1))

K.clear_session()
model = Sequential()
model.add(Conv2D(32, (3,3), strides = (2,2),activation='relu',input_shape=(28,28,1)))
model.add(BatchNormalization())
model.add(Conv2D(64, (3,3), strides = (2,2),activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Conv2D(128, (3,3), strides = (2,2),activation='relu'))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.summary()

# compile the keras model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), optimizer='adam',metrics = ['accuracy'])

# fit the keras model on the dataset
history = model.fit(x=train_images, y=train_labels, epochs=E, batch_size=BS, validation_data=(test_images,test_labels),verbose=1)
plot_history(history)
print('Minimum Validation Loss: ',min(history.history['val_loss']))
print('Maximum Accuracy: ',max(history.history['val_accuracy']))

