拓冰建站拓冰建站
首页 / 资讯中心 / 正文

YOLOv8 yolo11训练图像施工安全数据集 已标注为YOLO格式,可以直接用于模型训练

YOLOv8 yolo11训练图像施工安全数据集 已标注为YOLO格式可以直接用于模型训练使用YOLOv8来训练一个包含1206张图像的施工安全数据集。这个数据集包含5个类别已标注为YOLO格式可以直接用于模型训练。数据集描述数据量1206张图像类别0: 头盔helmet1: 无头盔no-helmet2: 无背心no-vest3: 人person4: 背心vest数据划分训练集997张验证集119张测试集90张标注格式YOLO格式应用场景施工安全检测数据集组织假设你的数据集目录结构如下深色版本construction_safety_dataset/├── images/│ ├── train/│ │ ├── 000001.jpg│ │ ├── 000002.jpg│ │ └── …│ ├── val/│ │ ├── 000001.jpg│ │ ├── 000002.jpg│ │ └── …│ └── test/│ ├── 000001.jpg│ ├── 000002.jpg│ └── …├── labels/│ ├── train/│ │ ├── 000001.txt│ │ ├── 000002.txt│ │ └── …│ ├── val/│ │ ├── 000001.txt│ │ ├── 000002.txt│ │ └── …│ └── test/│ ├── 000001.txt│ ├── 000002.txt│ └── …└── data.yaml # 数据配置文件数据配置文件创建或确认data.yaml文件是否正确配置了数据集路径和类别信息yaml深色版本train: ./images/train/ # 训练集图像路径val: ./images/val/ # 验证集图像路径test: ./images/test/ # 测试集图像路径Classesnc: 5 # 类别数量names:helmetno-helmetno-vestpersonvest # 类别名称列表数据划分代码如果你需要自己划分数据集可以使用以下Python代码python深色版本import osimport randomfrom shutil import copyfile定义源目录和目标目录source_images_dir ‘./construction_safety_dataset/images’source_labels_dir ‘./construction_safety_dataset/labels’target_train_dir ‘./construction_safety_dataset/images/train’target_val_dir ‘./construction_safety_dataset/images/val’target_test_dir ‘./construction_safety_dataset/images/test’target_train_labels_dir ‘./construction_safety_dataset/labels/train’target_val_labels_dir ‘./construction_safety_dataset/labels/val’target_test_labels_dir ‘./construction_safety_dataset/labels/test’创建目标目录os.makedirs(target_train_dir, exist_okTrue)os.makedirs(target_val_dir, exist_okTrue)os.makedirs(target_test_dir, exist_okTrue)os.makedirs(target_train_labels_dir, exist_okTrue)os.makedirs(target_val_labels_dir, exist_okTrue)os.makedirs(target_test_labels_dir, exist_okTrue)获取所有图像文件all_images [f for f in os.listdir(source_images_dir) if f.endswith(‘.jpg’)]random.shuffle(all_images)划分数据集train_ratio 0.8val_ratio 0.1test_ratio 0.1train_split int(train_ratio * len(all_images))val_split train_split int(val_ratio * len(all_images))train_images all_images[:train_split]val_images all_images[train_split:val_split]test_images all_images[val_split:]复制图像文件for img in train_images:copyfile(os.path.join(source_images_dir, img), os.path.join(target_train_dir, img))label img.replace(‘.jpg’, ‘.txt’)copyfile(os.path.join(source_labels_dir, label), os.path.join(target_train_labels_dir, label))for img in val_images:copyfile(os.path.join(source_images_dir, img), os.path.join(target_val_dir, img))label img.replace(‘.jpg’, ‘.txt’)copyfile(os.path.join(source_labels_dir, label), os.path.join(target_val_labels_dir, label))for img in test_images:copyfile(os.path.join(source_images_dir, img), os.path.join(target_test_dir, img))label img.replace(‘.jpg’, ‘.txt’)copyfile(os.path.join(source_labels_dir, label), os.path.join(target_test_labels_dir, label))安装YOLOv8如果你还没有安装YOLOv8可以使用以下命令安装bash深色版本pip install ultralytics训练模型使用YOLOv8训练模型的命令非常简单你可以直接使用以下命令开始训练bash深色版本cd path/to/construction_safety_dataset/克隆YOLOv8仓库git clone https://github.com/ultralytics/ultralytics.gitcd ultralytics开始训练python yolo.py detect train data…/data.yaml modelyolov8n.pt epochs100 imgsz640 batch16在这个命令中data…/data.yaml指定数据配置文件。modelyolov8n.pt指定预训练权重这里使用的是YOLOv8的小模型。epochs100训练轮数。imgsz640输入图像的大小。batch16批量大小。模型评估训练完成后可以使用以下命令评估模型在验证集上的表现bash深色版本python yolo.py detect val data…/data.yaml modelruns/detect/train/weights/best.pt imgsz640这里的runs/detect/train/weights/best.pt是训练过程中产生的最佳模型权重文件。模型预测你可以使用训练好的模型对新图像进行预测bash深色版本python yolo.py detect predict sourcepath/to/your/image.jpg modelruns/detect/train/weights/best.pt imgsz640 conf0.4 iou0.5查看训练结果训练过程中的日志和结果会保存在runs/detect/目录下你可以查看训练过程中的损失、精度等信息。数据增强为了进一步提高模型性能可以使用数据增强技术。以下是一个简单的数据增强示例安装albumentations库bash深色版本pip install -U albumentations在yolo.py中添加数据增强python深色版本import albumentations as Afrom albumentations.pytorch import ToTensorV2import cv2定义数据增强transform A.Compose([A.RandomSizedBBoxSafeCrop(width640, height640, erosion_rate0.2),A.HorizontalFlip(p0.5),A.VerticalFlip(p0.5),A.Rotate(limit10, p0.5, border_modecv2.BORDER_CONSTANT),A.ColorJitter(brightness0.2, contrast0.2, saturation0.2, hue0.2, p0.5),A.GaussNoise(var_limit(10.0, 50.0), p0.5),A.Normalize(mean[0.485, 0.456, 0.406], std[0.229, 0.224, 0.225]),ToTensorV2()], bbox_paramsA.BboxParams(format‘yolo’, label_fields[‘class_labels’]))在数据加载器中应用数据增强def collate_fn(batch):images, targets zip(*batch)transformed_images []transformed_targets []for img, target in zip(images, targets): bboxes target[bboxes] class_labels target[labels] augmented transform(imageimg, bboxesbboxes, class_labelsclass_labels) transformed_images.append(augmented[image]) transformed_targets.append({ bboxes: augmented[bboxes], labels: augmented[class_labels] }) return torch.stack(transformed_images), transformed_targets注意事项数据集质量确保数据集的质量包括清晰度、标注准确性等。模型选择可以选择更强大的模型版本如YOLOv8m、YOLOv8l等以提高性能。超参数调整根据实际情况调整超参数如批量大小batch、图像大小imgsz等。监控性能训练过程中监控损失函数和mAP指标确保模型收敛。通过上述步骤你可以使用YOLOv8来训练一个施工安全数据集并使用训练好的模型进行预测。
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门