基於YOLOv5預訓練模型 訓練自定義數據集 實現檢測並分類圖像中的Logo功能

2021-02-23 德鼎

自定義包含18個logo(對應18個開票方)的發票數據集

實現自動檢測並分類圖像中的Logo功能,準確率99%

訓練數據:共 863 張圖片文件:

訓練數據:共 863 張標籤文件,和以上圖片文件一一對應,內容為LOGO的分類名稱 以及 在圖片中的坐標區域值。

1. Environment preparation 

Python>=3.8 and PyTorch>=1.7.

Clone repo, https://github.com/ultralytics/yolov5

download tutorial dataset, https://www.kaggle.com/ultralytics/coco128

and install requirements.txt dependencies

$ git clone https://github.com/ultralytics/yolov5 # clone repo
$ cd yolov5
$ pip install -r requirements.txt # install dependencies

2. dataset.yaml explain

COCO128 is a small tutorial dataset composed of the first 128 images in COCO train2017. These same 128 images are used for both training and validation to verify our training pipeline is capable of overfitting. data/coco128.yaml, shown below, is the dataset configuration file that defines 1) an optional download command/URL for auto-downloading, 2) a path to a directory of training images (or path to a *.txt file with a list of training images), 3) the same for our validation images, 4) the number of classes, 5) a list of class names:

# download command/URL (optional)

download: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128.zip

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ../coco128/images/train2017/
val: ../coco128/images/train2017/

# number of classes
nc: 80

# class names
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']

3. Create Labels

After using a tool like CVAT, makesense.ai or Labelbox to label your images, export your labels to YOLO format, with one *.txt file per image (if no objects in image, no *.txt file is required). The *.txt file specifications are:

One row per object

Each row is class x_center y_center width height format.

Box coordinates must be in normalized xywh format (from 0 - 1). If your boxes are in pixels, divide x_center and width by image width, and y_center and height by image height.

Class numbers are zero-indexed (start from 0).

For example: online label tool: https://www.makesense.ai/

The label file corresponding to the above image contains 2 persons (class 0) and a tie (class 27):

4.Organize Directories

Organize your train and val images and labels according to the example below. In this example we assume /coco128 is next to the /yolov5 directory. YOLOv5 locates labels automatically for each image by replacing the last instance of /images/ in the images directory with /labels/. For example:

coco/images/train2017/000000109622.jpg # image
coco/labels/train2017/000000109622.txt # label

5. Select a Model

Select a pretrained model to start training from. Here we select YOLOv5s, the smallest and fastest model available. See our README table for a full comparison of all models.

6. Train

Train a YOLOv5s model on COCO128 by specifying dataset, batch-size, image size and either pretrained --weights yolov5s.pt (recommended), or randomly initialized --weights '' --cfg yolov5s.yaml (not recommended). Pretrained weights are auto-downloaded from the latest YOLOv5 release.

# Train YOLOv5s on COCO128 for 5 epochs
$ python train.py --img 640 --batch 16 --epochs 5 --data coco128.yaml --weights yolov5s.pt

All training results are saved to runs/train/ with incrementing run directories, i.e. runs/train/exp2, runs/train/exp3 etc. For more details see the Training section of our Google Colab Notebook. 

python train.py --img 640 --batch 4 --epochs 100 --data ./data/coco128.yaml --weights ./weights/yolov5s.pt

Model Summary: 283 layers, 7109391 parameters, 7109391 gradients, 16.5 GFLOPS

train: Scanning 'coco\coco188\labels\train2017.cache' for images and labels... 863 found, 0 missing, 0 empty, 0 corrupted: 100%|

val: Scanning 'coco\coco188\labels\train2017.cache' for images and labels... 863 found, 0 missing, 0 empty, 0 corrupted: 100%|

7. Validate result

python detect.py --source ./data/images --weights ./runs/train/exp23/weights/best.pt --conf 0.725

Visualize

Weights & Biases Logging (🚀 NEW)

Weights & Biases (W&B) is now integrated with YOLOv5 for real-time visualization and cloud logging of training runs. This allows for better run comparison and introspection, as well improved visibility and collaboration among team members. To enable W&B logging install wandb, and then train normally (you will be guided setup on first use).

$ pip install wandb

During training you will see live updates at https://www.wandb.com/, and you can create Detailed Reports of your results using the W&B Reports tool. 

Local Logging

All results are logged by default to runs/train, with a new experiment directory created for each new training as runs/train/exp2, runs/train/exp3, etc. View train and test jpgs to see mosaics, labels, predictions and augmentation effects. Note a Mosaic Dataloader is used for training (shown below), a new concept developed by Ultralytics and first featured in YOLOv4.

train_batch0.jpg shows train batch 0 mosaics and labels:

test_batch0_labels.jpg shows test batch 0 labels:

test_batch0_pred.jpg shows test batch 0 predictions:

