tensorflow反卷积:使用双线性插值获取初始化权重

tensorflow反卷积:使用双线性插值获取初始化权重

双线性插值是有两个变量的插值函数的线性插值扩展,其核心思想是在两个方向分别进行一次线性插值,常用于初始化卷积核的权重,是FCN语义分割必须进行的一步(如果选取高斯初始化或其他初始化方式,可能导致最后无法收敛)

tensorflow并没有直接给出双线性插值的初始化方式,需要我们自己动手写一下:

代码

from math import ceil
import tensorflow as tf
import numpy as np


def get_deconv_filter(f_shape):
    # 双线性插值
    width = f_shape[0]
    height = f_shape[1]
    f = ceil(width/2.0)
    c = (2 * f - 1 - f % 2) / (2.0 * f)

    bilinear = np.zeros([f_shape[0], f_shape[1]])

    for x in range(width):
        for y in range(height):
            value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
            bilinear[x, y] = value

    weights = np.zeros(f_shape)

    for i in range(f_shape[2]):
        weights[:, :, i, i] = bilinear

    init = tf.constant_initializer(value=weights,
                                   dtype=tf.float32)

    return tf.get_variable(name="up_filter", initializer=init,
                           shape=weights.shape)

这样就获得了一个双线性插值初始化的(反)卷积核;

weights = get_deconv_filter(f_shape)
            
deconv = tf.nn.conv2d_transpose(bottom, weights, output_shape,strides=strides, padding='SAME')                                       

再通过tf.nn.conv2d_transpose函数就生成了一个反卷积层。


版权声明:本文为weixin_44936889原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>