-
Notifications
You must be signed in to change notification settings - Fork 2
/
cnn_main.py
130 lines (102 loc) · 3.97 KB
/
cnn_main.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data.sampler import SubsetRandomSampler
import torchvision
import torchvision.transforms as transforms
from torchvision import datasets
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
#from resources.plotcm import plot_confusion_matrix
from itertools import product
torch.set_grad_enabled(True)
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=12, kernel_size=5)
#dropout example
#self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(in_features=12*4*4, out_features=120)
self.fc2 = nn.Linear(in_features=120, out_features=60)
self.out = nn.Linear(in_features=60, out_features=10)
def forward(self, t):
t = F.relu(self.conv1(t))
t = F.max_pool2d(t, kernel_size=2, stride=2)
t = F.relu(self.conv2(t))
t = F.max_pool2d(t, kernel_size=2, stride=2)
t = F.relu(self.fc1(t.reshape(-1, 12*4*4)))
t = F.relu(self.fc2(t))
t = self.out(t)
return t
def get_num_correct(preds, labels):
return preds.argmax(dim=1).eq(labels).sum().item()
transform = transforms.ToTensor()
train_set = datasets.FashionMNIST(
root='./data/FashionMNIST'
,train=True
,download=True
,transform = transform)
test_set = datasets.FashionMNIST(
root='./data/FashionMNIST'
,train=False
,download=True
,transform = transform)
train_sampler = SubsetRandomSampler(list(range(48000)))
valid_sampler = SubsetRandomSampler(list(range(12000)))
parameters = dict(
lr = [0.001], #0.01, 0.001
batch_size = [20] #10, 100, 1000
)
param_values = [v for v in parameters.values()]
model = Network()
for lr, batch_size in product(*param_values):
train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, sampler=train_sampler)
valid_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, sampler=valid_sampler)
optimizer = optim.Adam(model.parameters(), lr=lr)
for epoch in range(10):
total_loss, total_correct, valid_loss, valid_correct = 0, 0, 0, 0
for batch in train_loader: #Get Batch
images, labels = batch
preds = model(images) #Pass Batch
loss = F.cross_entropy(preds, labels) #Calculating Loss
optimizer.zero_grad()
loss.backward() #Calculating the Gradients
optimizer.step() #Updating the weights
total_loss += loss.item()
total_correct += get_num_correct(preds, labels)
for batch in valid_loader:
images, labels = batch
preds = model(images)
loss = F.cross_entropy(preds, labels)
valid_loss += loss.item()
valid_correct += get_num_correct(preds, labels)
print("epoch:", epoch, "train_correct:", total_correct, "train_loss:", total_loss,
'valid_correct', valid_correct, 'valid_loss:', valid_loss)
print('Lr: ', lr, 'batch size:', batch_size)
all_preds = []
targets = []
test_loader = torch.utils.data.DataLoader(test_set, batch_size=1000)
for batch in test_loader:
images, labels = batch
preds = model(images)
loss = F.cross_entropy(preds, labels)
all_preds.append(torch.max(preds, dim=1).indices)
targets.append(labels.data)
all_preds = torch.cat(all_preds)
targets = torch.cat(targets)
cm = confusion_matrix(targets, all_preds)
accuracy = accuracy_score(targets, all_preds)
print("Confusion_matrix: \n", cm)
print("Overall accuracy on test set: ", accuracy)
#save the model
PATH = 'yourpath/model1.pth'
torch.save(model, PATH)
#Load model
#model = TheModelClass(*args, **kwargs)
#model.load_state_dict(torch.load(PATH))
#model.eval()