1. 程式人生 > >tensorflow:雙線性插值反捲積

tensorflow:雙線性插值反捲積

首先生成3×3×3的黑色圖片
"""
生成3×3×3黑色影象
"""
def produce_image():
    size = 3
    x, y = ogrid[:size, :size]  # 第一部分產生多行一列 第二部分產生一行多列
    z = x + y
    z = z[:, :, newaxis]  # 增加第三維
    # print(z)
    img = repeat(z, 3, 2)/12  # 在第三維上覆制兩遍
    # print(img.shape)
    # print(img)
    io.imshow(img, interpolation='none')
    io.show()
    return img

列印結果:

雙線性插值反捲積程式碼如下:

"""
生成3×3×3黑色影象
"""
def produce_image():
    size = 3
    x, y = ogrid[:size, :size]  # 第一部分產生多行一列 第二部分產生一行多列
    z = x + y
    z = z[:, :, newaxis]  # 增加第三維
    # print(z)
    img = repeat(z, 3, 2)/12  # 在第三維上覆制兩遍
    # print(img.shape)
    # print(img)
    io.imshow(img, interpolation='none')
    io.show()
    return img

"""
上取樣 雙線性插值生成卷積核
"""
def upsampling_bilinear():
    #確定卷積核大小
    def get_kernel_size(factor):
        return 2*factor-factor%2
    # 建立相關矩陣
    def upsample_filt(size):
        factor=(size+1)//2
        if size%2==1:
            center=factor-1
        else:
            center=factor-0.5
        og=np.ogrid[:size,:size]
        # print(og)
        # print(og[0])
        # print(og[1])
        return (1-abs(og[0]-center)/factor)*(1-abs(og[1]-center)/factor)
    #進行上取樣卷積核
    def bilinear_upsample_weights(factor,number_of_classes):
        filter_size=get_kernel_size(factor)
        weights=np.zeros((filter_size,filter_size,
                  number_of_classes,number_of_classes),dtype=np.float32)
        upsample_kernel=upsample_filt(filter_size)
        # print(upsample_kernel)
        for i in range(number_of_classes):
            weights[:,:,i,i]=upsample_kernel
            # print(weights[:,:,i,i])
        # print(weights)
        # print(weights.shape)
        return weights
    weights=bilinear_upsample_weights(3,3)
    return weights
if __name__ == '__main__':
    import tensorflow as tf
    # upsampling()
    # upsampling_bilinear()
    image=produce_image()
    img = tf.cast(image, dtype=tf.float32)
    img = tf.expand_dims(img, 0)  # 增加維度
    #產生卷積核
    kerenel=upsampling_bilinear()
    #反捲積處理
    res=tf.nn.conv2d_transpose(img,kerenel,output_shape=[1,9,9,3],strides=[1,3,3,1],padding='SAME')
    with tf.Session() as sess:
        img = sess.run(res)
    io.imshow(img[0, :, :, :] , interpolation='none')
    io.show()

列印結果:能較好恢復原影象