Closed LeoNull101 closed 2 years ago
👋 Hello @LeoNull101, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.
If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.
If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.
For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.
Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:
git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install
YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.
Search before asking
YOLOv5 Component
Training
Bug
Traceback (most recent call last): File "train.py", line 667, in
main(opt)
File "train.py", line 562, in main
train(opt.hyp, opt, device, callbacks)
File "train.py", line 128, in train
model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
File "/content/yolov5/models/yolo.py", line 121, in init
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
File "/content/yolov5/models/yolo.py", line 135, in forward
return self._forward_once(x, profile, visualize) # single-scale inference, train
File "/content/yolov5/models/yolo.py", line 158, in _forward_once
x = m(x) # run
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, kwargs)
File "/content/yolov5/models/common.py", line 1005, in forward
softmax_att=self.attention(x)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(*input, *kwargs)
File "/content/yolov5/models/common.py", line 982, in forward
att=self.net(att)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in _call_impl
return forward_call(input, kwargs)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py", line 446, in forward
return self._conv_forward(input, self.weight, self.bias)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py", line 443, in _conv_forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [4, 128, 1, 1], expected input[1, 16, 1, 1] to have 128 channels, but got 16 channels instead
Environment
Using torch 1.10.0+cu111 (Tesla T4) in colab,other env all follow the requirement.txt
Minimal Reproducible Example
clone YOLOv5 and
!git clone https://github.com/ultralytics/yolov5 # clone repo %cd yolov5 %pip install -qr requirements.txt # install dependencies %pip install -q roboflow
import torch import os from IPython.display import Image, clear_output # to display images
print(f"Setup complete. Using torch {torch.version} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})")
!pip install roboflow
from roboflow import Roboflow rf = Roboflow(api_key="sr9Ld9Zy7jkFgHotOzwM") project = rf.workspace("nulllu").project("ship-phwt6") dataset = project.version(5).download("yolov5")#增强后数据集.
set up environment
os.environ["DATASET_DIRECTORY"] = "/content/datasets"
set up environment
os.environ["DATASET_DIRECTORY"] = "/content/datasets"
!python train.py --img 416 --batch 16 --epochs 60 --data {dataset.location}/data.yaml --weights '' --cfg /content/yolov5/models/yolov5s.yaml - --cache#new yolov5s.yaml
Additional
i make a new yaml for train as follow, and my dataset is about ship detection, and got only 1 class
YOLOv5 🚀 by Ultralytics, GPL-3.0 license
Parameters
nc: 80 # number of classes depth_multiple: 0.33 # model depth multiple width_multiple: 0.25 # layer channel multiple anchors:
YOLOv5 v6.0 backbone
backbone:
[from, number, module, args]
[[-1, 1, Conv, [64, 6, 2]], # 0-P1/2 [-1, 1, CondConv, [128, 3, 2, 1]], # 1-P2/4 [-1, 3, C3, [128]], [-1, 1, CondConv, [256, 3, 2, 1]], # 3-P3/8 [-1, 6, C3, [256]], [-1, 1, CondConv, [512, 3, 2, 1]], # 5-P4/16 [-1, 9, C3, [512]], [-1, 1, CondConv, [1024, 3, 2, 1]], # 7-P5/32 [-1, 3, C3, [1024]], [-1, 2, SPPF, [1024, 5]], # 9 ]
YOLOv5 v6.0 head
head: [[-1, 1, CondConv, [512, 1, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 6], 1, Concat, [1]], # cat backbone P4 [-1, 3, C3, [512, False]], # 13
[-1, 1, CondConv, [256, 1, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [[-1, 4], 1, Concat, [1]], # cat backbone P3 [-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, CondConv, [256, 3, 2, 1]], [[-1, 14], 1, Concat, [1]], # cat head P4 [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, CondConv, [512, 3, 2, 1]], [[-1, 10], 1, Concat, [1]], # cat head P5 [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) ]
also add the func in the common.py:
YOLOv5 🚀 by Ultralytics, GPL-3.0 license
""" Common modules """
import torch.nn.functional as F
import json import math import platform import warnings from collections import OrderedDict, namedtuple from copy import copy from pathlib import Path
from detectron2 import model_zoo
import cv2 import numpy as np import pandas as pd import requests import torch import torch.nn as nn import yaml from PIL import Image from torch.cuda import amp import torch.utils.model_zoo as model_zoo from utils.datasets import exif_transpose, letterbox from utils.general import (LOGGER, check_requirements, check_suffix, check_version, colorstr, increment_path, make_divisible, non_max_suppression, scale_coords, xywh2xyxy, xyxy2xywh) from utils.plots import Annotator, colors, save_one_box from utils.torch_utils import copy_attr, time_sync
def autopad(k, p=None): # kernel, padding
Pad to 'same'
class Conv(nn.Module):
Standard convolution
class DWConv(Conv):
Depth-wise convolution class
class TransformerLayer(nn.Module):
Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance)
class TransformerBlock(nn.Module):
Vision Transformer https://arxiv.org/abs/2010.11929
class Bottleneck(nn.Module):
Standard bottleneck
class SCNet(nn.Module): """ SCNet Variants Definations Parameters
def scnet50(pretrained=False, kwargs): """Constructs a SCNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = SCNet(SCBottleneck, [3, 4, 6, 3], deep_stem=False, stem_width=32, avg_down=False, avd=False, kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['scnet50'])) return model
class BottleneckCSP(nn.Module):
CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
class SCBottleneck(nn.Module): """SCNet SCBottleneck """ expansion = 4 pooling_r = 4 # down-sampling rate of the avg pooling layer in the K3 path of SC-Conv.
class C3(nn.Module):
CSP Bottleneck with 3 convolutions
class C3TR(C3):
C3 module with TransformerBlock()
class C3SPP(C3):
C3 module with SPP()
class C3Ghost(C3):
C3 module with GhostBottleneck()
class SPP(nn.Module):
Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729
class SPPF(nn.Module):
Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
class Focus(nn.Module):
Focus wh information into c-space
class GhostConv(nn.Module):
Ghost Convolution https://github.com/huawei-noah/ghostnet
class GhostBottleneck(nn.Module):
Ghost Bottleneck https://github.com/huawei-noah/ghostnet
class Contract(nn.Module):
Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40)
class Expand(nn.Module):
Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160)
class Concat(nn.Module):
Concatenate a list of tensors along dimension
class DetectMultiBackend(nn.Module):
YOLOv5 MultiBackend class for python inference on various backends
class AutoShape(nn.Module):
YOLOv5 input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS
class Detections:
YOLOv5 detections class for inference results
class Classify(nn.Module):
Classification head, i.e. x(b,c1,20,20) to x(b,c2)
class SCConv(nn.Module): def init(self, inplanes, planes, stride, padding, dilation, groups, pooling_r, norm_layer): super(SCConv, self).init() self.k2 = nn.Sequential( nn.AvgPool2d(kernel_size=pooling_r, stride=pooling_r), nn.Conv2d(inplanes, planes, kernel_size=3, stride=1, padding=padding, dilation=dilation, groups=groups, bias=False), norm_layer(planes), ) self.k3 = nn.Sequential( nn.Conv2d(inplanes, planes, kernel_size=3, stride=1, padding=padding, dilation=dilation, groups=groups, bias=False), norm_layer(planes), ) self.k4 = nn.Sequential( nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False), norm_layer(planes), )
输入为 [N, C, H, W],需要两个参数,in_planes为输特征通道数,K 为专家个数
class Attention(nn.Module): def init(self,in_planes,K): super().init() self.avgpool=nn.AdaptiveAvgPool2d(1) self.net=nn.Conv2d(in_planes, K, kernel_size=1) self.sigmoid=nn.Sigmoid()
class CondConv(nn.Module): def init(self,in_planes,out_planes,kernel_size,stride,padding=0, groups=1,K=4): super().init() self.in_planes = in_planes self.out_planes = out_planes self.K = K self.groups = groups self.kernel_size = kernel_size self.stride = stride self.padding = padding self.attention = Attention(in_planes=in_planes,K=K) self.weight = nn.Parameter(torch.randn(K,out_planes,in_planes//groups, kernel_size,kernel_size),requires_grad=True)
Are you willing to submit a PR?