-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
294 lines (279 loc) · 12.2 KB
/
utils.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
from mxnet import gluon
from mxnet import autograd
from mxnet import nd
from mxnet import image
from mxnet.gluon.data.vision import transforms
from mxnet.gluon.data.dataset import Dataset
import mxnet as mx
import sys
import logging
import numpy as np
logging.basicConfig(level=logging.INFO, filename='./log/capsule.log')
logger = logging.getLogger(__name__)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(level=logging.INFO)
logger.addHandler(stream_handler)
transform_train = transforms.Compose([transforms.RandomResizedCrop(32,(0.8,1.0),ratio=(1.0,1.0)),
transforms.RandomFlipLeftRight(),
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])])
transform_test = transforms.Compose([transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])])
class getndata(Dataset):
def __init__(self, train_data, transform, num):
super(getndata,self).__init__()
self._transform = transform
self.data = train_data
self.num = num
self._data = None
self._label = None
self._get_data()
def __getitem__(self, idx):
if self._transform is not None:
return self._transform(self._data[idx], self._label[idx])
return self._data[idx], self._label[idx]
def __len__(self):
return len(self._label)
def _get_data(self):
# raise NotImplementedError
num = np.array([0,0,0,0,0,0,0,0,0,0,0])
for i in range(len(self.data)):
if num[int(self.data._label[i])]<self.num:
num[int(self.data._label[i])] += 1
num[-1] += 1
if self._data is None:
self._data = nd.expand_dims(self.data._data[i], axis=0)
else:
self._data = nd.concat(*[self._data, nd.expand_dims(self.data._data[i], axis=0)], dim=0)
if self._label is None:
self._label = [self.data._label[i]]
else:
self._label.append(self.data._label[i])
elif num[-1] == self.num*10:
break
class getndata_for_cifar10(Dataset):
def __init__(self, train_data, transform, num):
super(getndata_for_cifar10,self).__init__()
self._transform = transform
self.data = train_data._data
self.num = num
self._data = None
self._label = None
self._get_data()
def __getitem__(self, idx):
if self._transform is not None:
return self._transform(self._data[idx]), self._label[idx]
return self._data[idx], self._label[idx]
def __len__(self):
return len(self._label)
def _get_data(self):
# raise NotImplementedError
num = np.array([0,0,0,0,0,0,0,0,0,0,0])
for i in range(len(self.data)):
if num[int(self.data._label[i])]<self.num:
num[int(self.data._label[i])] += 1
num[-1] += 1
if self._data is None:
self._data = nd.expand_dims(self.data._data[i], axis=0)
else:
self._data = nd.concat(*[self._data, nd.expand_dims(self.data._data[i], axis=0)], dim=0)
if self._label is None:
self._label = [self.data._label[i]]
else:
self._label.append(self.data._label[i])
elif num[-1] == self.num*10:
break
def load_data_cifar10(batch_size, n=None):
"""download the fashion mnist dataest and then load into memory"""
cifar_train = gluon.data.vision.CIFAR10(train=True).transform_first(transform_train)
cifar_test = gluon.data.vision.CIFAR10(train=False).transform_first(transform_test)
if n:
cifar_train = getndata_for_cifar10(cifar_train, transform_train, n)
train_data = gluon.data.DataLoader(
cifar_train, batch_size, shuffle=True, last_batch='discard', num_workers=4)
test_data = gluon.data.DataLoader(
cifar_test, batch_size, shuffle=False, num_workers=4)
return (train_data, test_data)
def load_data_fashion_mnist(batch_size, resize=None, n=None):
"""download the fashion mnist dataest and then load into memory"""
def transform_mnist(data, label):
if resize:
# resize to resize x resize
data = image.imresize(data, resize, resize)
# change data's shape from height x weight x channel to channel x height x weight
return nd.transpose(data.astype('float32'), (2,0,1))/255, label.astype('float32')
mnist_train = gluon.data.vision.FashionMNIST(train=True, transform=transform_mnist)
mnist_test = gluon.data.vision.FashionMNIST(train=False, transform=transform_mnist)
# get different training samples
if n:
mnist_train = getndata(mnist_train, transform_mnist, n)
train_data = gluon.data.DataLoader(
mnist_train, batch_size, shuffle=True, num_workers=4)
test_data = gluon.data.DataLoader(
mnist_test, batch_size, shuffle=False, num_workers=4)
return (train_data, test_data)
def load_data_mnist(batch_size, resize=None, n=None):
"""download the fashion mnist dataest and then load into memory"""
def transform_mnist(data, label):
if resize:
# resize to resize x resize
data = image.imresize(data, resize, resize)
# change data from height x weight x channel to channel x height x weight
return nd.transpose(data.astype('float32'), (2,0,1))/255, label.astype('float32')
mnist_train = gluon.data.vision.MNIST(train=True, transform=transform_mnist)
mnist_test = gluon.data.vision.MNIST(train=False, transform=transform_mnist)
if n:
import numpy as np
mnist = []
num = np.array([0,0,0,0,0,0,0,0,0,0,0])
for data in mnist_train:
if num[int(data[1])]<n:
num[int(data[1])] += 1
num[-1] += 1
mnist.append(data)
elif num[-1] == n*10:
mnist_train = mnist
break
train_data = gluon.data.DataLoader(
mnist_train, batch_size, shuffle=True, num_workers=4)
test_data = gluon.data.DataLoader(
mnist_test, batch_size, shuffle=False, num_workers=4)
return (train_data, test_data)
def try_gpu():
"""If GPU is available, return mx.gpu(0); else return mx.cpu()"""
try:
ctx = mx.gpu()
_ = nd.zeros((1,), ctx=ctx)
except:
ctx = mx.cpu()
return ctx
def SGD(params, lr):
for param in params:
param[:] = param - lr * param.grad
def accuracy(output, label):
# print('accuracy',output, label)
return nd.mean(nd.argmax(output,axis=1)==label).asscalar()
def _get_batch(batch, ctx):
"""return data and label on ctx"""
if isinstance(batch, mx.io.DataBatch):
data = batch.data[0]
label = batch.label[0]
else:
data, label = batch
return data.as_in_context(ctx), label.as_in_context(ctx)
def evaluate_accuracy(data_iterator, net, ctx=mx.cpu()):
acc = 0.
if isinstance(data_iterator, mx.io.MXDataIter):
data_iterator.reset()
for i, batch in enumerate(data_iterator):
data, label = _get_batch(batch, ctx)
output, _ = net(data)
acc += accuracy(output, label.astype('float32'))
return acc / (i+1)
def evaluate_acc(data_iterator, net, ctx=mx.cpu()):
acc = 0.
if isinstance(data_iterator, mx.io.MXDataIter):
data_iterator.reset()
for i, batch in enumerate(data_iterator):
data, label = _get_batch(batch, ctx)
output = net(data)
acc += accuracy(output, label.astype('float32'))
return acc / (i+1)
def embedding(data_iterator, net, ctx=mx.cpu()):
convnet_codes = None
resize_images = None
labels = None
for i, batch in enumerate(data_iterator):
data, label = _get_batch(batch, ctx)
idx = nd.arange(data.shape[0])
_, output = net(data)
output = output[idx.as_in_context(ctx), :, label]
output.wait_to_read()
if convnet_codes is None:
convnet_codes = output
else:
convnet_codes = nd.concat(*[convnet_codes, output], dim=0)
if labels is None:
labels = label
else:
labels = nd.concat(*[labels, label], dim=0)
images = data.copyto(mx.cpu())
if images.shape[1] != 1:
images[:,0,:,:] += 0.4914
images[:,1,:,:] += 0.4822
images[:,2,:,:] += 0.4465
images = nd.clip(images*255, 0, 255).astype('uint8')
if resize_images is None:
resize_images = images
else:
resize_images = nd.concat(*[resize_images, images], dim=0)
nd.save('convet.ndarray', convnet_codes.as_in_context(mx.cpu()))
nd.save('resize_image.ndarray', resize_images)
nd.save('label.ndarray', labels.astype('int32').as_in_context(mx.cpu()))
def traincaps(train_data, test_data, net, loss, trainer, dnet, dloss, trainer_d, ctx, num_epochs, lr_decay=None, print_batches=None):
best_acc = 0.
for epoch in range(num_epochs):
train_loss = 0
train_acc = 0
n = 0
Loss = []
net.digitcap_decode.is_train = True
for i, batch in enumerate(train_data):
data, label = batch
one_hot_label = nd.one_hot(label, 10)
with autograd.record():
idx = nd.arange(0,data.shape[0], ctx=ctx)
output, digitcaps = net(data.as_in_context(ctx))
doutput = dnet(digitcaps[idx, :, label.as_in_context(ctx)])
l = loss(output, one_hot_label.as_in_context(ctx))
dl = dloss(doutput, data.reshape((0, -1)).as_in_context(ctx))
L = l+dl
L.backward()
trainer_d.step(data.shape[0])
trainer.step(data.shape[0])
train_loss += nd.mean(L).asscalar()
Loss.append(nd.mean(L).asscalar())
train_acc += accuracy(output, label.astype('float32').as_in_context(ctx))
n = i + 1
if print_batches and n%print_batches == 0:
print('Batch %d | Loss: %f | Train acc: %f'%(n, train_loss/n, train_acc/n))
if lr_decay and epoch%lr_decay == 0:
trainer.set_learning_rate(lr=trainer.learning_rate*0.1)
net.digitcap_decode.is_train = False
test_acc = evaluate_accuracy(test_data, net, ctx)
if test_acc > best_acc:
net.save_parameters('./params/best_acc_%.3f.params'%test_acc)
best_acc = test_acc
logger.info('Epoch %d | Loss: %f | Train acc: %f | Test acc: %f'%(epoch, train_loss/n, train_acc/n, test_acc))
net.load_parameters('./params/best_acc_%.3f.params'%best_acc)
embedding(test_data, net, ctx)
def train(train_data, test_data, net, loss, trainer, ctx, num_epochs, lr_decay=None, print_batches=None):
best_acc = 0.
for epoch in range(num_epochs):
train_loss = 0
train_acc = 0
n = 0
Loss = []
for i, batch in enumerate(train_data):
data, label = batch
one_hot_label = nd.one_hot(label, 10)
with autograd.record():
output = net(data.as_in_context(ctx))
L = loss(output, one_hot_label.as_in_context(ctx))
L.backward()
trainer.step(data.shape[0])
train_loss += nd.mean(L).asscalar()
Loss.append(nd.mean(L).asscalar())
train_acc += accuracy(output, label.astype('float32').as_in_context(ctx))
n = i + 1
if print_batches and n%print_batches == 0:
print('Batch %d | Loss: %f | Train acc: %f'%(n, train_loss/n, train_acc/n))
if lr_decay and (epoch+1)%lr_decay == 0:
trainer.set_learning_rate(lr=trainer.learning_rate*0.1)
print('learning rate: ', trainer.learning_rate)
test_acc = evaluate_acc(test_data, net, ctx)
if test_acc > best_acc:
net.save_parameters('./params/best_acc_%.3f.params'%test_acc)
best_acc = test_acc
logger.info('Epoch %d | Loss: %f | Train acc: %f | Test acc: %f'%(epoch, train_loss/n, train_acc/n, test_acc))
net.load_parameters('./params/best_acc_%.3f.params'%best_acc)