吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2096|回复: 11
收起左侧

[学习记录] yolov8快速使用指南

  [复制链接]
18834161486 发表于 2024-5-23 15:11
简单的做一个yolov8(cpu)的使用教程。
第一步,python版本必须3.8以上(我用的是3.9)。
第二步,通过pip命令下载ultralytics,也可以直接通过pycharm的包管理工具来下载。哪个库安装失败就单独pip一下。
[Asm] 纯文本查看 复制代码
Requirement already satisfied: ultralytics in d:\pythonproject\venv\lib\site-packages (8.2.18)
Requirement already satisfied: matplotlib>=3.3.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (3.7.3)
Requirement already satisfied: opencv-python>=4.6.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (4.9.0.80)
Requirement already satisfied: pillow>=7.1.2 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (10.0.0)         
Requirement already satisfied: pyyaml>=5.3.1 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (6.0.1)          
Requirement already satisfied: requests>=2.23.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (2.31.0)      
Requirement already satisfied: scipy>=1.4.1 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (1.11.2)
Requirement already satisfied: torch>=1.8.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (2.3.0)
Requirement already satisfied: torchvision>=0.9.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (0.18.0)
Requirement already satisfied: tqdm>=4.64.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (4.66.2)
Requirement already satisfied: psutil in d:\pythonproject\venv\lib\site-packages (from ultralytics) (5.9.8)
Requirement already satisfied: py-cpuinfo in d:\pythonproject\venv\lib\site-packages (from ultralytics) (9.0.0)
Requirement already satisfied: thop>=0.1.1 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (0.1.1.post2209072238)
Requirement already satisfied: pandas>=1.1.4 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (2.1.3)
Requirement already satisfied: seaborn>=0.11.0 in d:\pythonproject\venv\lib\site-packages (from ultralytics) (0.13.2)
Requirement already satisfied: contourpy>=1.0.1 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (1.1.0)
Requirement already satisfied: cycler>=0.10 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (4.42.1)
Requirement already satisfied: kiwisolver>=1.0.1 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (1.4.5)
Requirement already satisfied: numpy<2,>=1.20 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (1.25.2)
Requirement already satisfied: packaging>=20.0 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (23.1)
Requirement already satisfied: pyparsing>=2.3.1 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (3.1.1)
Requirement already satisfied: python-dateutil>=2.7 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (2.8.2)
Requirement already satisfied: importlib-resources>=3.2.0 in d:\pythonproject\venv\lib\site-packages (from matplotlib>=3.3.0->ultralytics) (6.0.1)
Requirement already satisfied: pytz>=2020.1 in d:\pythonproject\venv\lib\site-packages (from pandas>=1.1.4->ultralytics) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in d:\pythonproject\venv\lib\site-packages (from pandas>=1.1.4->ultralytics) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (3.3.1)
Requirement already satisfied: idna<4,>=2.5 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (1.26.18)
Requirement already satisfied: certifi>=2017.4.17 in d:\pythonproject\venv\lib\site-packages (from requests>=2.23.0->ultralytics) (2023.7.22)
Requirement already satisfied: filelock in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (3.13.1)
Requirement already satisfied: typing-extensions>=4.8.0 in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (4.8.0)
Requirement already satisfied: sympy in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (1.12)
Requirement already satisfied: networkx in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (3.2.1)
Requirement already satisfied: jinja2 in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (3.1.3)
Requirement already satisfied: fsspec in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (2023.12.2)
Requirement already satisfied: mkl<=2021.4.0,>=2021.1.1 in d:\pythonproject\venv\lib\site-packages (from torch>=1.8.0->ultralytics) (2021.4.0)
Requirement already satisfied: colorama in d:\pythonproject\venv\lib\site-packages (from tqdm>=4.64.0->ultralytics) (0.4.6)
Requirement already satisfied: zipp>=3.1.0 in d:\pythonproject\venv\lib\site-packages (from importlib-resources>=3.2.0->matplotlib>=3.3.0->ultralytics) (3.16.2)
Requirement already satisfied: intel-openmp==2021.* in d:\pythonproject\venv\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch>=1.8.0->ultralytics) (2021.4.0)
Requirement already satisfied: tbb==2021.* in d:\pythonproject\venv\lib\site-packages (from mkl<=2021.4.0,>=2021.1.1->torch>=1.8.0->ultralytics) (2021.12.0)
Requirement already satisfied: six>=1.5 in d:\pythonproject\venv\lib\site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->ultralytics) (1.16.0)
Requirement already satisfied: MarkupSafe>=2.0 in d:\pythonproject\venv\lib\site-packages (from jinja2->torch>=1.8.0->ultralytics) (2.1.3)
Requirement already satisfied: mpmath>=0.19 in d:\pythonproject\venv\lib\site-packages (from sympy->torch>=1.8.0->ultralytics) (1.3.0)

