根据本教程:https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html,我正在使用PyTorch优化更快的RCNN
结果相当不错,但只有在向模型提供单个张量的情况下,才能进行预测。例如:
# This works well
>>> img, _ = dataset_test[3]
>>> img.shape
torch.Size([3, 1200, 1600])
>>> model.eval()
>>> with torch.no_grad():
.. preds = model([img.to(device)])但是当我一次输入多个张量时,我会得到这个错误:
>>> random_idx = torch.randint(high=50, size=(4,))
>>> images = torch.stack([dataset_test[idx][0] for idx in random_idx])
>>> images.shape
torch.Size([4, 3, 1200, 1600])
>>> with torch.no_grad():
.. preds = model(images.to(device))
RuntimeError Traceback (most recent call last)
<ipython-input-101-52caf8fee7a4> in <module>()
5 model.eval()
6 with torch.no_grad():
----> 7 prediction = model(images.to(device))
...
RuntimeError: The expanded size of the tensor (1600) must match the existing size (1066) at non-singleton dimension 2. Target sizes: [3, 1200, 1600]. Tensor sizes: [3, 800, 1066]编辑
当输入3D张量列表时工作(我的这种行为有点奇怪,我不明白为什么它不能与4D张量一起工作):
>>> random_idx = torch.randint(high=50, size=(4,))
>>> images = [dataset_test[idx][0].to(device) for idx in random_idx]
>>> images.shape
torch.Size([4, 3, 1200, 1600])
>>> with torch.no_grad():
.. preds = model(images)发布于 2019-08-12 22:52:16
在训练模式中,MaskRCNN期望张量列表作为“输入图像”,字典列表作为“目标”。这种特殊的设计选择是由于每个图像可以具有可变数量的对象的事实,即每个图像的目标张量将具有可变的维度,因此我们被迫使用列表而不是目标的批量张量。
然而,使用图像张量列表而不是使用批量张量仍然不是完全必要的。我的猜测是,为了一致性,他们也使用了图像的张量列表。此外,这还提供了一个额外的优势,即能够使用可变大小的图像作为输入,而不是固定大小。
由于这种特定的设计选择,模型也希望在评估模式期间将张量列表作为输入。
至于模型的速度性能,这种设计选择在评估过程中可能会产生一些负面影响,但我不能百分之百肯定地说。然而,在训练过程中,由于我们对每个图像具有可变维度的目标张量,因此我们被迫逐个迭代所有图像以进行损失计算。因此,在训练过程中,在图像张量列表上使用图像的批量张量不会有速度增益。
https://stackoverflow.com/questions/57457631
复制相似问题