0% found this document useful (0 votes)
5 views

DL3

The document outlines a process for training a convolutional neural network using the Fashion MNIST dataset with Keras. It includes steps for loading the dataset, preprocessing the images, defining the model architecture, compiling, training, and evaluating the model. Finally, it visualizes the predictions made by the model on test images.

Uploaded by

yaseeniqbal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DL3

The document outlines a process for training a convolutional neural network using the Fashion MNIST dataset with Keras. It includes steps for loading the dataset, preprocessing the images, defining the model architecture, compiling, training, and evaluating the model. Finally, it visualizes the predictions made by the model on test images.

Uploaded by

yaseeniqbal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# Import Libraries

import keras

import numpy as np

import matplotlib.pyplot as plt

fashion_mnist = keras.datasets.fashion_mnist

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

print(train_labels.shape)

# Class names are not included

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',

'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# Training Data

train_images.shape

# Pixel Values fall between 0 to 255, We need to scale them between 0 to 1

train_images = train_images / 255.0

test_images = test_images / 255.0

# Verify data is in correct format

plt.figure(figsize = (10, 10))

for i in range(25):

plt.subplot(5, 5, i + 1)

plt.xticks([])

plt.yticks([])

plt.grid(False)

plt.imshow(train_images[i], cmap = plt.cm.binary)

plt.xlabel(class_names[train_labels[i]])

plt.show
# Set up Layers

model = keras.Sequential([

keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1 )),

keras.layers.MaxPooling2D(pool_size=(2, 2)),

keras.layers.Flatten(),

keras.layers.Dense(128, activation='relu'),

keras.layers.Dense(10, activation='softmax')

])

model.summary()
# Compile the model

model.compile(optimizer = 'adam', loss = keras.losses.SparseCategoricalCrossentropy(),metrics =


['accuracy'])

# Train the model

model.fit(train_images, train_labels, epochs = 5, verbose=1)

# Test the accuracy on test data

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose = 2)

print("\n Test accuracy = ", test_acc*100)

print("\n Test loss = ", test_loss)

# predictions = probability_model.predict(test_images)

predictions = model.predict(test_images)

def plot_image(i, predictions_array, true_label, img):

true_label, img = true_label[i], img[i]

plt.grid(False)

plt.xticks([])

plt.yticks([])

plt.imshow(img, cmap=plt.cm.binary)

predicted_label = np.argmax(predictions_array)

if predicted_label == true_label:

color = 'blue'

else:

color = 'red'

plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],

100*np.max(predictions_array),

class_names[true_label]),

color=color)

# Let us plot the predictions, red means incorrect while blue means correct prediction.

rows = 5
cols = 3

total_images = rows * cols

plt.figure(figsize = (10, 10))

for i in range(total_images):

plt.subplot(rows, cols, i + 1)

plot_image(i, predictions[i], test_labels, test_images)

plt.tight_layout()

plt.show()

You might also like