tensorflow學習筆記-卷積,反卷積,空洞卷積

來源:互聯網
上載者:User
卷積

卷積函數為:

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,           data_format=None, name=None)

input為一個4-D的輸入,fileter為濾波器(卷積核),4-D,通常為[height, width, input_dim, output_dim],height, width分別表示卷積核的高,寬.input_dim, output_dim分別表式輸入維度,輸出維度.

import tensorflow as tfx1 = tf.constant(1.0, shape=[1, 5, 5, 3])x2 = tf.constant(1.0, shape=[1, 6, 6, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")sess = tf.Session()tf.global_variables_initializer().run(session=sess)x1_cov,  x2_cov = sess.run([y1, y2])print(x1_cov.shape)print(x2_cov.shape)

反卷積

反卷積函數為:

tf.nn.conv2d_transpose(value,                     filter,                     output_shape,                     strides,                     padding="SAME",                     data_format="NHWC",                     name=None)

output_shape為輸出shape,由於是卷積的反過程,因此這裡filter的輸入輸出為維度位置調換,[height, width, output_channels, in_channels].

import tensorflow as tfx1 = tf.constant(1.0, shape=[1, 5, 5, 3])x2 = tf.constant(1.0, shape=[1, 6, 6, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")y3 = tf.nn.conv2d_transpose(y1,kernel,output_shape=[1,5,5,3],    strides=[1,2,2,1],padding="SAME")y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,6,6,3],    strides=[1,2,2,1],padding="SAME")sess = tf.Session()tf.global_variables_initializer().run(session=sess)x1_cov,  x2_cov,y1_decov,y2_decov = sess.run([y1, y2,y3,y4])print(x1_cov.shape)print(x2_cov.shape)print(y1_decov.shape)print(y2_decov.shape)


注意output_shape需要根據input shape,filter shape,以及output_dim指定,但不可以隨意指定,例如

y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,5,5,3],    strides=[1,2,2,1],padding="SAME")

得到y4 shape為(1, 5, 5, 3),但是若設定output_shape=[1,7,7,3],

y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,7,7,3],    strides=[1,2,2,1],padding="SAME")

則出錯:

InvalidArgumentError (see above for traceback): Conv2DSlowBackpropInput: Size of out_backprop doesn’t match computed: actual = 3, computed = 4

 [[Node: conv2d_transpose_1 = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/gpu:0"](conv2d_transpose_1/output_shape, Const_2, Conv2D_1)]] [[Node: conv2d_transpose/_5 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_23_conv2d_transpose", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
空洞卷積(dilated convolution):

空洞卷積函數為:

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

fileter為濾波器(卷積核),格式與卷積相同,為[height, width, input_dim, output_dim].rate為對輸入的採樣步長(sample stride).

x1 = tf.constant(1.0, shape=[1, 5, 5, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y5=tf.nn.atrous_conv2d(x1,kernel,10,'SAME')

y5.shape為(1, 5, 5, 1).

完整調用代碼為:

import tensorflow as tfx1 = tf.constant(1.0, shape=[1, 5, 5, 3])x2 = tf.constant(1.0, shape=[1, 6, 6, 3])kernel = tf.constant(1.0, shape=[3, 3, 3, 1])y1 = tf.nn.conv2d(x1, kernel, strides=[1, 2, 2, 1], padding="SAME")y2 = tf.nn.conv2d(x2, kernel, strides=[1, 2, 2, 1], padding="SAME")y3 = tf.nn.conv2d_transpose(y1,kernel,output_shape=[1,5,5,3],    strides=[1,2,2,1],padding="SAME")y4 = tf.nn.conv2d_transpose(y2,kernel,output_shape=[1,6,6,3],    strides=[1,2,2,1],padding="SAME")y5=tf.nn.atrous_conv2d(x1,kernel,10,'SAME')sess = tf.Session()tf.global_variables_initializer().run(session=sess)x1_cov,  x2_cov,y1_decov,y2_decov,y5_dicov = sess.run([y1, y2,y3,y4,y5])print(x1_cov.shape)print(x2_cov.shape)print(y1_decov.shape)print(y2_decov.shape)print(y5_dicov.shape)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.