RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

使用yolov5训练自己的数据集时候,出现了下面的报错:

D:\programfiles\miniconda\envs\py38torch_gpu\python.exe C:/Users/liqiang/Desktop/yolov5_pytorch/YOLOV5/yolov5-master/train.py --data ../MaskDataSet/data.yaml --cfg models/yolov5s.yaml --weights " --batch-size  16"
Using CUDA device0 _CudaDeviceProperties(name='GeForce RTX 2080 Super with Max-Q Design', total_memory=8192MB)

Namespace(adam=False, batch_size=16, bucket='', cache_images=False, cfg='models/yolov5s.yaml', data='../MaskDataSet/data.yaml', device='', epochs=300, evolve=False, global_rank=-1, hyp='data/hyp.scratch.yaml', image_weights=False, img_size=[640, 640], local_rank=-1, logdir='runs/', multi_scale=False, name='', noautoanchor=False, nosave=False, notest=False, rect=False, resume=False, single_cls=False, sync_bn=False, total_batch_size=16, weights=' --batch-size  16', workers=0, world_size=1)
Start Tensorboard with "tensorboard --logdir runs/", view at http://localhost:6006/
2022-04-14 15:46:18.023268: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2022-04-14 15:46:18.023376: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Hyperparameters {'lr0': 0.01, 'lrf': 0.2, 'momentum': 0.937, 'weight_decay': 0.0005, 'warmup_epochs': 3.0, 'warmup_momentum': 0.8, 'warmup_bias_lr': 0.1, 'box': 0.05, 'cls': 0.5, 'cls_pw': 1.0, 'obj': 1.0, 'obj_pw': 1.0, 'iou_t': 0.2, 'anchor_t': 4.0, 'fl_gamma': 0.0, 'hsv_h': 0.015, 'hsv_s': 0.7, 'hsv_v': 0.4, 'degrees': 0.0, 'translate': 0.1, 'scale': 0.5, 'shear': 0.0, 'perspective': 0.0, 'flipud': 0.0, 'fliplr': 0.5, 'mosaic': 1.0, 'mixup': 0.0}

                 from  n    params  module                                  arguments                     
  0                -1  1      3520  models.common.Focus                     [3, 32, 3]                    
  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                
  2                -1  1     19904  models.common.BottleneckCSP             [64, 64, 1]                   
  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               
  4                -1  1    161152  models.common.BottleneckCSP             [128, 128, 3]                 
  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              
  6                -1  1    641792  models.common.BottleneckCSP             [256, 256, 3]                 
  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              
  8                -1  1    656896  models.common.SPP                       [512, 512, [5, 9, 13]]        
  9                -1  1   1248768  models.common.BottleneckCSP             [512, 512, 1, False]          
 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              
 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 12           [-1, 6]  1         0  models.common.Concat                    [1]                           
 13                -1  1    378624  models.common.BottleneckCSP             [512, 256, 1, False]          
 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              
 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 16           [-1, 4]  1         0  models.common.Concat                    [1]                           
 17                -1  1     95104  models.common.BottleneckCSP             [256, 128, 1, False]          
 18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              
 19          [-1, 14]  1         0  models.common.Concat                    [1]                           
 20                -1  1    313088  models.common.BottleneckCSP             [256, 256, 1, False]          
 21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]              
 22          [-1, 10]  1         0  models.common.Concat                    [1]                           
 23                -1  1   1248768  models.common.BottleneckCSP             [512, 512, 1, False]          
 24      [17, 20, 23]  1     18879  models.yolo.Detect                      [2, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
D:\programfiles\miniconda\envs\py38torch_gpu\lib\site-packages\torch\nn\functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  ..\c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Traceback (most recent call last):
  File "C:/Users/liqiang/Desktop/yolov5_pytorch/YOLOV5/yolov5-master/train.py", line 468, in
    train(hyp, opt, device, tb_writer)
  File "C:/Users/liqiang/Desktop/yolov5_pytorch/YOLOV5/yolov5-master/train.py", line 81, in train
    model = Model(opt.cfg, ch=3, nc=nc).to(device)  # create 就是咱们之前讲的创建模型那块
  File "C:\Users\liqiang\Desktop\yolov5_pytorch\YOLOV5\yolov5-master\models\yolo.py", line 89, in __init__
    self._initialize_biases()  # only run once
  File "C:\Users\liqiang\Desktop\yolov5_pytorch\YOLOV5\yolov5-master\models\yolo.py", line 149, in _initialize_biases
    b[:, 4] += math.log(8 / (640 / s) ** 2)  # obj (8 objects per 640 image)

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

Process finished with exit code 1
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation._第1张图片

 定位到报错的yolo.py文件:RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation._第2张图片

 修改:

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation._第3张图片

 改为:(添加with torch.no_grad()

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation._第4张图片

你可能感兴趣的:(计算机视觉,python)