-
Notifications
You must be signed in to change notification settings - Fork 23
/
train.py
257 lines (207 loc) · 10.5 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import scipy.io as sio
from torch.utils.data import TensorDataset, DataLoader
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import math
import time
import torch
from torch import nn
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
from torch.utils.data import TensorDataset, DataLoader
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import math
import time
from tqdm import tqdm
from models.apl import *
# from models.apl_plus import *
batch_size = 128
num_epochs = 200
# load data
data_amp = sio.loadmat('data/train_data_split_amp.mat')
train_data_amp = data_amp['train_data']
train_data = train_data_amp
# data_pha = sio.loadmat('data/train_data_split_pha.mat')
# train_data_pha = data_pha['train_data']
# train_data = np.concatenate((train_data_amp,train_data_pha),1)
train_activity_label = data_amp['train_activity_label']
train_location_label = data_amp['train_location_label']
train_label = np.concatenate((train_activity_label, train_location_label), 1)
num_train_instances = len(train_data)
train_data = torch.from_numpy(train_data).type(torch.FloatTensor)
train_label = torch.from_numpy(train_label).type(torch.LongTensor)
# train_data = train_data.view(num_train_instances, 1, -1)
# train_label = train_label.view(num_train_instances, 2)
train_dataset = TensorDataset(train_data, train_label)
train_data_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
data_amp = sio.loadmat('data/test_data_split_amp.mat')
test_data_amp = data_amp['test_data']
test_data = test_data_amp
# data_pha = sio.loadmat('data/test_data_split_pha.mat')
# test_data_pha = data_pha['test_data']
# test_data = np.concatenate((test_data_amp,test_data_pha), 1)
test_activity_label = data_amp['test_activity_label']
test_location_label = data_amp['test_location_label']
test_label = np.concatenate((test_activity_label, test_location_label), 1)
num_test_instances = len(test_data)
test_data = torch.from_numpy(test_data).type(torch.FloatTensor)
test_label = torch.from_numpy(test_label).type(torch.LongTensor)
# test_data = test_data.view(num_test_instances, 1, -1)
# test_label = test_label.view(num_test_instances, 2)
test_dataset = TensorDataset(test_data, test_label)
test_data_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
aplnet = ResNet(block=BasicBlock, layers=[1, 1, 1, 1], inchannel=52)
# aplnet = ResNet(block=BasicBlock, layers=[2, 2, 2, 2], inchannel=52)
# aplnet = ResNet(block=BasicBlock, layers=[3, 4, 6, 3], inchannel=52)
#
# aplnet = ResNet(block=Bottleneck, layers=[2, 3, 4, 6])
aplnet = aplnet.cuda()
criterion = nn.CrossEntropyLoss(size_average=False).cuda()
optimizer = torch.optim.Adam(aplnet.parameters(), lr=0.005)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
milestones=[10, 20, 30, 40, 60, 70, 80, 90, 100, 110, 120, 130,
140, 150, 160, 170, 180, 190, 200, 250, 300],
gamma=0.5)
train_loss_act = np.zeros([num_epochs, 1])
train_loss_loc = np.zeros([num_epochs, 1])
test_loss_act = np.zeros([num_epochs, 1])
test_loss_loc = np.zeros([num_epochs, 1])
train_acc_act = np.zeros([num_epochs, 1])
train_acc_loc = np.zeros([num_epochs, 1])
test_acc_act = np.zeros([num_epochs, 1])
test_acc_loc = np.zeros([num_epochs, 1])
for epoch in range(num_epochs):
print('Epoch:', epoch)
aplnet.train()
scheduler.step()
# for i, (samples, labels) in enumerate(train_data_loader):
loss_x = 0
loss_y = 0
for (samples, labels) in tqdm(train_data_loader):
samplesV = Variable(samples.cuda())
labels_act = labels[:, 0].squeeze()
labels_loc = labels[:, 1].squeeze()
labelsV_act = Variable(labels_act.cuda())
labelsV_loc = Variable(labels_loc.cuda())
# Forward + Backward + Optimize
optimizer.zero_grad()
predict_label_act, predict_label_loc,_,_,_,_,_,_,_ = aplnet(samplesV)
loss_act = criterion(predict_label_act, labelsV_act)
loss_loc = criterion(predict_label_loc, labelsV_loc)
loss = loss_act + loss_loc
# loss = loss_loc
# print(loss.item())
loss.backward()
optimizer.step()
# loss = loss1+0.5*loss2+0.25*loss3+0.25*loss4
# loss = loss1+loss2+loss3+loss4
loss_x += loss_act.item()
loss_y += loss_loc.item()
# loss.backward()
# optimizer.step()
train_loss_act[epoch] = loss_x / num_train_instances
train_loss_loc[epoch] = loss_y / num_train_instances
aplnet.eval()
# loss_x = 0
correct_train_act = 0
correct_train_loc = 0
for i, (samples, labels) in enumerate(train_data_loader):
with torch.no_grad():
samplesV = Variable(samples.cuda())
labels = labels.squeeze()
labels_act = labels[:, 0].squeeze()
labels_loc = labels[:, 1].squeeze()
labelsV_act = Variable(labels_act.cuda())
labelsV_loc = Variable(labels_loc.cuda())
predict_label_act, predict_label_loc,_,_,_,_,_,_,_ = aplnet(samplesV)
prediction = predict_label_loc.data.max(1)[1]
correct_train_loc += prediction.eq(labelsV_loc.data.long()).sum()
prediction = predict_label_act.data.max(1)[1]
correct_train_act += prediction.eq(labelsV_act.data.long()).sum()
loss_act = criterion(predict_label_act, labelsV_act)
loss_loc = criterion(predict_label_loc, labelsV_loc)
# loss_x += loss.item()
print("Activity Training accuracy:", (100 * float(correct_train_act) / num_train_instances))
print("Location Training accuracy:", (100 * float(correct_train_loc) / num_train_instances))
# train_loss[epoch] = loss_x / num_train_instances
train_acc_act[epoch] = 100 * float(correct_train_act) / num_train_instances
train_acc_loc[epoch] = 100 * float(correct_train_loc) / num_train_instances
trainacc_act = str(100 * float(correct_train_act) / num_train_instances)[0:6]
trainacc_loc = str(100 * float(correct_train_loc) / num_train_instances)[0:6]
loss_x = 0
loss_y = 0
correct_test_act = 0
correct_test_loc = 0
for i, (samples, labels) in enumerate(test_data_loader):
with torch.no_grad():
samplesV = Variable(samples.cuda())
labels_act = labels[:, 0].squeeze()
labels_loc = labels[:, 1].squeeze()
labelsV_act = Variable(labels_act.cuda())
labelsV_loc = Variable(labels_loc.cuda())
predict_label_act, predict_label_loc,_,_,_,_,_,_,_ = aplnet(samplesV)
prediction = predict_label_act.data.max(1)[1]
correct_test_act += prediction.eq(labelsV_act.data.long()).sum()
prediction = predict_label_loc.data.max(1)[1]
correct_test_loc += prediction.eq(labelsV_loc.data.long()).sum()
loss_act = criterion(predict_label_act, labelsV_act)
loss_loc = criterion(predict_label_loc, labelsV_loc)
loss_x += loss_act.item()
loss_y += loss_loc.item()
print("Activity Test accuracy:", (100 * float(correct_test_act) / num_test_instances))
print("Location Test accuracy:", (100 * float(correct_test_loc) / num_test_instances))
test_loss_act[epoch] = loss_x / num_test_instances
test_acc_act[epoch] = 100 * float(correct_test_act) / num_test_instances
test_loss_loc[epoch] = loss_y / num_test_instances
test_acc_loc[epoch] = 100 * float(correct_test_loc) / num_test_instances
testacc_act = str(100 * float(correct_test_act) / num_test_instances)[0:6]
testacc_loc = str(100 * float(correct_test_loc) / num_test_instances)[0:6]
if epoch == 0:
temp_test = correct_test_act
temp_train = correct_train_act
elif correct_test_act > temp_test:
torch.save(aplnet, 'weights/net1111epoch' + str(
epoch) + 'Train' + trainacc_act + 'Test' + testacc_act + 'Train' + trainacc_loc + 'Test' + testacc_loc + '.pkl')
temp_test = correct_test_act
temp_train = correct_train_act
# for learning curves
sio.savemat(
'result/net1111TrainLossAct_Train' + str(100 * float(temp_train) / num_train_instances)[
0:6] + 'Test' + str(
100 * float(temp_test) / num_test_instances)[0:6] + '.mat', {'train_loss': train_loss_act})
sio.savemat(
'result/net1111TestLossACT_Train' + str(100 * float(temp_train) / num_train_instances)[
0:6] + 'Test' + str(
100 * float(temp_test) / num_test_instances)[0:6] + '.mat', {'test_loss': test_loss_act})
sio.savemat(
'result/net1111TrainLossLOC_Train' + str(100 * float(temp_train) / num_train_instances)[
0:6] + 'Test' + str(
100 * float(temp_test) / num_test_instances)[0:6] + '.mat', {'train_loss': train_loss_loc})
sio.savemat(
'result/net1111TestLossLOC_Train' + str(100 * float(temp_train) / num_train_instances)[
0:6] + 'Test' + str(
100 * float(temp_test) / num_test_instances)[0:6] + '.mat', {'test_loss': test_loss_loc})
sio.savemat('result/net1111TrainAccuracyACT_Train' + str(
100 * float(temp_train) / num_train_instances)[0:6] + 'Test' + str(100 * float(temp_test) / num_test_instances)[
0:6] + '.mat', {'train_acc': train_acc_act})
sio.savemat('result/net1111TestAccuracyACT_Train' + str(
100 * float(temp_train) / num_train_instances)[0:6] + 'Test' + str(100 * float(temp_test) / num_test_instances)[
0:6] + '.mat', {'test_acc': test_acc_act})
print(str(100 * float(temp_test) / num_test_instances)[0:6])
sio.savemat('result/net1111TrainAccuracyLOC_Train' + str(
100 * float(temp_train) / num_train_instances)[0:6] + 'Test' + str(100 * float(temp_test) / num_test_instances)[
0:6] + '.mat', {'train_acc': train_acc_loc})
sio.savemat('result/net1111TestAccuracyLOC_Train' + str(
100 * float(temp_train) / num_train_instances)[0:6] + 'Test' + str(100 * float(temp_test) / num_test_instances)[
0:6] + '.mat', {'test_acc': test_acc_loc})