【问题标题】:Double and Float RuntimeError while training fasterrcnn_mobilenetDouble and Float RuntimeError while training fasterrcnn_mobilenet
【发布时间】:2021-09-26 07:45:02
【问题描述】:

在过去的几周里,我在训练我的检测模型时一直在努力调试这个新问题。几周前的培训效果很好,但现在我突然发现了这个新的前所未闻的错误。

我像这样制作模型并进行整体训练,但仍然出现错误。

#Model 
torch.set_default_dtype(torch.float)
backbone = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True)
backbone.roi_heads.box_predictor.cls_score.out_features = len(classes) 
backbone.roi_heads.box_predictor.bbox_pred.out_features = 4 * (len(classes))

#Training 
for epoch in range(epochs):
      train_one_epoch(net, optimizer, train_loader, device, epoch, print_freq=10)
      evaluate(net, test_loader, device=device)
    
    print("Time for Total Training {:0.2f}".format(time.time() - start_time))

    return net

我奇怪的错误是:

—> 27 train_one_epoch(net, optimizer, train_loader, device, epoch, print_freq=10)
28 evaluate(net, test_loader, device=device)
29

/content/engine.py in train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq)
44 if not math.isfinite(loss_value):
45 print(“Loss is {}, stopping training”.format(loss_value))
—> 46 print(loss_dict_reduced)
47 sys.exit(1)
48

/usr/local/lib/python3.7/dist-packages/torch/_tensor.py in backward(self, gradient, retain_graph, create_graph, inputs)
253 create_graph=create_graph,
254 inputs=inputs)
→ 255 torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
256
257 def register_hook(self, hook):

/usr/local/lib/python3.7/dist-packages/torch/autograd/init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
147 Variable.execution_engine.run_backward(
148 tensors, grad_tensors, retain_graph, create_graph, inputs,
→ 149 allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag
150
151

RuntimeError: Found dtype Double but expected Float.

我也有像这样编码的盒子和标签 box = torch.as_tensor(boxes, dtype = torch.float32) labels = torch.as_tensor(labels, dtype = torch.int64) 我的图像是浮点张量。

如何消除此运行时错误? 对于我的所有代码以及我的数据类、导入的库和通过here 进行训练检查,您还可以看到 train_one_epochs 在here 上的样子

感谢您的帮助, 萨塔克

【问题讨论】:

    标签: python deep-learning pytorch torchvision


    【解决方案1】:

    我找到了问题的答案。

    我复制了 engine.py 文件。这个想法是,如果您正在导入一个包,那么不要创建另一个具有完全相同名称的文件并将其也导入。

    此外,我在 google collab 中了解到,如果您对 .py 文件进行更改并重新导入它,collab 似乎不会注册更改。我发现工厂重启效果很好。

    萨塔克·耆那教

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2021-07-31
      • 2021-07-10
      • 1970-01-01
      • 2021-05-10
      • 2021-09-15
      • 1970-01-01
      • 2019-12-13
      相关资源
      最近更新 更多