yolov5

实操的时候老是提示找不到标签或者图片什么的,结果需要把图片文以及标签txt文件放在同一个目录中,不能分开放,因为yaml配置文件中我还没有找到可以分开配置路径的方法,暂时只能放到一个目录中,我的配置文件:

train: ../data/train_img
val: ../data/val_img

nc: 1  # 类别数量,这里是气球
names: ['balloon']  # 类别名称

训练数据集下载
先保存到自己的数据集,点击下载数据ZIP文件
https://openbayes.com/console/public/datasets/sFP84BCSkDW/2/overview

处理图片和标签数据集中的数据,转成yolo使用的格式,保存到txt文件中
data_init.py:

import json
import os
from PIL import Image

def convert_json_to_yolo(json_folder, img_folder, output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for json_file in os.listdir(json_folder):
        if json_file.endswith('.json'):
            with open(os.path.join(json_folder, json_file)) as f:
                data = json.load(f)

            # 假设图片名称与JSON文件名称相同,但扩展名不同
            image_name = json_file.replace('.json', '.jpg')  # 或其他扩展名
            img_path = os.path.join(img_folder, image_name)

            # 自动获取图像尺寸
            with Image.open(img_path) as img:
                img_width, img_height = img.size

            yolo_lines = []
            for bbox in data['bboxes']:
                x_coords = bbox['x_arr']
                y_coords = bbox['y_arr']
                
                x_min = min(x_coords)
                x_max = max(x_coords)
                y_min = min(y_coords)
                y_max = max(y_coords)

                # 计算YOLO格式的中心坐标和宽高
                x_center = (x_min + x_max) / 2
                y_center = (y_min + y_max) / 2
                width = x_max - x_min
                height = y_max - y_min

                # 转换为相对坐标
                x_center /= img_width
                y_center /= img_height
                width /= img_width
                height /= img_height

                yolo_lines.append(f"0 {x_center} {y_center} {width} {height}")  # 类别ID假设为0

            # 保存为YOLO格式文件
            with open(os.path.join(output_folder, image_name.replace('.jpg', '.txt')), 'w') as out_file:
                out_file.write('\n'.join(yolo_lines))

# 调用函数
convert_json_to_yolo('train_json', 'train_img', 'train_img')
convert_json_to_yolo('val_json', 'val_img', 'val_img')

训练
可能是电脑配置低了,我在执行1张图片训练的时候,顺利完成,但是把所有图片丢到目录训练时,结果看到:Killed 就结束了,后续再研究研究

python train.py --img 640 --batch 16 --epochs 50 --data data/balloon.yaml --weights yolov5s.pt --cache

使用训练出来的模型测试其它图片
这步还没有执行,不知道结果会是怎样,期待尝试

python detect.py --weights runs/train/exp/weights/best.pt --img 640 --conf 0.25 --source path/to/test/images

以下是AI提供的资料,仅作参考:
1. 环境搭建
安装Python和相关库
确保你安装了Python(推荐使用3.7及以上版本),然后安装必要的库。可以使用Anaconda管理你的环境:

conda create -n balloon-env python=3.8
conda activate balloon-env
pip install torch torchvision torchaudio  # 如果使用PyTorch
pip install opencv-python numpy matplotlib  # OpenCV和其他工具

安装YOLOv5
你可以使用YOLOv5,它的文档非常齐全,适合初学者。可以从GitHub克隆YOLOv5仓库:

git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -r requirements.txt

2. 准备数据集
确保你的数据集按照YOLO格式组织。文件结构大致如下:

/data
    /images
        train  # 训练集图片
        val    # 验证集图片
    /labels
        train  # 训练集标签
        val    # 验证集标签

标签文件的格式为每行一个物体,格式为:<class_id> <x_center> <y_center> ,所有数值为相对坐标(0到1之间)。

3. 配置训练参数
在yolov5/data目录下,创建一个新的配置文件(例如balloon.yaml)来描述你的数据集,内容如下:

train: ../data/images/train
val: ../data/images/val

nc: 1  # 类别数量,这里是气球
names: ['balloon']  # 类别名称

4. 训练模型
在YOLOv5目录下运行训练命令:

python train.py --img 640 --batch 16 --epochs 50 --data data/balloon.yaml --weights yolov5s.pt --cache

--img:设置输入图像的大小。
--batch:每次训练的批量大小。
--epochs:训练轮数。
--data:数据集配置文件。
--weights:可以选择使用YOLOv5的预训练模型。

5. 监控训练过程
训练过程中,你可以在控制台查看损失值和准确率。训练完成后,模型会保存在runs/train/exp/weights目录中,通常会生成best.pt和last.pt。

  1. 测试模型
    在训练完成后,你可以使用以下命令对新的图像进行测试:
python detect.py --weights runs/train/exp/weights/best.pt --img 640 --conf 0.25 --source path/to/test/images

--source:可以是单个图像、文件夹或视频。

7. 结果可视化
检测结果会保存在runs/detect/exp目录中,你可以查看输出的图片,并验证模型的效果。

8. 调整与优化
如果结果不理想,可以考虑以下方法进行优化:

增加数据集大小或多样性。
调整超参数(如学习率)。
进行数据增强。

标签: none

添加新评论