-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncNet_v2.py
302 lines (240 loc) · 11.1 KB
/
syncNet_v2.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import sys
import random
import datetime
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
#import matplotlib.pyplot as plt
import torch.optim as optim
from tensorboardX import SummaryWriter
writer = SummaryWriter(comment=str(random.randint(0,100)))
class RandomDatasetShift(Dataset):
def __init__(self, nsample, size):
length = 60*100000
self.len = 3 * nsample
self.data_v = torch.randn(nsample, size, length)
self.data_s = torch.sin(self.data_v)
self.v_neg_inc = np.random.randint(1, nsample, (nsample,))
self.s_neg_inc = np.random.randint(1, nsample, (nsample,))
def __getitem__(self, index):
if index % 3 == 0:
return torch.cat((self.data_v[index // 3], self.data_s[index // 3]), 0), 1
elif index % 3 == 1:
return torch.cat((self.data_v[index // 3], self.data_s[(index // 3 + self.v_neg_inc[index // 3]) % (self.len // 3)]), 0), 0
else:
return torch.cat((self.data_v[(index // 3 + self.s_neg_inc[index // 3]) % (self.len // 3)], self.data_s[index // 3]), 0), 0
def __len__(self):
return self.len
class RandomDataset(Dataset):
def __init__(self, nsample, size):
length = 60
self.len = 3 * nsample
self.data_v = torch.randn(nsample, size, length)
self.data_s = torch.sin(self.data_v)
self.v_neg_inc = np.random.randint(1, nsample, (nsample,))
self.s_neg_inc = np.random.randint(1, nsample, (nsample,))
def __getitem__(self, index):
if index % 3 == 0:
return torch.cat((self.data_v[index // 3], self.data_s[index // 3]), 0), 1
elif index % 3 == 1:
return torch.cat((self.data_v[index // 3], self.data_s[(index // 3 + self.v_neg_inc[index // 3]) % (self.len // 3)]), 0), 0
else:
return torch.cat((self.data_v[(index // 3 + self.s_neg_inc[index // 3]) % (self.len // 3)], self.data_s[index // 3]), 0), 0
def __len__(self):
return self.len
class RandomDataset2d(Dataset):
def __init__(self, nsample, width, noise_ratio):
height = 40
self.len = 3 * nsample
self.data_v = torch.randn(nsample, 1, height, width) # channel: 1
self.data_s = torch.sin(self.data_v) + noise_ratio * torch.randn(nsample, 1, height, width)
self.v_neg_inc = np.random.randint(1, nsample, (nsample,))
self.s_neg_inc = np.random.randint(1, nsample, (nsample,))
def __getitem__(self, index):
if index % 3 == 0:
return torch.cat((self.data_v[index // 3], self.data_s[index // 3]), 0), 1
elif index % 3 == 1:
return torch.cat((self.data_v[index // 3], self.data_s[(index // 3 + self.v_neg_inc[index // 3]) % (self.len // 3)]), 0), 0
else:
return torch.cat((self.data_v[(index // 3 + self.s_neg_inc[index // 3]) % (self.len // 3)], self.data_s[index // 3]), 0), 0
def __len__(self):
return self.len
# class Net(nn.Module):
# def __init__(self):
# super(Net, self).__init__()
# # 1 input image channel, 6 output channels, 5x5 square convolution
# # visual branch
# self.conv1a = nn.Conv1d(256, 64, 5)
# self.conv2a = nn.Conv1d(64, 512, 5)
# self.conv3a = nn.Conv1d(512, 1024, 5)
# # sensor branch
# self.conv1b = nn.Conv1d(256, 64, 5)
# self.conv2b = nn.Conv1d(64, 512, 5)
# self.conv3b = nn.Conv1d(512, 1024, 5)
# def forward(self, input_data):
# size = input_data.size()[1]
# x_v = input_data[:, :size // 2]
# x_s = input_data[:, size // 2:]
# # visual branch
# # Max pooling over a 4 window
# x_v = F.max_pool1d(F.relu(self.conv1a(x_v)), 4, stride=2)
# x_v = F.max_pool1d(F.relu(self.conv2a(x_v)), 4, stride=2)
# x_v = F.relu(self.conv3a(x_v))
# x_v = F.max_pool1d(x_v, x_v.size()[2])
# # sensor branch
# x_s = F.max_pool1d(F.relu(self.conv1b(x_s)), 4, stride=2)
# x_s = F.max_pool1d(F.relu(self.conv2b(x_s)), 4, stride=2)
# x_s = F.relu(self.conv3b(x_s))
# x_s = F.max_pool1d(x_s, x_s.size()[2])
# x_v = x_v.view(input_data.size()[0], 1, -1)
# x_v_norm = x_v.norm(p=2, dim=2, keepdim=True)
# x_v_normalized = x_v.div(x_v_norm.expand_as(x_v))
# x_s = x_s.view(input_data.size()[0], -1, 1)
# x_s_norm = x_s.norm(p=2, dim=1, keepdim=True)
# x_s_normalized = x_s.div(x_s_norm.expand_as(x_s))
# #x_out = torch.bmm(x_v_normalized, x_s_normalized).reshape((-1, 1))
# x_out = torch.bmm(x_v, x_s).reshape((-1, 1))
# return torch.cat((1 - x_out, x_out), 1)
class Net2d(nn.Module):
def __init__(self):
super(Net2d, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# visual branch
self.conv1a = nn.Conv2d(1, 64, (40, 5), stride=1)
self.conv2a = nn.Conv2d(64, 512, (1, 25), stride=1)
self.conv3a = nn.Conv2d(512, 1024, (1, 25), stride=1)
# sensor branch
self.conv1b = nn.Conv2d(1, 64, (40, 5), stride=1)
self.conv2b = nn.Conv2d(64, 512, (1, 25), stride=1)
self.conv3b = nn.Conv2d(512, 1024, (1, 25), stride=1)
def forward(self, input_data):
size = input_data.size()[1]
x_v = input_data[:, :size // 2]
x_s = input_data[:, size // 2:]
# visual branch
# Max pooling over a 4 window
x_v = F.max_pool2d(F.relu(self.conv1a(x_v)), (1, 4), stride=2) # relu+maxpool or maxpool+relu
x_v = F.max_pool2d(F.relu(self.conv2a(x_v)), (1, 4), stride=2)
x_v = F.relu(self.conv3a(x_v))
# print(x_v.size())
# print(x_v.size()[2])
# x_v = F.max_pool2d(x_v, x_v.size()[2])
# print(x_v.size())
# sensor branch
x_s = F.max_pool2d(F.relu(self.conv1b(x_s)), (1, 4), stride=2)
x_s = F.max_pool2d(F.relu(self.conv2b(x_s)), (1, 4), stride=2)
x_s = F.relu(self.conv3b(x_s))
# x_s = F.max_pool2d(x_s, x_s.size()[2])
x_v = x_v.view(input_data.size()[0], 1, -1)
# print(x_v.size())
# x_v_norm = x_v.norm(p=2, dim=2, keepdim=True)
# x_v_normalized = x_v.div(x_v_norm.expand_as(x_v))
x_s = x_s.view(input_data.size()[0], -1, 1)
# x_s_norm = x_s.norm(p=2, dim=1, keepdim=True)
# x_s_normalized = x_s.div(x_s_norm.expand_as(x_s))
# x_out = torch.bmm(x_v_normalized, x_s_normalized).reshape((-1, 1))
x_out = torch.bmm(x_v, x_s).reshape((-1, 1))
return torch.cat((1 - x_out, x_out), 1)
net = Net2d()
print(net)
def train_net(net, trainloader, valloader=None, epochs=1):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.0001, momentum=0.9)
running_total = running_correct = 0
for epoch in range(epochs): # loop over the dataset multiple times
correct = 0
total = 0
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
writer.add_scalar('data/training_loss', loss, i+epoch*len(trainloader))
loss.backward()
optimizer.step()
_, predicted = torch.max(outputs.data, 1)
running_total += labels.size(0)
running_correct += (predicted == labels).sum().item()
# print(predicted)
# print statistics
running_loss += loss.item()
if (i + epoch * len(trainloader)) % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f, training accuracy: %d %%' %
(epoch + 1, i + 1, running_loss / 2000, 100 * running_correct / running_total))
running_loss = 0.0
running_total = running_correct = 0
# save model parameters
if epoch%5 == 4:
torch.save(net.state_dict(), 'save_checkpoint_epoch'+str(epoch)+'_'+str(datetime.datetime.now())+'.pt')
with torch.no_grad():
for data in valloader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = net(inputs)
loss = criterion(outputs, labels)
writer.add_scalar('data/val_loss', loss, epoch*len(trainloader))
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the %d val images: %d %%' % (len(
valloader), 100 * correct / total))
print('Finished Training')
def test_net(net, testloader):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
def main():
batch_size = 1
trainloader = DataLoader(dataset=RandomDataset(10000, 256),
batch_size=batch_size, shuffle=True)
valloader = DataLoader(dataset=RandomDataset(2000, 256),
batch_size=batch_size, shuffle=True)
testloader = DataLoader(dataset=RandomDataset(2000, 256), batch_size=batch_size,
shuffle=False, num_workers=2)
net = Net()
train_net(net, trainloader, valloader=valloader, epochs=50)
test_net(net, testloader)
def main2d():
noise_ratio = 0
# noise_ratio = float(sys.argv[1])
print('noise_ratio: ', noise_ratio)
batch_size = 1
trainloader = DataLoader(dataset=RandomDataset2d(10000, 256, noise_ratio),
batch_size=batch_size, shuffle=True)
valloader = DataLoader(dataset=RandomDataset2d(2000, 256, noise_ratio),
batch_size=batch_size, shuffle=True)
testloader = DataLoader(dataset=RandomDataset2d(2000, 256, noise_ratio), batch_size=batch_size,
shuffle=False, num_workers=2)
net = Net2d()
train_net(net, trainloader, valloader=valloader, epochs=20)
test_net(net, testloader)
#params = list(net.parameters())
# print(len(params))
# print(params[0].size())
if __name__ == "__main__":
# print(os.path.basename(__file__))
main2d()
#load model parameters
# net = Net2d()
# net.load_state_dict(torch.load(PATH))