反卷积与反卷积核的初始化问题
导论:
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()
相关文章
- Coin Change
Coin Change 问题描述: 给定一些货币的面额,如coins[1,5,6,8];假定每种货币有无数个; 再给定总共金额,如total11; 求最少需要多少个货币能够得到total的总额。问题来源: leetcode 322. Coin Change 核心…...
2023/3/28 16:00:52 - 扔鸡蛋问题
扔鸡蛋问题 问题来源: 经典的动态规划问题,题设是这样的: 如果你有2颗鸡蛋,和一栋36层高的楼,现在你想知道在哪一层楼之下,鸡蛋不会被摔碎,应该如何用最少的测试次数对于任何答案楼层都能够使…...
2023/3/28 16:00:51 - kmp算法题解
KMP算法题解 题目来源: 28. Implement strStr() class Solution { public: void getNext(string p, int next[]){int pLen p.length();next[0] -1;int k -1;int j 0;while (j<pLen-1) {if(k-1||p[k]p[j]){k; j; next[j]k;}else{k next[k];}} }int strStr(s…...
2023/3/28 16:00:49 - KMP模式匹配中匹配的总子串数
POJ_3461_Oulipo 题目来源: POJ_3461_Oulipo 类似题目: 题目1094:String Matching 2006上海交通大学机试真题 #include<stdio.h> #include<string.h>int next[10001]; int len1,len2,total; char str[10001],buf[1000001]; voi…...
2023/3/28 16:00:45 - 堆箱子问题
Box Stacking Problem 问题来源: 堆箱子问题 #include <stdio.h> #include <algorithm> #include <vector>using namespace std;int MAX[101]; int RESULT[101]; vector<vector<int> >res;bool cmp(vector<int> A, vector<…...
2023/3/28 16:00:44 - 素数环问题
素数环问题 题目来源: 题目1459:Prime ring problem #include <stdio.h> #include <map> #include <vector>using namespace std;int num1;bool isprime(int n){ //判断数n是不是素数if (n<2) { //若n小于2,则…...
2023/3/28 16:00:43 - 通配符匹配
通配符匹配 问题来源: 44. Wildcard Matching #include <stdio.h> #include <string.h> #include <stdlib.h>using namespace std;bool dp[10001][10001]; //dp[i][j]表示string的子串(0~i位置的子串)&#x…...
2023/3/28 16:00:42 - 子序列的最大和
子序列的最大和 问题来源及讲解 #include <stdio.h>using namespace std;int maxSubArraySum(int a[],int size) { // int size sizeof(a)/sizeof(int); //注意:千万不能再调用函数里面来求数组的大小,因为传入的只是数组的首位元素的地址。…...
2023/3/28 16:00:41 - c语言scanf相关
c语言scanf相关 1. 空格相关: 如果你用scanf()函数的话,遇到空格会返回; 如果需要输入一个字符串,但是有空格怎么办: 但是有时候用该方法时,会提示gets不安全,怎么办? char str[10]…...
2023/3/28 16:00:39 - 分解质因数:整除问题
分解质因数:整除问题 问题来源: 题目1104:整除问题 解题思路: 在c语言中,1000!太大,不能直接计算,一般的处理也不行。 原理:如果b能整除a,那么a的每个质因…...
2023/3/28 16:00:38 - 九度1097:取中值
九度1097:取中值 题目来源: 题目1097:取中值 注意:中值不需要排序 #include <stdio.h> #include <stdlib.h>using namespace std; int s1[1000001]; int s2[1000001];int main(){int n;while(scanf("%d"…...
2023/3/28 16:00:37 - sort对二维char数组排序?
C中如何用std::sort对二维char数组排序? 问题描述: 定义一个 char str[10005][15],要对10000个char数组 风格的字符串排序. 典型错误做法: bool cmp(char a[], char b[]){int temp strcmp(a,b); return temp<0; } ......sort(str, s…...
2023/3/28 16:00:36 - 输出目录结构(路径打印)
输出目录结构(路径打印) 问题来源: 题目1090:路径打印 思路: 把所有的文件提取出来,存在一个vector里,对其按要求排序输出即可。 代码如下: #include <stdio.h> #include …...
2023/3/28 16:00:35 - 字符串处理之string
字符串处理之string 前言: 在机试当中,对于字符串的处理中使用string的话往往能够带来方便。 实例: 输出目录结构(路径打印) string输入输出问题。 在机试时,为了节约时间,使用scanf和print…...
2023/3/28 16:00:34 - vector摘记
vector摘记 在循环程序时,当重新初始化vector对象时,需要用刀resize函数 v.resize(0); //每次循环,需要把v置空 实例: 输出目录结构(路径打印)...
2023/3/28 16:00:33 - magic number
定义: 思想: // C program to generate odd sized magic squares#include<stdio.h> #include<string.h>// A function to generate odd sized magic squares void generateSquare(int n) {int magicSquare[n][n];// set all slots as 0memse…...
2023/3/28 16:00:32 - 文件操作1
#include <iostream> #include <fstream> #include <vector>using namespace std;struct Points{ // 用于存储“点”的x、y坐标int x;int y; };int main(int argc, const char * argv[]) {string ifilename;string ofilename;cin >> ifilename;cin &…...
2023/3/28 16:00:31 - 归并排序-优化
merge sort improvements 归并排序的改进: 改进1: 归并排序适合n比较大的算法;我们可以在递归时,当递归到比较小的子序列时,使用插入排序。 代码: package mergesort;public class MergeSort {public stat…...
2023/3/28 16:00:28 - 归并排序-bottom-up(非递归版本)
Bottom-up mergesort 由于之前讨论的归并排序时用到的是递归的方法;而递归会带来很大的开销,本节讨论由底向上的非递归版本的归并排序 思想: 对于给定的数组,设置变量sz为每次归并元素的个数。首先sz为1,也就是每单个…...
2023/3/28 16:00:27 - SoftReference使用心得
SoftReference的特点是它的一个实例保存对一个Java对象的软引用,该软引用的存在不妨碍垃圾收集线程对该Java对象的回收。也就是说,一旦SoftReference保存了对一个Java对象的软引用后,在垃圾线程对这个Java对象回收前,SoftReferenc…...
2023/3/28 16:00:23 - JAVA SMTP SSLTSL
props.setProperty("mail.transport.protocol","smtp"); props.setProperty("mail.smtp.host", "smtp.163.com"); props.setProperty("mail.smtp.auth", true);SSL设置 properties.setProperty("mail.smtp.socketFacto…...
2023/3/28 16:00:21 - MyEclipse 查找接口实现类
我们经常需要用Control 跟踪某个方法 但往往只能跟踪到这个方法的接口定义而要查看实现类时需要手动查看源码 在MyEclipse中 提供了一个非常方便的快捷键 Ctrl T 可以直接查看方法实现类中的代码...
2023/3/28 16:00:20 - 当前程序暂停 线程暂停
当前程序暂停: try{Thread thread Thread.currentThread();thread.sleep(3000); }catch (InterruptedException e) {e.printStackTrace(); }线程暂停: private Object object new Object(); try {synchronized (object) {object.wait(1500);} } catch…...
2023/3/28 16:00:20 - MySql数据文件本地路径
Mysql安装路径下的my.ini配置文件中的 datadir 配置。 #Path to the database root datadir"C:/ProgramData/MySQL/MySQL Server 6.0/Data/"...
2023/3/28 16:00:18 - 常用SQL语句
修改表名 ALTER TABLE old_name RENAME TO new_nameMySQL lastindexOf select substring_index(http://www.example.com/dev/archive/examples/test.htm,/,-1)...
2023/3/28 16:00:17 - DB2 SQL0613N 标识的主键、唯一键或表分区键太长或者包含太多的列和时间段。
项目中用到的DB2数据库,创建表时 出现了这个蛋疼的 SQL0613N错误 SQL 脚本: create table convertPrintWarningMessage(id BIGINT not null generated by default as identity,version decimal(19,0),server VARCHAR(1024) not null unique,type decima…...
2023/3/28 16:00:16 - 关于Calendar.WEEK_OF_YEAR中西方约定不同的问题
西方的week概念, 周日是每周的开始, 周六是每周的结束.但是中国的概念是周一是每周的开始 Calendar time Calendar.getInstance(); time.setFirstDayOfWeek(2);//2是西方的星期一 转自http://bbs.csdn.net/topics/390551479?page1...
2023/3/28 16:00:15 - h2 导出中文 STRINGDECODE 问题
H2导出SQL文件脚本:java -cp h2*.jar org.h2.tools.Script -url jdbc:h2:tcp://ip:port/... -user sa -script test.zip -options compression zip 当导出H2时,所有中文变为STRINGDECODE,不方便查看,且不易导入到其它数据库中&am…...
2023/3/28 16:00:14 - jsp下拉菜单三元表达式
<select name"loanLevel" style"width:100%;color:#666666;"><option value"一级" ${loan.LOAN_LEVEL 一级 ? selected : }>一级</option><option value"二级" ${loan.LOAN_LEVEL 二级 ? selected : }>二…...
2023/3/28 16:00:12 - 【Android笔记#1】xml实现编辑框/按钮的椭圆样(shape标签)+应用标题栏与状态栏颜色一致化
[Android菜鸟笔记]Jelly_Bean个人自学安卓疑惑纪录&笔记 █背景椭圆样式 et-bg.xml如下: <?xml version"1.0" encoding"utf-8"?> <shape xmlns:android"http://schemas.android.com/apk/res/android"><corne…...
2023/3/28 16:00:08
最新文章
- Coin Change
Coin Change 问题描述: 给定一些货币的面额,如coins[1,5,6,8];假定每种货币有无数个; 再给定总共金额,如total11; 求最少需要多少个货币能够得到total的总额。问题来源: leetcode 322. Coin Change 核心…...
2023/3/28 16:00:52 - 扔鸡蛋问题
扔鸡蛋问题 问题来源: 经典的动态规划问题,题设是这样的: 如果你有2颗鸡蛋,和一栋36层高的楼,现在你想知道在哪一层楼之下,鸡蛋不会被摔碎,应该如何用最少的测试次数对于任何答案楼层都能够使…...
2023/3/28 16:00:51 - kmp算法题解
KMP算法题解 题目来源: 28. Implement strStr() class Solution { public: void getNext(string p, int next[]){int pLen p.length();next[0] -1;int k -1;int j 0;while (j<pLen-1) {if(k-1||p[k]p[j]){k; j; next[j]k;}else{k next[k];}} }int strStr(s…...
2023/3/28 16:00:49 - KMP模式匹配中匹配的总子串数
POJ_3461_Oulipo 题目来源: POJ_3461_Oulipo 类似题目: 题目1094:String Matching 2006上海交通大学机试真题 #include<stdio.h> #include<string.h>int next[10001]; int len1,len2,total; char str[10001],buf[1000001]; voi…...
2023/3/28 16:00:45 - 堆箱子问题
Box Stacking Problem 问题来源: 堆箱子问题 #include <stdio.h> #include <algorithm> #include <vector>using namespace std;int MAX[101]; int RESULT[101]; vector<vector<int> >res;bool cmp(vector<int> A, vector<…...
2023/3/28 16:00:44 - 素数环问题
素数环问题 题目来源: 题目1459:Prime ring problem #include <stdio.h> #include <map> #include <vector>using namespace std;int num1;bool isprime(int n){ //判断数n是不是素数if (n<2) { //若n小于2,则…...
2023/3/28 16:00:43 - 通配符匹配
通配符匹配 问题来源: 44. Wildcard Matching #include <stdio.h> #include <string.h> #include <stdlib.h>using namespace std;bool dp[10001][10001]; //dp[i][j]表示string的子串(0~i位置的子串)&#x…...
2023/3/28 16:00:42 - 子序列的最大和
子序列的最大和 问题来源及讲解 #include <stdio.h>using namespace std;int maxSubArraySum(int a[],int size) { // int size sizeof(a)/sizeof(int); //注意:千万不能再调用函数里面来求数组的大小,因为传入的只是数组的首位元素的地址。…...
2023/3/28 16:00:41 - c语言scanf相关
c语言scanf相关 1. 空格相关: 如果你用scanf()函数的话,遇到空格会返回; 如果需要输入一个字符串,但是有空格怎么办: 但是有时候用该方法时,会提示gets不安全,怎么办? char str[10]…...
2023/3/28 16:00:39 - 分解质因数:整除问题
分解质因数:整除问题 问题来源: 题目1104:整除问题 解题思路: 在c语言中,1000!太大,不能直接计算,一般的处理也不行。 原理:如果b能整除a,那么a的每个质因…...
2023/3/28 16:00:38 - 九度1097:取中值
九度1097:取中值 题目来源: 题目1097:取中值 注意:中值不需要排序 #include <stdio.h> #include <stdlib.h>using namespace std; int s1[1000001]; int s2[1000001];int main(){int n;while(scanf("%d"…...
2023/3/28 16:00:37 - sort对二维char数组排序?
C中如何用std::sort对二维char数组排序? 问题描述: 定义一个 char str[10005][15],要对10000个char数组 风格的字符串排序. 典型错误做法: bool cmp(char a[], char b[]){int temp strcmp(a,b); return temp<0; } ......sort(str, s…...
2023/3/28 16:00:36 - 输出目录结构(路径打印)
输出目录结构(路径打印) 问题来源: 题目1090:路径打印 思路: 把所有的文件提取出来,存在一个vector里,对其按要求排序输出即可。 代码如下: #include <stdio.h> #include …...
2023/3/28 16:00:35 - 字符串处理之string
字符串处理之string 前言: 在机试当中,对于字符串的处理中使用string的话往往能够带来方便。 实例: 输出目录结构(路径打印) string输入输出问题。 在机试时,为了节约时间,使用scanf和print…...
2023/3/28 16:00:34 - vector摘记
vector摘记 在循环程序时,当重新初始化vector对象时,需要用刀resize函数 v.resize(0); //每次循环,需要把v置空 实例: 输出目录结构(路径打印)...
2023/3/28 16:00:33 - magic number
定义: 思想: // C program to generate odd sized magic squares#include<stdio.h> #include<string.h>// A function to generate odd sized magic squares void generateSquare(int n) {int magicSquare[n][n];// set all slots as 0memse…...
2023/3/28 16:00:32 - 文件操作1
#include <iostream> #include <fstream> #include <vector>using namespace std;struct Points{ // 用于存储“点”的x、y坐标int x;int y; };int main(int argc, const char * argv[]) {string ifilename;string ofilename;cin >> ifilename;cin &…...
2023/3/28 16:00:31 - 归并排序-优化
merge sort improvements 归并排序的改进: 改进1: 归并排序适合n比较大的算法;我们可以在递归时,当递归到比较小的子序列时,使用插入排序。 代码: package mergesort;public class MergeSort {public stat…...
2023/3/28 16:00:28 - 归并排序-bottom-up(非递归版本)
Bottom-up mergesort 由于之前讨论的归并排序时用到的是递归的方法;而递归会带来很大的开销,本节讨论由底向上的非递归版本的归并排序 思想: 对于给定的数组,设置变量sz为每次归并元素的个数。首先sz为1,也就是每单个…...
2023/3/28 16:00:27 - SoftReference使用心得
SoftReference的特点是它的一个实例保存对一个Java对象的软引用,该软引用的存在不妨碍垃圾收集线程对该Java对象的回收。也就是说,一旦SoftReference保存了对一个Java对象的软引用后,在垃圾线程对这个Java对象回收前,SoftReferenc…...
2023/3/28 16:00:23 - JAVA SMTP SSLTSL
props.setProperty("mail.transport.protocol","smtp"); props.setProperty("mail.smtp.host", "smtp.163.com"); props.setProperty("mail.smtp.auth", true);SSL设置 properties.setProperty("mail.smtp.socketFacto…...
2023/3/28 16:00:21 - MyEclipse 查找接口实现类
我们经常需要用Control 跟踪某个方法 但往往只能跟踪到这个方法的接口定义而要查看实现类时需要手动查看源码 在MyEclipse中 提供了一个非常方便的快捷键 Ctrl T 可以直接查看方法实现类中的代码...
2023/3/28 16:00:20 - 当前程序暂停 线程暂停
当前程序暂停: try{Thread thread Thread.currentThread();thread.sleep(3000); }catch (InterruptedException e) {e.printStackTrace(); }线程暂停: private Object object new Object(); try {synchronized (object) {object.wait(1500);} } catch…...
2023/3/28 16:00:20 - MySql数据文件本地路径
Mysql安装路径下的my.ini配置文件中的 datadir 配置。 #Path to the database root datadir"C:/ProgramData/MySQL/MySQL Server 6.0/Data/"...
2023/3/28 16:00:18 - 常用SQL语句
修改表名 ALTER TABLE old_name RENAME TO new_nameMySQL lastindexOf select substring_index(http://www.example.com/dev/archive/examples/test.htm,/,-1)...
2023/3/28 16:00:17 - DB2 SQL0613N 标识的主键、唯一键或表分区键太长或者包含太多的列和时间段。
项目中用到的DB2数据库,创建表时 出现了这个蛋疼的 SQL0613N错误 SQL 脚本: create table convertPrintWarningMessage(id BIGINT not null generated by default as identity,version decimal(19,0),server VARCHAR(1024) not null unique,type decima…...
2023/3/28 16:00:16 - 关于Calendar.WEEK_OF_YEAR中西方约定不同的问题
西方的week概念, 周日是每周的开始, 周六是每周的结束.但是中国的概念是周一是每周的开始 Calendar time Calendar.getInstance(); time.setFirstDayOfWeek(2);//2是西方的星期一 转自http://bbs.csdn.net/topics/390551479?page1...
2023/3/28 16:00:15 - h2 导出中文 STRINGDECODE 问题
H2导出SQL文件脚本:java -cp h2*.jar org.h2.tools.Script -url jdbc:h2:tcp://ip:port/... -user sa -script test.zip -options compression zip 当导出H2时,所有中文变为STRINGDECODE,不方便查看,且不易导入到其它数据库中&am…...
2023/3/28 16:00:14 - jsp下拉菜单三元表达式
<select name"loanLevel" style"width:100%;color:#666666;"><option value"一级" ${loan.LOAN_LEVEL 一级 ? selected : }>一级</option><option value"二级" ${loan.LOAN_LEVEL 二级 ? selected : }>二…...
2023/3/28 16:00:12 - 【Android笔记#1】xml实现编辑框/按钮的椭圆样(shape标签)+应用标题栏与状态栏颜色一致化
[Android菜鸟笔记]Jelly_Bean个人自学安卓疑惑纪录&笔记 █背景椭圆样式 et-bg.xml如下: <?xml version"1.0" encoding"utf-8"?> <shape xmlns:android"http://schemas.android.com/apk/res/android"><corne…...
2023/3/28 16:00:08