Ubuntu18下通过yolov5进行训练并预测

一、环境

 

Ubuntu18.04        
联想小新
yolov5s

二、前期准备

2.1、安装python3.7(python版本 > 3.6)_终端运行

sudo apt-get install python3.7

查看python版本

python --version

(ubuntu 默认自带python2版本)

2.2、安装pip(选python3的)

sudo apt install python-pip3(python3)

sudo apt install python-pip   (python2)

2.3、下载yolov5模型(需要翻一下)

2.3.1、安装git

sudo apt-get install git

2.3.2、安装完成后进行git配置

分别输入指令

 git config --global user.name "你的名字"    # 不要写入中文,与github账号一样 

 git config --global user.email "你的邮箱地址"

2.3.3、配置完成后,需要创建验证用的公钥

因为git是通过ssh的方式访问资源库的,所以需要在本地创建验证用的文件。

使用命令ssh-keygen -C 'you email address@gmail.com' -t rsa

(注意ssh-keygen之间没有空格),会在用户目录~/.ssh/下建立相应的密钥文件。

2.3.4、创建完公钥后,需要上传。

使用命令cd ~/.ssh

进入~/.ssh文件夹

输入gedit id_rsa.pub打开id_rsa.pub文件,复制其中所有内容。

将密钥填入github中

2.3.5、克隆yolov5

git clone git@github.com:ultralytics/yolov5.git

进入到yolov5文件夹中

cd yolov5

pip3 install -U -r requirements.txt

如果出错则

pip3 install --upgrade pillow -i https://pypi.tuna.tsinghua.edu.cn/simple  (换源)

2.5、官方训练、预测

进入yolov文件夹中,打开终端

训练:python3 train.py --img 640 --batch 2 --epoch 3 --data coco128.yaml --weights yolov5s.pt

预测:python detect.py --source=“data/images/bus.jpg” --weights=“yolov5s.pt”

 python3 detect.py --weights runs/train/exp/weights/best.pt --img 640 --conf 0.25 --source data/images

其中:img为图片大小、batch为图片张数、epoch:训练次数、data:训练的数据、weights:训练的框架、conf:精度、source来源

2.6、训练自己的模型

2.6.1、按规则创建文件夹

 !创建一个文件夹(Blue_two),在文件夹在创建detect文件夹(放需要进行预测的图片)和yolo文件夹

!yolo文件夹里新建images文件夹和labels文件夹,分别在两个文件夹中创建train1文件夹

!imges中的train1放训练的图片,labels中的train1放yolov5标定后的数据集

2.6.2、构建yaml文件

在yolov5文件夹下的data文件夹中创建xxx.yaml(car.yaml),写入

 其中

path        为上面构建的文件夹中的yolo文件夹

train、val为上面文件夹构建的train1文件夹(train:训练集、val:部分验证集)

nc:分类样本的数量

names:放标完数据集得到的classes.txt文件中的内容(要一样)

2.6.3、训练

python3 train.py --img 640 --batch 2 --epoch 3 --data car.yaml --weights yolov5s.pt

其中:

img:图片大小

batch:放入图片数

epoch:训练次数

data:训练数据

weights:训练的框架

conf:精度

source:来源

2.6.4、预测

python3 detect.py —weights runs/train/exp12/weights/best.pt —img 640 —conf 0.25 —source /home/winter/Car/yolo/images/train1

官方:https://github.com/ultralytics/yolov5

 Tutorials

python detect.py --source 0  # webcam
                            file.jpg  # image
                            file.mp4  # video
                            path/  # directory
                            path/*.jpg  # glob 该路径目录下的全部图片
                            'https://youtu.be/NUsoVlDFqZg'  # YouTube
                            'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream
#source后面可以放.jpg图片或.mp4视频

detect.py文件介绍

def run(weights='yolov5s.pt',  # model.pt path(s) 权重
        source='data/images',  # file/dir/URL/glob, 0 for webcam  图片
        imgsz=640,  # inference size (pixels)
        conf_thres=0.25,  # confidence threshold
        iou_thres=0.45,  # NMS IOU threshold
        max_det=1000,  # maximum detections per image
        device='',  # cuda device, i.e. 0 or 0,1,2,3 or cpu
        view_img=False,  # show results
        save_txt=False,  # save results to *.txt
        save_conf=False,  # save confidences in --save-txt labels
        save_crop=False,  # save cropped prediction boxes
        nosave=False,  # do not save images/videos
        classes=None,  # filter by class: --class 0, or --class 0 2 3
        agnostic_nms=False,  # class-agnostic NMS
        augment=False,  # augmented inference
        visualize=False,  # visualize features
        update=False,  # update all models
        project='runs/detect',  # save results to project/name
        name='exp',  # save results to project/name
        exist_ok=False,  # existing project/name ok, do not increment
        line_thickness=3,  # bounding box thickness (pixels)
        hide_labels=False,  # hide labels
        hide_conf=False,  # hide confidences
        half=False,  # use FP16 half-precision inference
        ):

def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model path(s)')   #指定网络模型,权重 默认情况下是yolov5s.pt 可修改default成yolov5m yolov5l等(训练自己的模型则先设为空)
    parser.add_argument('--source', type=str, default='data/images', help='file/dir/URL/glob, 0 for webcam')  #为网络指定一个输入  default为图片的路径
    parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')  #图片大小
    parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')  #置信区域,检测区域的置信度大于0.25才显示(越大越好)
    parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')  #圈出的框相交的区域,当大于某个值后在这两个框中选择一个较好的(避免重复判定)
    parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image') 
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--view-img', action='store_true', help='show results')  #只要设定该参数则为true(直接显示结果)(可在pycharm中可直接写入)
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')  #可进行多赋值
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')  #类型,人、车、招牌等  classes=物体编号
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')  
    parser.add_argument('--visualize', action='store_true', help='visualize features')
    parser.add_argument('--update', action='store_true', help='update all models') #把模型中不
    parser.add_argument('--project', default='runs/detect', help='save results to project/name')
    parser.add_argument('--name', default='exp', help='save results to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
    parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
    parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
    parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
    opt = parser.parse_args()
    opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1  # expand
    print_args(FILE.stem, opt)
    return opt

  • 0
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值