【问题标题】:calculate the output of unlabeled images using a CNN algorithm使用 CNN 算法计算未标记图像的输出
【发布时间】:2021-09-10 19:20:39
【问题描述】:

我设计了一种算法,该算法使用 CNN 算法来训练标记的训练数据并将其与标记的测试数据进行比较。此外,学习型 AI 在编写代码以对未标记数据进行分类并获得准确性方面存在问题。 首先,取一张未标记的图像并进行预处理,在预测的过程中,结果是torch尺寸小或无法加载。在这种情况下,我想知道使用哪种方法。

该部分的代码如下。

predict_img = PIL.Image.open('./test1/test.jpg')
a= transforms.Resize((32, 9))
b= transforms.ToTensor()
c= transforms.Normalize(torch.Tensor(mean), torch.Tensor(std))
t=a(predict_img)
u=b(t).view(-1, 3, 3, 3)
v=c(u)

print(v.shape)
print(model(v).shape())

single_prediction = model(v)
print('Prediction: ', torch.argmax(single_prediction.data, 1).float())

在这部分中,我尝试将 torchsize 设置为相同,因为标记的训练和测试图像被转换为​​ [6, 3, 100, 100],但失败了。此外,“RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 3, 3, 3], but got 3-dimensional input of size [3, 100, 100] 被输出,将 torchsize 更改为 [ 32, 3, 3, 3],但由​​于衍生错误而失败。 感谢您的帮助。

【问题讨论】:

    标签: pytorch conv-neural-network prediction torch


    【解决方案1】:

    我将尝试回答您遇到的运行时错误。

    出现此错误:RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 3, 3, 3],但得到了大小为 [3, 100, 100] 的 3 维输入

    您收到此错误是因为 Pytorch 中的 nn.Conv2d 或 Torch 库中的任何层都接受 [batch_size, channels, height, width] 形式的 4 维数据。但是,您的输入张量是 [3, 100, 100] 缺少 batch_size 因此它错误。 因此,解决此问题的方法是通过执行 (Where image.size = (3, 100, 100)) 为您的图像添加一个额外的 batch_size 维度

    v = v.unsqueeze(0)
    

    这会将张量形状从 (3, 100, 100) 更改为 (1, 3, 100, 100),现在可以进入您的神经网络 萨塔克耆那教

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 2022-11-22
      • 2014-04-27
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多