解决YOLOv5中训练时出现nan的情况

707 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第37天,点击查看活动详情

问题复现

yolov5训练时,出现系数为nan和0的问题。

cpu跑没有问题,gpu出现nan和0的问题。一般问题cuda问题和显卡的原因。

显卡为GTX 16XX系列的在cuda使用较新版本时会出现该问题。

例如我自己的问题。

pytorch为1.11.0时,DEll G3 显卡:GTX 1650 cuda11.6(cuda11.5调试过)都会出现该问题 。

基本情况:

AutoAnchor: 6.13 anchors/target, 1.000 Best Possible Recall (BPR). Current anchors are a good fit to dataset 
Image sizes 640 train, 640 val
Using 0 dataloader workers
Logging results to runs\train\exp7
Starting training for 100 epochs...
 
     Epoch   gpu_mem       box       obj       cls    labels  img_size
      0/99     1.88G       nan       nan       nan        10       640: 100%|██████████| 14/14 [00:35<00:00,  2.52s/it]
D:\19837\anaconda3\envs\pytorch\lib\site-packages\torch\optim\lr_scheduler.py:136: UserWarning: Detected call of `lr_scheduler.step()` before `optimizer.step()`. In PyTorch 1.1.0 and later, you should call them in the opposite order: `optimizer.step()` before `lr_scheduler.step()`.  Failure to do this will result in PyTorch skipping the first value of the learning rate schedule. See more details at https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate
  "https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate", UserWarning)
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 7/7 [00:07<00:00,  1.09s/it]
                 all        106          0          0          0          0          0
 
     Epoch   gpu_mem       box       obj       cls    labels  img_size
      1/99     1.96G       nan       nan       nan       104       640:   7%|▋         | 1/14 [00:02<00:38,  2.92s/it]
Process finished with exit code -1

解决方案

实际上一开始我也不知道这样的训练是好还是坏,毕竟我是第一次使用YOLO。

于是我只能一点点的排错,排到后面发现这个nan我一直没解决,于是开始着眼解决它。幸好网上我能找到点资料,只要将cuda换成10.2的版本就可以进行了。当前我自己使用的状况也是很好。就是cuda版本下降可能算力并没有完全释放,稍微有点慢,不过起码它能正常训练了。

具体10.2的版本怎么换,和普通安装最新版差不多,就是可能10.2的资源需要找一下。

还有记得要对应相应版本的torch,torchvision

下面这个网址里有很多的版本可以查找 download.pytorch.org/whl/torch_s…