首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >YOLO相关

YOLO相关

作者头像
小锋学长生活大爆炸
发布2021-11-24 11:02:58
发布2021-11-24 11:02:58
9610
举报

目录

报错

OpenCV can't augment image: 608 x 608

The size of tensor a (19) must match the size of tensor b (76) at non-singleton dimension 3

NotImplementedError: Create your own 'get_image_id' function"

view size is not compatible with input tensor’s size and stride

CUDA error: an illegal memory access was encountered

can't convert cuda:0 device type tensor to numpy.

知识点

YOLOv5超参介绍

YOLOv5n6.yaml文件介绍

报错

OpenCV can't augment image: 608 x 608

opencv版本问题,装的太高,降级:

代码语言:javascript
复制
pip install opencv_python==3.4.4.19

The size of tensor a (19) must match the size of tensor b (76) at non-singleton dimension 3

train.py文件中:

代码语言:javascript
复制
self.strides = [8, 16, 32] t改为 self.strides = [32, 16]

for i in range(3): 改为 for i in range(len(self.strides)):

NotImplementedError: Create your own 'get_image_id' function"

dataset.py文件中get_image_id函数:

先注释掉前面的:

代码语言:javascript
复制
raise NotImplementedError("Create your own 'get_image_id' function")

再根据自己图片的命名规则,提取名称中的id,如对于图片“level1_123.jpg”,可以这样写:

代码语言:javascript
复制
lv, no = os.path.splitext(os.path.basename(filename))[0].split("_")
lv = lv.replace("level", "")
no = f"{int(no):04d}"

view size is not compatible with input tensor’s size and stride

yolo_layer.py文件中,在view()前面加上contiguous(),如:

代码语言:javascript
复制
det_confs = det_confs.contiguous().view(output.size(0), num_anchors * output.size(2) * output.size(3), 1)

或者就用reshape来代替view(推荐):

代码语言:javascript
复制
det_confs = det_confs.reshape(output.size(0), num_anchors * output.size(2) * output.size(3), 1)

CUDA error: an illegal memory access was encountered

升级pytorch,我是从1.8.0直接升到最新的1.10.0,就好了。

代码语言:javascript
复制
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio===0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html

can't convert cuda:0 device type tensor to numpy.

utils/plots.py文件中,注释“if isinstance(output, torch.Tensor):”。需要这句:

代码语言:javascript
复制
output = output.cpu().numpy()

知识点

YOLOv5超参介绍

代码语言:javascript
复制
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
        meta = {'lr0': (1, 1e-5, 1e-1),  # 初始学习率(SGD=1E-2, Adam=1E-3)
                'lrf': (1, 0.01, 1.0),  # 余弦退火超参数学习率(lr0 * lrf)
                'momentum': (0.3, 0.6, 0.98),  # SGD学习率动量/Adam beta1
                'weight_decay': (1, 0.0, 0.001),  # 优化器权重衰减系数
                'warmup_epochs': (1, 0.0, 5.0),  # 预热学习epoch(fractions ok)
                'warmup_momentum': (1, 0.0, 0.95),  # 预热学习率动量
                'warmup_bias_lr': (1, 0.0, 0.2),  # 初始预热学习率
                'box': (1, 0.02, 0.2),  # giou损失的系数
                'cls': (1, 0.2, 4.0),  # 分类损失的系数
                'cls_pw': (1, 0.5, 2.0),  # 分类BCELoss中正样本的权重
                'obj': (1, 0.2, 4.0),  # obj损失的系数(像素级缩放)
                'obj_pw': (1, 0.5, 2.0),  # 物体BCELoss中正样本的权重
                'iou_t': (0, 0.1, 0.7),  # 标签与anchors的iou阈值
                'anchor_t': (1, 2.0, 8.0),  # 标签的长h宽w/anchor的长h_a宽w_a阈值, 即h/h_a, w/w_a都要在(1/2.0, 8.0)之间
                'anchors': (2, 2.0, 10.0),  # 每个输出网格的锚点(0为忽略)
                # 下面是一些数据增强的系数, 包括颜色空间和图片空间
                'fl_gamma': (0, 0.0, 2.0),  # 焦点损失gamma(efficientDet默认gamma=1.5)
                'hsv_h': (1, 0.0, 0.1),  # 图像hsv -色调增强(小数)
                'hsv_s': (1, 0.0, 0.9),  # 图像hsv -饱和度增强(小数)
                'hsv_v': (1, 0.0, 0.9),  # 图像hsv -明度增强(小数)
                'degrees': (1, 0.0, 45.0),  # 图像旋转(+/- 角度 )
                'translate': (1, 0.0, 0.9),  # 图像水平和垂直平移 (+/- 小数)
                'scale': (1, 0.0, 0.9),  # 图像缩放(+/- 比例)
                'shear': (1, 0.0, 10.0),  # 图像剪切(+/- 程度)
                'perspective': (0, 0.0, 0.001),  # 图像透视变换(+/- 小数),范围0-0.001
                'flipud': (1, 0.0, 1.0),  # 图像上下翻转 (probability)
                'fliplr': (0, 0.0, 1.0),  # 图像左右翻转 (probability)
                'mosaic': (1, 0.0, 1.0),  # 图像马赛克 (probability)
                'mixup': (1, 0.0, 1.0),  # 图像混合 (probability)
                'copy_paste': (1, 0.0, 1.0)}  # 段复制粘贴 (probability)
}

