欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TensorFlow中实现三维(3D)上下采样

程序员文章站 2022-03-20 12:50:56
...
def interpolate(x, scale=2, dhwc=None):
    def interpolate_torch(x):
        input = torch.Tensor(x)
        b, d, h, w, c = input.shape
        input = input.permute(0, 4, 1, 2, 3).contiguous()
        input_resized = F.interpolate(input, size=(int(d * scale), int(h * scale), int(w * scale)), mode='trilinear')
        input_resized = input_resized.permute(0, 2, 3, 4, 1).contiguous().numpy()
        return input_resized

    if dhwc is not None:
        d, h, w, c = dhwc[0], dhwc[1], dhwc[2], dhwc[3]  # dhwc stand for input size instead of output size!
    else:
        b, d, h, w, c = x.get_shape().as_list()[0], x.get_shape().as_list()[1], x.get_shape().as_list()[2], \
                        x.get_shape().as_list()[3], x.get_shape().as_list()[4]
    output = tf.py_func(lambda x: interpolate_torch(x), [x], tf.float32)
    output = tf.reshape(output, [-1, int(d * scale), int(h * scale), int(w * scale), int(c)])
    return output

provided by Bo Hu

相关标签: 日用小技能