我有一个输入图像,形状为[H,W,C]的numpy数组,其中H - 高度,W - 宽度和C - 通道。
我想把它转换成[B,C,H,W],其中B - 批量大小,每次应该等于1,并改变C的位置。
_image = np.array(_image)
h, w, c = _image.shape
image = torch.from_numpy(_image).unsqueeze_(0).view(1, c, h, w)
那么,这会保留图像,但不会移动原始图像的像素值吗?
答案 0 :(得分:2)
我更喜欢以下内容,它会保留原始图像不变,只需根据需要添加新轴:
_image = np.array(_image)
image = torch.from_numpy(_image)
image = image[np.newaxis, :]
# _unsqueeze works fine here too
然后根据需要交换轴:
image = image.permute(0, 3, 1, 2)
# permutation applies the following mapping
# axis0 -> axis0
# axis1 -> axis3
# axis2 -> axis1
# axis3 -> axis2