YOLOv5n6.yaml文件介绍

代码语言:javascript
复制
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license

# Parameters
nc: 80  # 目标的类别数量
depth_multiple: 0.33  # 模型深度。控制模块的数量,当模块的数量number不为1时,模块的数量 = number * depth。
width_multiple: 0.25  # 模型的宽度。控制卷积核的数量,卷积核的数量 = number  * width。
anchors:
  - [19,27,  44,40,  38,94]  # P3/8 检测小目标  19,27是一组尺寸,一共三组 
  - [96,68,  86,152,  180,137]  # P4/16
  - [140,301,  303,264,  238,542]  # P5/32
  - [436,615,  739,380,  925,792]  # P6/64 检测大目标

# YOLOv5 v6.0 backbone
backbone:
  # from   第一列 输入来自哪一层  -1代表上一层, 4代表第4层     
  # number 第二列 卷积核的数量    最终数量需要乘上width  
  # module 第三列 模块名称 包括:Conv Focus BottleneckCSP  SPP  
          # Focus, [64, 3]:对特征图的切片操作,模块参数中的 [64, 3] 解析得到[3, 32, 3] ,输入为3(RGB),输出为64*width_multiple = 32,3是卷积核 3*3
          # Conv, [512, 3, 2]:Conv由conv+Bn+Leaky_relu激活函数三者组成。512是卷积核数量,最终数量需要乘上width_multiple。3是卷积核 3*3。2是步长。
          # BottleneckCSP, [1024, False]:借鉴CSPNet网络结构,由三个卷积层和X个Res unint模块Concate组成,如果带有False参数就是没有使用Res unint模块,而是采用conv+Bn+Leaky_relu
          # SPP, [1024, [5, 9, 13]]:采用1×1,5×5,9×9,13×13的最大池化的方式,进行多尺度融合。
  # args   第四列 模块的参数   
  [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
   [-1, 1, Conv, [128, 3, 2]],   # 1-P2/4 卷积核的数量 = 128 * wdith = 128*width_multiple=32        
   [-1, 3, C3, [128]],           # 模块数量 = 3 * depth_multiple =3*0.33=1  
   [-1, 1, Conv, [256, 3, 2]],   # 3-P3/8
   [-1, 6, C3, [256]],           # 模块数量 = 6 * depth_multiple =6*0.33=2
   [-1, 1, Conv, [512, 3, 2]],   # 5-P4/16
   [-1, 9, C3, [512]],
   [-1, 1, Conv, [768, 3, 2]],   # 7-P5/32
   [-1, 3, C3, [768]],
   [-1, 1, Conv, [1024, 3, 2]],  # 9-P6/64
   [-1, 3, C3, [1024]],
   [-1, 1, SPPF, [1024, 5]],     # 11
  ]

# YOLOv5 v6.0 head
# 包括 Neck 和 Detector head 两部分
head:
  [[-1, 1, Conv, [768, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']], # 上采样 
   [[-1, 8], 1, Concat, [1]],  # cat backbone P5 代表cat上一层和第8层 
   [-1, 3, C3, [768, False]],  # 15 第15层        

   [-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4 代表cat上一层和第6层  
   [-1, 3, C3, [512, False]],  # 19 第19层  

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3 代表cat上一层和第4层  
   [-1, 3, C3, [256, False]],  # 23 (P3/8-small) 第23层 

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 20], 1, Concat, [1]],  # cat head P4 代表cat上一层和第20层
   [-1, 3, C3, [512, False]],  # 26 (P4/16-medium) 第26层 

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 16], 1, Concat, [1]],  # cat head P5 代表cat上一层和第16层
   [-1, 3, C3, [768, False]],  # 29 (P5/32-large) 第29层 

   [-1, 1, Conv, [768, 3, 2]],
   [[-1, 12], 1, Concat, [1]],  # cat head P6 代表cat上一层和第12层
   [-1, 3, C3, [1024, False]],  # 32 (P6/64-xlarge) 第32层 

   [[23, 26, 29, 32], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5, P6) 代表输入的层数23/26/29/32 
  ]
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/11/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 报错
    • OpenCV can't augment image: 608 x 608
    • The size of tensor a (19) must match the size of tensor b (76) at non-singleton dimension 3
    • NotImplementedError: Create your own 'get_image_id' function"
    • view size is not compatible with input tensor’s size and stride
    • CUDA error: an illegal memory access was encountered
    • can't convert cuda:0 device type tensor to numpy.
  • 知识点
    • YOLOv5超参介绍
    • YOLOv5n6.yaml文件介绍
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档