香蕉成熟度检测和识别2:基于深度学习YOLOv5神经网络实现香蕉成熟度检测和识别(含训练代码和数据集)

基于深度学习YOLOv5神经网络实现香蕉成熟度检测和识别,其能识别检测出香蕉成熟度:names = {0:'freshripe', 1:'freshunripe', 2:'overripe', 3:'ripe', 4:'rotten', 5:'unripe'}  CH_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怎么测试自己训练的结果-优快云博客

第二步: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

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

第五步:运行预测代码

#coding:utf-8
from ultralytics import YOLO
import cv2

# 所需加载的模型目录
path = 'models/best.pt'
# 需要检测的图片地址
img_path = "musa-acuminata-banana-9d0d6b30-4037-11ec-b5aa-94b86d66fd1d_jpg.rf.b8c6f66651835d01dc8bd851c72537c7.jpg"

# 加载预训练模型
# conf	0.25	object confidence threshold for detection
# iou	0.7	intersection over union (IoU) threshold for NMS
model = YOLO(path, task='detect')
# model = YOLO(path, task='detect',conf=0.5)


# 检测图片
results = model(img_path)
res = results[0].plot()
cv2.imshow("YOLOv5 Detection", res)
cv2.waitKey(0)

第六步:整个工程的内容

香蕉成熟度数据集、训练代码和预测代码

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

基于深度学习YOLOv5神经网络实现香蕉成熟度检测和识别(含训练代码、数据集和GUI交互界面)_哔哩哔哩_bilibili

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI街潜水的八角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值