当前位置: 首页 > article >正文

YOLOv8 训练15种动物目标检测模型

1. 下载数据集

https://hyper.ai/datasets/31084

2. 将图片分类文件夹转换为yolo格式标注文件

(这里相当于将整张图片作为一个标注框,检测结果不太精确)
代码:

import json
import os
import shutil
import cv2
import matplotlib.pyplot as plt

"""
将文件夹分类转yolo格式
"""


target = "./animal_data/"
def convert(size, box):
    dw = size[1]
    dh = size[0]
    # box x1 y1 x2 y2
    x = (box[0] + box[2]) / 2.0
    y = (box[1] + box[3]) / 2.0
    w = box[2] - box[0]
    h = box[3] - box[1]
    x = x / dw
    w = w / dw
    y = y / dh
    h = h / dh
    if w >= 1:
        w = 0.99
    if h >= 1:
        h = 0.99
    return (x, y, w, h)

classify = os.listdir(target)
classify.remove("train")
train_dir = target + "train/"
train_images = train_dir + "images/"
train_labels = train_dir + "labels/"
if not os.path.exists(train_labels):
    os.mkdir(train_labels)
    os.mkdir(train_images)
for idx in range(len(classify)):
    item = classify[idx]
    clazz = target + item

    # 遍历分类下面的所有图片
    imgs = os.listdir(clazz)
    for i in imgs:
        shutil.copyfile(os.path.join(clazz, i), os.path.join(train_images, i))

        img = cv2.imread(os.path.join(train_images, i))

        filename, _ = os.path.splitext(i)
        with open(train_labels + filename + ".txt", "w") as f:
            box = convert(img.shape, (0, 0, img.shape[0], img.shape[1]))
            f.write(str(idx)+" " + " ".join([str(a) for a in box]))


这里没对数据集进行train、test拆分

3. 进行训练

yaml:

path: C:\Users\lhq\Desktop\15-animals-data\animal_data

train: "C://Users//lhq//Desktop//15-animals-data//animal_data/train/"
val: "C://Users//lhq//Desktop//15-animals-data//animal_data/train/"
test: "C://Users//lhq//Desktop//15-animals-data//animal_data/train/"

nc: 15

names:
  0:1:2:3: 奶牛
  4: 鹿
  5:6: 海豚
  7: 大象
  8: 长颈鹿
  9:10: 袋鼠
  11: 狮子
  12: 熊猫
  13: 老虎
  14: 斑马

train.py:

from ultralytics import YOLO
from ultralytics.utils import DEFAULT_CFG
from datetime import datetime

current_time = datetime.now()
time_str = current_time.strftime("%Y-%m-%d_%H-%M-%S")  
DEFAULT_CFG.save_dir = f"./models/{time_str}"

if __name__ == "__main__":
    model = YOLO("yolov8n.pt")

    # Train the model
    results = model.train(data="animal.yaml", epochs=200, imgsz=224, device=0, save=True, save_period=1 ,batch=16)

4.检测

代码:

from ultralytics import YOLO

# Load a model
model = YOLO('best.pt')
# 可以是文件夹或图片
model.predict("u950468431,735343220fm253fmtauto.jpg", imgsz=224, save=True, device=0,plots=True)

检测结果:
在这里插入图片描述
在这里插入图片描述
部分数据推理不准确,比如:白色的猫可能检测出熊猫


http://www.kler.cn/news/282900.html

相关文章:

  • day03-面向对象-内部类泛型常用API
  • [底层原理] C/C++获取时间(将时间戳转换为年月日)?
  • EmguCV学习笔记 VB.Net 7.2 特征点检测
  • Java之内部类
  • 浅谈Kafka(三)
  • 深度学习基础--深度学习网络
  • 服务器内存飙升分析小记
  • PostgreSQL遍历所有的表并设置id为自增主键(基于自建函数)
  • FineReport帆软报表:使用JAVA批量更新报表里的数据集连接名
  • 【python量化分析专题】最新整理的已经实测可用的各类免费股票数据接口之实时交易数据
  • 『大模型笔记』林纳斯·托瓦兹(Linux之父):谈论热议与人工智能的未来!
  • Linux 网络技术栈,看这篇就够了!!
  • 【ACM独立出版 | 厦大主办】第五届计算机科学与管理科技国际学术会议(ICCSMT 2024,10月18-20)
  • 基于web 在线影院系统网站设计与实现
  • 交通流量监测检测系统源码分享 # [一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]
  • Qt 调用执行 Python 函数
  • zookeeper服务器动态上下线监听案例
  • 【MySQL数据库管理问答题】第4章 配置 MySQL
  • SpringBoot应用打成ZIP部署包
  • 18.神经网络 - 非线性激活
  • 【机器学习】梯度下降算法
  • 源码编译并安装Squid的方法
  • BEVDet4D:多帧时序信息融合方法详解
  • 富格林:正规方式顺利盈利出金
  • 性能测试的基本概念
  • Pycharm安装报错:Cannot detect a launch configuration 解决办法
  • 吴恩达机器学习笔记 四十五 基于内容的过滤的tensorFlow实现
  • 怎么解决 hash 碰撞,用 C++ 实现 hashMap?
  • Nosql数据库redis集群配置详解
  • Nginx轮询负载均衡配置指南:实现高效请求分发