赞
踩
# 测试是否安装成功
conda --version
# 创建一个虚拟python环境
conda create -n yolov8 python=3.8
# 查看已创建的虚拟环境
conda env list
# 切换激活到创建的环境中
activate yolov8
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple/
yolo predict model=weights\yolov8n.pt source=ultralytics-main\ultralytics\assets\zidane.jpg show=True save=True
1、安装标注工具库 labelimg 或 labelme
# 安装labelimg库
pip install labelimg -i https://pypi.tuna.tsinghua.edu.cn/simple/
# 安装labelme库
pip install labelme -i https://pypi.tuna.tsinghua.edu.cn/simple/
2、labelimg库的使用
3、标注完成后.xml文件转.txt文件 直接使用写好的小脚本
# -*- coding: UTF-8 -*-
import xml.etree.ElementTree as Et
import os
# 类别
CLASSES = ["font"]
# xml文件路径
xml_input = "labels/label_xml/"
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open(xml_input + image_id, 'r', encoding='utf-8')
image_id = image_id.split(".")[0]
out_file = open(f"labels/label_txt/{image_id}.txt", "w", encoding='utf-8')
tree = Et.parse(in_file)
root = tree.getroot()
size = root.find("size")
w = int(size.find("width").text)
h = int(size.find("height").text)
for obj in root.iter("object"):
difficult = obj.find("difficult").text
obj_cls = obj.find("name").text
if obj_cls not in CLASSES or int(difficult) == 1:
continue
cls_id = CLASSES.index(obj_cls)
xmlbox = obj.find("bndbox")
points = (float(xmlbox.find("xmin").text),
float(xmlbox.find("xmax").text),
float(xmlbox.find("ymin").text),
float(xmlbox.find("ymax").text))
bb = convert((w, h), points)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + "\n")
def make_label_txt():
filenames = os.listdir(xml_input)
for file in filenames:
print(file)
convert_annotation(file)
if __name__ == "__main__":
# 开始提取和转换
make_label_txt()
4、创建自己的训练数据集文件夹,这里按照coco128文件格式进行创建
5、开始进行训练
# 从YAML构建新模型,从头开始训练
yolo detect train data=my_data.yaml model=yolov8n.yaml epochs=100 imgsz=640 resume=True
6、对训练好的模型进行预测验证
yolo predict model=runs\detect\train\weights\best.pt source=datasets\yolo_demo\test\5bcebf0a644f42a18eb0e3d6ec237593.jpg show=True save=True
nvidia -smi
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。