-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFashionMNSIT models (CNN vs MLP).py
266 lines (207 loc) · 7.2 KB
/
FashionMNSIT models (CNN vs MLP).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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#This is a Machine learning model that predicts the Fashion MNIST dataset
#This program aims to define a Convolutional Neural Network
# and compare it's accuracy and time with Multilayer Perceptron (MLP)
#Coded by Mahmoud-K-Ismail
#Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoader
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential, \
Conv2d, MaxPool2d, Module, Softmax, BatchNorm2d, Dropout, Flatten
from torch.optim import Adam, SGD
import torch.nn.functional as F
import time
from torch.nn.functional import nll_loss, cross_entropy
import torch.optim as optim
## Loading and preparing the FashionMNIST dataset"""
train_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor()
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor()
)
# Visualize some images of the FashionMNIST dataset
# Size of training data
print(train_data.data.shape)
# Size of testing data
print(test_data.data.shape)
labels_map = {
0: "T-shirt/top",
1: "Trouser",
2: "Pullover",
3: "Dress",
4: "Coat",
5: "Sandal",
6: "Shirt",
7: "Sneaker",
8: "Bag",
9: "Ankle boot",
}
figure = plt.figure(figsize=(8, 8))
cols, rows = 4, 4
for i in range(1, cols * rows + 1):
sample_idx = i
img, label = train_data[sample_idx]
figure.add_subplot(rows, cols, i)
plt.title(labels_map[label])
plt.axis("off")
plt.imshow(img.squeeze(), cmap="gray")
plt.show()
# Initializing a dataloader with batch_size of 32
BATCH_SIZE = 32
trainloader = DataLoader(train_data,batch_size = BATCH_SIZE , shuffle = True)
testloader = DataLoader(test_data,batch_size = BATCH_SIZE , shuffle = False)
#Defining the Convolutional Neural Network
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.cnn_layers = Sequential(
#first we fix a kernel
#stride is how much you move every step
#adds a padding so that our kernel multiplies patches on the border with more detail
#4 is the output channel so we have 4 kernels being learned
# Defining a 2D convolution layer with a kernel of size 3; padding 1, and stride 1.
nn.Conv2d(in_channels = 1 , out_channels = 4, padding = 1, kernel_size = 3, stride = 1),
nn.ReLU(),
# Defining another 2D convolution layer
nn.Conv2d(in_channels = 4 , out_channels = 8, padding = 1, kernel_size = 3, stride = 1),
nn.ReLU()
)
# Define one linear layer
self.linear_layers = Sequential(
Flatten(), # Flatten the output from the CNN layers
nn.Linear(8 * 28 * 28, 10), # 4 channels * 28x28 image size from convolutions
)
# Defining the forward pass
def forward(self, x):
z = self.cnn_layers(x)
z = self.linear_layers(z) # to do
return z
##Training my CNN on the training set of FashionMNIST.
start_time = time.time()
convnet = ConvNet()
print(convnet.parameters)
# Optimizer
epochs = 5
learning_rate = 1e-4
optimizer = Adam(convnet.parameters(), lr=learning_rate)
# Choice of the loss
criterion = CrossEntropyLoss() # nll_loss
losses_cnn = []
for t in range(epochs):
for i, data in enumerate(trainloader):
inputs, labels = data
# set optimizer to zero grad to remove previous epoch gradients
optimizer.zero_grad()
# Evaluate the loss
inputs= inputs.view(32, 1, 28, 28) # Assuming each image is 28x28 and has 1 channel (grayscale)
outputs = convnet.forward(inputs)# to do
loss = criterion(outputs,labels) # to do
# backward propagation
# to do
loss.backward()
# One optimization step
optimizer.step()
# to do
losses_cnn.append(loss.item())
if not i % 2000:
print(t, i, loss.item())
end_time = time.time()
elapsed_time_CNN = end_time - start_time
## checking the accuracy of the CNN model on the testing set of FashionMNIST
size_test = test_data.data.shape[0]
correct = 0
for data in testloader:
inputs, labels = data
inputs= inputs.view(-1, 1, 28, 28)
outputs = convnet(inputs)
loss = criterion(outputs,labels)
predicted = torch.argmax(outputs, dim=1)
correct += (predicted == labels).sum()
final_accuracy_CNN = correct/size_test
############################ Initializing Training a Multilayer Perceptron############
# number of features (len of X cols)
input_dim = 28 * 28
# number of hidden layers
hidden_dim = 256
# number of classes (unique of y)
output_dim = 10
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.linear1 = nn.Linear(input_dim, hidden_dim)
self.linear2 = nn.Linear (hidden_dim,output_dim)
def forward(self, x):
h_relu = F.relu(self.linear1(x))
y_pred = self.linear2(h_relu)
return y_pred
start_time = time.time()
mlp = MLP()
print(mlp.parameters)
# Optimizer
epochs = 5
learning_rate = 1e-6
optimizer = optim.Adam(mlp.parameters(), lr=learning_rate)
# Choice of the loss
criterion = cross_entropy # nll_loss
losses = []
for t in range(epochs):
for i, data in enumerate(trainloader):
inputs, labels = data
# set optimizer to zero grad to remove previous epoch gradients
optimizer.zero_grad()
# Evaluate the loss
inputs = inputs.view(-1, input_dim)
outputs = mlp(inputs) #### not sure what input shoud I pass
# loss = nll_loss(outputs, labels)
loss = criterion(outputs,labels)
# backward propagation
loss.backward()
# One optimization step
optimizer.step()
losses.append(loss.item())
if not i % 2000:
print(t, i, loss.item())
#calculating time
end_time = time.time()
elapsed_time_MLP = end_time - start_time
#ploting the loss vs Iterations in MLP
plt.plot(losses, label = "MLP Loss")
plt.plot(losses_cnn, label="CNN Loss")
plt.legend()
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.title("Loss over iterations")
plt.show()
## checking the accuracy of the MLP model on the testing set of FashionMNIST
size_test = test_data.data.shape[0]
correct = 0
for data in testloader:
inputs, labels = data
inputs = inputs.view(-1, input_dim)
outputs = mlp(inputs)
loss = criterion(outputs,labels)
predicted = torch.argmax(outputs, dim=1)
correct += (predicted == labels).sum()
final_accuracy_MLP = correct/size_test
####################### Printing Conclusions ############################
#CNN accuracy
print(f"The CNN accuracy is: {final_accuracy_CNN}")
#MLP accuracy
print(f"The MLP accuracy is: {final_accuracy_MLP}")
# Calculate the elapsed time
print("Elapsed time For Conv neural network is :", elapsed_time_CNN, "seconds")
# Calculate the elapsed time
print("Elapsed time for MLP is :", elapsed_time_MLP, "seconds")
"""So in terms of time, multilinear perception is faster than the convolutional neural networks, however, in terms of accuracy the CNN surpases the MLP by far."""