重塑PyTorch的图像

时间:2019-08-12 20:13:46

标签: python image opencv keras pytorch

我曾经使用keras,它遵循的图像格式是[高度x宽度x通道x样本]。我决定切换到PyTorch。但是我没有切换数据加载方案。因此,现在我有了HpyWxCxS形状的numpy数组,而不是PyTorch所需的SxCxHxW。有人有什么想法可以转换吗?

2 个答案:

答案 0 :(得分:2)

首先,Keras格式为The Lab is a room. A desk is in the lab. On the desk are a personal computer and a note. Description of the note is "Password: wh23". Typing it on is an action applying to a topic and a touchable thing. Understand "Type [text] in/into/onto/on [something]" as typing it on. Instead of typing something on the computer: say "wrong password". Instead of typing "wh23" on the computer: say "welcome back, user!" test me with "x note / type wh23 into computer"
您需要做的只是(samples, height, width, channels)

如果幸运的话,您使用的是非默认配置moved = numpy.moveaxis(data, -1,1),则该配置与PyTorch的配置相同,即"channels_first"

当转换为割炬时:(samples, channels, height, width)

答案 1 :(得分:1)

您可以使用from_numpy函数将numpy数组轻松转换为pytorch中的张量:

import torch
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)

b现在可以在pytorch中使用