This repository was archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode3.py
161 lines (126 loc) · 5.25 KB
/
code3.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
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torch.autograd import Variable
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
# CUDA for PyTorch
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
#load data
train_images = pd.read_pickle('train_images.pkl')
train_labels = pd.read_csv('train_labels.csv')
test_images = pd.read_pickle('test_images.pkl')
train_labels = np.asarray(train_labels.Category)
def normalization(images):
pop_mean = []
pop_std0 = []
images2 = []
for image in images:
batch_mean = (image.mean())
batch_std0 = (image.std())
pop_mean.append(batch_mean)
pop_std0.append(batch_std0)
pop_mean = (sum(pop_mean)/len(pop_mean))
pop_std0 = (sum(pop_std0)/len(pop_std0))
for image in images:
image = (image - pop_mean)/pop_std0
images2.append(image)
return images2
train_images = np.asarray(normalization(train_images))
test_images = np.asarray(normalization(test_images))
features_numpy = train_images
targets_numpy = train_labels
features_train, features_test, targets_train, targets_test = train_test_split(features_numpy,
targets_numpy,
test_size = 0.2,
random_state = 44)
train_batch_size = 1000
test_batch_size = 1000
X_train = torch.from_numpy(features_train)
X_test = torch.from_numpy(features_test)
Y_train = torch.from_numpy(targets_train).type(torch.LongTensor)
Y_test = torch.from_numpy(targets_test).type(torch.LongTensor)
train = torch.utils.data.TensorDataset(X_train,Y_train)
test = torch.utils.data.TensorDataset(X_test,Y_test)
train_loader = torch.utils.data.DataLoader(train, batch_size = train_batch_size, shuffle = True)
test_loader = torch.utils.data.DataLoader(test, batch_size = test_batch_size, shuffle = False)
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.cnn_1 = nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size = 5, stride=1, padding=0)
self.cnn_2 = nn.Conv2d(in_channels = 16, out_channels = 32, kernel_size = 5, stride=1, padding=0)
self.relu = nn.LeakyReLU()
self.maxpool = nn.MaxPool2d(2,2)
self.dropout = nn.Dropout(p=0.2)
self.dropout2d = nn.Dropout2d(p=0.2)
self.fc1 = nn.Linear(5408, 64)
# self.fc2 = nn.Linear(128, 64)
self.out = nn.Linear(64, 10)
def forward(self,x):
out = self.cnn_1(x)
out = self.relu(out)
out = self.dropout2d(out)
out = self.maxpool(out)
out = self.cnn_2(out)
out = self.relu(out)
out = self.dropout2d(out)
out = self.maxpool(out)
out = out.view(out.size(0), -1)
out = self.fc1(out)
out = self.dropout(out)
# out = self.fc2(out)
# out = self.dropout(out)
out = self.out(out)
return out
model = CNN()
if use_cuda:
model = model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=0.0005)
epochs = 1000
train_losses, test_losses = [] ,[]
for epoch in range(epochs):
running_loss = 0
for images,labels in train_loader:
if use_cuda:
images, labels = images.cuda(), labels.cuda()
train = Variable(images.view(-1,1,64,64))
labels = Variable(labels)
optimizer.zero_grad()
output = model(train)
loss = criterion(output,labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
test_loss = 0
accuracy = 0
with torch.no_grad(): #Turning off gradients to speed up
model.eval()
for images,labels in test_loader:
if use_cuda:
images, labels = images.cuda(), labels.cuda()
test = Variable(images.view(-1,1,64,64))
labels = Variable(labels)
log_ps = model(test)
test_loss += criterion(log_ps,labels)
ps = torch.exp(log_ps)
top_p, top_class = ps.topk(1, dim = 1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
model.train()
train_losses.append(running_loss/len(train_loader))
test_losses.append(test_loss/len(test_loader))
print("Epoch: {}/{}.. ".format(epoch+1, epochs),
"Training Loss: {:.3f}.. ".format(running_loss/len(train_loader)),
"Test Loss: {:.3f}.. ".format(test_loss/len(test_loader)),
"Test Accuracy: {:.3f}".format(accuracy/len(test_loader)))
plt.figure(1)
plt.plot(train_losses, label='Training loss')
plt.figure(2)
plt.plot(test_losses, label='Validation loss')
plt.legend(frameon=False)
plt.show()