-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfashion_mnist.py
93 lines (70 loc) · 2.71 KB
/
fashion_mnist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""fashion_mnist.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1gncgD_q5H7Hb7D4jD04jGcrIsK-8JrRK
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
(xtrain,ytrain),(xtest,ytest) = tf.keras.datasets.fashion_mnist.load_data()
plt.imshow(xtrain[0])
plt.title(f"object = {ytrain[0]}")
plt.colorbar()
xtrain[0].shape
xtrain.shape
label_to_remove = 8
def remove_label(X, y, label):
mask = y != label # Create a mask where the label is not equal to the target label
return X[mask], y[mask]
# Remove class 8 from both training and testing datasets
xtrain, ytrain = remove_label(xtrain, ytrain, label_to_remove)
xtest, ytest = remove_label(xtest, ytest, label_to_remove)
label_to_remove = 6
def remove_label(X, y, label):
mask = y != label # Create a mask where the label is not equal to the target label
return X[mask], y[mask]
# Remove class 8 from both training and testing datasets
xtrain, ytrain = remove_label(xtrain, ytrain, label_to_remove)
xtest, ytest = remove_label(xtest, ytest, label_to_remove)
class_images = {}
for img, label in zip(xtrain, ytrain):
if label not in class_images:
class_images[label] = img
if len(class_images) == 10: # Stop when we have all 10 classes
break
plt.figure(figsize=(10, 2))
for i in range(10):
if i in class_images:
plt.imshow(class_images[i])
plt.title(f"Class {i}")
plt.show()
"""NORMALIZATION"""
xtrain,xtest = xtrain/255,xtest/255
"""NEURAL NETWORKS USING TENSORFLOW ANS KERAS."""
model = tf.keras.Sequential([
tf.keras.Input(shape = (28,28)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation = 'relu'),
tf.keras.layers.Dense(10,activation = 'softmax')
])
model.compile(optimizer = 'adam', metrics = ['accuracy'],loss = "sparse_categorical_crossentropy")
model.fit(xtrain,ytrain,epochs = 20,verbose = 1)
loss , accuracy = model.evaluate(xtest,ytest)
"""CLASSIFICATION REPORT ON TEST DATA"""
y_pred = model.predict(xtest)
y_pred_classes = y_pred.argmax(axis=1)
print(classification_report(ytest, y_pred_classes))
np.argmax(model.predict(xtest)[0])
"""TEST WITH ANY NEW IMAGE"""
test_image = tf.keras.preprocessing.image.load_img("NEW_IMAGE_PATH")
test_image = tf.image.rgb_to_grayscale(test_image)
test_image = tf.keras.preprocessing.image.img_to_array(test_image)
test_image = tf.image.resize(test_image,[28,28])
test_image = test_image.numpy().squeeze()
test_image = test_image.astype("float64")
plt.imshow(test_image)
test_image = test_image.reshape(1,28,28)
test_image = test_image/255
plt.title(f"ID = {np.argmax(model.predict(test_image))}")