tensorflow實現反捲積
阿新 • • 發佈:2019-02-19
先看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()
列印結果: