-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
134 lines (98 loc) · 3.86 KB
/
train.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
import torchvision.datasets as datasets
import matplotlib.pyplot as plt
from utils import *
dataset = datasets.ImageFolder('./dataset', transform=data_transform)
#split train/val : 80/20
train_set, val_set = torch.utils.data.random_split(dataset, [2208, 552])
print('Train set:', len(train_set))
print('Validation set:', len(val_set))
#Load dataset
batch_size = 32
train_load = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True)
val_load = torch.utils.data.DataLoader(dataset=val_set, batch_size=batch_size, shuffle=False)
#Show img after load
# def imgshow(img):
# img = img/2 + 0.5
# np_img = img.numpy()
# plt.figure(figsize=(20, 20))
# plt.imshow(np.transpose(np_img, (1, 2, 0)))
# plt.show()
# data_iter = iter(val_load)
# img, labels = data_iter.next()
# imgshow(torchvision.utils.make_grid(img))
model = CNN()
train_loss = []
val_loss = []
train_acc = []
val_acc = []
def Training_Model(model, epochs, parameters):
#Using CrossEntropyLoss, optim SGD
loss_f = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(parameters, lr=0.01)
model = model.cuda()
for epoch in range(epochs):
start = time.time()
correct = 0
iterations = 0
iter_loss = 0.0
model.train() #Set mode Train
for i, (inputs, labels) in enumerate(train_load, 0):
inputs = Variable(inputs)
labels = Variable(labels)
#Convert to Cuda() to use GPU
inputs = inputs.cuda()
labels = labels.cuda()
optimizer.zero_grad()
#Forward
outputs = model(inputs)
#Calculating loss
loss = loss_f(outputs, labels)
iter_loss += loss.item()
#Backpropagation
loss.backward()
optimizer.step()
# Record the correct predictions for training data
_, predicted = torch.max(outputs, 1)
correct += (predicted == labels).sum()
iterations += 1
train_loss.append(iter_loss/iterations)
train_acc.append((100 * correct / len(train_set)))
#Eval on validation set
loss = 0.0
correct = 0
iterations = 0
model.eval() #Set mode evaluation
#No_grad on Val_set
with torch.no_grad():
for i, (inputs, labels) in enumerate(val_load, 0):
inputs = Variable(inputs)
labels = Variable(labels)
#To Cuda()
inputs = inputs.cuda()
labels = labels.cuda()
#Forward and Caculating loss
outputs = model(inputs)
loss = loss_f(outputs, labels)
loss += loss.item()
# Record the correct predictions for val data
_, predicted = torch.max(outputs, 1)
correct += (predicted == labels).sum()
iterations += 1
val_loss.append(loss/iterations)
val_acc.append((100 * correct / len(val_set)))
stop = time.time()
print ('Epoch {}/{}, Training Loss: {:.3f}, Training Accuracy: {:.3f}, Val Loss: {:.3f}, Val Accuracy: {:.3f}, Time: {}s'
.format(epoch+1, epochs, train_loss[-1], train_acc[-1], val_loss[-1], val_acc[-1],stop-start))
epochs = 32
Training_Model(model=model, epochs=epochs, parameters=model.parameters())
#Save model
torch.save(model.state_dict(), 'weights/Face-Mask-Model.pt')
#Show chart acc and save Acc_chart
plt.plot(train_acc, label='Train_Accuracy')
plt.plot(val_acc, label='Val_Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')
plt.axis('equal')
plt.legend(loc=7)
plt.savefig('Acc_chart.png')
plt.show()