-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
172 lines (132 loc) · 5.6 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
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
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import glob
import hdf5storage
from random import shuffle
import time
import os
from model.Deasfn import Deasfn
from config.args import TrainArgs
import sys, getopt
os.environ["CUDA_VISIBLE_DEVICES"] = TrainArgs.CudaDevice
def getSequenceMinibatch(FileNames):
sequence_num = len(FileNames)
CsiDatas = torch.zeros(sequence_num, TrainArgs.NumFrames, 30, 25, 3, 3)
heatmaps = torch.zeros(sequence_num, TrainArgs.NumFrames, 57, 46, 62)
for i in range(sequence_num):
for j in range(TrainArgs.NumFrames):
data = hdf5storage.loadmat(FileNames[i][j], variable_names={'csi_serial', 'heatmaps'})
CsiDatas[i, j, :, :, :, :] = torch.from_numpy(data['csi_serial']).type(torch.FloatTensor).permute(1, 0, 2, 3)
heatmaps[i, j, :, :, :] = torch.from_numpy(data['heatmaps']).type(torch.FloatTensor)
return CsiDatas, heatmaps
def TakeIndex(elem):
return int(elem.split("/")[-1].split(".")[0])
def LoadDataset(DatasetName):
# load the training data
if DatasetName == 'SPE':
data = []
for action in TrainArgs.actions:
for subject in TrainArgs.subjects:
data.append(glob.glob(TrainArgs.DataPath + 'SPE/' + action + '/' + subject + '/train/*.mat'))
elif DatasetName == 'GPE':
data = []
for path in TrainArgs.GpePaths:
data.append(glob.glob(TrainArgs.DataPath + 'GPE/' + path + '/train/*.mat'))
else:
print("Dataset name should be SPE or GPE")
sys.exit()
# sort by index
for i in range(len(data)):
data[i].sort(key = TakeIndex)
# pack the data
TrainData = []
TrainRatio = TrainArgs.SpeTrainRatio if DatasetName == 'SPE' else TrainArgs.GpeTrainRatio
for i in range(len(data)) :
for j in range(0, int(len(data[i])*TrainRatio), TrainArgs.NumFrames):
per_sequence = []
for k in range(TrainArgs.NumFrames):
per_sequence.append(data[i][j+k*TrainArgs.DilatedRate])
TrainData.append(per_sequence)
return TrainData
def train(TrainData):
NumTrainData = len(TrainData)
NumBatch = int(np.floor(NumTrainData/TrainArgs.BatchSize))
print('NumFrames =', TrainArgs.NumFrames)
print('NumTrainData =', NumTrainData)
model = Deasfn()
model.weights_init()
optimizer = torch.optim.Adam(model.cuda().parameters(), lr=TrainArgs.LearningRate)
# load the model
if TrainArgs.LoadModel:
if os.path.isfile(TrainArgs.CheckPoint):
CheckPoint = torch.load(TrainArgs.CheckPoint)
StartEpoch = CheckPoint['epoch']
model.load_state_dict(CheckPoint['state_dict'])
optimizer.load_state_dict(CheckPoint['optimizer'])
print("=> loaded CheckPoint '{}' (epoch {})".format(TrainArgs.CheckPoint, CheckPoint['epoch']))
else:
StartEpoch = 0
print("=> no CheckPoint found at '{}'".format(TrainArgs.CheckPoint))
else:
StartEpoch = 0
print("=> do not load CheckPoint")
model = model.cuda()
criterion_L2 = nn.MSELoss(reduction='none').cuda()
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=TrainArgs.milestone, gamma=0.5)
model.train()
for epoch in range(StartEpoch, TrainArgs.MaxEpoch):
print('=> epoch:', epoch)
start = time.time()
shuffle(TrainData)
# in each minibatch
for batch_index in range(NumBatch):
if batch_index < NumBatch:
FileNames = TrainData[batch_index*TrainArgs.BatchSize:(batch_index+1)*TrainArgs.BatchSize]
else:
FileNames = TrainData[NumBatch*TrainArgs.BatchSize:]
CsiDatas, heatmaps = getSequenceMinibatch(FileNames)
CsiDatas = Variable(CsiDatas.cuda())
heatmaps = Variable(heatmaps.cuda())
heatmaps = heatmaps.permute(1, 0, 2, 3, 4).reshape(-1, 57, 46, 62)
mask = torch.ones(TrainArgs.BatchSize*TrainArgs.NumFrames, 57, 46, 62).cuda()
mask = TrainArgs.k * torch.abs(heatmaps) + mask
PredictJH, PredictPAF = model(CsiDatas)
prediction = torch.cat((PredictJH, PredictPAF), axis=1)
loss = torch.sum(torch.mul(mask, criterion_L2(heatmaps, prediction)))
if (batch_index+1) % TrainArgs.PrintFreq == 0 or (batch_index+1) == NumBatch:
print("Batch {}/{}\t Loss {:.3f}".format(batch_index+1, NumBatch, loss.item()/TrainArgs.NumFrames/TrainArgs.BatchSize))
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
endl = time.time()
print('Costing time:', (endl-start)/60)
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print('Current time:', current_time)
if not os.path.exists('./checkpoint'):
os.makedirs('./checkpoint')
torch.save({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
}, TrainArgs.CheckPoint)
def main():
DatasetName = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'd:' , ["dataset="])
for opt, arg in opts:
if opt in ('-d', '--dataset'):
DatasetName = arg
else:
sys.exit()
except getopt.GetoptError:
print ("getopt error!")
sys.exit()
assert DatasetName != None, 'python3 TrainDEASFN.py --dataset=SPE/GPE'
TrainData = LoadDataset(DatasetName)
train(TrainData)
if __name__ == '__main__':
main()