简体   繁体   English

如何在Tensorflow中创建Inception模块

[英]How do you create an inception module in tensorflow

Looking at the tensorflow page: https://github.com/tensorflow/models/tree/master/inception 查看tensorflow页面: https : //github.com/tensorflow/models/tree/master/inception

They show an image with their architecture, specifically their 'inception' modules which contain in parallel: 他们显示了具有其体系结构的图像,特别是并行包含的“ inception”模块:

  • conv layer of 1x1 1x1的转换层
  • conv layer of 3x3 3x3转换层
  • conv layer of 5x5 5x5的转换层
  • ave pooling + 1x1 conv 平均池+ 1x1转换

Followed by an 'concat' layer. 随后是“ concat”层。

在此处输入图片说明

How can I create this in tensorflow? 我如何在tensorflow中创建这个?

I figured I could do something along the lines of this to create the parallel operations: 我认为我可以按照这种方式做一些事情来创建并行操作:

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

But how do I combine the 4 operations into an concat layer? 但是,如何将这4个操作组合到concat层中?

I did something very similar to what you did, and then finished it off with tf.concat() . 我做了与您所做的非常相似的事情,然后使用tf.concat()完成了它。 Note the axis=3 which matches my 4d tensors and concats to the 4th dimension (index 3). 请注意axis=3 ,它与我的4d张量和连贯匹配到第4维(索引3)。 Documentation for it is here . 它的文档在这里

My final code ended up something like this: 我的最终代码最终如下所示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM