Skip to content

Commit 44b37bb

Browse files
authored
Merge pull request #1368 from gzrp/dev-postgresql
Add the training script for the xceptionet
2 parents 8e7b5d9 + dcb2567 commit 44b37bb

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

examples/singa_peft/examples/autograd/xceptionnet.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,186 @@ def forward(self, x):
117117

118118

119119
__all__ = ['Xception']
120+
121+
122+
class Xception(layer.Layer):
123+
"""
124+
Xception optimized for the ImageNet dataset, as specified in
125+
https://arxiv.org/pdf/1610.02357.pdf
126+
"""
127+
128+
def __init__(self, num_classes=1000):
129+
""" Constructor
130+
Args:
131+
num_classes: number of classes
132+
"""
133+
super(Xception, self).__init__()
134+
self.num_classes = num_classes
135+
136+
self.conv1 = layer.Conv2d(3, 32, 3, 2, 0, bias=False)
137+
self.bn1 = layer.BatchNorm2d(32)
138+
self.relu1 = layer.ReLU()
139+
140+
self.conv2 = layer.Conv2d(32, 64, 3, 1, 1, bias=False)
141+
self.bn2 = layer.BatchNorm2d(64)
142+
self.relu2 = layer.ReLU()
143+
# Relu Layer
144+
145+
self.block1 = Block(64,
146+
128,
147+
2,
148+
2,
149+
padding=0,
150+
start_with_relu=False,
151+
grow_first=True)
152+
self.block2 = Block(128,
153+
256,
154+
2,
155+
2,
156+
padding=0,
157+
start_with_relu=True,
158+
grow_first=True)
159+
self.block3 = Block(256,
160+
728,
161+
2,
162+
2,
163+
padding=0,
164+
start_with_relu=True,
165+
grow_first=True)
166+
167+
self.block4 = Block(728,
168+
728,
169+
3,
170+
1,
171+
start_with_relu=True,
172+
grow_first=True)
173+
self.block5 = Block(728,
174+
728,
175+
3,
176+
1,
177+
start_with_relu=True,
178+
grow_first=True)
179+
self.block6 = Block(728,
180+
728,
181+
3,
182+
1,
183+
start_with_relu=True,
184+
grow_first=True)
185+
self.block7 = Block(728,
186+
728,
187+
3,
188+
1,
189+
start_with_relu=True,
190+
grow_first=True)
191+
192+
self.block8 = Block(728,
193+
728,
194+
3,
195+
1,
196+
start_with_relu=True,
197+
grow_first=True)
198+
self.block9 = Block(728,
199+
728,
200+
3,
201+
1,
202+
start_with_relu=True,
203+
grow_first=True)
204+
self.block10 = Block(728,
205+
728,
206+
3,
207+
1,
208+
start_with_relu=True,
209+
grow_first=True)
210+
self.block11 = Block(728,
211+
728,
212+
3,
213+
1,
214+
start_with_relu=True,
215+
grow_first=True)
216+
217+
self.block12 = Block(728,
218+
1024,
219+
2,
220+
2,
221+
start_with_relu=True,
222+
grow_first=False)
223+
224+
self.conv3 = layer.SeparableConv2d(1024, 1536, 3, 1, 1)
225+
self.bn3 = layer.BatchNorm2d(1536)
226+
self.relu3 = layer.ReLU()
227+
228+
# Relu Layer
229+
self.conv4 = layer.SeparableConv2d(1536, 2048, 3, 1, 1)
230+
self.bn4 = layer.BatchNorm2d(2048)
231+
232+
self.relu4 = layer.ReLU()
233+
self.globalpooling = layer.MaxPool2d(10, 1)
234+
self.flatten = layer.Flatten()
235+
self.fc = layer.Linear(2048, num_classes)
236+
237+
def features(self, input):
238+
x = self.conv1(input)
239+
x = self.bn1(x)
240+
x = self.relu1(x)
241+
242+
x = self.conv2(x)
243+
x = self.bn2(x)
244+
x = self.relu2(x)
245+
246+
x = self.block1(x)
247+
x = self.block2(x)
248+
x = self.block3(x)
249+
x = self.block4(x)
250+
x = self.block5(x)
251+
x = self.block6(x)
252+
x = self.block7(x)
253+
x = self.block8(x)
254+
x = self.block9(x)
255+
x = self.block10(x)
256+
x = self.block11(x)
257+
x = self.block12(x)
258+
259+
x = self.conv3(x)
260+
x = self.bn3(x)
261+
x = self.relu3(x)
262+
263+
x = self.conv4(x)
264+
x = self.bn4(x)
265+
return x
266+
267+
def logits(self, features):
268+
x = self.relu4(features)
269+
x = self.globalpooling(x)
270+
x = self.flatten(x)
271+
x = self.fc(x)
272+
return x
273+
274+
def forward(self, input):
275+
x = self.features(input)
276+
x = self.logits(x)
277+
return x
278+
279+
280+
if __name__ == '__main__':
281+
model = Xception(num_classes=1000)
282+
print('Start initialization............')
283+
dev = device.create_cuda_gpu_on(0)
284+
285+
niters = 20
286+
batch_size = 16
287+
IMG_SIZE = 299
288+
sgd = opt.SGD(lr=0.1, momentum=0.9, weight_decay=1e-5)
289+
290+
tx = tensor.Tensor((batch_size, 3, IMG_SIZE, IMG_SIZE), dev)
291+
ty = tensor.Tensor((batch_size,), dev, tensor.int32)
292+
autograd.training = True
293+
x = np.random.randn(batch_size, 3, IMG_SIZE, IMG_SIZE).astype(np.float32)
294+
y = np.random.randint(0, 1000, batch_size, dtype=np.int32)
295+
tx.copy_from_numpy(x)
296+
ty.copy_from_numpy(y)
297+
298+
with trange(niters) as t:
299+
for _ in t:
300+
x = model(tx)
301+
loss = autograd.softmax_cross_entropy(x, ty)
302+
sgd(loss)

0 commit comments

Comments
 (0)