-
Notifications
You must be signed in to change notification settings - Fork 23
/
eval_time.py
162 lines (130 loc) · 5.52 KB
/
eval_time.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
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])
start = time.time()
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()
aplnet.eval()
batch_size = 512
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)
# act1, loc1, x, c1, c2, c3, c4, act, loc = aplnet(samplesV)
prediction = predict_label_act.data.max(1)[1]
# sio.savemat('vis/timeactResult.mat',{'act_prediction':prediction.cpu().numpy()})
correct_test_act += prediction.eq(labelsV_act.data.long()).sum()
# print(correct_test_act)
# prediction = predict_label_loc.data.max(1)[1]
# sio.savemat('vis/timelocResult.mat', {'loc_prediction': prediction.cpu().numpy()})
correct_test_loc += prediction.eq(labelsV_loc.data.long()).sum()
# print(correct_test_loc)
endl = time.time()
print(endl-start)