-
Notifications
You must be signed in to change notification settings - Fork 14
/
MXNet_Gluon.py
executable file
·233 lines (175 loc) · 7.24 KB
/
MXNet_Gluon.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
# %%
import os, tarfile
import cv2 as cv
import numpy as np
import mxnet as mx
from tqdm import tqdm
from mxnet import io, nd, gluon, init, autograd
from mxnet.gluon.data.vision import datasets
from mxnet.gluon import nn, data, utils
from matplotlib import pyplot as plt
from multiprocessing import cpu_count
CPU_COUNT = cpu_count()
print("Package Loaded!")
# %%
# Data Prepare
'''
Download Flower classification dataset
'''
SAVE_PATH = "../../../data"
URL = 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz'
file_name = URL.split("/")[-1]
os.makedirs(SAVE_PATH, exist_ok=True)
data = utils.download(URL, SAVE_PATH)
PATH = os.path.join(SAVE_PATH, "flower_photos")
with tarfile.open(os.path.join(SAVE_PATH, file_name)) as tf:
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner=numeric_owner)
safe_extract(tf, SAVE_PATH)
category_list = [i for i in os.listdir(PATH) if os.path.isdir(os.path.join(PATH, i)) ]
print(category_list, '\n')
num_classes = len(category_list)
img_size = 150
def read_img(path, img_size):
img = cv.imread(path)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = cv.resize(img, (img_size, img_size))
return img
imgs_tr = []
labs_tr = []
imgs_val = []
labs_val = []
for i, category in enumerate(category_list):
path = os.path.join(PATH, category)
imgs_list = os.listdir(path)
print("Total '%s' images : %d"%(category, len(imgs_list)))
ratio = int(np.round(0.05 * len(imgs_list)))
print("%s Images for Training : %d"%(category, len(imgs_list[ratio:])))
print("%s Images for Validation : %d"%(category, len(imgs_list[:ratio])))
print("================================\n")
imgs = [read_img(os.path.join(path, img),img_size) for img in imgs_list]
labs = [i]*len(imgs_list)
imgs_tr += imgs[ratio:]
labs_tr += labs[ratio:]
imgs_val += imgs[:ratio]
labs_val += labs[:ratio]
imgs_tr = np.array(imgs_tr)/255.
labs_tr = np.array(labs_tr)
imgs_val = np.array(imgs_val)/255.
labs_val = np.array(labs_val)
print(imgs_tr.shape, labs_tr.shape)
print(imgs_val.shape, labs_val.shape)
# %%
# Build network
class Residual_Block(gluon.Block):
def __init__(self, output_channel, strides=1, use_branch=True):
super(Residual_Block, self).__init__()
self.branch1 = lambda x:x
if use_branch:
self.branch1 = nn.Conv2D(output_channel, (1, 1), strides)
self.branch2 = nn.Sequential()
self.branch2.add(nn.Conv2D(output_channel//4, 1, strides))
self.branch2.add(nn.BatchNorm())
self.branch2.add(nn.Activation('relu'))
self.branch2.add(nn.Conv2D(output_channel//4, (3, 3), (1, 1), (1, 1)))
self.branch2.add(nn.BatchNorm())
self.branch2.add(nn.Activation('relu'))
self.branch2.add(nn.Conv2D(output_channel, (1, 1), (1, 1)))
self.branch2.add(nn.BatchNorm())
self.relu = nn.Activation('relu')
def forward(self, x):
out = self.branch2(x)
out = self.relu(out + self.branch1(x))
return out
class Build_Resnet(gluon.Block):
def __init__(self, num_classes=1000, num_layer=16):
super(Build_Resnet, self).__init__()
blocks_dict = {
50: [3, 4, 6, 3],
101: [3, 4, 23, 3],
152: [3, 8, 36, 3]
}
num_channel_list = [256, 512, 1024, 2048]
assert num_layer in blocks_dict.keys(), "Number of layer must be in %s"%blocks_dict.keys()
self.stem = nn.Sequential()
self.stem.add(nn.Conv2D(64, (7, 7), (2, 2), (3, 3)))
self.stem.add(nn.BatchNorm())
self.stem.add(nn.Activation('relu'))
self.stem.add(nn.MaxPool2D((3, 3), (2, 2), (1, 1)))
self.main_net = nn.Sequential()
for idx, num_iter in enumerate(blocks_dict[num_layer]):
for j in range(num_iter):
if j==0:
self.main_net.add(Residual_Block(num_channel_list[idx], strides=2))
else:
self.main_net.add(Residual_Block( num_channel_list[idx], use_branch=False))
self.classifier = nn.Sequential()
self.classifier.add(nn.GlobalAvgPool2D())
self.classifier.add(nn.Flatten())
self.classifier.add(nn.Dense(num_classes))
def forward(self, x):
x = self.stem(x)
x = self.main_net(x)
x = self.classifier(x)
return x
resnet = Build_Resnet(num_classes=5, num_layer=50)
gpus = mx.test_utils.list_gpus()
ctx = [mx.gpu(i) for i in gpus] if gpus else [mx.cpu()]
resnet.initialize(ctx=ctx[0])
cross_entropy = gluon.loss.SoftmaxCELoss()
trainer = gluon.Trainer(resnet.collect_params(), 'adam', {'learning_rate': 0.001})
print("Setting Done!")
# %%
epochs=100
batch_size=64
class DataIterLoader():
def __init__(self, X, Y, batch_size=1, shuffle=True, ctx=mx.cpu()):
self.data_iter = io.NDArrayIter(data=gluon.utils.split_and_load(np.transpose(X, [0, 3, 1, 2]), ctx_list=ctx, batch_axis=0),
label=gluon.utils.split_and_load(Y, ctx_list=ctx, batch_axis=0),
batch_size=batch_size, shuffle=shuffle)
self.len = len(X)
def __iter__(self):
self.data_iter.reset()
return self
def __next__(self):
batch = self.data_iter.__next__()
assert len(batch.data) == len(batch.label) == 1
data = batch.data[0]
label = batch.label[0]
return data, label
train_loader = DataIterLoader(imgs_tr, labs_tr, batch_size, ctx=ctx)
validation_loader = DataIterLoader(imgs_val, labs_val, batch_size, ctx=ctx)
print("\nStart Training!")
for epoch in range(epochs):
train_loss, train_acc, valid_loss, valid_acc = 0., 0., 0., 0.
#tic = time.time()
# forward + backward
for step, (batch_img, batch_lab) in enumerate(train_loader):
with autograd.record():
output = resnet(batch_img)
loss = cross_entropy(output, batch_lab)
loss.backward()
# update parameters
trainer.step(batch_size)
correct = np.argmax(output.asnumpy(), axis = 1)
acc = np.mean(correct == batch_lab.asnumpy())
train_loss += loss.mean().asnumpy()[0]
train_acc += acc
for idx, (val_img, val_lab) in enumerate(validation_loader):
output = resnet(val_img)
loss = cross_entropy(output, val_lab)
correct = np.argmax(output.asnumpy(), axis = 1)
acc = np.mean(correct == val_lab.asnumpy())
valid_loss += loss.mean().asnumpy()[0]
valid_acc += acc
print(f"Epoch : {epoch+1}, loss : {train_loss/(step+1)}, acc : {train_acc/(step+1)}, \
val_loss : {valid_loss/(idx+1)}, val_acc : {valid_acc/(idx+1)}")