def transform(im, pixel_means, need_mean=False):
"""
transform into mxnet tensor
substract pixel size and transform to correct format
:param im: [height, width, channel] in BGR
:param pixel_means: [[[R, G, B pixel means]]]
:return: [batch, channel, height, width]
"""
im = im.copy()
im[:, :, (0, 1, 2)] = im[:, :, (2, 1, 0)]
im = im.astype(float)
if need_mean:
im -= pixel_means
im_tensor = im[np.newaxis, :]
# put channel first
channel_swap = (0, 3, 1, 2)
im_tensor = im_tensor.transpose(channel_swap)
return im_tensor
评论列表
文章目录