1# Licensed to the Apache Software Foundation (ASF) under one 2# or more contributor license agreements. See the NOTICE file 3# distributed with this work for additional information 4# regarding copyright ownership. The ASF licenses this file 5# to you under the Apache License, Version 2.0 (the 6# "License"); you may not use this file except in compliance 7# with the License. You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, 12# software distributed under the License is distributed on an 13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14# KIND, either express or implied. See the License for the 15# specific language governing permissions and limitations 16# under the License. 17 18"""References: 19 20Szegedy, Christian, Wei Liu, Yangqing Jia, Pierre Sermanet, Scott Reed, Dragomir 21Anguelov, Dumitru Erhan, Vincent Vanhoucke, and Andrew Rabinovich. "Going deeper 22with convolutions." arXiv preprint arXiv:1409.4842 (2014). 23 24""" 25 26import mxnet as mx 27 28def ConvFactory(data, num_filter, kernel, stride=(1,1), pad=(0, 0), name=None, suffix=''): 29 conv = mx.symbol.Convolution(data=data, num_filter=num_filter, kernel=kernel, stride=stride, pad=pad, name='conv_%s%s' %(name, suffix)) 30 act = mx.symbol.Activation(data=conv, act_type='relu', name='relu_%s%s' %(name, suffix)) 31 return act 32 33def InceptionFactory(data, num_1x1, num_3x3red, num_3x3, num_d5x5red, num_d5x5, pool, proj, name): 34 # 1x1 35 c1x1 = ConvFactory(data=data, num_filter=num_1x1, kernel=(1, 1), name=('%s_1x1' % name)) 36 # 3x3 reduce + 3x3 37 c3x3r = ConvFactory(data=data, num_filter=num_3x3red, kernel=(1, 1), name=('%s_3x3' % name), suffix='_reduce') 38 c3x3 = ConvFactory(data=c3x3r, num_filter=num_3x3, kernel=(3, 3), pad=(1, 1), name=('%s_3x3' % name)) 39 # double 3x3 reduce + double 3x3 40 cd5x5r = ConvFactory(data=data, num_filter=num_d5x5red, kernel=(1, 1), name=('%s_5x5' % name), suffix='_reduce') 41 cd5x5 = ConvFactory(data=cd5x5r, num_filter=num_d5x5, kernel=(5, 5), pad=(2, 2), name=('%s_5x5' % name)) 42 # pool + proj 43 pooling = mx.symbol.Pooling(data=data, kernel=(3, 3), stride=(1, 1), pad=(1, 1), pool_type=pool, name=('%s_pool_%s_pool' % (pool, name))) 44 cproj = ConvFactory(data=pooling, num_filter=proj, kernel=(1, 1), name=('%s_proj' % name)) 45 # concat 46 concat = mx.symbol.Concat(*[c1x1, c3x3, cd5x5, cproj], name='ch_concat_%s_chconcat' % name) 47 return concat 48 49def get_symbol(num_classes = 1000, **kwargs): 50 data = mx.sym.Variable("data") 51 conv1 = ConvFactory(data, 64, kernel=(7, 7), stride=(2,2), pad=(3, 3), name="conv1") 52 pool1 = mx.sym.Pooling(conv1, kernel=(3, 3), stride=(2, 2), pool_type="max") 53 conv2 = ConvFactory(pool1, 64, kernel=(1, 1), stride=(1,1), name="conv2") 54 conv3 = ConvFactory(conv2, 192, kernel=(3, 3), stride=(1, 1), pad=(1,1), name="conv3") 55 pool3 = mx.sym.Pooling(conv3, kernel=(3, 3), stride=(2, 2), pool_type="max") 56 57 in3a = InceptionFactory(pool3, 64, 96, 128, 16, 32, "max", 32, name="in3a") 58 in3b = InceptionFactory(in3a, 128, 128, 192, 32, 96, "max", 64, name="in3b") 59 pool4 = mx.sym.Pooling(in3b, kernel=(3, 3), stride=(2, 2), pool_type="max") 60 in4a = InceptionFactory(pool4, 192, 96, 208, 16, 48, "max", 64, name="in4a") 61 in4b = InceptionFactory(in4a, 160, 112, 224, 24, 64, "max", 64, name="in4b") 62 in4c = InceptionFactory(in4b, 128, 128, 256, 24, 64, "max", 64, name="in4c") 63 in4d = InceptionFactory(in4c, 112, 144, 288, 32, 64, "max", 64, name="in4d") 64 in4e = InceptionFactory(in4d, 256, 160, 320, 32, 128, "max", 128, name="in4e") 65 pool5 = mx.sym.Pooling(in4e, kernel=(3, 3), stride=(2, 2), pool_type="max") 66 in5a = InceptionFactory(pool5, 256, 160, 320, 32, 128, "max", 128, name="in5a") 67 in5b = InceptionFactory(in5a, 384, 192, 384, 48, 128, "max", 128, name="in5b") 68 pool6 = mx.sym.Pooling(in5b, kernel=(7, 7), stride=(1,1), global_pool=True, pool_type="avg") 69 flatten = mx.sym.Flatten(data=pool6) 70 fc1 = mx.sym.FullyConnected(data=flatten, num_hidden=num_classes) 71 softmax = mx.symbol.SoftmaxOutput(data=fc1, name='softmax') 72 return softmax 73