如何在tensorflow中创建inception模块

Xel*_*eli 4 python machine-learning tensorflow

查看tensorflow页面:https://github.com/tensorflow/models/tree/master/inception

他们展示了其架构的图像,特别是其“inception”模块,其中并行包含:

  • 1x1 的卷积层
  • 3x3 的卷积层
  • 5x5 的卷积层
  • ave 池化 + 1x1 转换

接下来是“concat”层。

在此输入图像描述

我怎样才能在张量流中创建这个?

我想我可以按照这个思路做一些事情来创建并行操作:

start_layer = input_data

filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)
one_by_one = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([3,3,channels,filter_count], stddev=0.1)
three_by_three = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([5,5,channels,filter_count], stddev=0.1)
five_by_five = tf.nn.conv2d(start_layer, filter, strides=[1,1,1,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)
pooling = tf.nn.avg_pool(start_layer, filter, strides=[1,2,2,1], padding='SAME')

filter = tf.Variable(tf.truncated_normal([1,1,channels,filter_count], stddev=0.1)
pooling = tf.nn.conv2d(pooling, filter, strides=[1,1,1,1], padding='SAME')

#connect one_by_one, three_by_three, five_by_five, pooling into an concat layer
Run Code Online (Sandbox Code Playgroud)

但是如何将这 4 个操作组合成一个连接层呢?

Sea*_*int 5

我做了一些与你所做的非常相似的事情,然后用 完成了它tf.concat()。请注意,axis=3它与我的 4d 张量相匹配并连接到第四维(索引 3)。它的文档在这里

我的最终代码最终是这样的:

def inception2d(x, in_channels, filter_count):
    # bias dimension = 3*filter_count and then the extra in_channels for the avg pooling
    bias = tf.Variable(tf.truncated_normal([3*filter_count + in_channels], mu, sigma)),

    # 1x1
    one_filter = tf.Variable(tf.truncated_normal([1, 1, in_channels, filter_count], mu, sigma))
    one_by_one = tf.nn.conv2d(x, one_filter, strides=[1, 1, 1, 1], padding='SAME')

    # 3x3
    three_filter = tf.Variable(tf.truncated_normal([3, 3, in_channels, filter_count], mu, sigma))
    three_by_three = tf.nn.conv2d(x, three_filter, strides=[1, 1, 1, 1], padding='SAME')

    # 5x5
    five_filter = tf.Variable(tf.truncated_normal([5, 5, in_channels, filter_count], mu, sigma))
    five_by_five = tf.nn.conv2d(x, five_filter, strides=[1, 1, 1, 1], padding='SAME')

    # avg pooling
    pooling = tf.nn.avg_pool(x, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME')

    x = tf.concat([one_by_one, three_by_three, five_by_five, pooling], axis=3)  # Concat in the 4th dim to stack
    x = tf.nn.bias_add(x, bias)
    return tf.nn.relu(x)
Run Code Online (Sandbox Code Playgroud)