HoloLens2 and YOLOv5 (1)——自制数据集在google colab上进行yolov5模型训练

本文详细介绍了如何利用RoboFlow收集和标注医疗影像数据,创建高质量数据集,并在Google Colab上训练YOLOv5模型进行目标检测。内容包括数据集准备、模型训练以及训练结果的应用。作者分享了从数据准备到模型部署的全过程,并提供了训练代码和技巧。
摘要由CSDN通过智能技术生成


(该专栏内容持续更新中 ⋯ ⋯ \cdots\cdots )

1. 序言

  首先这个项目是FZI研究所IDiL项目的一部分,主要是探索人工智能和机器学习在医疗领域的应用。我的内容是在Hololens2设备上设计一个应用,来实现对工作中的药片、药盒进行分类及计数,检查是否正确,同时辅助医护人员的日常工作。由于目前整体结构还没有想的特别清楚,所以会一边做一边更新,目前不是最终版本。等项目完成会给出Github地址。

项目是英文做的,有些地方就不翻译了,都是简单的词汇,相信大家理解起来没有问题的(其实就是太懒了@_@)。

2. 准备自己的数据集(不需要的直接跳过)

  下面的内容和链接可以说是亲妈教程了,即便是第一次接触的小白也完全没问题,大佬或是已经有现成数据集的请直接跳过哦.

(1) Collect pictures.

For all the tasks try to get the most variable and diverse training dataset. Here are some tips:

  • get images from different angles
  • change lightning conditions
  • take images with good quality and in focus
  • change object size and distance.

The following two links provide some methods about how to make a good dataset. Please read them carefully.
Tips for Best Training Results
how to prepare images for a training dataset

(2) Use roboflow to label the images.

When labeling images, there are a few tips to bear in mind. Note that these tips are generally true, but can alter based on the context of your problem.

  • Label around the entirety of an object. It is best to include a little bit of non-object buffer than it is to exclude a portion of the object with a rectangular label. So, aim to have boxes that tightly mirror the objects you want to label, but do not cut off part of the objects. Your model will understand edges far better this way.
  • For occluded objects, label them entirely. If an object is out of view due to another object being in front of it, label the object out of view as if you could see its entirety. Your model will begin to understand the true bounds of objects this way.
  • For objects partially out of frame, generally label them. This tip especially depends on your problem, but in general, even a partial object is still an object to be labeled.
  • If outsourcing a labeling job, provide crystal clear instructions. If you’re ready to scale up your labeling operations and bring on outside help, be incredibly explicit in your instructions (like mentioning the importance of labeling around an object rather than cutting a portion of the object out of bounds per our first tip!)
    https://blog.roboflow.com/tips-for-how-to-label-images/

这个在线的Label工具非常推荐,相信很多人对它已经非常熟悉了。不仅有很多制作好的数据集以供大家平时的训练和学习,还有非常多的Tutorials可以学习。创建一个自己的项目,上传图片并标记,在上面对数据集进行增强,版本控制,具体功能自己去看吧,总之很实用。

(3) Dataset health check and augmention

Health check: Assessing and improving the quality of your dataset.
After health check, you can get the basic information of the data set, such as, annotations, average Image size, class balance, etc.

Image Transformations: Apply preprocessing and augmentations to images.
Preprocessing should be applied to your training, validation, and testing set to assure learning and inference occurs on the same image properties, such as, auto-orient, resize, grayscale, and Auto-Adjust Contrast.
Image augmentation can increase the generalizability of your model’s performance by increasing the diversity of learning examples for your model, such as, flip, rotation, shear, exposure, noise, etc.

The following two links are the tutorials provided by roboflow. Read them if you want.
dataset health check
image-transformations

(4) Download the dataset in YOLO fromat.

选择你自己的数据集的一个版本,选择想要的格式,然后可以选择直接下载到本地,也提供URL和API,总之很方便,然后甚至可以直接在roboflow在线训练自己的数据集。

到这一步自己的数据集就做好啦,对于小样本大概几百张图片一个人花一周时间就能完成。按照教程来数据集的质量一定没问题的。我自己用两个手机,在一天的不同时段、不同背景、不同光源、不同距离、不同角度下拍照,最终一次训练就得到了很好的结果,所以,你也可以的!因为yolo输入图片会被压缩成较低的分辨率,所以倒也不必去追求特别好的拍照设备。

3. 在colab上训练YOLOv5

  在自己电脑上训练可以参考博主@BIT可达鸭的这篇文章【小白CV】手把手教你用YOLOv5训练自己的数据集(从Windows环境配置到模型部署),感谢博主。

  我之所以选择使用Google colab,也是想学习一下colab的使用,当然还有自己电脑gpu太垃圾啦,然后因为colab上已经搭好环境了,yolo还有在colab上训练的教程,我就选择这样做了。但是因为后续还有一系列的开发,个人建议如果自己电脑条件允许,还是在自己电脑上做吧。

  关于在colab上训练yolo有很多教程可以参考,我接下来会提供给大家两个方法:

A. 在colab上搞定一切。就是把数据集和模型都下载到colab,然后把训练结果存到drive以备后续使用。

B. 在Google drive上搞定一切。就是把数据集和模型都放到到Google drive,然后训练,这样断开后数据也不会丢失了(colab 24小时会自动断开连接)。

