# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/NidhiChaurasia/LGMVIP-DataScience/blob/main/Fashion_Accessories_prediction_model_using_Convolutional_Neural_Network.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="8GFYbcZrsUT-"
# Neural Networks for Machine Learning & CNN | Fake Images with ML

# + id="u9ZSPz_IqyVz"
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
import seaborn as sns
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# + id="R5xkWrkFroa3"
mnist = tf.keras.datasets.fashion_mnist

# + colab={"base_uri": "https://localhost:8080/"} id="3T_KlO55sgj4" outputId="76345552-4959-4c24-8cb6-ae31e620a36f"
(x_train,y_train),(x_test,y_test) = mnist.load_data()

# + colab={"base_uri": "https://localhost:8080/"} id="tXsBLVH6yIvO" outputId="05d960a8-4798-4614-9578-ddf13f1e64c2"
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 683} id="Qbg4P5oGy6_L" outputId="a434049e-32c9-4eeb-ba44-b0d5bc91492d"
plt.figure(figsize = (12,12))
for i in range(144):
  plt.subplot(12,12,i+1)
  plt.axis('off')
  plt.imshow(x_train[i],cmap='pink')

# + [markdown] id="2nHS_XIF107x"
# There are 6000 images per sample as shown in the below bar chart:

# + colab={"base_uri": "https://localhost:8080/", "height": 338} id="IytINA-hz9kk" outputId="c307a681-93b8-49dc-f9ae-c4669b5710a3"
sns.countplot(y_train)

# + id="IDIEZ6qQ1wEk"
x_train = x_train.reshape([60000,28,28,1])
x_test = x_test.reshape([10000,28,28,1])

# + id="wJ85Z_zg3Pw7"
from keras.utils.np_utils import to_categorical
y_train = to_categorical(y_train,num_classes=10)
y_test = to_categorical(y_test,num_classes=10)

# + colab={"base_uri": "https://localhost:8080/"} id="GVedVvTC4a67" outputId="64239df0-c739-4134-9559-6f1d23ed23b2"
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

# + id="jkS8IUQo5XCz"
train_datagen = ImageDataGenerator(
    rescale = 1./255,
    rotation_range = 10,
    zoom_range = 0.1,
    width_shift_range = 0.1,
    height_shift_range  = 0.1,
)
train_datagen.fit(x_train)

# + id="6V1JcCkFYQiy"
val_datagen = ImageDataGenerator(rescale = 1./255)
val_datagen.fit(x_test)

# + [markdown] id="FNRTurGSia7p"
# Building a Convolutional Neural Network : The Architecture

# + colab={"base_uri": "https://localhost:8080/"} id="X8eiZqlgYziz" outputId="6f126c0e-c5c1-4dce-c5f6-f12cfebd95e8"
model = tf.keras.models.Sequential([
                                    tf.keras.layers.Conv2D(64 , (3,3),padding = 'same',activation = 'relu',input_shape =(28,28,1)),
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    tf.keras.layers.Conv2D(64 , (3,3),activation = 'relu'),
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    tf.keras.layers.Conv2D(128 , (3,3),activation = 'relu'), #128 no. of filters in third convolution
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    tf.keras.layers.Flatten(),
                                    tf.keras.layers.Dense(256,activation = 'relu'),
                                    tf.keras.layers.Dense(10,activation= 'softmax')
])
                                    
model.summary()

# + id="nwAaHIpFiisZ"
from keras.optimizers import Adam
model.compile(optimizer=Adam(),
              loss = 'categorical_crossentropy',
              metrics = ['accuracy'])

# + [markdown] id="TIibWjCGj7fo"
# # Training the model

# + colab={"base_uri": "https://localhost:8080/"} id="1WDBrjRPjBf9" outputId="5f042248-3c58-4ec8-af78-80bf47e36d4b"
history = model.fit(
train_datagen.flow(x_train,y_train,batch_size=64),
validation_data = val_datagen.flow(x_test,y_test),
batch_size = 64,
epochs = 20,
verbose = 1,
)

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="G3o9_wKfk6wk" outputId="02a3b7c7-f722-4686-e04c-9916408f69e5"
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

plt.figure()
plt.plot(acc,color = 'purple',label = 'Training Acuracy')
plt.plot(val_acc,color = 'blue',label = 'Validation Accuracy')
plt.legend()

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="YT6cCGp5oUJn" outputId="d3657577-3f5d-4018-e3d0-751b76edbb67"
loss = history.history['loss']
val_loss = history.history['val_loss']

plt.figure()
plt.plot(loss,color = 'purple',label = 'Training Loss')
plt.plot(val_loss,color = 'blue',label = 'Validation Loss')
plt.legend()

StarcoderdataJupyterScriptsDedupFiltered