Training losses and performance metrics are also logged to Tensorboard and a custom results.txt logfile which is plotted as results.png (below) after training completes. Here we show YOLOv5s trained on COCO128 to 300 epochs, starting from scratch (blue), and from pretrained --weights yolov5s.pt (orange).

from utils.utils import plot_results
plot_results(save_dir='runs/train/exp') # plot results.txt as results.png

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Google Colab and Kaggle notebooks with free GPU:

Google Cloud Deep Learning VM. See GCP Quickstart Guide

Amazon Deep Learning AMI. See AWS Quickstart Guide

Docker Image. See Docker Quickstart Guide 

參考:

https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

相關焦點

  • 使用 YOLOv5 訓練自動駕駛目標檢測網絡
    由於我們的目的是基於bdd100k數據集來訓練檢測少量特定交通物體的模型,我們不需要訓練檢測80類網絡的模型,所有我們得重新創建一個uc_data.yaml文件來描述bdd100k數據集的數據特性。由於我們模型的輸出不是coco數據集的80個類,而是13類,因此我們得修改此處的輸出類別數量為13。
  • yolov5實戰之皮卡丘檢測
    記得之前在剛接觸的時候看到博客中寫到,深度學習分三個層次,第一個層次是分類,第二個層次是檢測,第三個層次是分割。人臉識別算是分類問題,也就是一直在第一個層次···一直都想有機會了解下第二個層次,奈何精力有限,工作中也沒有實際的項目需要。最近正好有個不急的檢測項目,趁此機會入門檢測吧。工作中聽同事提到yolov5效果不錯,而且文檔指導也比較豐富,因此選擇從此入手,順便也熟悉下pytorch。
  • 如何使用yolov5圖像訓練識別
    install -r requirements.txt 第二步:(自定義數據集合 -- 圖像注釋)這裡使用HyperLabel來進行標記圖像,該工具在線版本,也有一個月的免費試用,下面我將對礦泉水進行label標記來作為指導https://sense.sixgill.com/label-and-train/data-labeler
  • Scaled-YOLOv4 快速開始,訓練自定義數據集
    usp=sharing現有模型測試準備 COCO 數據集下載 COCO 數據集 http://cocodataset.org/,coco2017├── annotations│   ├── instances_train2017.json│   └── instances_val2017.json├── test2017├──
  • 【模型訓練】ubuntu 編譯 Darknet 與 YOLO 訓練
    關於 VOC 格式數據怎麼製作的,可以參考我的這篇:《【經驗分享】目標檢測 VOC 格式數據集製作》,裡面介紹的比較詳細了。2.2 Yolo 訓練   在有了數據集後,然後搞來模型結構文件和預訓練權重就可以開啟愉快的煉丹之旅。
  • 乾貨 | YOLOV5 訓練自動駕駛數據集,並轉Tensorrt,收藏!
    環境配置配置文件修改訓練推理轉TensorrtBDD100K是最大的開放式駕駛視頻數據集之一,其中包含10萬個視頻和10個任務,目的是方便評估自動駕駛圖像識別算法的的進展。每個高解析度視頻一共40秒。該數據集包括超過1000個小時的駕駛數據,總共超過1億幀。這些視頻帶有GPU / IMU數據以獲取軌跡信息。該數據集具有地理,環境和天氣多樣性,從而能讓模型能夠識別多種場景,具備更多的泛化能力。這些豐富的戶外場景和複雜的車輛運動使感知任務更具挑戰性。該
  • PyTorch 版 YOLOv4 更新了,可自定義數據集,還集成了 MobileNet
    網上有很多基於 TF/Keras 和 Caffe 等的復現版本,但不少項目只給了代碼,並沒有給出模型在 COCO、PASCAL VOC 數據集上的訓練結果。近日,有研究者在 GitHub 上開源了一個項目:基於 PyTorch 深度學習框架的 YOLOv4 復現版本,該版本基於 YOLOv4 作者給出的實現 AlexeyAB/darknet,並在 PASCAL VOC、COCO 和自定義數據集上運行。
  • 深度學習圖像處理技術篇(一):YOLOv4 VS YOLOv4-tiny
    與基於分類器的系統相比,YOLO模型具有多個優勢。它可以在一個幀中識別多個對象。它在測試時查看整個圖像,因此其預測由圖像中的全局上下文提供。它還像R-CNN這樣的系統需要單個網絡評估來進行預測,而R-CNN的單個圖像需要成千上萬個。這使其速度非常快,比R-CNN快1000倍以上,比Fast R-CNN快100倍。
  • 小白學PyTorch | 5 torchvision預訓練模型與數據集全覽
    翻譯過來就是:torchvision包由流行的數據集、模型體系結構和通用的計算機視覺圖像轉換組成。,到root的位置;train:True的話,數據集下載的是訓練數據集;False的話則下載測試數據集(真方便,都不用自己劃分了)transform:這個是對圖像進行處理的transform,比方說旋轉平移縮放,輸入的是PIL格式的圖像(不是tensor矩陣);target_transform:這個是對圖像標籤進行處理的函數(這個我沒用過不太確定,也許是做標籤平滑那種的處理?)
  • 預訓練模型遷移學習
    深度學習擅長解決的一個問題是圖像分類。圖像分類的目標是根據一組合理的類別對指定的圖片進行分類。從深度學習的角度來看,圖像分類問題可以通過遷移學習的方法來解決。本文介紹了如何通過遷移學習來解決圖像分類的問題。本文中所提出的實現方式是基於Python語言的Keras。
  • yolov5訓練過程可視化
    我們介紹過yolov5已經在本地集成了Weights & Biases,也就是wandb,這樣就可以方便的追蹤模型訓練的整個過程,包括模型的性能、超參數、GPU的使用情況、模型預測,還有數據集。說到底,wandb就是個可視化的工具。
  • YOLO系列:YOLOv1,YOLOv2,YOLOv3,YOLOv4,YOLOv5簡介
    其他細節,例如使用激活函數使用leak RELU,模型用ImageNet預訓練等等優點通用性強。YOLO對於藝術類作品中的物體檢測同樣適用。它對非自然圖像物體的檢測率遠遠高於DPM和RCNN系列檢測方法。缺點由於輸出層為全連接層,因此在檢測時,YOLO訓練模型只支持與訓練圖像相同的輸入解析度。
  • 使用google colab訓練YOLOv5模型
    YOLOv5模型訓練來到google drive,點擊左上方的Newcolab_yolov5創建一個新的文件夾colabcolab_yolov5接下來,將準備好的口罩數據集上傳到colab文件夾中,這個數據集,前面我們在YOLOv5模型訓練的時候用過,可以到下面的地址下載原始連結 https://public.roboflow.ai/object-detection
  • 【小白學PyTorch】5.torchvision預訓練模型與數據集全覽
    翻譯過來就是:torchvision包由流行的數據集、模型體系結構和通用的計算機視覺圖像轉換組成。簡單地說就是常用數據集+常見模型+常見圖像增強方法這個torchvision中主要有包組成: 1 torchvision.datssets 包含賊多的數據集,包含下面的:官方說明了:All the datasets have almost
  • YOLOV3目標檢測算法的模型訓練教程
    資源的下載:基於darknet框架在Linux系統下訓練YOLOV3數據集,下載darknet文件,連結: https://download.csdn.net/download/qq_41900772/11340101 。YOLOV3算法模型訓練主要用到文件夾中的:cfg、data,以及後文中所建立的數據集。
  • 使用Python+OpenCV+yolov5實現行人目標檢測
    隨著這種廣泛的實際應用,目標檢測自然成為一個活躍的研究領域。我們在Fynd的研究團隊一直在訓練一個行人檢測模型來支持我們的目標跟蹤模型。在本文中,我們將介紹如何選擇一個模型架構,創建一個數據集,並為我們的特定用例進行行人檢測模型的訓練。什麼是目標檢測目標檢測是一種計算機視覺技術,它允許我們識別和定位圖像或視頻中的物體。目標檢測可以理解為兩部分,目標定位和目標分類。
  • 如何在無人機上部署YOLOv4物體檢測器
    最終的平均損失從0.05(對於小模型和簡單訓練數據)到3.0(對於大模型和複雜訓練數據)不等。當訓練停止之後,可以從darknet\backup中取出最新保存的訓練權重.weights,並選擇它們中檢測效果最好的例如,當訓練9000次停止後,效果最好的模型可能是之前保存權重中的一個(7000,8000,9000),這是因為過擬合(Overfiting)現象。
  • albert-chinese-ner使用預訓練語言模型ALBERT做中文NER
    AI項目體驗地址 https://loveai.techalbert_zh海量中文語料上預訓練ALBERT模型:參數更少,效果更好。預訓練小模型也能拿下13項NLP任務,ALBERT三大改造登頂GLUE基準一鍵運行10個數據集、9個基線模型、不同任務上模型效果的詳細對比模型性能與對比(英文) Performance
  • 乾貨 | YOLOv5在建築工地中安全帽佩戴檢測的應用
    五、增加數據集的分類該項目是使用 YOLOv5 v2.x 來訓練在智能工地安全領域中頭盔目標檢測的應用,先來一波演示!/models 下選擇一個你需要的模型然後複製一份出來,將文件開頭的 nc = 修改為數據集的分類數,下面是借鑑 .
  • YOLOv4 的各種新實現、配置、測試、訓練資源匯總
    近日最火的莫過於 YOLOv4 的橫空出世,CV君在第一時間進行了 YOLOv4的論文解讀:YOLOv4來了!