tensorflow实现反卷积

先看ogrid用法
from numpy import ogrid,repeat,newaxis
from skimage import io
import numpy as np
size=3
x,y=ogrid[:size,:size]#第一部分产生多行一列 第二部分产生一行多列
print(x)
print(y)

打印结果:

newaxis用法:

"""
newaxis用法 增加维度
"""
x=np.random.randint(1,8,size=(2,3,4))
print(x.shape)
y=x[:,np.newaxis,:,:]
print(y.shape)
y=x[:,:,np.newaxis,:]
print(y.shape)

打印结果:

repeat用法:

"""
repeat用法 拓展
"""
a=np.array([1,2,3])
b=repeat(a,2)
print(b)
x = np.array([[1,2],[3,4]])
y=np.repeat(x, 2)
print(y)
y=np.repeat(x, 3, axis=1)
print(y)
y=np.repeat(x, [1, 2], axis=0)
print(y)

"""
生成3×3×3黑色图像
"""
size=3
x,y=ogrid[:size,:size]#第一部分产生多行一列 第二部分产生一行多列
z=x+y
z=z[:,:,newaxis]#增加第三维
img=repeat(z,3,2)#在第三维上复制
io.imshow(img,interpolation='none')
io.show()

从一个5×5×3上采样生成9×9×3  图像

"""
生成5×5×3黑色图像
"""
size=5
x,y=ogrid[:size,:size]#第一部分产生多行一列 第二部分产生一行多列
z=x+y
z=z[:,:,newaxis]#增加第三维
img=repeat(z,3,2)/12#在第三维上复制
io.imshow(img,interpolation='none')
io.show()
"""
upsampling 生成9×9的图像
"""
import tensorflow as tf
img=tf.cast(img,dtype=tf.float32)
img=tf.expand_dims(img,0)#增加维度
#随机生成卷积核
kernel=tf.random_uniform(shape=[5,5,3,3],dtype=tf.float32)
# kernel=tf.random_normal(shape=[5,5,3,3],dtype=tf.float32)
#反卷积
res=tf.nn.conv2d_transpose(img,kernel,output_shape=[1,9,9,3],strides=[1,1,1,1],padding='VALID')
with tf.Session() as sess:
    img=sess.run(res)
io.imshow(img[0,:,:,:]/np.argmax(img),interpolation='none')
io.show()

打印结果:

猜你喜欢

转载自blog.csdn.net/fanzonghao/article/details/81098795