反卷积与反卷积核的初始化问题
导论:
https://blog.csdn.net/qq_30638831/article/details/81532892
https://cv-tricks.com/image-segmentation/transpose-convolution-in-tensorflow/
https://zhuanlan.zhihu.com/p/38964806
tf.nn.conv2d_transpose(
conv, 卷积后的结果 ,假设为 (16,375,250,64)
权重的初始化, 使用线性插值,请参考后面, [3,3,3,64] [kernel,kernel,输出特征个数,输入特征个数],
输出的初始化, [16,750,500,3] [batch,height,width,chanel] chanel必须与输出特征个数相等
strides=[1,2,2,1],padding='SAME'
)
conts = tf.nn.conv2d_transpose(pool,kernel2,output_shape,strides=[1,2,2,1],padding='SAME')
插值的目的: 把图像更精确的语义分割与得到原像素
差值等大小的filter:
from math import ceil
import numpy as npimport tensorflow as tfdef __get_deconv_filter(f_shape):"""Compute bilinear filter and return it"""filt_width = f_shape[0] #计算kernel的宽filt_height = f_shape[1] #计算kernel的长half_width = ceil(filt_width /2.0)center = (2 * half_width - 1 - half_width % 2) / (2.0 * half_width) # 计算某点的权值 对这个点进行插值bilinear = np.zeros([filt_width, filt_height])for x in range(filt_width):for y in range(filt_height):value = (1 - abs(x / half_width - center)) * (1 - abs(y / half_width - center))bilinear[x, y] = valueweights = np.zeros(f_shape)for i in range(f_shape[2]):weights[:, :, i, i] = bilinearprint(weights[:, :, i, i])init = tf.constant_initializer(value=weights,dtype=tf.float32)return tf.get_variable(name="up_filter", initializer=init,shape=weights.shape)
a = __get_deconv_filter([3, 3, 3, 3])
差值翻倍的kernel:
def get_kernel_size(factor):"""Find the kernel size given the desired factor of upsampling."""#获取kernel的大小return 2 * factor - factor % 2def upsample_filt(size):"""Make a 2D bilinear kernel suitable for upsampling of the given (h, w) size."""factor = (size + 1) // 2if size % 2 == 1:center = factor - 1else:center = factor - 0.5og = np.ogrid[:size, :size]return (1 - abs(og[0] - center) / factor) * \(1 - abs(og[1] - center) / factor)def bilinear_upsample_weights(factor, number_of_classes):"""Create weights matrix for transposed convolution with bilinear filterinitialization."""filter_size = get_kernel_size(factor)weights = np.zeros((filter_size,filter_size,3,4), dtype=np.float32)upsample_kernel = upsample_filt(filter_size)for i in range(3):weights[:, :, i, i] = upsample_kernelreturn weightsprint(bilinear_upsample_weights(2,21).shape)
import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as pltfrom math import ceil'''一张图片的反卷积
'''im = Image.open('timg.jpg')
images = np.asarray(im)
print(images.shape)images = np.reshape(images,[1,750,500,3])img = tf.Variable(images,dtype=tf.float32)
# kernel = tf.get_variable(name='a',shape=[3, 3, 3, 3], dtype=tf.float32,
# initializer=tf.contrib.layers.xavier_initializer())# 卷积核
kernel = tf.get_variable(name='a',shape=[3, 3, 3, 64], dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())def __get_deconv_filter(f_shape):"""Compute bilinear filter and return it"""filt_width = f_shape[0] #计算kernel的宽filt_height = f_shape[1] #计算kernel的长half_width = ceil(filt_width /2.0)center = (2 * half_width - 1 - half_width % 2) / (2.0 * half_width) # 计算某点的权值 对这个点进行插值bilinear = np.zeros([filt_width, filt_height])for x in range(filt_width):for y in range(filt_height):value = (1 - abs(x / half_width - center)) * (1 - abs(y / half_width - center))bilinear[x, y] = valueweights = np.zeros(f_shape)for i in range(f_shape[2]):weights[:, :, i, i] = bilinearprint(weights[:, :, i, i])init = tf.constant_initializer(value=weights,dtype=tf.float32)return init# 反卷积核
kernel2 = tf.get_variable(name='a1',shape=[3, 3, 3, 64], dtype=tf.float32,initializer=__get_deconv_filter([3,3,3,64]))#tf.nn.conv2d(input=input_op, filter=weights, strides=[1, dh, dw, 1], padding="SAME")# 卷积
conv1 = tf.nn.conv2d(input=img, filter=kernel,strides=[1, 1, 1, 1], padding="SAME")
# print(conv1)
# 池化
pool = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")shape_ = pool.get_shape().as_list()
print(shape_) #[1, 375, 250, 64]
output_shape = [shape_[0], shape_[1] * 2, shape_[2] * 2, 3]print('pool:',pool.get_shape())
# 反卷积操作
conts = tf.nn.conv2d_transpose(pool,kernel2,output_shape,strides=[1,2,2,1],padding='SAME')# print(conv1.get_shape())a = tf.transpose(conts, [0, 3, 1, 2])b = tf.transpose(tf.squeeze(a) , [1,2,0])with tf.Session() as sess:sess.run(tf.global_variables_initializer())# conv1_convert = sess.run(tf.transpose(conts, [0, 3, 1, 2]))# fig6, ax6 = plt.subplots(nrows=3, ncols=8, figsize=(8, 8))# plt.title('Pool2 32x7x7')# for i in range(8):# for j in range(8):# ax6[i][j].imshow(conv1_convert[0][(i + 1) * j])# plt.show()plt.imshow(sess.run(b))plt.show()
反卷积与反卷积核的初始化问题相关推荐
- cnn stride and padding_彻底搞懂CNN中的卷积和反卷积
前言 卷积和反卷积在CNN中经常被用到,想要彻底搞懂并不是那么容易.本文主要分三个部分来讲解卷积和反卷积,分别包括概念.工作过程.代码示例,其中代码实践部分主结合TensorFlow框架来进行实践.给 ...
- 反卷积(Deconvolution)、上采样(UNSampling)与上池化(UnPooling)加入自己的思考(tensorflow函数)(一)
ps 之前是做分类的根本就是没有很深的接触反卷积(Deconvolution).上采样(UNSampling)与上池化(UnPooling)等,要写这个主要是我在找unet代码时候发现反卷积这一步正常 ...
- 深度学习之卷积和反卷积
ps 零零总总接触深度学习有1年了,虽然时间是一段一段的.现在再拾起来做一个新的项目,有些东西又要重新理解,感觉麻烦.现在就再次学习时候有些困惑的地方捋一遍. 1.卷积 说到卷积,我现在还有印象的是大 ...
- python 反卷积(DeConv) tensorflow反卷积(DeConv)(实现原理+手写)
Tensorflow反卷积(DeConv)实现原理+手写python代码实现反卷积(DeConv) 理解: https://www.zhihu.com/question/43609045/answer ...
- 反卷积原理和实际代码详细讲解!
做的项目里涉及到了反卷积,上网查了很多资料发现有的只讲了原理,没有直观的代码实践,有些讲了代码却又完全忽视原理,所以想要以这篇博文做一个小小的整合,方便以后查阅. 文章目录 反卷积原理 首先来看卷积操 ...
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
文章目录 反卷积的作用 卷积中padding的几个概念 No Padding Half(Same) Padding Full Padding 反卷积 反卷积中的Padding参数 反卷积的stride ...
- Tensorflow卷积与反卷积(目前看到的最详细的解释)
卷积操作 tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 除去name参数用以指定该操作 ...
- 池化层、感受野、空洞卷积、反卷积
池化层 在卷积神经网络中,连续几个卷积层接连在一起,称为卷积模块,随着卷积层增加,参数总数也相应增加,为了减少后续卷积层的参数,在卷积层之间加入池化层,提取抽象特征.池化核在输入上滑动,并通过函数对局 ...
- Tensorflow反卷积(DeConv)实现原理+手写python代码实现反卷积(DeConv)
最近看到一个巨牛的人工智能教程,分享一下给大家.教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家.平时碎片时间可以当小说看,[点这里可以去膜拜一下大神的" ...
最新文章
- 人民日报:人工智能,务实发展是正道
- nginx的重定向,反向代理以及负载均衡
- 【学术相关】建议收藏,到底哪些行为是学术不端?
- 给定的 columnmapping 与源或目标中的任意列均不匹配_闻歌研究 | 图文自动匹配任务研究调研...
- Adaboost 算法的原理与推导(转载)
- 201671010144 2016-2017 《java程序设计》--对象与类!
- 使用Fastjson提示No serializer found for class
- 利用关系图表深度挖掘潜在决策影响关系——微软CRM炫酷介绍之四
- 表单内如何直接贴图而不用上传图片_重磅更新|偷偷告诉你,表单大师官网改版啦啦啦啦...
- python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function
- aix oracle 创建实例,11gR2 for AIX使用dbca创建数据库遇到ORA-03113错误的案例
- 当c语言学到大成时,教孩子学编程(信息学奥赛C语言版)
- Matlab:实现Fra圆孔衍射仿真
- (半)自动化爬虫系统该包含的功能点及相关介绍
- java程序员电脑内存配置_学习JAVA对电脑配置有要求吗
- STC8H开发(十四): I2C驱动RX8025T高精度实时时钟芯片
- 13.	Zigbee应用程序框架开发指南 - 多网络支持
- 2021程序员笔记本电脑推荐
- 【网络】华为网络设备认证登录设置
- 学习Java的第十周
热门文章
- 高德地图 Web JS API示例学习笔记(3)——地图(三维地图)
- 浙工商计算机调剂,2021考研调剂:浙江工商大学硕士研究生调剂信息公告
- 修改电脑OEM信息IE标题栏修改
- Oracle OEM 重建 及 案例 说明
- “鬼影”病毒疑“躲猫猫”避风,金山发专杀工具施救
- QQ好友列表导出用JTree树实现
- java 3d验证码_Java实现验证码具体代码(图片、汉字)
- 数据结构与算法基础(青岛大学-王卓)(2)
- VST3主机开发(二)——插件调用
- Java之父的高龄码农路 硅谷公司的年龄歧视