tensorflow:双线性插值反卷积(非封装函数)

 源代码

https://blog.csdn.net/fanzonghao/article/details/81104094?ops_request_misc=&request_id=&biz_id=102&utm_term=tensorflow%E5%A4%84%E7%90%86%E5%8F%8C%E7%BA%BF%E6%80%A7%E6%8F%92%E5%80%BC%E7%AE%97%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-81104094.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187icon-default.png?t=LA92https://blog.csdn.net/fanzonghao/article/details/81104094?ops_request_misc=&request_id=&biz_id=102&utm_term=tensorflow%E5%A4%84%E7%90%86%E5%8F%8C%E7%BA%BF%E6%80%A7%E6%8F%92%E5%80%BC%E7%AE%97%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-81104094.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187

但在源代码使用后发现无法在tensorflow2.0上运行,同时源代码未标注各个使用库

更正后如下:


# 生成3×3×3黑色图像
def produce_image():
    size = 3
    x, y = np.ogrid[:size, :size]  # 第一部分产生多行一列 第二部分产生一行多列
    z = x + y
    z = z[:, :, np.newaxis]  # 增加第三维
    # print(z)
    img = np.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 - np.abs(og[0] - center) / factor) * (1 - np.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

    import numpy as np
    from skimage import io

    # 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')

    tf.compat.v1.disable_eager_execution()
    res = tf.constant(res)
    sess = tf.compat.v1.Session()
    img = sess.run(res)
    io.imshow(img[0, :, :, :], interpolation='none')
    io.show()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值