├── README.md
├── __init__.py
├── activations.py
├── augmentations.py
├── autoanchor.py
├── autobatch.py
├── aws
├── __init__.py
├── mime.sh
├── resume.py
└── userdata.sh
├── callbacks.py
├── data
├── Argoverse.yaml
├── GlobalWheat2020.yaml
├── Objects365.yaml
├── SKU-110K.yaml
├── VOC.yaml
├── VisDrone.yaml
├── coco.yaml
├── coco128.yaml
├── data.yaml
├── fire_data.yaml
├── hyps
│ ├── hyp.finetune.yaml
│ ├── hyp.finetune_objects365.yaml
│ ├── hyp.scratch-high.yaml
│ ├── hyp.scratch-low.yaml
│ ├── hyp.scratch-med.yaml
│ └── hyp.scratch.yaml
├── images
│ ├── bus.jpg
│ ├── fishman.jpg
│ ├── phone
│ │ ├── phone_1310.jpg
│ │ ├── phone_2096.jpg
│ │ ├── phone_2402.jpg
│ │ ├── phone_2423.jpg
│ │ ├── phone_419.jpg
│ │ ├── phone_633.jpg
│ │ ├── phone_689.jpg
│ │ └── phone_89.jpg
│ └── zidane.jpg
├── mask_data.yaml
├── scripts
│ ├── download_weights.sh
│ ├── get_coco.sh
│ └── get_coco128.sh
└── xView.yaml
├── datasets.py
├── datasets_not_print.py
├── downloads.py
├── flask_rest_api
├── README.md
├── example_request.py
└── restapi.py
├── general.py
├── images
├── 1_20.jpg
├── 1_67.jpg
├── 1c992e2b-108a-4e3c-859d-ae84d6f8ce7f.jpg
├── UI
│ ├── logo.jpeg
│ ├── lufei.png
│ ├── qq.png
│ ├── right.jpeg
│ ├── up.jpeg
│ └── xf.jpg
├── right.jpeg
├── tmp
│ ├── single_result.jpg
│ ├── single_result_vid.jpg
│ ├── tmp_upload.jpeg
│ ├── tmp_upload.jpg
│ ├── tmp_upload.png
│ └── upload_show_result.jpg
└── up.jpeg
├── loggers
├── __init__.py
├── __pycache__
│ └── __init__.cpython-38.pyc
└── wandb
│ ├── README.md
│ ├── __init__.py
│ ├── __pycache__
│ ├── __init__.cpython-38.pyc
│ └── wandb_utils.cpython-38.pyc
│ ├── log_dataset.py
│ ├── sweep.py
│ ├── sweep.yaml
│ └── wandb_utils.py
├── loss.py
├── metrics.py
├── models
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-38.pyc
│ ├── common.cpython-38.pyc
│ ├── experimental.cpython-38.pyc
│ └── yolo.cpython-38.pyc
├── common.py
├── experimental.py
├── hub
│ ├── anchors.yaml
│ ├── yolov3-spp.yaml
│ ├── yolov3-tiny.yaml
│ ├── yolov3.yaml
│ ├── yolov5-bifpn.yaml
│ ├── yolov5-fpn.yaml
│ ├── yolov5-p2.yaml
│ ├── yolov5-p6.yaml
│ ├── yolov5-p7.yaml
│ ├── yolov5-panet.yaml
│ ├── yolov5l6.yaml
│ ├── yolov5m6.yaml
│ ├── yolov5n6.yaml
│ ├── yolov5s-ghost.yaml
│ ├── yolov5s-transformer.yaml
│ ├── yolov5s6.yaml
│ └── yolov5x6.yaml
├── mask_yolov5l.yaml
├── mask_yolov5m.yaml
├── mask_yolov5s.yaml
├── tf.py
├── yolo.py
├── yolov5l.yaml
├── yolov5m.yaml
├── yolov5n.yaml
├── yolov5s.yaml
└── yolov5x.yaml
├── pic
├── 1.png
├── 11.png
├── 12.png
├── 13.png
├── 14.png
├── 2.png
├── 3.png
├── 4.png
└── 5.png
├── plots.py
├── torch_utils.py
├── ui
├── __init__.py
├── ji.py
├── server_main.py
├── train_server.py
└── x.py
└── yolov5s.pt
/README.md:
--------------------------------------------------------------------------------
1 | ## yolov5-fire
2 | yolov5-fire:基于YoloV5的火灾检测系统,将深度学习算法应用于火灾识别与检测领域,致力于研发准确高效的火灾识别与检测方法,实现图像中火灾区域的定位,为火灾检测技术走向实际应用提供理论和技术支持。
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | ## 环境配置
11 | 基于 Windows10 操作系统,python3.7,torch1.20,cuda11以及torchvision0.40的环境,使用VOC格式数据集进行训练。
12 | 训练前将标签文件放在fire_yolo_format文件夹下的labels文件夹中,训练前将图片文件放在fire_yolo_format文件夹下的images文件夹中。
13 |
14 | ## 训练样本集设计
15 |
16 | 从线上收集了2059张包含起火点事物的图片,组合训练集和测试集,训练集包括1442张图像,测试集包括617张图像,通过labelimg对起火位置进行标注,如图所示。
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | ## 模型训练过程
25 |
26 | 模型训练流程图、训练过程及测试结果
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | 模型训练
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | 模型检测
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | ## 基于Yolov5的火灾检测系统
59 |
60 | 系统界面设计及效果图
61 |
62 |
63 |
64 |
65 |
66 |
67 | 图片检测界面
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | 摄像头实时检测界面
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | 视频文件检测界面
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/__init__.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | utils/initialization
4 | """
5 |
6 |
7 | def notebook_init():
8 | # For YOLOv5 notebooks
9 | print('Checking setup...')
10 | from IPython import display # to display images and clear console output
11 |
12 | from utils.general import emojis
13 | from utils.torch_utils import select_device # YOLOv5 imports
14 |
15 | display.clear_output()
16 | select_device(newline=False)
17 | print(emojis('Setup complete ✅'))
18 | return display
19 |
--------------------------------------------------------------------------------
/activations.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Activation functions
4 | """
5 |
6 | import torch
7 | import torch.nn as nn
8 | import torch.nn.functional as F
9 |
10 |
11 | # SiLU https://arxiv.org/pdf/1606.08415.pdf ----------------------------------------------------------------------------
12 | class SiLU(nn.Module): # export-friendly version of nn.SiLU()
13 | @staticmethod
14 | def forward(x):
15 | return x * torch.sigmoid(x)
16 |
17 |
18 | class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
19 | @staticmethod
20 | def forward(x):
21 | # return x * F.hardsigmoid(x) # for torchscript and CoreML
22 | return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for torchscript, CoreML and ONNX
23 |
24 |
25 | # Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
26 | class Mish(nn.Module):
27 | @staticmethod
28 | def forward(x):
29 | return x * F.softplus(x).tanh()
30 |
31 |
32 | class MemoryEfficientMish(nn.Module):
33 | class F(torch.autograd.Function):
34 | @staticmethod
35 | def forward(ctx, x):
36 | ctx.save_for_backward(x)
37 | return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
38 |
39 | @staticmethod
40 | def backward(ctx, grad_output):
41 | x = ctx.saved_tensors[0]
42 | sx = torch.sigmoid(x)
43 | fx = F.softplus(x).tanh()
44 | return grad_output * (fx + x * sx * (1 - fx * fx))
45 |
46 | def forward(self, x):
47 | return self.F.apply(x)
48 |
49 |
50 | # FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
51 | class FReLU(nn.Module):
52 | def __init__(self, c1, k=3): # ch_in, kernel
53 | super().__init__()
54 | self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
55 | self.bn = nn.BatchNorm2d(c1)
56 |
57 | def forward(self, x):
58 | return torch.max(x, self.bn(self.conv(x)))
59 |
60 |
61 | # ACON https://arxiv.org/pdf/2009.04759.pdf ----------------------------------------------------------------------------
62 | class AconC(nn.Module):
63 | r""" ACON activation (activate or not).
64 | AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
65 | according to "Activate or Not: Learning Customized Activation" .
66 | """
67 |
68 | def __init__(self, c1):
69 | super().__init__()
70 | self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
71 | self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
72 | self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))
73 |
74 | def forward(self, x):
75 | dpx = (self.p1 - self.p2) * x
76 | return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x
77 |
78 |
79 | class MetaAconC(nn.Module):
80 | r""" ACON activation (activate or not).
81 | MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network
82 | according to "Activate or Not: Learning Customized Activation" .
83 | """
84 |
85 | def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r
86 | super().__init__()
87 | c2 = max(r, c1 // r)
88 | self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
89 | self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
90 | self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)
91 | self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)
92 | # self.bn1 = nn.BatchNorm2d(c2)
93 | # self.bn2 = nn.BatchNorm2d(c1)
94 |
95 | def forward(self, x):
96 | y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)
97 | # batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891
98 | # beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable
99 | beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed
100 | dpx = (self.p1 - self.p2) * x
101 | return dpx * torch.sigmoid(beta * dpx) + self.p2 * x
102 |
--------------------------------------------------------------------------------
/augmentations.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Image augmentation functions
4 | """
5 |
6 | import math
7 | import random
8 |
9 | import cv2
10 | import numpy as np
11 |
12 | from utils.general import LOGGER, check_version, colorstr, resample_segments, segment2box
13 | from utils.metrics import bbox_ioa
14 |
15 |
16 | class Albumentations:
17 | # YOLOv5 Albumentations class (optional, only used if package is installed)
18 | def __init__(self):
19 | self.transform = None
20 | try:
21 | import albumentations as A
22 | check_version(A.__version__, '1.0.3', hard=True) # version requirement
23 |
24 | self.transform = A.Compose([
25 | A.Blur(p=0.01),
26 | A.MedianBlur(p=0.01),
27 | A.ToGray(p=0.01),
28 | A.CLAHE(p=0.01),
29 | A.RandomBrightnessContrast(p=0.0),
30 | A.RandomGamma(p=0.0),
31 | A.ImageCompression(quality_lower=75, p=0.0)],
32 | bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels']))
33 |
34 | LOGGER.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p))
35 | except ImportError: # package not installed, skip
36 | pass
37 | except Exception as e:
38 | LOGGER.info(colorstr('albumentations: ') + f'{e}')
39 |
40 | def __call__(self, im, labels, p=1.0):
41 | if self.transform and random.random() < p:
42 | new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed
43 | im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
44 | return im, labels
45 |
46 |
47 | def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5):
48 | # HSV color-space augmentation
49 | if hgain or sgain or vgain:
50 | r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
51 | hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
52 | dtype = im.dtype # uint8
53 |
54 | x = np.arange(0, 256, dtype=r.dtype)
55 | lut_hue = ((x * r[0]) % 180).astype(dtype)
56 | lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
57 | lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
58 |
59 | im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
60 | cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im) # no return needed
61 |
62 |
63 | def hist_equalize(im, clahe=True, bgr=False):
64 | # Equalize histogram on BGR image 'im' with im.shape(n,m,3) and range 0-255
65 | yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
66 | if clahe:
67 | c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
68 | yuv[:, :, 0] = c.apply(yuv[:, :, 0])
69 | else:
70 | yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
71 | return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
72 |
73 |
74 | def replicate(im, labels):
75 | # Replicate labels
76 | h, w = im.shape[:2]
77 | boxes = labels[:, 1:].astype(int)
78 | x1, y1, x2, y2 = boxes.T
79 | s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
80 | for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
81 | x1b, y1b, x2b, y2b = boxes[i]
82 | bh, bw = y2b - y1b, x2b - x1b
83 | yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
84 | x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
85 | im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b] # im4[ymin:ymax, xmin:xmax]
86 | labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
87 |
88 | return im, labels
89 |
90 |
91 | def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
92 | # Resize and pad image while meeting stride-multiple constraints
93 | shape = im.shape[:2] # current shape [height, width]
94 | if isinstance(new_shape, int):
95 | new_shape = (new_shape, new_shape)
96 |
97 | # Scale ratio (new / old)
98 | r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
99 | if not scaleup: # only scale down, do not scale up (for better val mAP)
100 | r = min(r, 1.0)
101 |
102 | # Compute padding
103 | ratio = r, r # width, height ratios
104 | new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
105 | dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
106 | if auto: # minimum rectangle
107 | dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
108 | elif scaleFill: # stretch
109 | dw, dh = 0.0, 0.0
110 | new_unpad = (new_shape[1], new_shape[0])
111 | ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
112 |
113 | dw /= 2 # divide padding into 2 sides
114 | dh /= 2
115 |
116 | if shape[::-1] != new_unpad: # resize
117 | im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
118 | top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
119 | left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
120 | im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
121 | return im, ratio, (dw, dh)
122 |
123 |
124 | def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
125 | border=(0, 0)):
126 | # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
127 | # targets = [cls, xyxy]
128 |
129 | height = im.shape[0] + border[0] * 2 # shape(h,w,c)
130 | width = im.shape[1] + border[1] * 2
131 |
132 | # Center
133 | C = np.eye(3)
134 | C[0, 2] = -im.shape[1] / 2 # x translation (pixels)
135 | C[1, 2] = -im.shape[0] / 2 # y translation (pixels)
136 |
137 | # Perspective
138 | P = np.eye(3)
139 | P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
140 | P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
141 |
142 | # Rotation and Scale
143 | R = np.eye(3)
144 | a = random.uniform(-degrees, degrees)
145 | # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
146 | s = random.uniform(1 - scale, 1 + scale)
147 | # s = 2 ** random.uniform(-scale, scale)
148 | R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
149 |
150 | # Shear
151 | S = np.eye(3)
152 | S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
153 | S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
154 |
155 | # Translation
156 | T = np.eye(3)
157 | T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
158 | T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
159 |
160 | # Combined rotation matrix
161 | M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
162 | if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
163 | if perspective:
164 | im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114))
165 | else: # affine
166 | im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
167 |
168 | # Visualize
169 | # import matplotlib.pyplot as plt
170 | # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
171 | # ax[0].imshow(im[:, :, ::-1]) # base
172 | # ax[1].imshow(im2[:, :, ::-1]) # warped
173 |
174 | # Transform label coordinates
175 | n = len(targets)
176 | if n:
177 | use_segments = any(x.any() for x in segments)
178 | new = np.zeros((n, 4))
179 | if use_segments: # warp segments
180 | segments = resample_segments(segments) # upsample
181 | for i, segment in enumerate(segments):
182 | xy = np.ones((len(segment), 3))
183 | xy[:, :2] = segment
184 | xy = xy @ M.T # transform
185 | xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine
186 |
187 | # clip
188 | new[i] = segment2box(xy, width, height)
189 |
190 | else: # warp boxes
191 | xy = np.ones((n * 4, 3))
192 | xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
193 | xy = xy @ M.T # transform
194 | xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine
195 |
196 | # create new boxes
197 | x = xy[:, [0, 2, 4, 6]]
198 | y = xy[:, [1, 3, 5, 7]]
199 | new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
200 |
201 | # clip
202 | new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
203 | new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)
204 |
205 | # filter candidates
206 | i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
207 | targets = targets[i]
208 | targets[:, 1:5] = new[i]
209 |
210 | return im, targets
211 |
212 |
213 | def copy_paste(im, labels, segments, p=0.5):
214 | # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
215 | n = len(segments)
216 | if p and n:
217 | h, w, c = im.shape # height, width, channels
218 | im_new = np.zeros(im.shape, np.uint8)
219 | for j in random.sample(range(n), k=round(p * n)):
220 | l, s = labels[j], segments[j]
221 | box = w - l[3], l[2], w - l[1], l[4]
222 | ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
223 | if (ioa < 0.30).all(): # allow 30% obscuration of existing labels
224 | labels = np.concatenate((labels, [[l[0], *box]]), 0)
225 | segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
226 | cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
227 |
228 | result = cv2.bitwise_and(src1=im, src2=im_new)
229 | result = cv2.flip(result, 1) # augment segments (flip left-right)
230 | i = result > 0 # pixels to replace
231 | # i[:, :] = result.max(2).reshape(h, w, 1) # act over ch
232 | im[i] = result[i] # cv2.imwrite('debug.jpg', im) # debug
233 |
234 | return im, labels, segments
235 |
236 |
237 | def cutout(im, labels, p=0.5):
238 | # Applies image cutout augmentation https://arxiv.org/abs/1708.04552
239 | if random.random() < p:
240 | h, w = im.shape[:2]
241 | scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
242 | for s in scales:
243 | mask_h = random.randint(1, int(h * s)) # create random masks
244 | mask_w = random.randint(1, int(w * s))
245 |
246 | # box
247 | xmin = max(0, random.randint(0, w) - mask_w // 2)
248 | ymin = max(0, random.randint(0, h) - mask_h // 2)
249 | xmax = min(w, xmin + mask_w)
250 | ymax = min(h, ymin + mask_h)
251 |
252 | # apply random color mask
253 | im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
254 |
255 | # return unobscured labels
256 | if len(labels) and s > 0.03:
257 | box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
258 | ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
259 | labels = labels[ioa < 0.60] # remove >60% obscured labels
260 |
261 | return labels
262 |
263 |
264 | def mixup(im, labels, im2, labels2):
265 | # Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
266 | r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
267 | im = (im * r + im2 * (1 - r)).astype(np.uint8)
268 | labels = np.concatenate((labels, labels2), 0)
269 | return im, labels
270 |
271 |
272 | def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
273 | # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
274 | w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
275 | w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
276 | ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
277 | return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
278 |
--------------------------------------------------------------------------------
/autoanchor.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Auto-anchor utils
4 | """
5 |
6 | import random
7 |
8 | import numpy as np
9 | import torch
10 | import yaml
11 | from tqdm import tqdm
12 |
13 | from utils.general import LOGGER, colorstr, emojis
14 |
15 | PREFIX = colorstr('AutoAnchor: ')
16 |
17 |
18 | def check_anchor_order(m):
19 | # Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
20 | a = m.anchors.prod(-1).view(-1) # anchor area
21 | da = a[-1] - a[0] # delta a
22 | ds = m.stride[-1] - m.stride[0] # delta s
23 | if da.sign() != ds.sign(): # same order
24 | LOGGER.info(f'{PREFIX}Reversing anchor order')
25 | m.anchors[:] = m.anchors.flip(0)
26 |
27 |
28 | def check_anchors(dataset, model, thr=4.0, imgsz=640):
29 | # Check anchor fit to data, recompute if necessary
30 | m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect()
31 | shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
32 | scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale
33 | wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh
34 |
35 | def metric(k): # compute metric
36 | r = wh[:, None] / k[None]
37 | x = torch.min(r, 1 / r).min(2)[0] # ratio metric
38 | best = x.max(1)[0] # best_x
39 | aat = (x > 1 / thr).float().sum(1).mean() # anchors above threshold
40 | bpr = (best > 1 / thr).float().mean() # best possible recall
41 | return bpr, aat
42 |
43 | anchors = m.anchors.clone() * m.stride.to(m.anchors.device).view(-1, 1, 1) # current anchors
44 | bpr, aat = metric(anchors.cpu().view(-1, 2))
45 | s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). '
46 | if bpr > 0.98: # threshold to recompute
47 | LOGGER.info(emojis(f'{s}Current anchors are a good fit to dataset ✅'))
48 | else:
49 | LOGGER.info(emojis(f'{s}Anchors are a poor fit to dataset ⚠️, attempting to improve...'))
50 | na = m.anchors.numel() // 2 # number of anchors
51 | try:
52 | anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
53 | except Exception as e:
54 | LOGGER.info(f'{PREFIX}ERROR: {e}')
55 | new_bpr = metric(anchors)[0]
56 | if new_bpr > bpr: # replace anchors
57 | anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
58 | m.anchors[:] = anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss
59 | check_anchor_order(m)
60 | LOGGER.info(f'{PREFIX}New anchors saved to model. Update model *.yaml to use these anchors in the future.')
61 | else:
62 | LOGGER.info(f'{PREFIX}Original anchors better than new anchors. Proceeding with original anchors.')
63 |
64 |
65 | def kmean_anchors(dataset='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
66 | """ Creates kmeans-evolved anchors from training dataset
67 |
68 | Arguments:
69 | dataset: path to data.yaml, or a loaded dataset
70 | n: number of anchors
71 | img_size: image size used for training
72 | thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
73 | gen: generations to evolve anchors using genetic algorithm
74 | verbose: print all results
75 |
76 | Return:
77 | k: kmeans evolved anchors
78 |
79 | Usage:
80 | from utils.autoanchor import *; _ = kmean_anchors()
81 | """
82 | from scipy.cluster.vq import kmeans
83 |
84 | thr = 1 / thr
85 |
86 | def metric(k, wh): # compute metrics
87 | r = wh[:, None] / k[None]
88 | x = torch.min(r, 1 / r).min(2)[0] # ratio metric
89 | # x = wh_iou(wh, torch.tensor(k)) # iou metric
90 | return x, x.max(1)[0] # x, best_x
91 |
92 | def anchor_fitness(k): # mutation fitness
93 | _, best = metric(torch.tensor(k, dtype=torch.float32), wh)
94 | return (best * (best > thr).float()).mean() # fitness
95 |
96 | def print_results(k, verbose=True):
97 | k = k[np.argsort(k.prod(1))] # sort small to large
98 | x, best = metric(k, wh0)
99 | bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
100 | s = f'{PREFIX}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr\n' \
101 | f'{PREFIX}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, ' \
102 | f'past_thr={x[x > thr].mean():.3f}-mean: '
103 | for i, x in enumerate(k):
104 | s += '%i,%i, ' % (round(x[0]), round(x[1]))
105 | if verbose:
106 | LOGGER.info(s[:-2])
107 | return k
108 |
109 | if isinstance(dataset, str): # *.yaml file
110 | with open(dataset, errors='ignore') as f:
111 | data_dict = yaml.safe_load(f) # model dict
112 | from utils.datasets import LoadImagesAndLabels
113 | dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
114 |
115 | # Get label wh
116 | shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
117 | wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh
118 |
119 | # Filter
120 | i = (wh0 < 3.0).any(1).sum()
121 | if i:
122 | LOGGER.info(f'{PREFIX}WARNING: Extremely small objects found. {i} of {len(wh0)} labels are < 3 pixels in size.')
123 | wh = wh0[(wh0 >= 2.0).any(1)] # filter > 2 pixels
124 | # wh = wh * (np.random.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1
125 |
126 | # Kmeans calculation
127 | LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...')
128 | s = wh.std(0) # sigmas for whitening
129 | k, dist = kmeans(wh / s, n, iter=30) # points, mean distance
130 | assert len(k) == n, f'{PREFIX}ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}'
131 | k *= s
132 | wh = torch.tensor(wh, dtype=torch.float32) # filtered
133 | wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered
134 | k = print_results(k, verbose=False)
135 |
136 | # Plot
137 | # k, d = [None] * 20, [None] * 20
138 | # for i in tqdm(range(1, 21)):
139 | # k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
140 | # fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True)
141 | # ax = ax.ravel()
142 | # ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.')
143 | # fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh
144 | # ax[0].hist(wh[wh[:, 0]<100, 0],400)
145 | # ax[1].hist(wh[wh[:, 1]<100, 1],400)
146 | # fig.savefig('wh.png', dpi=200)
147 |
148 | # Evolve
149 | npr = np.random
150 | f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma
151 | pbar = tqdm(range(gen), desc=f'{PREFIX}Evolving anchors with Genetic Algorithm:') # progress bar
152 | for _ in pbar:
153 | v = np.ones(sh)
154 | while (v == 1).all(): # mutate until a change occurs (prevent duplicates)
155 | v = ((npr.random(sh) < mp) * random.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
156 | kg = (k.copy() * v).clip(min=2.0)
157 | fg = anchor_fitness(kg)
158 | if fg > f:
159 | f, k = fg, kg.copy()
160 | pbar.desc = f'{PREFIX}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}'
161 | if verbose:
162 | print_results(k, verbose)
163 |
164 | return print_results(k)
165 |
--------------------------------------------------------------------------------
/autobatch.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Auto-batch utils
4 | """
5 |
6 | from copy import deepcopy
7 |
8 | import numpy as np
9 | import torch
10 | from torch.cuda import amp
11 |
12 | from utils.general import LOGGER, colorstr
13 | from utils.torch_utils import profile
14 |
15 |
16 | def check_train_batch_size(model, imgsz=640):
17 | # Check YOLOv5 training batch size
18 | with amp.autocast():
19 | return autobatch(deepcopy(model).train(), imgsz) # compute optimal batch size
20 |
21 |
22 | def autobatch(model, imgsz=640, fraction=0.9, batch_size=16):
23 | # Automatically estimate best batch size to use `fraction` of available CUDA memory
24 | # Usage:
25 | # import torch
26 | # from utils.autobatch import autobatch
27 | # model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False)
28 | # print(autobatch(model))
29 |
30 | prefix = colorstr('AutoBatch: ')
31 | LOGGER.info(f'{prefix}Computing optimal batch size for --imgsz {imgsz}')
32 | device = next(model.parameters()).device # get model device
33 | if device.type == 'cpu':
34 | LOGGER.info(f'{prefix}CUDA not detected, using default CPU batch-size {batch_size}')
35 | return batch_size
36 |
37 | d = str(device).upper() # 'CUDA:0'
38 | properties = torch.cuda.get_device_properties(device) # device properties
39 | t = properties.total_memory / 1024 ** 3 # (GiB)
40 | r = torch.cuda.memory_reserved(device) / 1024 ** 3 # (GiB)
41 | a = torch.cuda.memory_allocated(device) / 1024 ** 3 # (GiB)
42 | f = t - (r + a) # free inside reserved
43 | LOGGER.info(f'{prefix}{d} ({properties.name}) {t:.2f}G total, {r:.2f}G reserved, {a:.2f}G allocated, {f:.2f}G free')
44 |
45 | batch_sizes = [1, 2, 4, 8, 16]
46 | try:
47 | img = [torch.zeros(b, 3, imgsz, imgsz) for b in batch_sizes]
48 | y = profile(img, model, n=3, device=device)
49 | except Exception as e:
50 | LOGGER.warning(f'{prefix}{e}')
51 |
52 | y = [x[2] for x in y if x] # memory [2]
53 | batch_sizes = batch_sizes[:len(y)]
54 | p = np.polyfit(batch_sizes, y, deg=1) # first degree polynomial fit
55 | b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size)
56 | LOGGER.info(f'{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%)')
57 | return b
58 |
--------------------------------------------------------------------------------
/aws/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/aws/__init__.py
--------------------------------------------------------------------------------
/aws/mime.sh:
--------------------------------------------------------------------------------
1 | # AWS EC2 instance startup 'MIME' script https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
2 | # This script will run on every instance restart, not only on first start
3 | # --- DO NOT COPY ABOVE COMMENTS WHEN PASTING INTO USERDATA ---
4 |
5 | Content-Type: multipart/mixed; boundary="//"
6 | MIME-Version: 1.0
7 |
8 | --//
9 | Content-Type: text/cloud-config; charset="us-ascii"
10 | MIME-Version: 1.0
11 | Content-Transfer-Encoding: 7bit
12 | Content-Disposition: attachment; filename="cloud-config.txt"
13 |
14 | #cloud-config
15 | cloud_final_modules:
16 | - [scripts-user, always]
17 |
18 | --//
19 | Content-Type: text/x-shellscript; charset="us-ascii"
20 | MIME-Version: 1.0
21 | Content-Transfer-Encoding: 7bit
22 | Content-Disposition: attachment; filename="userdata.txt"
23 |
24 | #!/bin/bash
25 | # --- paste contents of userdata.sh here ---
26 | --//
27 |
--------------------------------------------------------------------------------
/aws/resume.py:
--------------------------------------------------------------------------------
1 | # Resume all interrupted trainings in yolov5/ dir including DDP trainings
2 | # Usage: $ python utils/aws/resume.py
3 |
4 | import os
5 | import sys
6 | from pathlib import Path
7 |
8 | import torch
9 | import yaml
10 |
11 | FILE = Path(__file__).resolve()
12 | ROOT = FILE.parents[2] # YOLOv5 root directory
13 | if str(ROOT) not in sys.path:
14 | sys.path.append(str(ROOT)) # add ROOT to PATH
15 |
16 | port = 0 # --master_port
17 | path = Path('').resolve()
18 | for last in path.rglob('*/**/last.pt'):
19 | ckpt = torch.load(last)
20 | if ckpt['optimizer'] is None:
21 | continue
22 |
23 | # Load opt.yaml
24 | with open(last.parent.parent / 'opt.yaml', errors='ignore') as f:
25 | opt = yaml.safe_load(f)
26 |
27 | # Get device count
28 | d = opt['device'].split(',') # devices
29 | nd = len(d) # number of devices
30 | ddp = nd > 1 or (nd == 0 and torch.cuda.device_count() > 1) # distributed data parallel
31 |
32 | if ddp: # multi-GPU
33 | port += 1
34 | cmd = f'python -m torch.distributed.run --nproc_per_node {nd} --master_port {port} train.py --resume {last}'
35 | else: # single-GPU
36 | cmd = f'python train.py --resume {last}'
37 |
38 | cmd += ' > /dev/null 2>&1 &' # redirect output to dev/null and run in daemon thread
39 | print(cmd)
40 | os.system(cmd)
41 |
--------------------------------------------------------------------------------
/aws/userdata.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # AWS EC2 instance startup script https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
3 | # This script will run only once on first instance start (for a re-start script see mime.sh)
4 | # /home/ubuntu (ubuntu) or /home/ec2-user (amazon-linux) is working dir
5 | # Use >300 GB SSD
6 |
7 | cd home/ubuntu
8 | if [ ! -d yolov5 ]; then
9 | echo "Running first-time script." # install dependencies, download COCO, pull Docker
10 | git clone https://github.com/ultralytics/yolov5 -b master && sudo chmod -R 777 yolov5
11 | cd yolov5
12 | bash data/scripts/get_coco.sh && echo "COCO done." &
13 | sudo docker pull ultralytics/yolov5:latest && echo "Docker done." &
14 | python -m pip install --upgrade pip && pip install -r requirements.txt && python detect.py && echo "Requirements done." &
15 | wait && echo "All tasks done." # finish background tasks
16 | else
17 | echo "Running re-start script." # resume interrupted runs
18 | i=0
19 | list=$(sudo docker ps -qa) # container list i.e. $'one\ntwo\nthree\nfour'
20 | while IFS= read -r id; do
21 | ((i++))
22 | echo "restarting container $i: $id"
23 | sudo docker start $id
24 | # sudo docker exec -it $id python train.py --resume # single-GPU
25 | sudo docker exec -d $id python utils/aws/resume.py # multi-scenario
26 | done <<<"$list"
27 | fi
28 |
--------------------------------------------------------------------------------
/callbacks.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Callback utils
4 | """
5 |
6 |
7 | class Callbacks:
8 | """"
9 | Handles all registered callbacks for YOLOv5 Hooks
10 | """
11 |
12 | # Define the available callbacks
13 | _callbacks = {
14 | 'on_pretrain_routine_start': [],
15 | 'on_pretrain_routine_end': [],
16 |
17 | 'on_train_start': [],
18 | 'on_train_epoch_start': [],
19 | 'on_train_batch_start': [],
20 | 'optimizer_step': [],
21 | 'on_before_zero_grad': [],
22 | 'on_train_batch_end': [],
23 | 'on_train_epoch_end': [],
24 |
25 | 'on_val_start': [],
26 | 'on_val_batch_start': [],
27 | 'on_val_image_end': [],
28 | 'on_val_batch_end': [],
29 | 'on_val_end': [],
30 |
31 | 'on_fit_epoch_end': [], # fit = train + val
32 | 'on_model_save': [],
33 | 'on_train_end': [],
34 |
35 | 'teardown': [],
36 | }
37 |
38 | def register_action(self, hook, name='', callback=None):
39 | """
40 | Register a new action to a callback hook
41 |
42 | Args:
43 | hook The callback hook name to register the action to
44 | name The name of the action for later reference
45 | callback The callback to fire
46 | """
47 | assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"
48 | assert callable(callback), f"callback '{callback}' is not callable"
49 | self._callbacks[hook].append({'name': name, 'callback': callback})
50 |
51 | def get_registered_actions(self, hook=None):
52 | """"
53 | Returns all the registered actions by callback hook
54 |
55 | Args:
56 | hook The name of the hook to check, defaults to all
57 | """
58 | if hook:
59 | return self._callbacks[hook]
60 | else:
61 | return self._callbacks
62 |
63 | def run(self, hook, *args, **kwargs):
64 | """
65 | Loop through the registered actions and fire all callbacks
66 |
67 | Args:
68 | hook The name of the hook to check, defaults to all
69 | args Arguments to receive from YOLOv5
70 | kwargs Keyword Arguments to receive from YOLOv5
71 | """
72 |
73 | assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"
74 |
75 | for logger in self._callbacks[hook]:
76 | logger['callback'](*args, **kwargs)
77 |
--------------------------------------------------------------------------------
/data/Argoverse.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Argoverse-HD dataset (ring-front-center camera) http://www.cs.cmu.edu/~mengtial/proj/streaming/
3 | # Example usage: python train.py --data Argoverse.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── Argoverse ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/Argoverse # dataset root dir
12 | train: Argoverse-1.1/images/train/ # train images (relative to 'path') 39384 images
13 | val: Argoverse-1.1/images/val/ # val images (relative to 'path') 15062 images
14 | test: Argoverse-1.1/images/test/ # test images (optional) https://eval.ai/web/challenges/challenge-page/800/overview
15 |
16 | # Classes
17 | nc: 8 # number of classes
18 | names: ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck', 'traffic_light', 'stop_sign'] # class names
19 |
20 |
21 | # Download script/URL (optional) ---------------------------------------------------------------------------------------
22 | download: |
23 | import json
24 |
25 | from tqdm import tqdm
26 | from utils.general import download, Path
27 |
28 |
29 | def argoverse2yolo(set):
30 | labels = {}
31 | a = json.load(open(set, "rb"))
32 | for annot in tqdm(a['annotations'], desc=f"Converting {set} to YOLOv5 format..."):
33 | img_id = annot['image_id']
34 | img_name = a['images'][img_id]['name']
35 | img_label_name = img_name[:-3] + "txt"
36 |
37 | cls = annot['category_id'] # instance class id
38 | x_center, y_center, width, height = annot['bbox']
39 | x_center = (x_center + width / 2) / 1920.0 # offset and scale
40 | y_center = (y_center + height / 2) / 1200.0 # offset and scale
41 | width /= 1920.0 # scale
42 | height /= 1200.0 # scale
43 |
44 | img_dir = set.parents[2] / 'Argoverse-1.1' / 'labels' / a['seq_dirs'][a['images'][annot['image_id']]['sid']]
45 | if not img_dir.exists():
46 | img_dir.mkdir(parents=True, exist_ok=True)
47 |
48 | k = str(img_dir / img_label_name)
49 | if k not in labels:
50 | labels[k] = []
51 | labels[k].append(f"{cls} {x_center} {y_center} {width} {height}\n")
52 |
53 | for k in labels:
54 | with open(k, "w") as f:
55 | f.writelines(labels[k])
56 |
57 |
58 | # Download
59 | dir = Path('../datasets/Argoverse') # dataset root dir
60 | urls = ['https://argoverse-hd.s3.us-east-2.amazonaws.com/Argoverse-HD-Full.zip']
61 | download(urls, dir=dir, delete=False)
62 |
63 | # Convert
64 | annotations_dir = 'Argoverse-HD/annotations/'
65 | (dir / 'Argoverse-1.1' / 'tracking').rename(dir / 'Argoverse-1.1' / 'images') # rename 'tracking' to 'images'
66 | for d in "train.json", "val.json":
67 | argoverse2yolo(dir / annotations_dir / d) # convert VisDrone annotations to YOLO labels
68 |
--------------------------------------------------------------------------------
/data/GlobalWheat2020.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Global Wheat 2020 dataset http://www.global-wheat.com/
3 | # Example usage: python train.py --data GlobalWheat2020.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── GlobalWheat2020 ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/GlobalWheat2020 # dataset root dir
12 | train: # train images (relative to 'path') 3422 images
13 | - images/arvalis_1
14 | - images/arvalis_2
15 | - images/arvalis_3
16 | - images/ethz_1
17 | - images/rres_1
18 | - images/inrae_1
19 | - images/usask_1
20 | val: # val images (relative to 'path') 748 images (WARNING: train set contains ethz_1)
21 | - images/ethz_1
22 | test: # test images (optional) 1276 images
23 | - images/utokyo_1
24 | - images/utokyo_2
25 | - images/nau_1
26 | - images/uq_1
27 |
28 | # Classes
29 | nc: 1 # number of classes
30 | names: ['wheat_head'] # class names
31 |
32 |
33 | # Download script/URL (optional) ---------------------------------------------------------------------------------------
34 | download: |
35 | from utils.general import download, Path
36 |
37 | # Download
38 | dir = Path(yaml['path']) # dataset root dir
39 | urls = ['https://zenodo.org/record/4298502/files/global-wheat-codalab-official.zip',
40 | 'https://github.com/ultralytics/yolov5/releases/download/v1.0/GlobalWheat2020_labels.zip']
41 | download(urls, dir=dir)
42 |
43 | # Make Directories
44 | for p in 'annotations', 'images', 'labels':
45 | (dir / p).mkdir(parents=True, exist_ok=True)
46 |
47 | # Move
48 | for p in 'arvalis_1', 'arvalis_2', 'arvalis_3', 'ethz_1', 'rres_1', 'inrae_1', 'usask_1', \
49 | 'utokyo_1', 'utokyo_2', 'nau_1', 'uq_1':
50 | (dir / p).rename(dir / 'images' / p) # move to /images
51 | f = (dir / p).with_suffix('.json') # json file
52 | if f.exists():
53 | f.rename((dir / 'annotations' / p).with_suffix('.json')) # move to /annotations
54 |
--------------------------------------------------------------------------------
/data/Objects365.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Objects365 dataset https://www.objects365.org/
3 | # Example usage: python train.py --data Objects365.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── Objects365 ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/Objects365 # dataset root dir
12 | train: images/train # train images (relative to 'path') 1742289 images
13 | val: images/val # val images (relative to 'path') 80000 images
14 | test: # test images (optional)
15 |
16 | # Classes
17 | nc: 365 # number of classes
18 | names: ['Person', 'Sneakers', 'Chair', 'Other Shoes', 'Hat', 'Car', 'Lamp', 'Glasses', 'Bottle', 'Desk', 'Cup',
19 | 'Street Lights', 'Cabinet/shelf', 'Handbag/Satchel', 'Bracelet', 'Plate', 'Picture/Frame', 'Helmet', 'Book',
20 | 'Gloves', 'Storage box', 'Boat', 'Leather Shoes', 'Flower', 'Bench', 'Potted Plant', 'Bowl/Basin', 'Flag',
21 | 'Pillow', 'Boots', 'Vase', 'Microphone', 'Necklace', 'Ring', 'SUV', 'Wine Glass', 'Belt', 'Monitor/TV',
22 | 'Backpack', 'Umbrella', 'Traffic Light', 'Speaker', 'Watch', 'Tie', 'Trash bin Can', 'Slippers', 'Bicycle',
23 | 'Stool', 'Barrel/bucket', 'Van', 'Couch', 'Sandals', 'Basket', 'Drum', 'Pen/Pencil', 'Bus', 'Wild Bird',
24 | 'High Heels', 'Motorcycle', 'Guitar', 'Carpet', 'Cell Phone', 'Bread', 'Camera', 'Canned', 'Truck',
25 | 'Traffic cone', 'Cymbal', 'Lifesaver', 'Towel', 'Stuffed Toy', 'Candle', 'Sailboat', 'Laptop', 'Awning',
26 | 'Bed', 'Faucet', 'Tent', 'Horse', 'Mirror', 'Power outlet', 'Sink', 'Apple', 'Air Conditioner', 'Knife',
27 | 'Hockey Stick', 'Paddle', 'Pickup Truck', 'Fork', 'Traffic Sign', 'Balloon', 'Tripod', 'Dog', 'Spoon', 'Clock',
28 | 'Pot', 'Cow', 'Cake', 'Dinning Table', 'Sheep', 'Hanger', 'Blackboard/Whiteboard', 'Napkin', 'Other Fish',
29 | 'Orange/Tangerine', 'Toiletry', 'Keyboard', 'Tomato', 'Lantern', 'Machinery Vehicle', 'Fan',
30 | 'Green Vegetables', 'Banana', 'Baseball Glove', 'Airplane', 'Mouse', 'Train', 'Pumpkin', 'Soccer', 'Skiboard',
31 | 'Luggage', 'Nightstand', 'Tea pot', 'Telephone', 'Trolley', 'Head Phone', 'Sports Car', 'Stop Sign',
32 | 'Dessert', 'Scooter', 'Stroller', 'Crane', 'Remote', 'Refrigerator', 'Oven', 'Lemon', 'Duck', 'Baseball Bat',
33 | 'Surveillance Camera', 'Cat', 'Jug', 'Broccoli', 'Piano', 'Pizza', 'Elephant', 'Skateboard', 'Surfboard',
34 | 'Gun', 'Skating and Skiing shoes', 'Gas stove', 'Donut', 'Bow Tie', 'Carrot', 'Toilet', 'Kite', 'Strawberry',
35 | 'Other Balls', 'Shovel', 'Pepper', 'Computer Box', 'Toilet Paper', 'Cleaning Products', 'Chopsticks',
36 | 'Microwave', 'Pigeon', 'Baseball', 'Cutting/chopping Board', 'Coffee Table', 'Side Table', 'Scissors',
37 | 'Marker', 'Pie', 'Ladder', 'Snowboard', 'Cookies', 'Radiator', 'Fire Hydrant', 'Basketball', 'Zebra', 'Grape',
38 | 'Giraffe', 'Potato', 'Sausage', 'Tricycle', 'Violin', 'Egg', 'Fire Extinguisher', 'Candy', 'Fire Truck',
39 | 'Billiards', 'Converter', 'Bathtub', 'Wheelchair', 'Golf Club', 'Briefcase', 'Cucumber', 'Cigar/Cigarette',
40 | 'Paint Brush', 'Pear', 'Heavy Truck', 'Hamburger', 'Extractor', 'Extension Cord', 'Tong', 'Tennis Racket',
41 | 'Folder', 'American Football', 'earphone', 'Mask', 'Kettle', 'Tennis', 'Ship', 'Swing', 'Coffee Machine',
42 | 'Slide', 'Carriage', 'Onion', 'Green beans', 'Projector', 'Frisbee', 'Washing Machine/Drying Machine',
43 | 'Chicken', 'Printer', 'Watermelon', 'Saxophone', 'Tissue', 'Toothbrush', 'Ice cream', 'Hot-air balloon',
44 | 'Cello', 'French Fries', 'Scale', 'Trophy', 'Cabbage', 'Hot dog', 'Blender', 'Peach', 'Rice', 'Wallet/Purse',
45 | 'Volleyball', 'Deer', 'Goose', 'Tape', 'Tablet', 'Cosmetics', 'Trumpet', 'Pineapple', 'Golf Ball',
46 | 'Ambulance', 'Parking meter', 'Mango', 'Key', 'Hurdle', 'Fishing Rod', 'Medal', 'Flute', 'Brush', 'Penguin',
47 | 'Megaphone', 'Corn', 'Lettuce', 'Garlic', 'Swan', 'Helicopter', 'Green Onion', 'Sandwich', 'Nuts',
48 | 'Speed Limit Sign', 'Induction Cooker', 'Broom', 'Trombone', 'Plum', 'Rickshaw', 'Goldfish', 'Kiwi fruit',
49 | 'Router/modem', 'Poker Card', 'Toaster', 'Shrimp', 'Sushi', 'Cheese', 'Notepaper', 'Cherry', 'Pliers', 'CD',
50 | 'Pasta', 'Hammer', 'Cue', 'Avocado', 'Hamimelon', 'Flask', 'Mushroom', 'Screwdriver', 'Soap', 'Recorder',
51 | 'Bear', 'Eggplant', 'Board Eraser', 'Coconut', 'Tape Measure/Ruler', 'Pig', 'Showerhead', 'Globe', 'Chips',
52 | 'Steak', 'Crosswalk Sign', 'Stapler', 'Camel', 'Formula 1', 'Pomegranate', 'Dishwasher', 'Crab',
53 | 'Hoverboard', 'Meat ball', 'Rice Cooker', 'Tuba', 'Calculator', 'Papaya', 'Antelope', 'Parrot', 'Seal',
54 | 'Butterfly', 'Dumbbell', 'Donkey', 'Lion', 'Urinal', 'Dolphin', 'Electric Drill', 'Hair Dryer', 'Egg tart',
55 | 'Jellyfish', 'Treadmill', 'Lighter', 'Grapefruit', 'Game board', 'Mop', 'Radish', 'Baozi', 'Target', 'French',
56 | 'Spring Rolls', 'Monkey', 'Rabbit', 'Pencil Case', 'Yak', 'Red Cabbage', 'Binoculars', 'Asparagus', 'Barbell',
57 | 'Scallop', 'Noddles', 'Comb', 'Dumpling', 'Oyster', 'Table Tennis paddle', 'Cosmetics Brush/Eyeliner Pencil',
58 | 'Chainsaw', 'Eraser', 'Lobster', 'Durian', 'Okra', 'Lipstick', 'Cosmetics Mirror', 'Curling', 'Table Tennis']
59 |
60 |
61 | # Download script/URL (optional) ---------------------------------------------------------------------------------------
62 | download: |
63 | from pycocotools.coco import COCO
64 | from tqdm import tqdm
65 |
66 | from utils.general import Path, download, np, xyxy2xywhn
67 |
68 | # Make Directories
69 | dir = Path(yaml['path']) # dataset root dir
70 | for p in 'images', 'labels':
71 | (dir / p).mkdir(parents=True, exist_ok=True)
72 | for q in 'train', 'val':
73 | (dir / p / q).mkdir(parents=True, exist_ok=True)
74 |
75 | # Train, Val Splits
76 | for split, patches in [('train', 50 + 1), ('val', 43 + 1)]:
77 | print(f"Processing {split} in {patches} patches ...")
78 | images, labels = dir / 'images' / split, dir / 'labels' / split
79 |
80 | # Download
81 | url = f"https://dorc.ks3-cn-beijing.ksyun.com/data-set/2020Objects365%E6%95%B0%E6%8D%AE%E9%9B%86/{split}/"
82 | if split == 'train':
83 | download([f'{url}zhiyuan_objv2_{split}.tar.gz'], dir=dir, delete=False) # annotations json
84 | download([f'{url}patch{i}.tar.gz' for i in range(patches)], dir=images, curl=True, delete=False, threads=8)
85 | elif split == 'val':
86 | download([f'{url}zhiyuan_objv2_{split}.json'], dir=dir, delete=False) # annotations json
87 | download([f'{url}images/v1/patch{i}.tar.gz' for i in range(15 + 1)], dir=images, curl=True, delete=False, threads=8)
88 | download([f'{url}images/v2/patch{i}.tar.gz' for i in range(16, patches)], dir=images, curl=True, delete=False, threads=8)
89 |
90 | # Move
91 | for f in tqdm(images.rglob('*.jpg'), desc=f'Moving {split} images'):
92 | f.rename(images / f.name) # move to /images/{split}
93 |
94 | # Labels
95 | coco = COCO(dir / f'zhiyuan_objv2_{split}.json')
96 | names = [x["name"] for x in coco.loadCats(coco.getCatIds())]
97 | for cid, cat in enumerate(names):
98 | catIds = coco.getCatIds(catNms=[cat])
99 | imgIds = coco.getImgIds(catIds=catIds)
100 | for im in tqdm(coco.loadImgs(imgIds), desc=f'Class {cid + 1}/{len(names)} {cat}'):
101 | width, height = im["width"], im["height"]
102 | path = Path(im["file_name"]) # image filename
103 | try:
104 | with open(labels / path.with_suffix('.txt').name, 'a') as file:
105 | annIds = coco.getAnnIds(imgIds=im["id"], catIds=catIds, iscrowd=None)
106 | for a in coco.loadAnns(annIds):
107 | x, y, w, h = a['bbox'] # bounding box in xywh (xy top-left corner)
108 | xyxy = np.array([x, y, x + w, y + h])[None] # pixels(1,4)
109 | x, y, w, h = xyxy2xywhn(xyxy, w=width, h=height, clip=True)[0] # normalized and clipped
110 | file.write(f"{cid} {x:.5f} {y:.5f} {w:.5f} {h:.5f}\n")
111 | except Exception as e:
112 | print(e)
113 |
--------------------------------------------------------------------------------
/data/SKU-110K.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # SKU-110K retail items dataset https://github.com/eg4000/SKU110K_CVPR19
3 | # Example usage: python train.py --data SKU-110K.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── SKU-110K ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/SKU-110K # dataset root dir
12 | train: train.txt # train images (relative to 'path') 8219 images
13 | val: val.txt # val images (relative to 'path') 588 images
14 | test: test.txt # test images (optional) 2936 images
15 |
16 | # Classes
17 | nc: 1 # number of classes
18 | names: ['object'] # class names
19 |
20 |
21 | # Download script/URL (optional) ---------------------------------------------------------------------------------------
22 | download: |
23 | import shutil
24 | from tqdm import tqdm
25 | from utils.general import np, pd, Path, download, xyxy2xywh
26 |
27 | # Download
28 | dir = Path(yaml['path']) # dataset root dir
29 | parent = Path(dir.parent) # download dir
30 | urls = ['http://trax-geometry.s3.amazonaws.com/cvpr_challenge/SKU110K_fixed.tar.gz']
31 | download(urls, dir=parent, delete=False)
32 |
33 | # Rename directories
34 | if dir.exists():
35 | shutil.rmtree(dir)
36 | (parent / 'SKU110K_fixed').rename(dir) # rename dir
37 | (dir / 'labels').mkdir(parents=True, exist_ok=True) # create labels dir
38 |
39 | # Convert labels
40 | names = 'image', 'x1', 'y1', 'x2', 'y2', 'class', 'image_width', 'image_height' # column names
41 | for d in 'annotations_train.csv', 'annotations_val.csv', 'annotations_test.csv':
42 | x = pd.read_csv(dir / 'annotations' / d, names=names).values # annotations
43 | images, unique_images = x[:, 0], np.unique(x[:, 0])
44 | with open((dir / d).with_suffix('.txt').__str__().replace('annotations_', ''), 'w') as f:
45 | f.writelines(f'./images/{s}\n' for s in unique_images)
46 | for im in tqdm(unique_images, desc=f'Converting {dir / d}'):
47 | cls = 0 # single-class dataset
48 | with open((dir / 'labels' / im).with_suffix('.txt'), 'a') as f:
49 | for r in x[images == im]:
50 | w, h = r[6], r[7] # image width, height
51 | xywh = xyxy2xywh(np.array([[r[1] / w, r[2] / h, r[3] / w, r[4] / h]]))[0] # instance
52 | f.write(f"{cls} {xywh[0]:.5f} {xywh[1]:.5f} {xywh[2]:.5f} {xywh[3]:.5f}\n") # write label
53 |
--------------------------------------------------------------------------------
/data/VOC.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC
3 | # Example usage: python train.py --data VOC.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── VOC ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/VOC
12 | train: # train images (relative to 'path') 16551 images
13 | - images/train2012
14 | - images/train2007
15 | - images/val2012
16 | - images/val2007
17 | val: # val images (relative to 'path') 4952 images
18 | - images/test2007
19 | test: # test images (optional)
20 | - images/test2007
21 |
22 | # Classes
23 | nc: 20 # number of classes
24 | names: ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
25 | 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'] # class names
26 |
27 |
28 | # Download script/URL (optional) ---------------------------------------------------------------------------------------
29 | download: |
30 | import xml.etree.ElementTree as ET
31 |
32 | from tqdm import tqdm
33 | from utils.general import download, Path
34 |
35 |
36 | def convert_label(path, lb_path, year, image_id):
37 | def convert_box(size, box):
38 | dw, dh = 1. / size[0], 1. / size[1]
39 | x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2]
40 | return x * dw, y * dh, w * dw, h * dh
41 |
42 | in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml')
43 | out_file = open(lb_path, 'w')
44 | tree = ET.parse(in_file)
45 | root = tree.getroot()
46 | size = root.find('size')
47 | w = int(size.find('width').text)
48 | h = int(size.find('height').text)
49 |
50 | for obj in root.iter('object'):
51 | cls = obj.find('name').text
52 | if cls in yaml['names'] and not int(obj.find('difficult').text) == 1:
53 | xmlbox = obj.find('bndbox')
54 | bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
55 | cls_id = yaml['names'].index(cls) # class id
56 | out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')
57 |
58 |
59 | # Download
60 | dir = Path(yaml['path']) # dataset root dir
61 | url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/'
62 | urls = [url + 'VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
63 | url + 'VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
64 | url + 'VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
65 | download(urls, dir=dir / 'images', delete=False)
66 |
67 | # Convert
68 | path = dir / f'images/VOCdevkit'
69 | for year, image_set in ('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test'):
70 | imgs_path = dir / 'images' / f'{image_set}{year}'
71 | lbs_path = dir / 'labels' / f'{image_set}{year}'
72 | imgs_path.mkdir(exist_ok=True, parents=True)
73 | lbs_path.mkdir(exist_ok=True, parents=True)
74 |
75 | image_ids = open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt').read().strip().split()
76 | for id in tqdm(image_ids, desc=f'{image_set}{year}'):
77 | f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
78 | lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
79 | f.rename(imgs_path / f.name) # move image
80 | convert_label(path, lb_path, year, id) # convert labels to YOLO format
81 |
--------------------------------------------------------------------------------
/data/VisDrone.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # VisDrone2019-DET dataset https://github.com/VisDrone/VisDrone-Dataset
3 | # Example usage: python train.py --data VisDrone.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── VisDrone ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/VisDrone # dataset root dir
12 | train: VisDrone2019-DET-train/images # train images (relative to 'path') 6471 images
13 | val: VisDrone2019-DET-val/images # val images (relative to 'path') 548 images
14 | test: VisDrone2019-DET-test-dev/images # test images (optional) 1610 images
15 |
16 | # Classes
17 | nc: 10 # number of classes
18 | names: ['pedestrian', 'people', 'bicycle', 'car', 'van', 'truck', 'tricycle', 'awning-tricycle', 'bus', 'motor']
19 |
20 |
21 | # Download script/URL (optional) ---------------------------------------------------------------------------------------
22 | download: |
23 | from utils.general import download, os, Path
24 |
25 | def visdrone2yolo(dir):
26 | from PIL import Image
27 | from tqdm import tqdm
28 |
29 | def convert_box(size, box):
30 | # Convert VisDrone box to YOLO xywh box
31 | dw = 1. / size[0]
32 | dh = 1. / size[1]
33 | return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh
34 |
35 | (dir / 'labels').mkdir(parents=True, exist_ok=True) # make labels directory
36 | pbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}')
37 | for f in pbar:
38 | img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).size
39 | lines = []
40 | with open(f, 'r') as file: # read annotation.txt
41 | for row in [x.split(',') for x in file.read().strip().splitlines()]:
42 | if row[4] == '0': # VisDrone 'ignored regions' class 0
43 | continue
44 | cls = int(row[5]) - 1
45 | box = convert_box(img_size, tuple(map(int, row[:4])))
46 | lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n")
47 | with open(str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'labels' + os.sep), 'w') as fl:
48 | fl.writelines(lines) # write label.txt
49 |
50 |
51 | # Download
52 | dir = Path(yaml['path']) # dataset root dir
53 | urls = ['https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-train.zip',
54 | 'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-val.zip',
55 | 'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-dev.zip',
56 | 'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-challenge.zip']
57 | download(urls, dir=dir)
58 |
59 | # Convert
60 | for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':
61 | visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels
62 |
--------------------------------------------------------------------------------
/data/coco.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # COCO 2017 dataset http://cocodataset.org
3 | # Example usage: python train.py --data coco.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── coco ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/coco # dataset root dir
12 | train: train2017.txt # train images (relative to 'path') 118287 images
13 | val: val2017.txt # train images (relative to 'path') 5000 images
14 | test: test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
15 |
16 | # Classes
17 | nc: 80 # number of classes
18 | names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
19 | 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
20 | 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
21 | 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
22 | 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
23 | 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
24 | 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
25 | 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
26 | 'hair drier', 'toothbrush'] # class names
27 |
28 |
29 | # Download script/URL (optional)
30 | download: |
31 | from utils.general import download, Path
32 |
33 | # Download labels
34 | segments = False # segment or box labels
35 | dir = Path(yaml['path']) # dataset root dir
36 | url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/'
37 | urls = [url + ('coco2017labels-segments.zip' if segments else 'coco2017labels.zip')] # labels
38 | download(urls, dir=dir.parent)
39 |
40 | # Download data
41 | urls = ['http://images.cocodataset.org/zips/train2017.zip', # 19G, 118k images
42 | 'http://images.cocodataset.org/zips/val2017.zip', # 1G, 5k images
43 | 'http://images.cocodataset.org/zips/test2017.zip'] # 7G, 41k images (optional)
44 | download(urls, dir=dir / 'images', threads=3)
45 |
--------------------------------------------------------------------------------
/data/coco128.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017)
3 | # Example usage: python train.py --data coco128.yaml
4 | # parent
5 | # ├── yolov5
6 | # └── datasets
7 | # └── coco128 ← downloads here
8 |
9 |
10 | # 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, ..]
11 | path: ../datasets/coco128 # dataset root dir
12 | train: images/train2017 # train images (relative to 'path') 128 images
13 | val: images/train2017 # val images (relative to 'path') 128 images
14 | test: # test images (optional)
15 |
16 | # Classes
17 | nc: 80 # number of classes
18 | names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
19 | 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
20 | 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
21 | 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
22 | 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
23 | 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
24 | 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
25 | 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
26 | 'hair drier', 'toothbrush'] # class names
27 |
28 |
29 | # Download script/URL (optional)
30 | download: https://ultralytics.com/assets/coco128.zip
31 |
--------------------------------------------------------------------------------
/data/data.yaml:
--------------------------------------------------------------------------------
1 | # Custom data for safety helmet
2 |
3 |
4 | # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
5 | train: /home/data/yolo_format/images/train
6 | val: /home/data/yolo_format/images/val
7 |
8 | # number of classes
9 | nc: 2
10 |
11 | # class names
12 | names: ['phone', 'person']
13 |
--------------------------------------------------------------------------------
/data/fire_data.yaml:
--------------------------------------------------------------------------------
1 | # Custom data for safety helmet
2 |
3 |
4 | # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
5 | train: F:/sssssssssd/det/yolo/fire/fire_yolo_format/images/train
6 | val: F:/sssssssssd/det/yolo/fire/fire_yolo_format/images/val
7 |
8 | # number of classes
9 | nc: 2
10 |
11 | # class names
12 | names: ['fire', 'nofire']
13 |
--------------------------------------------------------------------------------
/data/hyps/hyp.finetune.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Hyperparameters for VOC finetuning
3 | # python train.py --batch 64 --weights yolov5m.pt --data VOC.yaml --img 512 --epochs 50
4 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
5 |
6 | # Hyperparameter Evolution Results
7 | # Generations: 306
8 | # P R mAP.5 mAP.5:.95 box obj cls
9 | # Metrics: 0.6 0.936 0.896 0.684 0.0115 0.00805 0.00146
10 |
11 | lr0: 0.0032
12 | lrf: 0.12
13 | momentum: 0.843
14 | weight_decay: 0.00036
15 | warmup_epochs: 2.0
16 | warmup_momentum: 0.5
17 | warmup_bias_lr: 0.05
18 | box: 0.0296
19 | cls: 0.243
20 | cls_pw: 0.631
21 | obj: 0.301
22 | obj_pw: 0.911
23 | iou_t: 0.2
24 | anchor_t: 2.91
25 | # anchors: 3.63
26 | fl_gamma: 0.0
27 | hsv_h: 0.0138
28 | hsv_s: 0.664
29 | hsv_v: 0.464
30 | degrees: 0.373
31 | translate: 0.245
32 | scale: 0.898
33 | shear: 0.602
34 | perspective: 0.0
35 | flipud: 0.00856
36 | fliplr: 0.5
37 | mosaic: 1.0
38 | mixup: 0.243
39 | copy_paste: 0.0
40 |
--------------------------------------------------------------------------------
/data/hyps/hyp.finetune_objects365.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | lr0: 0.00258
4 | lrf: 0.17
5 | momentum: 0.779
6 | weight_decay: 0.00058
7 | warmup_epochs: 1.33
8 | warmup_momentum: 0.86
9 | warmup_bias_lr: 0.0711
10 | box: 0.0539
11 | cls: 0.299
12 | cls_pw: 0.825
13 | obj: 0.632
14 | obj_pw: 1.0
15 | iou_t: 0.2
16 | anchor_t: 3.44
17 | anchors: 3.2
18 | fl_gamma: 0.0
19 | hsv_h: 0.0188
20 | hsv_s: 0.704
21 | hsv_v: 0.36
22 | degrees: 0.0
23 | translate: 0.0902
24 | scale: 0.491
25 | shear: 0.0
26 | perspective: 0.0
27 | flipud: 0.0
28 | fliplr: 0.5
29 | mosaic: 1.0
30 | mixup: 0.0
31 | copy_paste: 0.0
32 |
--------------------------------------------------------------------------------
/data/hyps/hyp.scratch-high.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Hyperparameters for high-augmentation COCO training from scratch
3 | # python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300
4 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
5 |
6 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
7 | lrf: 0.2 # final OneCycleLR learning rate (lr0 * lrf)
8 | momentum: 0.937 # SGD momentum/Adam beta1
9 | weight_decay: 0.0005 # optimizer weight decay 5e-4
10 | warmup_epochs: 3.0 # warmup epochs (fractions ok)
11 | warmup_momentum: 0.8 # warmup initial momentum
12 | warmup_bias_lr: 0.1 # warmup initial bias lr
13 | box: 0.05 # box loss gain
14 | cls: 0.3 # cls loss gain
15 | cls_pw: 1.0 # cls BCELoss positive_weight
16 | obj: 0.7 # obj loss gain (scale with pixels)
17 | obj_pw: 1.0 # obj BCELoss positive_weight
18 | iou_t: 0.20 # IoU training threshold
19 | anchor_t: 4.0 # anchor-multiple threshold
20 | # anchors: 3 # anchors per output layer (0 to ignore)
21 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
22 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
23 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
24 | hsv_v: 0.4 # image HSV-Value augmentation (fraction)
25 | degrees: 0.0 # image rotation (+/- deg)
26 | translate: 0.1 # image translation (+/- fraction)
27 | scale: 0.9 # image scale (+/- gain)
28 | shear: 0.0 # image shear (+/- deg)
29 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
30 | flipud: 0.0 # image flip up-down (probability)
31 | fliplr: 0.5 # image flip left-right (probability)
32 | mosaic: 1.0 # image mosaic (probability)
33 | mixup: 0.1 # image mixup (probability)
34 | copy_paste: 0.1 # segment copy-paste (probability)
35 |
--------------------------------------------------------------------------------
/data/hyps/hyp.scratch-low.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Hyperparameters for low-augmentation COCO training from scratch
3 | # python train.py --batch 64 --cfg yolov5n6.yaml --weights '' --data coco.yaml --img 640 --epochs 300 --linear
4 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
5 |
6 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
7 | lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf)
8 | momentum: 0.937 # SGD momentum/Adam beta1
9 | weight_decay: 0.0005 # optimizer weight decay 5e-4
10 | warmup_epochs: 3.0 # warmup epochs (fractions ok)
11 | warmup_momentum: 0.8 # warmup initial momentum
12 | warmup_bias_lr: 0.1 # warmup initial bias lr
13 | box: 0.05 # box loss gain
14 | cls: 0.5 # cls loss gain
15 | cls_pw: 1.0 # cls BCELoss positive_weight
16 | obj: 1.0 # obj loss gain (scale with pixels)
17 | obj_pw: 1.0 # obj BCELoss positive_weight
18 | iou_t: 0.20 # IoU training threshold
19 | anchor_t: 4.0 # anchor-multiple threshold
20 | # anchors: 3 # anchors per output layer (0 to ignore)
21 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
22 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
23 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
24 | hsv_v: 0.4 # image HSV-Value augmentation (fraction)
25 | degrees: 0.0 # image rotation (+/- deg)
26 | translate: 0.1 # image translation (+/- fraction)
27 | scale: 0.5 # image scale (+/- gain)
28 | shear: 0.0 # image shear (+/- deg)
29 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
30 | flipud: 0.0 # image flip up-down (probability)
31 | fliplr: 0.5 # image flip left-right (probability)
32 | mosaic: 1.0 # image mosaic (probability)
33 | mixup: 0.0 # image mixup (probability)
34 | copy_paste: 0.0 # segment copy-paste (probability)
35 |
--------------------------------------------------------------------------------
/data/hyps/hyp.scratch-med.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Hyperparameters for medium-augmentation COCO training from scratch
3 | # python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300
4 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
5 |
6 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
7 | lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf)
8 | momentum: 0.937 # SGD momentum/Adam beta1
9 | weight_decay: 0.0005 # optimizer weight decay 5e-4
10 | warmup_epochs: 3.0 # warmup epochs (fractions ok)
11 | warmup_momentum: 0.8 # warmup initial momentum
12 | warmup_bias_lr: 0.1 # warmup initial bias lr
13 | box: 0.05 # box loss gain
14 | cls: 0.3 # cls loss gain
15 | cls_pw: 1.0 # cls BCELoss positive_weight
16 | obj: 0.7 # obj loss gain (scale with pixels)
17 | obj_pw: 1.0 # obj BCELoss positive_weight
18 | iou_t: 0.20 # IoU training threshold
19 | anchor_t: 4.0 # anchor-multiple threshold
20 | # anchors: 3 # anchors per output layer (0 to ignore)
21 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
22 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
23 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
24 | hsv_v: 0.4 # image HSV-Value augmentation (fraction)
25 | degrees: 0.0 # image rotation (+/- deg)
26 | translate: 0.1 # image translation (+/- fraction)
27 | scale: 0.9 # image scale (+/- gain)
28 | shear: 0.0 # image shear (+/- deg)
29 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
30 | flipud: 0.0 # image flip up-down (probability)
31 | fliplr: 0.5 # image flip left-right (probability)
32 | mosaic: 1.0 # image mosaic (probability)
33 | mixup: 0.1 # image mixup (probability)
34 | copy_paste: 0.0 # segment copy-paste (probability)
35 |
--------------------------------------------------------------------------------
/data/hyps/hyp.scratch.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Hyperparameters for COCO training from scratch
3 | # python train.py --batch 40 --cfg yolov5m.yaml --weights '' --data coco.yaml --img 640 --epochs 300
4 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
5 |
6 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
7 | lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf)
8 | momentum: 0.937 # SGD momentum/Adam beta1
9 | weight_decay: 0.0005 # optimizer weight decay 5e-4
10 | warmup_epochs: 3.0 # warmup epochs (fractions ok)
11 | warmup_momentum: 0.8 # warmup initial momentum
12 | warmup_bias_lr: 0.1 # warmup initial bias lr
13 | box: 0.05 # box loss gain
14 | cls: 0.5 # cls loss gain
15 | cls_pw: 1.0 # cls BCELoss positive_weight
16 | obj: 1.0 # obj loss gain (scale with pixels)
17 | obj_pw: 1.0 # obj BCELoss positive_weight
18 | iou_t: 0.20 # IoU training threshold
19 | anchor_t: 4.0 # anchor-multiple threshold
20 | # anchors: 3 # anchors per output layer (0 to ignore)
21 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
22 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
23 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
24 | hsv_v: 0.4 # image HSV-Value augmentation (fraction)
25 | degrees: 0.0 # image rotation (+/- deg)
26 | translate: 0.1 # image translation (+/- fraction)
27 | scale: 0.5 # image scale (+/- gain)
28 | shear: 0.0 # image shear (+/- deg)
29 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
30 | flipud: 0.0 # image flip up-down (probability)
31 | fliplr: 0.5 # image flip left-right (probability)
32 | mosaic: 1.0 # image mosaic (probability)
33 | mixup: 0.0 # image mixup (probability)
34 | copy_paste: 0.0 # segment copy-paste (probability)
35 |
--------------------------------------------------------------------------------
/data/images/bus.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/bus.jpg
--------------------------------------------------------------------------------
/data/images/fishman.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/fishman.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_1310.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_1310.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_2096.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_2096.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_2402.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_2402.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_2423.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_2423.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_419.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_419.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_633.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_633.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_689.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_689.jpg
--------------------------------------------------------------------------------
/data/images/phone/phone_89.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/phone/phone_89.jpg
--------------------------------------------------------------------------------
/data/images/zidane.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/data/images/zidane.jpg
--------------------------------------------------------------------------------
/data/mask_data.yaml:
--------------------------------------------------------------------------------
1 | # Custom data for safety helmet
2 |
3 |
4 | # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
5 | train: F:/up/1212/YOLO_Mask/score/images/train
6 | val: F:/up/1212/YOLO_Mask/score/images/val
7 |
8 | # number of classes
9 | nc: 2
10 |
11 | # class names
12 | names: ['mask', 'face']
13 |
--------------------------------------------------------------------------------
/data/scripts/download_weights.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
3 | # Download latest models from https://github.com/ultralytics/yolov5/releases
4 | # Example usage: bash path/to/download_weights.sh
5 | # parent
6 | # └── yolov5
7 | # ├── yolov5s.pt ← downloads here
8 | # ├── yolov5m.pt
9 | # └── ...
10 |
11 | python - <= cls >= 0, f'incorrect class index {cls}'
74 |
75 | # Write YOLO label
76 | if id not in shapes:
77 | shapes[id] = Image.open(file).size
78 | box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], clip=True)
79 | with open((labels / id).with_suffix('.txt'), 'a') as f:
80 | f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt
81 | except Exception as e:
82 | print(f'WARNING: skipping one label for {file}: {e}')
83 |
84 |
85 | # Download manually from https://challenge.xviewdataset.org
86 | dir = Path(yaml['path']) # dataset root dir
87 | # urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels
88 | # 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images
89 | # 'https://d307kc0mrhucc3.cloudfront.net/val_images.zip'] # 5G, 282 val images (no labels)
90 | # download(urls, dir=dir, delete=False)
91 |
92 | # Convert labels
93 | convert_labels(dir / 'xView_train.geojson')
94 |
95 | # Move images
96 | images = Path(dir / 'images')
97 | images.mkdir(parents=True, exist_ok=True)
98 | Path(dir / 'train_images').rename(dir / 'images' / 'train')
99 | Path(dir / 'val_images').rename(dir / 'images' / 'val')
100 |
101 | # Split
102 | autosplit(dir / 'images' / 'train')
103 |
--------------------------------------------------------------------------------
/downloads.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Download utils
4 | """
5 |
6 | import os
7 | import platform
8 | import subprocess
9 | import time
10 | import urllib
11 | from pathlib import Path
12 | from zipfile import ZipFile
13 |
14 | import requests
15 | import torch
16 |
17 |
18 | def gsutil_getsize(url=''):
19 | # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
20 | s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')
21 | return eval(s.split(' ')[0]) if len(s) else 0 # bytes
22 |
23 |
24 | def safe_download(file, url, url2=None, min_bytes=1E0, error_msg=''):
25 | # Attempts to download file from url or url2, checks and removes incomplete downloads < min_bytes
26 | file = Path(file)
27 | assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}"
28 | try: # url1
29 | print(f'Downloading {url} to {file}...')
30 | torch.hub.download_url_to_file(url, str(file))
31 | assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check
32 | except Exception as e: # url2
33 | file.unlink(missing_ok=True) # remove partial downloads
34 | print(f'ERROR: {e}\nRe-attempting {url2 or url} to {file}...')
35 | os.system(f"curl -L '{url2 or url}' -o '{file}' --retry 3 -C -") # curl download, retry and resume on fail
36 | finally:
37 | if not file.exists() or file.stat().st_size < min_bytes: # check
38 | file.unlink(missing_ok=True) # remove partial downloads
39 | print(f"ERROR: {assert_msg}\n{error_msg}")
40 | print('')
41 |
42 |
43 | def attempt_download(file, repo='ultralytics/yolov5'): # from utils.downloads import *; attempt_download()
44 | # Attempt file download if does not exist
45 | file = Path(str(file).strip().replace("'", ''))
46 |
47 | if not file.exists():
48 | # URL specified
49 | name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc.
50 | if str(file).startswith(('http:/', 'https:/')): # download
51 | url = str(file).replace(':/', '://') # Pathlib turns :// -> :/
52 | name = name.split('?')[0] # parse authentication https://url.com/file.txt?auth...
53 | safe_download(file=name, url=url, min_bytes=1E5)
54 | return name
55 |
56 | # GitHub assets
57 | file.parent.mkdir(parents=True, exist_ok=True) # make parent dir (if required)
58 | try:
59 | response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
60 | assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
61 | tag = response['tag_name'] # i.e. 'v1.0'
62 | except: # fallback plan
63 | assets = ['yolov5n.pt', 'yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt',
64 | 'yolov5n6.pt', 'yolov5s6.pt', 'yolov5m6.pt', 'yolov5l6.pt', 'yolov5x6.pt']
65 | try:
66 | tag = subprocess.check_output('git tag', shell=True, stderr=subprocess.STDOUT).decode().split()[-1]
67 | except:
68 | tag = 'v6.0' # current release
69 |
70 | if name in assets:
71 | safe_download(file,
72 | url=f'https://github.com/{repo}/releases/download/{tag}/{name}',
73 | # url2=f'https://storage.googleapis.com/{repo}/ckpt/{name}', # backup url (optional)
74 | min_bytes=1E5,
75 | error_msg=f'{file} missing, try downloading from https://github.com/{repo}/releases/')
76 |
77 | return str(file)
78 |
79 |
80 | def gdrive_download(id='16TiPfZj7htmTyhntwcZyEEAejOUxuT6m', file='tmp.zip'):
81 | # Downloads a file from Google Drive. from yolov5.utils.downloads import *; gdrive_download()
82 | t = time.time()
83 | file = Path(file)
84 | cookie = Path('cookie') # gdrive cookie
85 | print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')
86 | file.unlink(missing_ok=True) # remove existing file
87 | cookie.unlink(missing_ok=True) # remove existing cookie
88 |
89 | # Attempt file download
90 | out = "NUL" if platform.system() == "Windows" else "/dev/null"
91 | os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')
92 | if os.path.exists('cookie'): # large file
93 | s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'
94 | else: # small file
95 | s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'
96 | r = os.system(s) # execute, capture return
97 | cookie.unlink(missing_ok=True) # remove existing cookie
98 |
99 | # Error check
100 | if r != 0:
101 | file.unlink(missing_ok=True) # remove partial
102 | print('Download error ') # raise Exception('Download error')
103 | return r
104 |
105 | # Unzip if archive
106 | if file.suffix == '.zip':
107 | print('unzipping... ', end='')
108 | ZipFile(file).extractall(path=file.parent) # unzip
109 | file.unlink() # remove zip
110 |
111 | print(f'Done ({time.time() - t:.1f}s)')
112 | return r
113 |
114 |
115 | def get_token(cookie="./cookie"):
116 | with open(cookie) as f:
117 | for line in f:
118 | if "download" in line:
119 | return line.split()[-1]
120 | return ""
121 |
122 | # Google utils: https://cloud.google.com/storage/docs/reference/libraries ----------------------------------------------
123 | #
124 | #
125 | # def upload_blob(bucket_name, source_file_name, destination_blob_name):
126 | # # Uploads a file to a bucket
127 | # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
128 | #
129 | # storage_client = storage.Client()
130 | # bucket = storage_client.get_bucket(bucket_name)
131 | # blob = bucket.blob(destination_blob_name)
132 | #
133 | # blob.upload_from_filename(source_file_name)
134 | #
135 | # print('File {} uploaded to {}.'.format(
136 | # source_file_name,
137 | # destination_blob_name))
138 | #
139 | #
140 | # def download_blob(bucket_name, source_blob_name, destination_file_name):
141 | # # Uploads a blob from a bucket
142 | # storage_client = storage.Client()
143 | # bucket = storage_client.get_bucket(bucket_name)
144 | # blob = bucket.blob(source_blob_name)
145 | #
146 | # blob.download_to_filename(destination_file_name)
147 | #
148 | # print('Blob {} downloaded to {}.'.format(
149 | # source_blob_name,
150 | # destination_file_name))
151 |
--------------------------------------------------------------------------------
/flask_rest_api/README.md:
--------------------------------------------------------------------------------
1 | # Flask REST API
2 |
3 | [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) [API](https://en.wikipedia.org/wiki/API)s are
4 | commonly used to expose Machine Learning (ML) models to other services. This folder contains an example REST API
5 | created using Flask to expose the YOLOv5s model from [PyTorch Hub](https://pytorch.org/hub/ultralytics_yolov5/).
6 |
7 | ## Requirements
8 |
9 | [Flask](https://palletsprojects.com/p/flask/) is required. Install with:
10 |
11 | ```shell
12 | $ pip install Flask
13 | ```
14 |
15 | ## Run
16 |
17 | After Flask installation run:
18 |
19 | ```shell
20 | $ python3 restapi.py --port 5000
21 | ```
22 |
23 | Then use [curl](https://curl.se/) to perform a request:
24 |
25 | ```shell
26 | $ curl -X POST -F image=@zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5s'
27 | ```
28 |
29 | The model inference results are returned as a JSON response:
30 |
31 | ```json
32 | [
33 | {
34 | "class": 0,
35 | "confidence": 0.8900438547,
36 | "height": 0.9318675399,
37 | "name": "person",
38 | "width": 0.3264600933,
39 | "xcenter": 0.7438579798,
40 | "ycenter": 0.5207948685
41 | },
42 | {
43 | "class": 0,
44 | "confidence": 0.8440024257,
45 | "height": 0.7155083418,
46 | "name": "person",
47 | "width": 0.6546785235,
48 | "xcenter": 0.427829951,
49 | "ycenter": 0.6334488392
50 | },
51 | {
52 | "class": 27,
53 | "confidence": 0.3771208823,
54 | "height": 0.3902671337,
55 | "name": "tie",
56 | "width": 0.0696444362,
57 | "xcenter": 0.3675483763,
58 | "ycenter": 0.7991207838
59 | },
60 | {
61 | "class": 27,
62 | "confidence": 0.3527112305,
63 | "height": 0.1540903747,
64 | "name": "tie",
65 | "width": 0.0336618312,
66 | "xcenter": 0.7814827561,
67 | "ycenter": 0.5065554976
68 | }
69 | ]
70 | ```
71 |
72 | An example python script to perform inference using [requests](https://docs.python-requests.org/en/master/) is given
73 | in `example_request.py`
74 |
--------------------------------------------------------------------------------
/flask_rest_api/example_request.py:
--------------------------------------------------------------------------------
1 | """Perform test request"""
2 | import pprint
3 |
4 | import requests
5 |
6 | DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s"
7 | TEST_IMAGE = "zidane.jpg"
8 |
9 | image_data = open(TEST_IMAGE, "rb").read()
10 |
11 | response = requests.post(DETECTION_URL, files={"image": image_data}).json()
12 |
13 | pprint.pprint(response)
14 |
--------------------------------------------------------------------------------
/flask_rest_api/restapi.py:
--------------------------------------------------------------------------------
1 | """
2 | Run a rest API exposing the yolov5s object detection model
3 | """
4 | import argparse
5 | import io
6 |
7 | import torch
8 | from flask import Flask, request
9 | from PIL import Image
10 |
11 | app = Flask(__name__)
12 |
13 | DETECTION_URL = "/v1/object-detection/yolov5s"
14 |
15 |
16 | @app.route(DETECTION_URL, methods=["POST"])
17 | def predict():
18 | if not request.method == "POST":
19 | return
20 |
21 | if request.files.get("image"):
22 | image_file = request.files["image"]
23 | image_bytes = image_file.read()
24 |
25 | img = Image.open(io.BytesIO(image_bytes))
26 |
27 | results = model(img, size=640) # reduce size=320 for faster inference
28 | return results.pandas().xyxy[0].to_json(orient="records")
29 |
30 |
31 | if __name__ == "__main__":
32 | parser = argparse.ArgumentParser(description="Flask API exposing YOLOv5 model")
33 | parser.add_argument("--port", default=5000, type=int, help="port number")
34 | args = parser.parse_args()
35 |
36 | model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True) # force_reload to recache
37 | app.run(host="0.0.0.0", port=args.port) # debug=True causes Restarting with stat
38 |
--------------------------------------------------------------------------------
/images/1_20.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/1_20.jpg
--------------------------------------------------------------------------------
/images/1_67.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/1_67.jpg
--------------------------------------------------------------------------------
/images/1c992e2b-108a-4e3c-859d-ae84d6f8ce7f.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/1c992e2b-108a-4e3c-859d-ae84d6f8ce7f.jpg
--------------------------------------------------------------------------------
/images/UI/logo.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/UI/logo.jpeg
--------------------------------------------------------------------------------
/images/UI/lufei.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/UI/lufei.png
--------------------------------------------------------------------------------
/images/UI/qq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/UI/qq.png
--------------------------------------------------------------------------------
/images/UI/right.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/UI/right.jpeg
--------------------------------------------------------------------------------
/images/UI/up.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/UI/up.jpeg
--------------------------------------------------------------------------------
/images/UI/xf.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/UI/xf.jpg
--------------------------------------------------------------------------------
/images/right.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/right.jpeg
--------------------------------------------------------------------------------
/images/tmp/single_result.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/tmp/single_result.jpg
--------------------------------------------------------------------------------
/images/tmp/single_result_vid.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/tmp/single_result_vid.jpg
--------------------------------------------------------------------------------
/images/tmp/tmp_upload.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/tmp/tmp_upload.jpeg
--------------------------------------------------------------------------------
/images/tmp/tmp_upload.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/tmp/tmp_upload.jpg
--------------------------------------------------------------------------------
/images/tmp/tmp_upload.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/tmp/tmp_upload.png
--------------------------------------------------------------------------------
/images/tmp/upload_show_result.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/tmp/upload_show_result.jpg
--------------------------------------------------------------------------------
/images/up.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/images/up.jpeg
--------------------------------------------------------------------------------
/loggers/__init__.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Logging utils
4 | """
5 |
6 | import os
7 | import warnings
8 | from threading import Thread
9 |
10 | import pkg_resources as pkg
11 | import torch
12 | from torch.utils.tensorboard import SummaryWriter
13 |
14 | from utils.general import colorstr, emojis
15 | from utils.loggers.wandb.wandb_utils import WandbLogger
16 | from utils.plots import plot_images, plot_results
17 | from utils.torch_utils import de_parallel
18 |
19 | LOGGERS = ('csv', 'tb', 'wandb') # text-file, TensorBoard, Weights & Biases
20 | RANK = int(os.getenv('RANK', -1))
21 |
22 | try:
23 | import wandb
24 |
25 | assert hasattr(wandb, '__version__') # verify package import not local dir
26 | if pkg.parse_version(wandb.__version__) >= pkg.parse_version('0.12.2') and RANK in [0, -1]:
27 | wandb_login_success = wandb.login(timeout=30)
28 | if not wandb_login_success:
29 | wandb = None
30 | except (ImportError, AssertionError):
31 | wandb = None
32 |
33 |
34 | class Loggers():
35 | # YOLOv5 Loggers class
36 | def __init__(self, save_dir=None, weights=None, opt=None, hyp=None, logger=None, include=LOGGERS):
37 | self.save_dir = save_dir
38 | self.weights = weights
39 | self.opt = opt
40 | self.hyp = hyp
41 | self.logger = logger # for printing results to console
42 | self.include = include
43 | self.keys = ['train/box_loss', 'train/obj_loss', 'train/cls_loss', # train loss
44 | 'metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', # metrics
45 | 'val/box_loss', 'val/obj_loss', 'val/cls_loss', # val loss
46 | 'x/lr0', 'x/lr1', 'x/lr2'] # params
47 | for k in LOGGERS:
48 | setattr(self, k, None) # init empty logger dictionary
49 | self.csv = True # always log to csv
50 |
51 | # Message
52 | if not wandb:
53 | prefix = colorstr('Weights & Biases: ')
54 | s = f"{prefix}run 'pip install wandb' to automatically track and visualize YOLOv5 🚀 runs (RECOMMENDED)"
55 | print(emojis(s))
56 |
57 | # TensorBoard
58 | s = self.save_dir
59 | if 'tb' in self.include and not self.opt.evolve:
60 | prefix = colorstr('TensorBoard: ')
61 | self.logger.info(f"{prefix}Start with 'tensorboard --logdir {s.parent}', view at http://localhost:6006/")
62 | self.tb = SummaryWriter(str(s))
63 |
64 | # W&B
65 | if wandb and 'wandb' in self.include:
66 | wandb_artifact_resume = isinstance(self.opt.resume, str) and self.opt.resume.startswith('wandb-artifact://')
67 | run_id = torch.load(self.weights).get('wandb_id') if self.opt.resume and not wandb_artifact_resume else None
68 | self.opt.hyp = self.hyp # add hyperparameters
69 | self.wandb = WandbLogger(self.opt, run_id)
70 | else:
71 | self.wandb = None
72 |
73 | def on_pretrain_routine_end(self):
74 | # Callback runs on pre-train routine end
75 | paths = self.save_dir.glob('*labels*.jpg') # training labels
76 | if self.wandb:
77 | self.wandb.log({"Labels": [wandb.Image(str(x), caption=x.name) for x in paths]})
78 |
79 | def on_train_batch_end(self, ni, model, imgs, targets, paths, plots, sync_bn):
80 | # Callback runs on train batch end
81 | if plots:
82 | if ni == 0:
83 | if not sync_bn: # tb.add_graph() --sync known issue https://github.com/ultralytics/yolov5/issues/3754
84 | with warnings.catch_warnings():
85 | warnings.simplefilter('ignore') # suppress jit trace warning
86 | self.tb.add_graph(torch.jit.trace(de_parallel(model), imgs[0:1], strict=False), [])
87 | if ni < 3:
88 | f = self.save_dir / f'train_batch{ni}.jpg' # filename
89 | Thread(target=plot_images, args=(imgs, targets, paths, f), daemon=True).start()
90 | if self.wandb and ni == 10:
91 | files = sorted(self.save_dir.glob('train*.jpg'))
92 | self.wandb.log({'Mosaics': [wandb.Image(str(f), caption=f.name) for f in files if f.exists()]})
93 |
94 | def on_train_epoch_end(self, epoch):
95 | # Callback runs on train epoch end
96 | if self.wandb:
97 | self.wandb.current_epoch = epoch + 1
98 |
99 | def on_val_image_end(self, pred, predn, path, names, im):
100 | # Callback runs on val image end
101 | if self.wandb:
102 | self.wandb.val_one_image(pred, predn, path, names, im)
103 |
104 | def on_val_end(self):
105 | # Callback runs on val end
106 | if self.wandb:
107 | files = sorted(self.save_dir.glob('val*.jpg'))
108 | self.wandb.log({"Validation": [wandb.Image(str(f), caption=f.name) for f in files]})
109 |
110 | def on_fit_epoch_end(self, vals, epoch, best_fitness, fi):
111 | # Callback runs at the end of each fit (train+val) epoch
112 | x = {k: v for k, v in zip(self.keys, vals)} # dict
113 | if self.csv:
114 | file = self.save_dir / 'results.csv'
115 | n = len(x) + 1 # number of cols
116 | s = '' if file.exists() else (('%20s,' * n % tuple(['epoch'] + self.keys)).rstrip(',') + '\n') # add header
117 | with open(file, 'a') as f:
118 | f.write(s + ('%20.5g,' * n % tuple([epoch] + vals)).rstrip(',') + '\n')
119 |
120 | if self.tb:
121 | for k, v in x.items():
122 | self.tb.add_scalar(k, v, epoch)
123 |
124 | if self.wandb:
125 | self.wandb.log(x)
126 | self.wandb.end_epoch(best_result=best_fitness == fi)
127 |
128 | def on_model_save(self, last, epoch, final_epoch, best_fitness, fi):
129 | # Callback runs on model save event
130 | if self.wandb:
131 | if ((epoch + 1) % self.opt.save_period == 0 and not final_epoch) and self.opt.save_period != -1:
132 | self.wandb.log_model(last.parent, self.opt, epoch, fi, best_model=best_fitness == fi)
133 |
134 | def on_train_end(self, last, best, plots, epoch, results):
135 | # Callback runs on training end
136 | if plots:
137 | plot_results(file=self.save_dir / 'results.csv') # save results.png
138 | files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))]
139 | files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] # filter
140 |
141 | if self.tb:
142 | import cv2
143 | for f in files:
144 | self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC')
145 |
146 | if self.wandb:
147 | self.wandb.log({"Results": [wandb.Image(str(f), caption=f.name) for f in files]})
148 | # Calling wandb.log. TODO: Refactor this into WandbLogger.log_model
149 | if not self.opt.evolve:
150 | wandb.log_artifact(str(best if best.exists() else last), type='model',
151 | name='run_' + self.wandb.wandb_run.id + '_model',
152 | aliases=['latest', 'best', 'stripped'])
153 | self.wandb.finish_run()
154 | else:
155 | self.wandb.finish_run()
156 | self.wandb = WandbLogger(self.opt)
157 |
--------------------------------------------------------------------------------
/loggers/__pycache__/__init__.cpython-38.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/usernameisalreadytaKeN1122/yolov5-fire/e35c38ca98ff49e7025daa445d176a15bd9ee401/loggers/__pycache__/__init__.cpython-38.pyc
--------------------------------------------------------------------------------
/loggers/wandb/README.md:
--------------------------------------------------------------------------------
1 | 📚 This guide explains how to use **Weights & Biases** (W&B) with YOLOv5 🚀. UPDATED 29 September 2021.
2 | * [About Weights & Biases](#about-weights-&-biases)
3 | * [First-Time Setup](#first-time-setup)
4 | * [Viewing runs](#viewing-runs)
5 | * [Advanced Usage: Dataset Versioning and Evaluation](#advanced-usage)
6 | * [Reports: Share your work with the world!](#reports)
7 |
8 | ## About Weights & Biases
9 | Think of [W&B](https://wandb.ai/site?utm_campaign=repo_yolo_wandbtutorial) like GitHub for machine learning models. With a few lines of code, save everything you need to debug, compare and reproduce your models — architecture, hyperparameters, git commits, model weights, GPU usage, and even datasets and predictions.
10 |
11 | Used by top researchers including teams at OpenAI, Lyft, Github, and MILA, W&B is part of the new standard of best practices for machine learning. How W&B can help you optimize your machine learning workflows:
12 |
13 | * [Debug](https://wandb.ai/wandb/getting-started/reports/Visualize-Debug-Machine-Learning-Models--VmlldzoyNzY5MDk#Free-2) model performance in real time
14 | * [GPU usage](https://wandb.ai/wandb/getting-started/reports/Visualize-Debug-Machine-Learning-Models--VmlldzoyNzY5MDk#System-4) visualized automatically
15 | * [Custom charts](https://wandb.ai/wandb/customizable-charts/reports/Powerful-Custom-Charts-To-Debug-Model-Peformance--VmlldzoyNzY4ODI) for powerful, extensible visualization
16 | * [Share insights](https://wandb.ai/wandb/getting-started/reports/Visualize-Debug-Machine-Learning-Models--VmlldzoyNzY5MDk#Share-8) interactively with collaborators
17 | * [Optimize hyperparameters](https://docs.wandb.com/sweeps) efficiently
18 | * [Track](https://docs.wandb.com/artifacts) datasets, pipelines, and production models
19 |
20 | ## First-Time Setup
21 |
22 | Toggle Details
23 | When you first train, W&B will prompt you to create a new account and will generate an **API key** for you. If you are an existing user you can retrieve your key from https://wandb.ai/authorize. This key is used to tell W&B where to log your data. You only need to supply your key once, and then it is remembered on the same device.
24 |
25 | W&B will create a cloud **project** (default is 'YOLOv5') for your training runs, and each new training run will be provided a unique run **name** within that project as project/name. You can also manually set your project and run name as:
26 |
27 | ```shell
28 | $ python train.py --project ... --name ...
29 | ```
30 |
31 | YOLOv5 notebook example:
32 |
33 |
34 |
35 |
36 |
37 | ## Viewing Runs
38 |
39 | Toggle Details
40 | Run information streams from your environment to the W&B cloud console as you train. This allows you to monitor and even cancel runs in realtime . All important information is logged:
41 |
42 | * Training & Validation losses
43 | * Metrics: Precision, Recall, mAP@0.5, mAP@0.5:0.95
44 | * Learning Rate over time
45 | * A bounding box debugging panel, showing the training progress over time
46 | * GPU: Type, **GPU Utilization**, power, temperature, **CUDA memory usage**
47 | * System: Disk I/0, CPU utilization, RAM memory usage
48 | * Your trained model as W&B Artifact
49 | * Environment: OS and Python types, Git repository and state, **training command**
50 |
51 |
52 |
53 |
54 |
55 |
56 | ## Advanced Usage
57 | You can leverage W&B artifacts and Tables integration to easily visualize and manage your datasets, models and training evaluations. Here are some quick examples to get you started.
58 |
59 |
1. Visualize and Version Datasets
60 | Log, visualize, dynamically query, and understand your data with W&B Tables. You can use the following command to log your dataset as a W&B Table. This will generate a {dataset}_wandb.yaml file which can be used to train from dataset artifact.
61 |
62 | Usage
63 | Code $ python utils/logger/wandb/log_dataset.py --project ... --name ... --data ..
64 |
65 | 
66 |
67 |
68 |
2: Train and Log Evaluation simultaneousy
69 | This is an extension of the previous section, but it'll also training after uploading the dataset. This also evaluation Table
70 | Evaluation table compares your predictions and ground truths across the validation set for each epoch. It uses the references to the already uploaded datasets,
71 | so no images will be uploaded from your system more than once.
72 |
73 | Usage
74 | Code $ python utils/logger/wandb/log_dataset.py --data .. --upload_data
75 |
76 | 
77 |
78 |
79 |
3: Train using dataset artifact
80 | When you upload a dataset as described in the first section, you get a new config file with an added `_wandb` to its name. This file contains the information that
81 | can be used to train a model directly from the dataset artifact. This also logs evaluation
82 |
83 | Usage
84 | Code $ python utils/logger/wandb/log_dataset.py --data {data}_wandb.yaml
85 |
86 | 
87 |
88 |
89 |
4: Save model checkpoints as artifacts
90 | To enable saving and versioning checkpoints of your experiment, pass `--save_period n` with the base cammand, where `n` represents checkpoint interval.
91 | You can also log both the dataset and model checkpoints simultaneously. If not passed, only the final model will be logged
92 |
93 |
94 | Usage
95 | Code $ python train.py --save_period 1
96 |
97 | 
98 |
99 |
100 |
101 |
102 |
5: Resume runs from checkpoint artifacts.
103 | Any run can be resumed using artifacts if the --resume argument starts with wandb-artifact:// prefix followed by the run path, i.e, wandb-artifact://username/project/runid . This doesn't require the model checkpoint to be present on the local system.
104 |
105 |
106 | Usage
107 | Code $ python train.py --resume wandb-artifact://{run_path}
108 |
109 | 
110 |
111 |
112 |
6: Resume runs from dataset artifact & checkpoint artifacts.
113 | Local dataset or model checkpoints are not required. This can be used to resume runs directly on a different device
114 | The syntax is same as the previous section, but you'll need to lof both the dataset and model checkpoints as artifacts, i.e, set bot --upload_dataset or
115 | train from _wandb.yaml file and set --save_period
116 |
117 |
118 | Usage
119 | Code $ python train.py --resume wandb-artifact://{run_path}
120 |
121 | 
122 |
123 |
124 |
125 |
126 |
127 |