推荐使用方法A,因为Google drive上面免费容量是15G,如果你数据集很大或者之前就已经占用大部分了可能容量不够,而colab的可用内存有69G,colab pro还有更多内存。最重要的,从Google drive上读取数据会严重拖慢训练速度。两个方法对于小数据集来说到是无所谓,反正就是慢也用不了多长时间。但是大数据集且epoch较大,一定要用方法A。

我用的方法A,自制的数据集一共690个图片,训练100epoch,很快就训练完了,然后程序会把训练结果上传到wandb进行可视化。

(1) 准备工作

打开并登录colabdrive,新建一个笔记本。
设置笔记本使用GPU, 点击 修改->笔记本设置->硬件加速器->GPU
在这里插入图片描述

装载Goole云端硬盘,点击左侧图标
在这里插入图片描述
或是输入以下代码

from google.colab import drive
drive.mount('/content/drive')

点击右上角”重新连接“,之后就可以使用gpu了。

!nvidia-smi    # 查看GPU信息

在这里插入图片描述
解决Colab长时间无操作就掉线的问题
在Colab界面点击 自定义及控制->更多工具->开发者工具->console,
或使用快捷键Ctrl+Shift+I 打开控制台,复制以下代码然后回车。

function ClickConnect(){
    console.log("Working");
    document.querySelector("colab-toolbar-button#connect").click()
}
setInterval(ClickConnect, 60000)
(4) 训练方式A(推荐)

首先就是把yolov5和数据集(这里是dataset.v1)下载到本地

# clone YOLOv5 from github and install all therequirements
%cd /content/
!git clone https://github.com/ultralytics/yolov5
%cd yolov5
!pip install -U -r requirements.txt
!pip install wandb # To enable W&B logging install wandb, and then train normally (you will be guided setup on first use).

# download your dataset
%cd /content/
!curl -L "https://app.roboflow.com/ds/***************" > roboflow.zip    # use your own download code here
!unzip roboflow.zip -d dataset.v1/
!rm roboflow.zip

无论是使用使用你自己在roboflow上的dataset,还是public datasets,选择Export Format YOLO v5 PyTorch,然后复制download code到上面。
在这里插入图片描述
打开/content/dataset.v1/data.yaml,把里面的train和val的地址修改正确。从roboflow下载下来的yolo数据集不用改nc和names。
在这里插入图片描述
打开 /yolov5/models/yolov5m.yaml 将里面的 nc改为你的数据集的类别数。(这里是6)
在这里插入图片描述

根据自己的实际情况选一个模型YOLOv5s/m/l/x,这里用的是YOLOv5m。预训练权重很重要,是不翻车的关键。设置合适的参数,如何不写的话就是使用默认值。然后运行下面的代码开始训练。

%cd yolov5
!python train.py --img 640 --batch-size 16 --epoch 100 --data /content/dataset.v1/data.yaml --cfg yolov5m.yaml --weights yolov5m.pt

过程中会询问你是否visualize,选1并输入你wanbd的API。训练结果按顺序保存在 drive/MyDrive/yolov5/runs/train/exp*。登录wanbd即可查看你所有的训练结果的图表。

最后把训练结果保存在你的Google drive上面就可以了

# store train reuslts
!cp -r /content/yolov5/runs/train/exp /content/drive/MyDrive/myresults/   #  make sure exp* is the folder you want
(3) 训练方式B

这个就简单啦,把数据集(dataset.v1)和yolov5下载放到google drive上的同级目录下,结构如下:
在这里插入图片描述
同样打开data.yaml并修改trainval的地址为他们现在的地址。
在这里插入图片描述
打开yolov5m.yaml将里面的 nc改为你的数据集的类别数。
然后运行下面的代码开始训练

# install all the required packages
%cd /content/drive/MyDrive/yolov5
!pip install -U -r requirements.txt
!pip install wandb     # sign in wandb and copy your API key

# start training
!python train.py --img 640 --batch-size 16 --epoch 100 --data /content/drive/MyDrive/dataset.v1/data.yaml --cfg yolov5m.yaml --weights yolov5m.pt
4. 用训练结果做目标检测

打开你的训练结果exp*,在weights里面有best.pt和last.pt。接下来就用这个best.pt来推理。

!python detect.py --source path/*.jpg --weights best.pt --conf 0.25
!python detect.py --source 0  # webcam
                            file.jpg  # image 
                            file.mp4  # video
                            path/  # directory
                            path/*.jpg  # glob
                            'https://youtu.be/NUsoVlDFqZg'  # YouTube video
                            'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream
5. 结束

到这里yolov5的使用就结束了,感觉自己好像个老妈子,啰哩啰嗦的,总觉得大家应该都懂代码里哪些需要根据自己的情况修改,不用我多说,但又怕有小傻瓜 @_@ 。写完脑袋晕乎乎的,有错误一定要指出啊拜托拜托。

关于yolo解析的文章有很多,知乎上一位作者@初识CV做个一个相关知识的汇总,分享一下,感谢。
目标检测之YOLO算法:YOLOv1,YOLOv2,YOLOv3,TinyYOLO,YOLOv4,YOLOv5,YOLObile,YOLOF详解

相关文献:

You Only Look Once: Unified, Real-Time Object Detection.
YOLO9000: Better, Faster, Stronger.
YOLOv3: An Incremental Improvement.
YOLOv4: Optimal Speed and Accuracy of Object Detection.
YOLO Nano: a Highly Compact You Only Look Once Convolutional Neural Network for Object Detection.
Fast YOLO: A Fast You Only Look Once System for Real-time Embedded Object Detection in Video.

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值