[Python] 纯文本查看 复制代码
pip install ultralytics

第三步,前往github网站:https://github.com/ultralytics/ultralytics下载yolov8n.pt。
第四步,创建一个yolov8的项目,名称为yolo环境,详情如下:
[Asm] 纯文本查看 复制代码
yolo环境
--yolov8n.pt
--官方示例.py

第五步,向官方示例.py文件中添加内容如下:
[Python] 纯文本查看 复制代码
from ultralytics import YOLO

# Create a new YOLO model from scratch
model = YOLO("yolov8n.yaml")

# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolov8n.pt")

# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=3)

# Evaluate the model's performance on the validation set
results = model.val()

# 百度上一个公交车的图片
results = model("https://i2.hdslb.com/bfs/archive/b4815f0dbfd250194b63789d87d66b6f2fd145b9.jpg")

# Export the model to ONNX format
success = model.export(format="onnx")

第六步,运行官方示例.py,完毕后会自动添加一些文件,结构如下:
[Asm] 纯文本查看 复制代码
yolo环境
--runs
----detect
------train
------train2
--名字太长就不写了.jpg
--yolov8n.pt
--官方示例.py
yolo
--datasets
----coco8
------images
--------train
--------val
------labels
--------train
--------val

这些文件自己看看就行,主要是先把官方示例给跑通了,中途可能有个报错onnx这个库没有,自己pip下载就行。
第七步,创建一个新的项目文件取名为yolov8,,把yolov8n.pt移到里面,在里面创建一个文件夹为dataset,在dataset里面创建两个文件夹分别是images和labels。
第八步,在百度上下载50张图片,图片上包含一个人和一个鸟,名字最好重命名一下,把图片放在在images文件夹里。
第九步,下载标注工具,pip install -U label-studio。可能会报错ERROR: Operation cancelled by user,以管理员身份运行pycharm就好了。这个安装的依赖挺多的,慢慢下载。
第十步,执行label-studio start,会提示Starting development server at http://0.0.0.0:8080/,打开链接。
第十一步,注册账号,登录。依次点击create project--Project Name(输入项目名称)--Description(输入描述)--点击Data Import--点击upload files(这里上传图片,把images里面的图片都全选)--全部上传完毕点击Labeling Setup--左边栏保持不变,中间选择第一行第三个飞机画框的那个点击一下--新页面中左边有一个add按钮,在上方输入person,点击add。输入bird,点击add,把按钮右侧无关标签删了。点击标签可以更换颜色。最后点击右上角save。转到新界面。--点击label all tasks--选择标签开始框选--点击框可以调节大小--框选完了点击submit,全部标注完成之后点击最上方的项目名称(这个是你自己创建的项目名),检查一下是否第三列都为1。点击右上角export。选择yolo,然后点击export。会自动下载。
第十二步,将下载到的文件分别替换为dataset里面的images和labels文件夹。在images和labels文件夹里各创建一个train和val文件夹。images的train里面留40个图片,val留10个图片,对应labels的train里面留40个txt,val留10个txt,记得名字要对应,不要乱分。
第十三步,在yolov8文件夹下面创建一个ceshi.yaml文件,内容如下
[Python] 纯文本查看 复制代码
path: 'D:\pythonProject\yolov8\dataset\images'
train: 'train'
val: 'val'
nc: 2 #标签个数
names: [ 'person','bird' ] #添加标签的顺序要一致

