|
| 1 | +# BLVC Googlenet, model from the paper: |
| 2 | +# "Going Deeper with Convolutions" |
| 3 | +# Original source: |
| 4 | +# https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet |
| 5 | +# License: unrestricted use |
| 6 | + |
| 7 | +# Download pretrained weights from: |
| 8 | +# https://s3.amazonaws.com/lasagne/recipes/pretrained/imagenet/blvc_googlenet.pkl |
| 9 | + |
| 10 | +from lasagne.layers import InputLayer |
| 11 | +from lasagne.layers import DenseLayer |
| 12 | +from lasagne.layers import ConcatLayer |
| 13 | +from lasagne.layers import NonlinearityLayer |
| 14 | +from lasagne.layers import GlobalPoolLayer |
| 15 | +from lasagne.layers.dnn import Conv2DDNNLayer as ConvLayer |
| 16 | +from lasagne.layers.dnn import MaxPool2DDNNLayer as PoolLayerDNN |
| 17 | +from lasagne.layers import MaxPool2DLayer as PoolLayer |
| 18 | +from lasagne.layers import LocalResponseNormalization2DLayer as LRNLayer |
| 19 | +from lasagne.nonlinearities import softmax, linear |
| 20 | + |
| 21 | + |
| 22 | +def build_inception_module(name, input_layer, nfilters): |
| 23 | + # nfilters: (pool_proj, 1x1, 3x3_reduce, 3x3, 5x5_reduce, 5x5) |
| 24 | + net = {} |
| 25 | + net['pool'] = PoolLayerDNN(input_layer, pool_size=3, stride=1, pad=1) |
| 26 | + net['pool_proj'] = ConvLayer(net['pool'], nfilters[0], 1) |
| 27 | + |
| 28 | + net['1x1'] = ConvLayer(input_layer, nfilters[1], 1) |
| 29 | + |
| 30 | + net['3x3_reduce'] = ConvLayer(input_layer, nfilters[2], 1) |
| 31 | + net['3x3'] = ConvLayer(net['3x3_reduce'], nfilters[3], 3, pad=1) |
| 32 | + |
| 33 | + net['5x5_reduce'] = ConvLayer(input_layer, nfilters[4], 1) |
| 34 | + net['5x5'] = ConvLayer(net['5x5_reduce'], nfilters[5], 5, pad=2) |
| 35 | + |
| 36 | + net['output'] = ConcatLayer([ |
| 37 | + net['1x1'], |
| 38 | + net['3x3'], |
| 39 | + net['5x5'], |
| 40 | + net['pool_proj'], |
| 41 | + ]) |
| 42 | + |
| 43 | + return {'{}/{}'.format(name, k): v for k, v in net.items()} |
| 44 | + |
| 45 | + |
| 46 | +def build_model(): |
| 47 | + net = {} |
| 48 | + net['input'] = InputLayer((None, 3, None, None)) |
| 49 | + net['conv1/7x7_s2'] = ConvLayer(net['input'], 64, 7, stride=2, pad=3) |
| 50 | + net['pool1/3x3_s2'] = PoolLayer(net['conv1/7x7_s2'], |
| 51 | + pool_size=3, |
| 52 | + stride=2, |
| 53 | + ignore_border=False) |
| 54 | + net['pool1/norm1'] = LRNLayer(net['pool1/3x3_s2'], alpha=0.00002, k=1) |
| 55 | + net['conv2/3x3_reduce'] = ConvLayer(net['pool1/norm1'], 64, 1) |
| 56 | + net['conv2/3x3'] = ConvLayer(net['conv2/3x3_reduce'], 192, 3, pad=1) |
| 57 | + net['conv2/norm2'] = LRNLayer(net['conv2/3x3'], alpha=0.00002, k=1) |
| 58 | + net['pool2/3x3_s2'] = PoolLayer(net['conv2/norm2'], pool_size=3, stride=2) |
| 59 | + |
| 60 | + net.update(build_inception_module('inception_3a', |
| 61 | + net['pool2/3x3_s2'], |
| 62 | + [32, 64, 96, 128, 16, 32])) |
| 63 | + net.update(build_inception_module('inception_3b', |
| 64 | + net['inception_3a/output'], |
| 65 | + [64, 128, 128, 192, 32, 96])) |
| 66 | + net['pool3/3x3_s2'] = PoolLayer(net['inception_3b/output'], |
| 67 | + pool_size=3, stride=2) |
| 68 | + |
| 69 | + net.update(build_inception_module('inception_4a', |
| 70 | + net['pool3/3x3_s2'], |
| 71 | + [64, 192, 96, 208, 16, 48])) |
| 72 | + net.update(build_inception_module('inception_4b', |
| 73 | + net['inception_4a/output'], |
| 74 | + [64, 160, 112, 224, 24, 64])) |
| 75 | + net.update(build_inception_module('inception_4c', |
| 76 | + net['inception_4b/output'], |
| 77 | + [64, 128, 128, 256, 24, 64])) |
| 78 | + net.update(build_inception_module('inception_4d', |
| 79 | + net['inception_4c/output'], |
| 80 | + [64, 112, 144, 288, 32, 64])) |
| 81 | + net.update(build_inception_module('inception_4e', |
| 82 | + net['inception_4d/output'], |
| 83 | + [128, 256, 160, 320, 32, 128])) |
| 84 | + net['pool4/3x3_s2'] = PoolLayer(net['inception_4e/output'], |
| 85 | + pool_size=3, stride=2) |
| 86 | + |
| 87 | + net.update(build_inception_module('inception_5a', |
| 88 | + net['pool4/3x3_s2'], |
| 89 | + [128, 256, 160, 320, 32, 128])) |
| 90 | + net.update(build_inception_module('inception_5b', |
| 91 | + net['inception_5a/output'], |
| 92 | + [128, 384, 192, 384, 48, 128])) |
| 93 | + |
| 94 | + net['pool5/7x7_s1'] = GlobalPoolLayer(net['inception_5b/output']) |
| 95 | + net['loss3/classifier'] = DenseLayer(net['pool5/7x7_s1'], |
| 96 | + num_units=1000, |
| 97 | + nonlinearity=linear) |
| 98 | + net['prob'] = NonlinearityLayer(net['loss3/classifier'], |
| 99 | + nonlinearity=softmax) |
| 100 | + return net |
0 commit comments