首页 > 解决方案 > RuntimeError: 标量类型 Double 的预期对象,但在调用 _th_max 时为参数 #2 'other' 获得了标量类型 Float

问题描述

文件“/home/jake/venv/lib/python3.7/site-packages/torchvision/models/detection/rpn.py”,第 274 行,assign_targets_to_anchors match_quality_matrix = self.box_similarity(gt_boxes, anchors_per_image) 文件“/home/ jake/venv/lib/python3.7/site-packages/torchvision/ops/boxes.py”,第 153 行,在 box_iou 中 lt = torch.max(boxes1[:, None, :2], boxes2[:, :2 ]) # [N,M,2] RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'other' in call to _th_max

收到以下错误,这是类型

annotations-> [{'boxes': tensor([[131.5124, 169.4232, 165.8235, 209.7217],
        [157.2659, 185.7604, 199.0850, 210.4478],
        [184.4726, 171.3594, 211.5178, 210.3267],
        [170.5867, 154.1751, 211.7600, 188.7858],
        [162.8364, 156.2324, 182.1313, 186.7285],
        [131.2702, 149.8185, 149.4349, 172.3276],
        [192.0614, 140.5002, 208.4500, 155.2642],
        [165.2583, 140.5002, 188.2670, 158.0476],
        [169.5371, 131.9080, 193.3531, 151.1497],
        [131.1895, 133.2392, 140.1507, 156.3534]], device='cuda:0',
       dtype=torch.float64), 'labels': tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], device='cuda:0'), 'area': tensor([1382.6857, 1032.4053, 1053.8795, 1425.0382,  588.4218,  408.8705,
         241.9614,  403.7413,  458.2574,  207.1317], device='cuda:0'), 'iscrowd': tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device='cuda:0')}]

我认为 box 类型是 float64 。这导致问题如何解决它加倍?

标签: pythonpytorchtensortorchtorchvision

解决方案


没有任何上下文很难说。

但解决方法是调用mytensor = mytensor.double()每个张量,mymodel = mymodel.double()并将数据和模型转换为相同的双精度类型。或者,您可以替换.double().float()浮点类型。两种方法都应该正常工作


推荐阅读