使用TensorFlow-Slim实现Inception模块

注:在TensorFlow的GitHub代码库上可以找到完整的Inception-v3模型的源码(网址为https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/python/slim/nets/inception_v3.py

直接使用TensorFlow原始API实现一个卷积层:

with tf.variable_scope(scope_name):
    weights = tf.get_variable("weight", [CONV_SIZE, CONV_SIZE, IMG_CHANNEL, CONV_DEEP],
                                    initializer=tf.truncated_normal_initializer(stddev=0.1))
    biases = tf.get_variable("bias", [CONV_DEEP], initializer=tf.constant_initializer(0.0))
    conv = tf.nn.conv2d(INPUT, weights, [1, 1, 1, 1], padding='SAME')
    relu = tf.nn.relu(tf.nn.bias_add(conv, biases))

若使用TensorFlow-Slim实现,可以在一行中实现一个卷积层的前向传播算法:

slim = tf.contrib.slim

# slim.conv2d函数有3个参数是必填的。第一个参数为输入节点矩阵,第二个参数是当前卷积层过滤器的深度,第三个参数是过滤器的尺寸。
# 可选的参数有过滤器移动的步长、是否使用全0填充、激活函数的选择以及变量的命名空间等
net = slim.conv2d(input, 32, [3, 3])

以下代码是使用TensorFlow-Slim对复杂的一个Inception模块的实现:

import tensorflow as tf
slim = tf.contrib.slim

# slim.grg_scope函数可以用于设置默认的参数取值,第一个参数是一个函数列表,在这个列表中的函数将使用默认的参数取值
# 例如调用slim.conv2d(net, 320, [1, 1])函数时会自动加上stride=1和padding='SAME'的参数
# 如果在函数调用时制定了stride,这里设置的默认值就不会再使用
with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'):
    net = 上一层输出节点矩阵
    # 为一个Inception模块声明一个统一的变量命名空间
    with tf.variable_scope('Mixed_7c'):
        # 给Inception模块中每一条路径声明一个命名空间
        with tf.variable_scope('Branch_0'):
            # 实现一个过滤器边长为1,深度为320的卷积层
            branch_0 = slim.conv2d(net, 320, [1, 1], scope='Conv2d_0a_1x1')

        # Inception模块中第二条路径,这条计算路径上的结构本身也是一个Inception结构
        with tf.variable_scope('Branch_1'):
            branch_1 = slim.conv2d(net, 384, [1, 1], scope='Conv2d_0a_1x1')
            # tf.concat函数可以将多个矩阵拼接起来
            # tf.concat函数的第一个参数指定了拼接的维度,这里给出的3代表了矩阵是在深度这个维度上进行拼接,可以看为z轴
            branch_1 = tf.concat(3, [
                slim.conv2d(branch_1, 384, [1, 3], scope='Conv2d_0c_1x3'),
                slim.conv2d(branch_1, 384, [3, 1], scope='Conv2d_0c_3x1')
            ])

        # Inception模块中第三条路径,此计算路径也是一个Inception结构
        with tf.variable_scope('Branch_2'):
            branch_2 = slim.conv2d(net, 448, [1, 1], scope='Conv2d_0a_1x1')
            branch_2 = slim.conv2d(branch_2, 384, [3, 3], scope='Conv2d_0b_3x3')
            branch_2 = tf.concat(3, [
                slim.conv2d(branch_2, 384, [1, 3], scope='Conv2d_0c_1x3'),
                slim.conv2d(branch_2, 384, [3, 1], scope='Conv2d_0c_3x1')
            ])

        # Inception模块中第四条路径
        with tf.variable_scope('Branch_3'):
            branch_3 = slim.avg_pool2d(net, [3, 3], scope='Avgpool_0a_3x3')
            branch_3 = slim.conv2d(branch_3, 192, [1, 1], scope='Conv2d_0b_1x1')

        # 当前Inception模块的最后输出是由上面四个计算结果拼接得到的
        net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值