-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_Temporal_1ch.py
executable file
·151 lines (143 loc) · 6.51 KB
/
train_Temporal_1ch.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
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torch.nn.functional as F
import os
import pandas as pd
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
from torchvision import datasets, transforms
from Dataloder.MotionHistory_dataloder import MotionDataset
from Dataloder.MotionHistory_dataloder import MotionHistory_Dataset
from torch.autograd import Variable
from PIL import Image
from statistics import mean
from torch.utils.data.dataset import Subset
from Network.Temporal.MobileNet import MobileNet_V2_Temporal
from sklearn.model_selection import train_test_split
EpochNum = 21
Height = 224
Width = 224
BatchSize = 16
LearningRate = 0.001
DatasetPath = r""
valDatasetPath = r""
modelPath = r""
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class Trainer():
def __init__(self,model, optimizer, criterion, trainLoader, valLoader, transform):
self.model = model
self.optimizer = optimizer
self.criterion = criterion
self.trainLoader = trainLoader
self.valLoader = valLoader
self.transform = transform
self.totalTrainLoss = []
self.TrainCorrect = []
self.totalValLoss = []
self.ValCorrect = []
self.max_acc = 0.0
self.min_loss = 100
self.Fig = plt.figure(figsize=[10,10])
def Train(self,epoch):
self.model.train()
train_loss,train_acc = 0.0,0.0
t_loss,t_acc = 0.0,0.0
train_log = ""
for batchIdx,(img,label) in enumerate(self.trainLoader):
img,label = Variable(img.cuda()),Variable(label.cuda())
output = self.model(img)
loss = self.criterion(output,label)
train_loss += loss.data.item()
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
pred = output.data.max(dim=1)[1]
train_acc += pred.eq(label.data).cpu().sum()
t_loss = train_loss/((batchIdx+1)*BatchSize)
t_acc = 100*train_acc.data.item() / ((batchIdx+1)*BatchSize)
train_log = "epoch : {:3} train_loss : {:3.12} train_acc : {:3.12}".format(str(epoch+1), str(t_loss), str(t_acc))
print("\r"+train_log,end="")
self.totalTrainLoss.append(t_loss)
self.TrainCorrect.append(t_acc)
self.model.eval()
val_loss,val_acc = 0.0,0.0
v_loss,v_acc = 0.0,0.0
val_log = ""
with torch.no_grad():
for batchIdx,(img,label) in enumerate(self.valLoader):
img,label = Variable(img.cuda()),Variable(label.cuda())
output = self.model(img)
loss = self.criterion(output,label)
val_loss += loss.data.item()
pred = output.data.max(dim=1)[1]
val_acc += pred.eq(label.data).cpu().sum()
v_loss = val_loss/((batchIdx+1)*BatchSize)
v_acc = 100*val_acc.data.item() / ((batchIdx+1)*BatchSize)
val_log = train_log + " val_loss : {:3.9} val_acc : {:3.9}".format(str(v_loss), str(v_acc))
print("\r"+val_log,end="")
self.totalValLoss.append(v_loss)
self.ValCorrect.append(v_acc)
print()
if v_acc > self.max_acc:
self.max_acc = v_acc
with open("models/"+str(modelPath)+"/"+str(modelPath)+"_max-Acc.pth", "wb") as savePath:
torch.save({'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,},savePath)
if v_loss < self.min_loss:
self.min_loss = v_loss
with open("models/"+str(modelPath)+"/"+str(modelPath)+"_min-Loss.pth", "wb") as savePath:
torch.save({'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,},savePath)
def graph_plot(self):
plt.clf()
plt.style.use('ggplot')
lossFig = self.Fig.add_subplot(2,1,1)
plt.title('Loss Graph')
plt.plot(self.totalTrainLoss, label='train loss')
plt.plot(self.totalValLoss, label='validation loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
accuracyFig = self.Fig.add_subplot(2,1,2)
plt.title('Accuracy Graph')
plt.minorticks_on()
plt.ylim(0,100)
plt.plot(self.TrainCorrect, label='train acc')
plt.plot(self.ValCorrect, label='validation acc')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
def main(self):
for epoch in range(EpochNum):
self.Train(epoch)
self.graph_plot()
self.Fig.savefig("models/"+str(modelPath)+"/"+str(modelPath)+".png")
if __name__ == '__main__':
transform = transforms.Compose([
transforms.Resize([Height, Width]),
transforms.ToTensor(),
])
os.mkdir("Models/"+str(modelPath))
criterion = nn.CrossEntropyLoss()
train_dataset = MotionHistory_Dataset(DatasetPath, transform, 5)
val_dataset = MotionHistory_Dataset(valDatasetPath, transform, 5)
print("Load DataSet 「"+str(DatasetPath)+"」")
#train_dataset, val_dataset, train_label, test_label = train_test_split(dataset, dataset_labels, test_size=0.2, random_state=100,shuffle=True
trainLoader = torch.utils.data.DataLoader(train_dataset, batch_size=BatchSize, shuffle=True, num_workers=os.cpu_count(), pin_memory=True)
valLoader = torch.utils.data.DataLoader(val_dataset, batch_size=BatchSize, shuffle=False, num_workers=os.cpu_count(), pin_memory=True)
print(len(train_dataset),len(val_dataset))
MobileNet = MobileNet_V2_Temporal()
model = MobileNet.model.cuda()
#os.mkdir("Models/"+str(modelPath))
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(),lr=LearningRate)
#train_dataset = Subset(dataset,train_dataset)
print("Learning rate :",LearningRate)
train = Trainer(model, optimizer, criterion,trainLoader, valLoader, transform)
train.main()