-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
executable file
·106 lines (95 loc) · 3.02 KB
/
common.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm, trange
class CNN_Model(nn.Module):
def __init__(self):
super(CNN_Model, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=3,
out_channels=32,
kernel_size=4,
stride=1,
padding=0,
),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.conv2 = nn.Sequential(
nn.Conv2d(
in_channels=32,
out_channels=32,
kernel_size=4,
stride=1,
padding=0,
),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.conv3 = nn.Sequential(
nn.Conv2d(
in_channels=32,
out_channels=32,
kernel_size=4,
stride=1,
padding=0,
),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.conv4 = nn.Sequential(
nn.Conv2d(
in_channels=32,
out_channels=32,
kernel_size=4,
stride=1,
padding=0,
),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc1 = nn.Linear(31968, 200)
self.fc2 = nn.Linear(200, 18)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.fc2(x)
return x
def train(model, device, train_loader, optimizer, criterion, epochs):
'''
train the model
'''
step = 0
model.train()
t = trange(epochs)
for epoch in t:
for i, (inputs, labels) in enumerate(train_loader, 0):
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
t.set_description("step %d" % step)
step+=1
print("\tTraining loss : {:.3f}".format(loss))
print('Finished Training')
def test(model, device, test_loader):
'''
test the model
'''
print('Accuracy testing...')
correct = 0
total = 0
model.eval()
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
outputs = model(data)
_, predicted = torch.max(outputs.data, 1)
total += target.size(0)
correct += (predicted == target).sum()
print("Accuracy : %d %%" % (100 * correct / total))