-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCNN.py
164 lines (141 loc) · 4.37 KB
/
CNN.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
# -*- coding: utf-8 -*-
"""
@Time : 2022/03/01 11:34
@Author :KI
@File :CNN.py
@Motto:Hungry And Humble
"""
import copy
import os
import random
import numpy as np
import torch
import torch.nn as nn
from torch import optim
from torch.autograd import Variable
from tqdm import tqdm
from data_process import load_data
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def setup_seed(seed):
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
setup_seed(20)
class cnn(nn.Module):
def __init__(self):
super(cnn, self).__init__()
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=3,
out_channels=16,
kernel_size=3,
stride=2,
),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
#
self.conv2 = nn.Sequential(
nn.Conv2d(
in_channels=16,
out_channels=32,
kernel_size=3,
stride=2,
),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
#
self.conv3 = nn.Sequential(
nn.Conv2d(
in_channels=32,
out_channels=64,
kernel_size=3,
stride=2,
),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.fc1 = nn.Linear(3 * 3 * 64, 64)
self.fc2 = nn.Linear(64, 10)
self.out = nn.Linear(10, 2)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
# print(x.size())
x = x.view(x.shape[0], -1)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
# x = self.sigmoid(self.out(x))
x = F.log_softmax(x, dim=1)
return x
def get_val_loss(model, Val):
model.eval()
criterion = nn.CrossEntropyLoss().to(device)
val_loss = []
for (data, target) in Val:
data, target = Variable(data).to(device), Variable(target.long()).to(device)
output = model(data)
loss = criterion(output, target)
val_loss.append(loss.cpu().item())
return np.mean(val_loss)
def train():
Dtr, Val, Dte = load_data()
print('train...')
epoch_num = 30
best_model = None
min_epochs = 5
min_val_loss = 5
model = cnn().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.0008)
criterion = nn.CrossEntropyLoss().to(device)
# criterion = nn.BCELoss().to(device)
for epoch in tqdm(range(epoch_num), ascii=True):
train_loss = []
for batch_idx, (data, target) in enumerate(Dtr, 0):
data, target = Variable(data).to(device), Variable(target.long()).to(device)
# target = target.view(target.shape[0], -1)
# print(target)
optimizer.zero_grad()
output = model(data)
# print(output)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss.append(loss.cpu().item())
# validation
val_loss = get_val_loss(model, Val)
model.train()
if epoch + 1 > min_epochs and val_loss < min_val_loss:
min_val_loss = val_loss
best_model = copy.deepcopy(model)
tqdm.write('Epoch {:03d} train_loss {:.5f} val_loss {:.5f}'.format(epoch, np.mean(train_loss), val_loss))
torch.save(best_model.state_dict(), "model/cnn.pkl")
def test():
Dtr, Val, Dte = load_data()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = cnn().to(device)
model.load_state_dict(torch.load("model/cnn.pkl"), False)
model.eval()
total = 0
current = 0
for (data, target) in Dte:
data, target = data.to(device), target.to(device)
outputs = model(data)
predicted = torch.max(outputs.data, 1)[1].data
total += target.size(0)
current += (predicted == target).sum()
print('Accuracy:%d%%' % (100 * current / total))
if __name__ == '__main__':
train()
test()