最近写深度学习网络需要对feature map进行双线性插值,opencv的resize不能对图像进行批处理,网上找到的双线性插值的代码基本都是三重for循环实现的,效率太低下,干脆自己用numpy库的矩阵运算实现了一个。
双线性插值原理参考:
https://zhuanlan.zhihu.com/p/112030273
感谢大佬Orz。
本文知乎同款:https://zhuanlan.zhihu.com/p/266845896
import numpy as np
def bilinear_interpolate(source, scale=2, pad=0.5):
sour_shape = source.shape
(sh, sw) = (sour_shape[-2], sour_shape[-1])
padding = pad*np.ones((sour_shape[0], sour_shape[1], sh+1, sw+1))
padding[:,:,:-1,:-1] = source
(th, tw) = (round(scale*sh), round(scale*sw))
grid = np.array(np.meshgrid(np.arange(th), np.arange(tw)), dtype=np.float32)
xy = np.copy(grid)
xy[0] *= sh/th
xy[1] *= sw/tw
x = xy[0].flatten()
y = xy[1].flatten()
c