【笔记】tf.nn.conv2d()参数表:input, filter, strides, padding, use_cudnn_on_gpu, data_format,dilations,name

注:可以看出来tf和torch在卷积方面的不同点,存在一点于:

tf 对fliter进行了定义,用来控制输入输出维度;torch将该功能集成到了Conv2d中。

正文:

tf 中 二维卷积函数tf.nn.conv2d()定义:

def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_format="NHWC", dilations=[1, 1, 1, 1], name=None)

 tf.nn.conv2d()函数使用示例:

# coding: utf-8
import tensorflow as tf
 
# 定义输入图像size
img_size = 256
# 卷积核大小
kernel_size = 7
# 卷积步长
stride_size = 1
 
# 读入图像文件
image_value = tf.read_file('./xxx.jpg')
# 图像编码
img = tf.image.decode_jpeg(image_value, channels=3)
# 格式转换
img = tf.to_float(img, name='ToFloat')
# 调整图像大小到定义尺寸
img = tf.image.resize_images(img, [img_size,img_size],method=0)
 
# 第一个参数1是输入图片数量,最后一个3个RGB3个维度
batch_shape = (1,img_size,img_size,3)
# 维度转换,为卷积做准备(卷积的输入特征图的rank必须是4)
img = tf.reshape(img,batch_shape)
 
# 卷积核大小5×5,深度是3(跟RGB3个维度保持一致),特征图(卷积核)数量是7
filter = tf.Variable(tf.random_normal([kernel_size,kernel_size,3,7]))
# 步长1
strides_shape=[1,stride_size,stride_size,1]
# 定义卷积操作
op_conv2d = tf.nn.conv2d(img, filter, strides_shape, padding='SAME')
 
# 创建运行sess
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    out_img= sess.run(op_conv2d)
    print('输入图像维度: {}'.format(img.shape))
    print('输出图像维度: {}'.format(out_img.shape))
    # 输入图像维度: (1, 256, 256, 3)
    # 输出特征图维度: (1, 256, 256, 7)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值