如何将ByteTensor转换为图像张量



我正在通过matlab将图像转换为二进制文件,并尝试将二进制matlab矢量(1d(加载到python中,并将其转换为ByteTensors:

img = np.fromfile(dir_train + image_name)
img = torch.ByteTensor(img) 

这很好用。字节张量的维数为2。之后,我想把它们转换回图像,就像重塑它们一样,因为神经网络(resnet18(需要4维张量。最好的方法是什么?

目前,我的网络需要[64,3,7,7]维度的张量,但字节张量是[81914]。

我是这样解决的:

img = np.fromfile(dir_train + image_name, 'bool')    # read in the binary file
img = img.reshape(1, 350, 350)    # reshape binary file
img = torch.ByteTensor(img)    # convert to ByteTensor
img = img.type(torch.FloatTensor)    # convert to FloatTensor

我认为没有比给字节跳动打字更好的方法了。除此之外,我更改了网络架构的第一个卷积层(resnet18(,以确保输入预期输入与convertet二进制输入匹配。

resnet18.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False) 

最新更新