Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的

实验环境:tensorflow版本1.2.0,python2.7

介绍

关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论:

1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional networks for semantic segmentation[C]. Computer Vision and Pattern Recognition, 2015.

2.Yu, Fisher, and Vladlen Koltun. “Multi-scale context aggregation by dilated convolutions.” arXiv preprint arXiv:1511.07122 (2015).

3.如何理解空洞卷积(dilated convolution)?

其实用一句话概括就是,在不用pooling的情况下扩大感受野(pooling层会导致信息损失)

为了阅读方便再贴一些相关链接:

【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

【TensorFlow】tf.nn.conv2d_transpose是怎样实现反卷积的?

惯例先展示函数:

tf.nn.atrous_conv2d(value,filters,rate,padding,name=None)

除去name参数用以指定该操作的name,与方法有关的一共四个参数:

value:
指需要做卷积的输入图像,要求是一个4维Tensor,具有[batch, height, width, channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数]

filters:
相当于CNN中的卷积核,要求是一个4维Tensor,具有[filter_height, filter_width, channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],同理这里第三维channels,就是参数value的第四维

rate:
要求是一个int型的正数,正常的卷积操作应该会有stride(即卷积核的滑动步长),但是空洞卷积是没有stride参数的,这一点尤其要注意。取而代之,它使用了新的rate参数,那么rate参数有什么用呢?它定义为我们在输入图像上卷积时的采样间隔,你可以理解为卷积核当中穿插了(rate-1)数量的“0”,把原来的卷积核插出了很多“洞洞”,这样做卷积时就相当于对原图像的采样间隔变大了。具体怎么插得,可以看后面更加详细的描述。此时我们很容易得出rate=1时,就没有0插入,此时这个函数就变成了普通卷积。

padding:
string类型的量,只能是”SAME”,”VALID”其中之一,这个值决定了不同边缘填充方式。

ok,完了,到这就没有参数了,或许有的小伙伴会问那“stride”参数呢。其实这个函数已经默认了stride=1,也就是滑动步长无法改变,固定为1。

结果返回一个Tensor,填充方式为“VALID”时,返回[batch,height-2*(filter_width-1),width-2*(filter_height-1),out_channels]的Tensor,填充方式为“SAME”时,返回[batch, height, width, out_channels]的Tensor,这个结果怎么得出来的?先不急,我们通过一段程序形象的演示一下空洞卷积。

实验

首先创建一张2通道图

img = tf.constant(value=[[[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]]]],dtype=tf.float32)
img = tf.concat(values=[img,img],axis=3)

然后用一个3*3卷积核去做卷积

filter = tf.constant(value=1, shape=[3,3,2,5], dtype=tf.float32)
out_img = tf.nn.atrous_conv2d(value=img, filters=filter, rate=1)

建立好了img和filter,就可以做卷积了

out_img = tf.nn.conv2d(input=img, filter=filter, strides=[1,1,1,1], padding='VALID')

输出5个channel,我们设置rate=1,此时空洞卷积可以看做普通的卷积,分别在SAME和VALID模式下输出如下:

ok,调整rate=2,继续运行程序

out_img = tf.nn.atrous_conv2d(value=img, filters=filter, rate=2, padding='SAME')

查看输出结果

[[[[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]
[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]]

[[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]
[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]]

[[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]
[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]]

[[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]
[ 16. 16. 16. 16. 16.]
[ 24. 24. 24. 24. 24.]]]]

这个结果怎么出来的呢?再用一张图

这里我们看到rate=2时,通过穿插“0”,卷积核由3*3膨胀到了5*5。再看看“VALID”模式下,会发生什么?

直接报错了。因为卷积核的大小已经超过了原图大小

好了,看到这里相信大家对于空洞卷积有了基本的了解了。那么,填充方式为“VALID”时,返回[batch,height-2*(filter_width-1),width-2*(filter_height-1),out_channels]的Tensor,这个结果,相信大家就可以证明了。

代码清单

import tensorflow as tf

img = tf.constant(value=[[[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[4]]]],dtype=tf.float32)
img = tf.concat(values=[img,img],axis=3)
filter = tf.constant(value=1, shape=[3,3,2,5], dtype=tf.float32)
out_img1 = tf.nn.atrous_conv2d(value=img, filters=filter, rate=1, padding='SAME')
out_img2 = tf.nn.atrous_conv2d(value=img, filters=filter, rate=1, padding='VALID')
out_img3 = tf.nn.atrous_conv2d(value=img, filters=filter, rate=2, padding='SAME')