第十三步,在yolov8文件夹下面创建一个训练.py,内容如下:
[Asm] 纯文本查看 复制代码
from ultralytics import YOLO


model = YOLO("yolov8n.pt")
model.train(
    data="ceshi.yaml",
    epochs=100,#次数
    imgsz=640,
    device='cpu'
)

第十四步,运行训练.py,
第十五步,在yolov8文件夹下面创建一个检测.py,内容如下。同时把训练好的best.pt移动到yolov8文件下,下载一个图片命名为bus.jpg放到yolov8文件下
[Python] 纯文本查看 复制代码
import cv2
from ultralytics import YOLO

model = YOLO("best.pt")
results = model.predict(
    source="bus.jpg",  # 被检测图片
    device='cpu',
    save=False,
    conf=0.7,  # 置信度>=0.7才显示出来
)


# 获取返回值中心坐标
def getRes(results):
    res = {}
    for r in results:
        for i, detection in enumerate(r.boxes.xywh):
            label = r.names[int(r.boxes.cls[i])]
            x = int(detection[0].item())
            y = int(detection[1].item())
            if label not in res:
                res[label] = []
            res[label].append((x, y))
    return res


# 将中心点坐标显示到图片中
res = getRes(results)
img = cv2.imread('bus.jpg')
for a in res:
    for b in res[a]:
        img = cv2.circle(img, b, 5, (255, 0, 0), 5)
cv2.imshow('4556', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


# 框选
def result_show(res):
    annotated_frame = res[0].plot()
    cv2.imshow("YOLOv8 Inference", annotated_frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# result_show(results)

第十六步,运行检测.py。可以通过result_show(result)来看到被检测情况。

免费评分

参与人数 6吾爱币 +13 热心值 +5 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
gusong125 + 1 + 1 我很赞同!
bluesky007sky + 1 + 1 谢谢@Thanks!
冬天冷了多穿点 + 2 + 1 谢谢@Thanks!
burning + 1 + 1 我很赞同!
初见悲风 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

msn882 发表于 2024-5-23 16:21
来几个图片啊
 楼主| 18834161486 发表于 2024-5-23 17:44

公司电脑加密,发不了。会抽时间回家搞个视频教程,可以跟着视频学。
burning 发表于 2024-5-23 21:37
18834161486 发表于 2024-5-23 17:44
公司电脑加密,发不了。会抽时间回家搞个视频教程,可以跟着视频学。

提前谢谢  大佬的教程
江男 发表于 2024-5-23 22:01
干什么用的?
 楼主| 18834161486 发表于 2024-5-23 22:12
burning 发表于 2024-5-23 21:37
提前谢谢  大佬的教程

哔站搜索萌新本炘,刚出的的教程。
 楼主| 18834161486 发表于 2024-5-23 22:13

目标检测,游戏方面的话主要是fps类游戏的自瞄。
gusong125 发表于 2024-5-27 09:32
感谢大佬分享
wincao 发表于 2024-5-29 16:17
试LZ的方法,第6步时:
需要Downloading coco8.zip,100%后也不能通过。
[Shell] 纯文本查看 复制代码
AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n...
C:\ProgramData\miniconda3\envs\yolo\lib\site-packages\torch\nn\modules\conv.py:456: UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\cudnn\Conv_v8.cpp:919.)
qw754852 发表于 2024-6-23 09:25
18834161486 发表于 2024-5-23 17:44
公司电脑加密,发不了。会抽时间回家搞个视频教程,可以跟着视频学。

提前谢谢  大佬的教程
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 10:11

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表