-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtrain_helmet.py
198 lines (176 loc) · 7.41 KB
/
train_helmet.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
import torch
import math
import torch.nn as nn
from torch.autograd import Variable
from torchvision import transforms, models
import argparse
import os
from torch.utils.data import DataLoader
import random
import cv2
from torch.utils.data import Dataset
def image_list(imageRoot, txt='list.txt'):
f = open(txt, 'wt')
for (label, filename) in enumerate(sorted(os.listdir(imageRoot), reverse=False)):
if os.path.isdir(os.path.join(imageRoot, filename)):
for imagename in os.listdir(os.path.join(imageRoot, filename)):
name, ext = os.path.splitext(imagename)
ext = ext[1:]
if ext == 'jpg' or ext == 'png' or ext == 'bmp':
f.write('%s %d\n' % (os.path.join(imageRoot, filename, imagename), label))
f.close()
def shuffle_split(listFile, trainFile, valFile):
with open(listFile, 'r') as f:
records = f.readlines()
random.shuffle(records)
num = len(records)
trainNum = int(num * 0.8)
with open(trainFile, 'w') as f:
f.writelines(records[0:trainNum])
with open(valFile, 'w') as f1:
f1.writelines(records[trainNum:])
class MyDataset(Dataset):
def __init__(self, txt, transform=None, target_transform=None):
fh = open(txt, 'r')
imgs = []
for line in fh:
line = line.strip('\n')
line = line.rstrip()
words = line.split()
imgs.append((words[0], int(words[1])))
self.imgs = imgs
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
fn, label = self.imgs[index]
img = cv2.imread(fn, cv2.IMREAD_COLOR)
if self.transform is not None:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.imgs)
class Net64x64(nn.Module):
def __init__(self):
super(Net64x64, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, 1), # 64x64x64
nn.BatchNorm2d(64),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv2d(64, 64, 3, 1, 1), # 64x64x64
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, 1, 1), # 64x64x64
nn.BatchNorm2d(64)
)
self.conv3 = nn.Sequential(
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64)
)
self.conv4 = nn.Sequential(
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64)
)
self.pool = nn.MaxPool2d(2)
self.dense = nn.Sequential(
nn.Linear(64 * 8 * 8, 128), # fc4 64*3*3 -> 128
nn.ReLU(),
nn.Linear(128, 3) # fc5 128->10
)
def forward(self, x):
conv1_out = self.conv1(x) # 32x64x64
conv2_out = self.conv2(conv1_out) + conv1_out
conv2_out = self.pool(conv2_out)# 64x32x32
conv3_out = self.conv3(conv2_out) + conv2_out
conv3_out = self.pool(conv3_out)# 64x16x16
conv4_out = self.conv4(conv3_out) + conv3_out
conv4_out = self.pool(conv4_out)# 64x8x8
res = conv4_out.view(conv4_out.size(0), -1) # batch x (64*3*3)
#res = conv3_out.reshape((256,64*3*3))
out = self.dense(res)
return out
def train():
os.makedirs('./output', exist_ok=True)
if True: #not os.path.exists('output/total.txt'):
image_list(args.datapath, 'output/total.txt')
shuffle_split('output/total.txt', 'output/train.txt', 'output/val.txt')
train_data = MyDataset(txt='output/train.txt', transform=transforms.ToTensor())
val_data = MyDataset(txt='output/val.txt', transform=transforms.ToTensor())
train_loader = DataLoader(dataset=train_data, batch_size=args.batch_size, shuffle=True)
val_loader = DataLoader(dataset=val_data, batch_size=args.batch_size)
model = Net64x64()
#model = models.resnet18(num_classes=3) # 调用内置模型
#model.load_state_dict(torch.load('./output/params_10.pth'))
#from torchsummary import summary
#summary(model, (3, 28, 28))
if args.cuda:
print('training with cuda')
model.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=1e-3)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, [30, 60], 0.1)
loss_func = nn.CrossEntropyLoss()
for epoch in range(args.epochs):
# training-----------------------------------
model.train()
train_loss = 0
train_acc = 0
for batch, (batch_x, batch_y) in enumerate(train_loader):
if args.cuda:
batch_x, batch_y = Variable(batch_x.cuda()), Variable(batch_y.cuda())
else:
batch_x, batch_y = Variable(batch_x), Variable(batch_y)
out = model(batch_x) # 256x3x28x28 out 256x10
loss = loss_func(out, batch_y)
train_loss += loss.item()
pred = torch.max(out, 1)[1]
train_correct = (pred == batch_y).sum()
train_acc += train_correct.item()
print('epoch: %2d/%d batch %3d/%d Train Loss: %.3f, Acc: %.3f'
% (epoch + 1, args.epochs, batch, math.ceil(len(train_data) / args.batch_size),
loss.item(), train_correct.item() / len(batch_x)))
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step() # 更新learning rate
print('Train Loss: %.6f, Acc: %.3f' % (train_loss / (math.ceil(len(train_data)/args.batch_size)),
train_acc / (len(train_data))))
# evaluation--------------------------------
model.eval()
eval_loss = 0
eval_acc = 0
for batch_x, batch_y in val_loader:
if args.cuda:
batch_x, batch_y = Variable(batch_x.cuda()), Variable(batch_y.cuda())
else:
batch_x, batch_y = Variable(batch_x), Variable(batch_y)
out = model(batch_x)
loss = loss_func(out, batch_y)
eval_loss += loss.item()
pred = torch.max(out, 1)[1]
num_correct = (pred == batch_y).sum()
eval_acc += num_correct.item()
print('Val Loss: %.6f, Acc: %.3f' % (eval_loss / (math.ceil(len(val_data)/args.batch_size)),
eval_acc / (len(val_data))))
# save model --------------------------------
if (epoch + 1) % 1 == 0:
# torch.save(model, 'output/model_' + str(epoch+1) + '.pth')
torch.save(model.state_dict(), 'output/params_' + str(epoch + 1) + '.pth')
#to_onnx(model, 3, 28, 28, 'params.onnx')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--datapath', required=True, help='data path')
parser.add_argument('--batch_size', type=int, default=256, help='training batch size')
parser.add_argument('--epochs', type=int, default=300, help='number of epochs to train')
parser.add_argument('--use_cuda', default=True, help='using CUDA for training')
args = parser.parse_args()
args.cuda = args.use_cuda and torch.cuda.is_available()
if args.cuda:
torch.backends.cudnn.benchmark = True
train()