Closed LeoNull101 closed 2 years ago
@LeoNull101 👋 Hello! Thanks for asking about YOLOv5 🚀 dataset formatting. To train correctly your data must be in YOLOv5 format. Please see our Train Custom Data tutorial for full documentation on dataset setup and all steps required to start training your first model. A few excerpts from the tutorial:
COCO128 is an example small tutorial dataset composed of the first 128 images in COCO train2017. These same 128 images are used for both training and validation to verify our training pipeline is capable of overfitting. data/coco128.yaml, shown below, is the dataset config file that defines 1) the dataset root directory path
and relative paths to train
/ val
/ test
image directories (or *.txt files with image paths), 2) the number of classes nc
and 3) a list of class names
:
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco128 # dataset root dir
train: images/train2017 # train images (relative to 'path') 128 images
val: images/train2017 # val images (relative to 'path') 128 images
test: # test images (optional)
# Classes
nc: 80 # number of classes
names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush' ] # class names
After using a tool like Roboflow Annotate to label your images, export your labels to YOLO format, with one *.txt
file per image (if no objects in image, no *.txt
file is required). The *.txt
file specifications are:
class x_center y_center width height
format.x_center
and width
by image width, and y_center
and height
by image height.The label file corresponding to the above image contains 2 persons (class 0
) and a tie (class 27
):
Organize your train and val images and labels according to the example below. YOLOv5 assumes /coco128
is inside a /datasets
directory next to the /yolov5
directory. YOLOv5 locates labels automatically for each image by replacing the last instance of /images/
in each image path with /labels/
. For example:
../datasets/coco128/images/im0.jpg # image
../datasets/coco128/labels/im0.txt # label
Good luck 🍀 and let us know if you have any other questions!
👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.
Access additional YOLOv5 🚀 resources:
Access additional Ultralytics ⚡ resources:
Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!
Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!
Search before asking
Question
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
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 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 model_urls = { 'scnet50': 'https://backseason.oss-cn-beijing.aliyuncs.com/scnet/scnet50-dc6a7e87.pth', 'scnet50_v1d': 'https://backseason.oss-cn-beijing.aliyuncs.com/scnet/scnet50_v1d-4109d1e1.pth', 'scnet101': 'https://backseason.oss-cn-beijing.aliyuncs.com/scnet/scnet101-44c5b751.pth',
'scnet101_v1d': coming soon...
}
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)
Additional
my dataste is about ship detection and got only one class, and i hope to change the normal conv to CondConv, and the error comes