首页 > 解决方案 > 火炬挤压,狭窄,切片?

问题描述

我有一组图像,我在这些图像上运行深度学习模型,以在 pytorch 中获得一些预测张量。这里有关于张量和形状/重塑的好的新手教程吗?

x = prediction['masks']

我知道的这个对象包含对象检测预测的掩码。但我不清楚如何一次提取一个掩码以将该掩码乘以我刚刚运行推理的图像。

在此处输入图像描述

我现在只是在整批中使用 1 张图像来玩。但一开始的第 0 维让我离开了这里。我看着尝试torch.squeeze(x,0)但没有任何作用

最后,我只想将我的掩码作为一个opencv或numpy数组/矩阵..在推理测试图像上像这样..

dst = opencvimage * mask_img

cv2.imshow('image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

opencvimage 是一种ndarray: (9212,1631,3)听起来很合理的 RGB 或 BGR 格式。

编辑 - 添加张量的制作方式

这是重要的评分功能..当我打包张量以摆脱0th dim形状轴时,听起来我应该在这里摆弄

   images = ImageDataset(paths_to_images, transform=transform)
    loader = torch.utils.data.DataLoader(images, batch_size=1, num_workers=1)

    all_predictions = []
    with torch.no_grad():
        for batch in loader:
            predictions = list(model(batch.to(device)))
            for prediction in predictions:
                all_predictions.append(prediction)

    #  Take predictions and masks and draw on the images

    return predictions

现在阅读它..似乎对面具的预测是空的..这意味着我需要重新访问我认为的教程..或者我是如何加载东西的

我添加了一张支票来查看这些盒子。它们看起来也是空的??

在此处输入图像描述

标签: numpypytorchtensortorchvision

解决方案


推荐阅读