+关注继续查看

# Tensorflow反卷积（DeConv）实现原理+手写python代码实现反卷积（DeConv）

[[1,0,1],
[0,2,1],
[1,1,0]]

[[ 1, 0, 1],
[-1, 1, 0],
[ 0,-1, 0]]

## 2 代码实现

input_data=[
[[1,0,1],
[0,2,1],
[1,1,0]],

[[2,0,2],
[0,1,0],
[1,0,0]],

[[1,1,1],
[2,2,0],
[1,1,1]],

[[1,1,2],
[1,0,1],
[0,2,2]]

]
weights_data=[
[[[ 1, 0, 1],
[-1, 1, 0],
[ 0,-1, 0]],
[[-1, 0, 1],
[ 0, 0, 1],
[ 1, 1, 1]],
[[ 0, 1, 1],
[ 2, 0, 1],
[ 1, 2, 1]],
[[ 1, 1, 1],
[ 0, 2, 1],
[ 1, 0, 1]]],

[[[ 1, 0, 2],
[-2, 1, 1],
[ 1,-1, 0]],
[[-1, 0, 1],
[-1, 2, 1],
[ 1, 1, 1]],
[[ 0, 0, 0],
[ 2, 2, 1],
[ 1,-1, 1]],
[[ 2, 1, 1],
[ 0,-1, 1],
[ 1, 1, 1]]]
]

#根据输入map([h,w])和卷积核([k,k]),计算卷积后的feature map
import numpy as np
def compute_conv(fm,kernel):
[h,w]=fm.shape
[k,_]=kernel.shape
r=int(k/2)
#定义边界填充0后的map
#保存计算结果
rs=np.zeros([h,w],np.float32)
#将输入在指定该区域赋值，即除了4个边界后，剩下的区域
#对每个点为中心的区域遍历
for i in range(1,h+1):
for j in range(1,w+1):
#取出当前点为中心的k*k区域
#计算当前点的卷积,对k*k个点点乘后求和
rs[i-1][j-1]=np.sum(roi*kernel)

return rs

#填充0
def fill_zeros(input):
[c,h,w]=input.shape
rs=np.zeros([c,h*2+1,w*2+1],np.float32)

for i in range(c):
for j in range(h):
for k in range(w):
rs[i,2*j+1,2*k+1]=input[i,j,k]
return rs

def my_deconv(input,weights):
#weights shape=[out_c,in_c,h,w]
[out_c,in_c,h,w]=weights.shape
out_h=h*2
out_w=w*2
rs=[]
for i in range(out_c):
w=weights[i]
tmp=np.zeros([out_h,out_w],np.float32)
for j in range(in_c):
conv=compute_conv(input[j],w[j])
#注意裁剪，最后一行和最后一列去掉
tmp=tmp+conv[0:out_h,0:out_w]
rs.append(tmp)

return rs

def main():
input=np.asarray(input_data,np.float32)
input= fill_zeros(input)
weights=np.asarray(weights_data,np.float32)
deconv=my_deconv(input,weights)

print(np.asarray(deconv))

if __name__=='__main__':
main()

[[[  4.   3.   6.   2.   7.   3.]
[  4.   3.   3.   2.   7.   5.]
[  8.   6.   8.   5.  11.   2.]
[  3.   2.   7.   2.   3.   3.]
[  5.   5.  11.   3.   9.   3.]
[  2.   1.   4.   5.   4.   4.]]

[[  4.   1.   7.   0.   7.   2.]
[  5.   6.   0.   1.   8.   5.]
[  8.   0.   8.  -2.  14.   2.]
[  3.   3.   9.   8.   1.   0.]
[  3.   0.  13.   0.  11.   2.]
[  3.   5.   3.   1.   3.   0.]]]

import tensorflow as tf
import numpy as np
def tf_conv2d_transpose(input,weights):
#input_shape=[n,height,width,channel]
input_shape = input.get_shape().as_list()
#weights shape=[height,width,out_c,in_c]
weights_shape=weights.get_shape().as_list()
output_shape=[input_shape[0], input_shape[1]*2 , input_shape[2]*2 , weights_shape[2]]

print("output_shape:",output_shape)

deconv=tf.nn.conv2d_transpose(input,weights,output_shape=output_shape,
return deconv

def main():
weights_np=np.asarray(weights_data,np.float32)
#将输入的每个卷积核旋转180°
weights_np=np.rot90(weights_np,2,(2,3))

const_input = tf.constant(input_data , tf.float32)
const_weights = tf.constant(weights_np , tf.float32 )

input = tf.Variable(const_input,name="input")
#[c,h,w]------>[h,w,c]
input=tf.transpose(input,perm=(1,2,0))
#[h,w,c]------>[n,h,w,c]
input=tf.expand_dims(input,0)

#weights shape=[out_c,in_c,h,w]
weights = tf.Variable(const_weights,name="weights")
#[out_c,in_c,h,w]------>[h,w,out_c,in_c]
weights=tf.transpose(weights,perm=(2,3,0,1))

#执行tensorflow的反卷积
deconv=tf_conv2d_transpose(input,weights)

init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)

deconv_val  = sess.run(deconv)

hwc=deconv_val[0]
print(hwc)

if __name__=='__main__':
main()

[[  4.   3.   6.   2.   7.   3.]
[  4.   3.   3.   2.   7.   5.]
[  8.   6.   8.   5.  11.   2.]
[  3.   2.   7.   2.   3.   3.]
[  5.   5.  11.   3.   9.   3.]
[  2.   1.   4.   5.   4.   4.]]
[[  4.   1.   7.   0.   7.   2.]
[  5.   6.   0.   1.   8.   5.]
[  8.   0.   8.  -2.  14.   2.]
[  3.   3.   9.   8.   1.   0.]
[  3.   0.  13.   0.  11.   2.]
[  3.   5.   3.   1.   3.   0.]]

|
7天前
|

15 0
|
7天前
|
TensorFlow 算法框架/工具 Python

12 1
|
2月前
|

98 1
|
2月前
|

38 0
|
2月前
|

36 0
|
2月前
|

37 0
|
5月前
|
TensorFlow 算法框架/工具 Python
【TensorFlow】Python中Tensorflow包安装使用存在的问题

71 0
|
5月前
|

【TensorFlow】Python虚拟环境中的依赖包迁移到断网环境
Python虚拟环境中的依赖包迁移到断网环境
67 0
|
5月前
|

126 0
|
5月前
|

100 0