|  | 
|  | 1 | +""" | 
|  | 2 | +2017/12/02 | 
|  | 3 | +""" | 
|  | 4 | + | 
|  | 5 | +import tensorflow as tf | 
|  | 6 | +import numpy as np | 
|  | 7 | + | 
|  | 8 | + | 
|  | 9 | +class SqueezeNet(object): | 
|  | 10 | +    def __init__(self, inputs, nb_classes=1000, is_training=True): | 
|  | 11 | +        # conv1 | 
|  | 12 | +        net = tf.layers.conv2d(inputs, 96, [7, 7], strides=[2, 2], | 
|  | 13 | +                                 padding="SAME", activation=tf.nn.relu, | 
|  | 14 | +                                 name="conv1") | 
|  | 15 | +        # maxpool1 | 
|  | 16 | +        net = tf.layers.max_pooling2d(net, [3, 3], strides=[2, 2], | 
|  | 17 | +                                      name="maxpool1") | 
|  | 18 | +        # fire2 | 
|  | 19 | +        net = self._fire(net, 16, 64, "fire2") | 
|  | 20 | +        # fire3 | 
|  | 21 | +        net = self._fire(net, 16, 64, "fire3") | 
|  | 22 | +        # fire4 | 
|  | 23 | +        net = self._fire(net, 32, 128, "fire4") | 
|  | 24 | +        # maxpool4 | 
|  | 25 | +        net = tf.layers.max_pooling2d(net, [3, 3], strides=[2, 2], | 
|  | 26 | +                                      name="maxpool4") | 
|  | 27 | +        # fire5 | 
|  | 28 | +        net = self._fire(net, 32, 128, "fire5") | 
|  | 29 | +        # fire6 | 
|  | 30 | +        net = self._fire(net, 48, 192, "fire6") | 
|  | 31 | +        # fire7 | 
|  | 32 | +        net = self._fire(net, 48, 192, "fire7") | 
|  | 33 | +        # fire8 | 
|  | 34 | +        net = self._fire(net, 64, 256, "fire8") | 
|  | 35 | +        # maxpool8 | 
|  | 36 | +        net = tf.layers.max_pooling2d(net, [3, 3], strides=[2, 2], | 
|  | 37 | +                                      name="maxpool8") | 
|  | 38 | +        # fire9 | 
|  | 39 | +        net = self._fire(net, 64, 256, "fire9") | 
|  | 40 | +        # conv10 | 
|  | 41 | +        net = tf.layers.conv2d(net, 1000, [1, 1], strides=[1, 1], | 
|  | 42 | +                               padding="SAME", activation=tf.nn.relu, | 
|  | 43 | +                               name="conv10") | 
|  | 44 | +        # avgpool10 | 
|  | 45 | +        net = tf.layers.average_pooling2d(net, [13, 13], strides=[1, 1], | 
|  | 46 | +                                          name="avgpool10") | 
|  | 47 | +        # squeeze the axis | 
|  | 48 | +        net = tf.squeeze(net, axis=[1, 2]) | 
|  | 49 | + | 
|  | 50 | +        self.logits = net | 
|  | 51 | +        self.prediction = tf.nn.softmax(net) | 
|  | 52 | + | 
|  | 53 | + | 
|  | 54 | +    def _fire(self, inputs, squeeze_depth, expand_depth, scope): | 
|  | 55 | +        with tf.variable_scope(scope): | 
|  | 56 | +            squeeze = tf.layers.conv2d(inputs, squeeze_depth, [1, 1], | 
|  | 57 | +                                       strides=[1, 1], padding="SAME", | 
|  | 58 | +                                       activation=tf.nn.relu, name="squeeze") | 
|  | 59 | +            # squeeze | 
|  | 60 | +            expand_1x1 = tf.layers.conv2d(squeeze, expand_depth, [1, 1], | 
|  | 61 | +                                          strides=[1, 1], padding="SAME", | 
|  | 62 | +                                          activation=tf.nn.relu, name="expand_1x1") | 
|  | 63 | +            expand_3x3 = tf.layers.conv2d(squeeze, expand_depth, [3, 3], | 
|  | 64 | +                                          strides=[1, 1], padding="SAME", | 
|  | 65 | +                                          activation=tf.nn.relu, name="expand_3x3") | 
|  | 66 | +            return tf.concat([expand_1x1, expand_3x3], axis=3) | 
|  | 67 | + | 
|  | 68 | + | 
|  | 69 | +if __name__ == "__main__": | 
|  | 70 | +    inputs = tf.random_normal([32, 224, 224, 3]) | 
|  | 71 | +    net = SqueezeNet(inputs) | 
|  | 72 | +    print(net.prediction) | 
0 commit comments