【问题标题】:Predict multiple pictures and save prediction in array预测多张图片并将预测保存在数组中
【发布时间】:2021-04-26 11:47:30
【问题描述】:

我目前正在寻找一种方法来预测多张图片并将预测保存在一个数组中。我目前的方法是将所有图像张量保存在一个数组中,遍历数组并预测每个图像并将输入保存在数组中。有没有更高效的方法?

我的 1 张图片预测代码

def predict_image(path):
    print("Prediction in progress")
    image = Image.open(path)

    transformation = transforms.Compose([
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    img_tensor = transformation(image).float()
    img_tensor = img_tensor.unsqueeze_(0)

    if torch.cuda.is_available():
        img_tensor.cuda()

    input = Variable(img_tensor)
    output = model(input)
    index = output.data.numpy().argmax()
    return index

【问题讨论】:

    标签: python machine-learning computer-vision pytorch


    【解决方案1】:

    我认为这可能是在测试时预测输出的最佳方法之一。

    您可能想尝试的另一件事是以批量方式提供输入。 由于您将所有张量存储在一个列表中,您可以使用torch.stack(list) 将多个张量堆叠在一个唯一的张量中。然后你就可以把它喂给模型了。

    注意,这样你必须先验决定批次的大小,你必须注意不要夸大其词。 PyTorch 将张量存储在 RAM(如果您使用 CPU)或 VRAM(如果您使用 GPU)中,因此批量增加太多可能会导致 CUDA OOM error .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多