正确的数据加载器设置,以训练更快的rcnn-resnet50,以便使用pytorch进行对象检测



我正在尝试训练 pytorchetorchvision.models.detection.fasterrcnn_resnet50_fpn来检测我自己图像中的物体。

根据文档,此模型需要一个图像列表和一个字典列表 "框"和"标签"作为键。所以我的数据存储器__getitem__()如下所示:

def __getitem__(self, idx):
# load images
_, img = self.images[idx].getImage()
img = Image.fromarray(img, mode='RGB')
objects = self.images[idx].objects
boxes = []
labels = []
for o in objects:
# append bbox to boxes
boxes.append([o.x, o.y, o.x+o.width, o.y+o.height])
# append the 4th char of class_id, the number of lights (1-4)
labels.append(int(str(o.class_id)[3]))
# convert everything into a torch.Tensor
boxes = torch.as_tensor(boxes, dtype=torch.float32)
labels = torch.as_tensor(labels, dtype=torch.int64)
target = {}
target["boxes"] = boxes
target["labels"] = labels
# transforms consists only of transforms.Compose([transforms.ToTensor()]) for the time being
if self.transforms is not None:
img = self.transforms(img)
return img, target

据我所知,它返回的正是所问的内容。我的数据加载器看起来像这样

data_loader = torch.utils.data.DataLoader(
dataset, batch_size=4, shuffle=False, num_workers=2)

但是,当它进入这个阶段时:for images, targets in dataloaders[phase]:它提高了

运行时错误:参数无效 0:张量的大小必须匹配,维度 0 除外。在 C:\w\1\s\windows\pytorch\aten\src\TH/generalric/THTensor.cpp:689 处获得维度 12 和 7

有人可以指出我正确的方向吗?

@jodag是对的,我必须编写一个单独的整理函数,以便网络像预期的那样接收数据。就我而言,我只需要绕过默认功能。

最新更新