-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_color.py
179 lines (150 loc) · 6.52 KB
/
train_color.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import cvtorchvision.cvtransforms as cvTransforms
import torchvision.datasets as dset
import numpy as np
import os
import argparse
from Net.colorNet import myNet_ocr_color
import cv2
from tqdm import tqdm
import matplotlib.pyplot as plt
train_loss_list = []
val_loss_list = []
accuracy_list = []
def cv_imread(path):
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
return img
class CrossEntropyLabelSmooth(nn.Module):
def __init__(self, num_classes, epsilon=0.1, use_gpu=True):
super(CrossEntropyLabelSmooth, self).__init__()
self.num_classes = num_classes
self.epsilon = epsilon
self.use_gpu = use_gpu
self.logsoftmax = nn.LogSoftmax(dim=1)
def forward(self, inputs, targets):
log_probs = self.logsoftmax(inputs)
targets = torch.zeros(log_probs.size()).scatter_(1, targets.unsqueeze(1).cpu(), 1)
if self.use_gpu: targets = targets.cuda()
targets = (1 - self.epsilon) * targets + self.epsilon / self.num_classes
loss = (- targets * log_probs).mean(0).sum()
return loss
def fix_bn(m):
classname = m.__class__.__name__
if classname.find('BatchNorm') != -1:
if m.num_features != 5 and m.num_features != 12:
m.eval()
def train(epoch):
print('\nEpoch: %d' % epoch)
print(scheduler.get_lr())
model.train()
model.apply(fix_bn)
epoch_loss = 0.0
total_batches = len(trainloader)
for batch_idx, (img, label) in enumerate(tqdm(trainloader, desc=f'Training Epoch {epoch}')):
image = Variable(img.cuda())
label = Variable(label.cuda())
optimizer.zero_grad()
_, out = model(image)
loss = criterion(out, label)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
if batch_idx % 50 == 0:
print("Epoch:%d [%d|%d] loss:%f lr:%s" % (
epoch, batch_idx, total_batches, loss.mean(), scheduler.get_lr()))
avg_epoch_loss = epoch_loss / total_batches
train_loss_list.append(avg_epoch_loss)
print(f"Avg Loss for Epoch {epoch}: {avg_epoch_loss}")
scheduler.step()
def val(epoch):
print("\nValidation Epoch: %d" % epoch)
model.eval()
total = 0
correct = 0
with torch.no_grad():
for batch_idx, (img, label) in enumerate(valloader):
image = Variable(img.cuda())
label = Variable(label.cuda())
_, out = model(image)
_, predicted = torch.max(out.data, 1)
total += image.size(0)
correct += predicted.data.eq(label.data).cuda().sum()
accuracy = 1.0 * correct.cpu().numpy() / total
accuracy_list.append(accuracy)
print("Acc: %f " % ((1.0 * correct.cpu().numpy()) / total))
exModelName = opt.model_path + '/' + str(format(accuracy, ".6f")) + "_" + "epoch_" + str(epoch) + "_model" + ".pth"
torch.save({'cfg': cfg, 'state_dict': model.state_dict()}, exModelName)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--weights', type=str,
default='weights/plate_rec_ocr.pth') # 车牌识别模型
parser.add_argument('--train_path', type=str, default='datasets/plate_color/train') # 颜色训练集
parser.add_argument('--val_path', type=str, default='datasets/plate_color/val') # 颜色验证集
parser.add_argument('--num_color', type=int, default=5)
parser.add_argument('--num_workers', type=int, default=8)
parser.add_argument('--batchSize', type=int, default=256)
parser.add_argument('--epoch', type=int, default=120)
parser.add_argument('--lr', type=float, default=0.0025)
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--model_path', type=str, default='color_model', help='model_path')
opt = parser.parse_args()
print(opt)
device = torch.device("cuda" if torch.cuda.is_available() and opt.device == 'cuda' else 'cpu')
torch.backends.cudnn.benchmark = True
if not os.path.exists(opt.model_path):
os.mkdir(opt.model_path)
mean_value = (0.588, 0.588, 0.588)
std_value = (0.193, 0.193, 0.193)
transform_train = cvTransforms.Compose([
cvTransforms.Resize((48, 168)),
cvTransforms.RandomHorizontalFlip(),
cvTransforms.ToTensorNoDiv(),
cvTransforms.NormalizeCaffe(mean_value, std_value)
])
transform_val = cvTransforms.Compose([
cvTransforms.Resize((48, 168)),
cvTransforms.ToTensorNoDiv(),
cvTransforms.NormalizeCaffe(mean_value, std_value),
])
rec_model_Path = opt.weights # 车牌识别模型
checkPoint = torch.load(rec_model_Path, map_location=torch.device('cuda' if opt.device == 'cuda' else 'cpu'))
cfg = checkPoint["cfg"]
print(cfg)
model = myNet_ocr_color(cfg=cfg, color_num=opt.num_color)
model_dict = checkPoint['state_dict']
model.load_state_dict(model_dict, strict=False)
trainset = dset.ImageFolder(opt.train_path, transform=transform_train, loader=cv_imread)
valset = dset.ImageFolder(opt.val_path, transform=transform_val, loader=cv_imread)
print(len(valset))
trainloader = torch.utils.data.DataLoader(trainset, batch_size=opt.batchSize, shuffle=True,
num_workers=opt.num_workers)
valloader = torch.utils.data.DataLoader(valset, batch_size=opt.batchSize, shuffle=False,
num_workers=opt.num_workers)
model = model.to(device)
for name, value in model.named_parameters():
if name not in ['color_classifier.weight', 'color_classifier.bias', 'color_bn.weight', 'color_bn.bias',
'conv1.weight', 'conv1.bias', 'bn1.weight', 'bn1.bias']:
value.requires_grad = False
params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.SGD(params, lr=opt.lr, momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, float(opt.epoch))
criterion = CrossEntropyLabelSmooth(opt.num_color)
criterion.cuda()
for epoch in range(opt.epoch):
train(epoch)
val(epoch)
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_loss_list, label='Train Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(accuracy_list, label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.savefig(os.path.join(opt.model_path, 'training_curves.png'))
plt.close()