#error
#out_img4 = tf.nn.atrous_conv2d(value=img, filters=filter, rate=2, padding='VALID')

with tf.Session() as sess:
  print 'rate=1, SAME mode result:'
  print(sess.run(out_img1))

  print 'rate=1, VALID mode result:'
  print(sess.run(out_img2))

  print 'rate=2, SAME mode result:'
  print(sess.run(out_img3))

  # error
  #print 'rate=2, VALID mode result:'
  #print(sess.run(out_img4))

到此这篇关于Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的的文章就介绍到这了,更多相关Tensorflow tf.nn.atrous_conv2d空洞卷积内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的

    今天来介绍一下Tensorflow里面的反卷积操作,网上反卷积的用法的介绍比较少,希望这篇教程可以帮助到各位 反卷积出自这篇论文:Deconvolutional Networks,有兴趣的同学自行了解 首先无论你如何理解反卷积,请时刻记住一点,反卷积操作是卷积的反向 如果你随时都记住上面强调的重点,那你基本就理解一大半了,接下来通过一些函数的介绍为大家强化这个观念 conv2d_transpose(value, filter, output_shape, strides, padding="SA

  • TensorFlow tf.nn.conv2d实现卷积的方式

    实验环境:tensorflow版本1.2.0,python2.7 介绍 惯例先展示函数: tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 除去name参数用以指定该操作的name,与方法有关的一共五个参数: input: 指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[

  • TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是tf.nn.softmax_cross_entropy_with_logits,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化的值 tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 除去name参数用以指定该操作的name,与方法有关的一共两个参数: 第一个参数logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[bat

  • 对tensorflow中tf.nn.conv1d和layers.conv1d的区别详解

    在用tensorflow做一维的卷积神经网络的时候会遇到tf.nn.conv1d和layers.conv1d这两个函数,但是这两个函数有什么区别呢,通过计算得到一些规律. 1.关于tf.nn.conv1d的解释,以下是Tensor Flow中关于tf.nn.conv1d的API注解: Computes a 1-D convolution given 3-D input and filter tensors. Given an input tensor of shape [batch, in_wi

  • TensorFlow tf.nn.max_pool实现池化操作方式

    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow] tf.nn.conv2d实现卷积的方式 tf.nn.max_pool(value, ksize, strides, padding, name=None) 参数是四个,和卷积很类似: 第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape 第

  • Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的

    实验环境:tensorflow版本1.2.0,python2.7 介绍 depthwise_conv2d来源于深度可分离卷积: Xception: Deep Learning with Depthwise Separable Convolutions tf.nn.depthwise_conv2d(input,filter,strides,padding,rate=None,name=None,data_format=None) 除去name参数用以指定该操作的name,data_format指定

  • Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的

    实验环境:tensorflow版本1.2.0,python2.7 介绍 关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论: 1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional networks for semantic segmentation[C]. Computer Vision and Pattern Recognition, 2015. 2.Yu, Fisher, and Vladlen Koltun. "Mu

  • python人工智能tensorflow函数tf.nn.dropout使用方法

    目录 前言 tf.nn.dropout函数介绍 例子 代码 keep_prob = 0.5 keep_prob = 1 前言 神经网络在设置的神经网络足够复杂的情况下,可以无限逼近一段非线性连续函数,但是如果神经网络设置的足够复杂,将会导致过拟合(overfitting)的出现,就好像下图这样. 看到这个蓝色曲线,我就知道: 很明显蓝色曲线是overfitting的结果,尽管它很好的拟合了每一个点的位置,但是曲线是歪歪曲曲扭扭捏捏的,这个的曲线不具有良好的鲁棒性,在实际工程实验中,我们更希望得到

  • tf.nn.conv2d与tf.layers.conv2d的区别及说明

    目录 tf.nn.conv2d与tf.layers.conv2d的区别 tf.nn.conv2d tf.layers.conv2d tf.nn.conv2d和tf.layers.conv2d的学习 总结 tf.nn.conv2d与tf.layers.conv2d的区别 在写CNN中注意到tensorflow目前有tf.nn.conv2d和tf.layers.conv2d这两个很相似的API. tf.nn.conv2d, 需要自行传入初始化好的filter(四个维度),在初始化filter或者说W

  • PyTorch 普通卷积和空洞卷积实例

    如下所示: import numpy as np from torchvision.transforms import Compose, ToTensor from torch import nn import torch.nn.init as init def transform(): return Compose([ ToTensor(), # Normalize((12,12,12),std = (1,1,1)), ]) arr = range(1,26) arr = np.reshape

随机推荐