PyTorch框架——基于深度学习YOLOv5神经网络水果蔬菜检测识别系统

发布于:2025-02-10 ⋅ 阅读:(30) ⋅ 点赞:(0)

基于深度学习YOLOv5神经网络水果蔬菜检测识别系统,其能识别的水果蔬菜有15种,# 水果的种类 names: ['黑葡萄', '绿葡萄', '樱桃', '西瓜', '龙眼', '香蕉', '芒果', '菠萝', '柚子', '草莓', '苹果', '柑橘', '火龙果', '梨子', '花生', '黄瓜', '土豆', '大蒜', '茄子', '白萝卜', '辣椒', '胡萝卜', '花菜', '白菜', '番茄', '西蓝花', '橙子'],见如下

第一步:YOLOv5介绍

YOLOv5是一种目标检测算法,它是YOLO(You Only Look Once)系列的最新版本。YOLOv5在YOLOv4的基础上进行了改进和优化,以提高检测的准确性和速度。

YOLOv5采用了一些新的技术和方法来改进目标检测的性能。其中包括以下几个方面:

  1. 损失函数:YOLOv5使用了CIOU_Loss作为bounding box的损失函数。CIOU_Loss是一种改进的IOU_Loss,可以更好地衡量目标框的位置和大小。

  2. 非极大值抑制(NMS):YOLOv5使用NMS来抑制重叠的边界框,以减少重复检测的问题。

  3. 聚类anchors:YOLOv5使用k-means聚类算法来生成anchors,这些anchors用于检测不同尺度的目标。

总的来说,YOLOv5在YOLOv4的基础上进行了一些改进和优化,以提高目标检测的准确性和速度。

标注数据,YOLOv5的训练和测试步骤,可以参考我的这篇博客:手把手教你通过YOLOv5训练自己的目标检测模型_yolov5怎么测试自己训练的结果-CSDN博客

第二步:YOLOv5网络结构

第三步:代码展示

# Ultralytics YOLO 🚀, AGPL-3.0 license

from pathlib import Path

from ultralytics.engine.model import Model
from ultralytics.models import yolo
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, OBBModel, PoseModel, SegmentationModel, WorldModel
from ultralytics.utils import ROOT, yaml_load


class YOLO(Model):
    """YOLO (You Only Look Once) object detection model."""

    def __init__(self, model="yolo11n.pt", task=None, verbose=False):
        """Initialize YOLO model, switching to YOLOWorld if model filename contains '-world'."""
        path = Path(model)
        if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}:  # if YOLOWorld PyTorch model
            new_instance = YOLOWorld(path, verbose=verbose)
            self.__class__ = type(new_instance)
            self.__dict__ = new_instance.__dict__
        else:
            # Continue with default YOLO initialization
            super().__init__(model=model, task=task, verbose=verbose)

    @property
    def task_map(self):
        """Map head to model, trainer, validator, and predictor classes."""
        return {
            "classify": {
                "model": ClassificationModel,
                "trainer": yolo.classify.ClassificationTrainer,
                "validator": yolo.classify.ClassificationValidator,
                "predictor": yolo.classify.ClassificationPredictor,
            },
            "detect": {
                "model": DetectionModel,
                "trainer": yolo.detect.DetectionTrainer,
                "validator": yolo.detect.DetectionValidator,
                "predictor": yolo.detect.DetectionPredictor,
            },
            "segment": {
                "model": SegmentationModel,
                "trainer": yolo.segment.SegmentationTrainer,
                "validator": yolo.segment.SegmentationValidator,
                "predictor": yolo.segment.SegmentationPredictor,
            },
            "pose": {
                "model": PoseModel,
                "trainer": yolo.pose.PoseTrainer,
                "validator": yolo.pose.PoseValidator,
                "predictor": yolo.pose.PosePredictor,
            },
            "obb": {
                "model": OBBModel,
                "trainer": yolo.obb.OBBTrainer,
                "validator": yolo.obb.OBBValidator,
                "predictor": yolo.obb.OBBPredictor,
            },
        }


class YOLOWorld(Model):
    """YOLO-World object detection model."""

    def __init__(self, model="yolov8s-world.pt", verbose=False) -> None:
        """
        Initialize YOLOv8-World model with a pre-trained model file.

        Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default
        COCO class names.

        Args:
            model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
            verbose (bool): If True, prints additional information during initialization.
        """
        super().__init__(model=model, task="detect", verbose=verbose)

        # Assign default COCO class names when there are no custom names
        if not hasattr(self.model, "names"):
            self.model.names = yaml_load(ROOT / "cfg/datasets/coco8.yaml").get("names")

    @property
    def task_map(self):
        """Map head to model, validator, and predictor classes."""
        return {
            "detect": {
                "model": WorldModel,
                "validator": yolo.detect.DetectionValidator,
                "predictor": yolo.detect.DetectionPredictor,
                "trainer": yolo.world.WorldTrainer,
            }
        }

    def set_classes(self, classes):
        """
        Set classes.

        Args:
            classes (List(str)): A list of categories i.e. ["person"].
        """
        self.model.set_classes(classes)
        # Remove background if it's given
        background = " "
        if background in classes:
            classes.remove(background)
        self.model.names = classes

        # Reset method class names
        # self.predictor = None  # reset predictor otherwise old names remain
        if self.predictor:
            self.predictor.model.names = classes

第四步:统计训练过程的一些指标,相关指标都有

第五步:运行(支持图片、文件夹、摄像头和视频功能)

第六步:整个工程的内容

有训练代码和训练好的模型以及训练过程,提供数据,提供GUI界面代码

项目完整文件下载请见演示与介绍视频的简介处给出:➷➷➷

PyTorch框架——基于深度学习YOLOv5神经网络水果蔬菜检测识别系统_哔哩哔哩_bilibili


网站公告

今日签到

点亮在社区的每一天
去签到