害虫检测和识别3:基于深度学习YOLO26神经网络实现害虫检测和识别(含训练代码、数据集和GUI交互界面)
基于深度学习YOLO26神经网络实现害虫检测和识别,其能识别检测出102种害虫检测:
names = {
0: 'rice_leaf_roller', 1: 'rice_leaf_caterpillar', 2: 'paddy_stem_maggot', 3: 'asiatic_rice_borer',
4: 'yellow_rice_borer', 5: 'rice_gall_midge', 6: 'Rice_Stemfly', 7: 'brown_plant_hopper',
8: 'white_backed_plant_hopper', 9: 'small_brown_plant_hopper', 10: 'rice_water_weevil', 11: 'rice_leafhopper',
12: 'grain_spreader_thrips', 13: 'rice_shell_pest', 14: 'grub', 15: 'mole_cricket',
16: 'wireworm', 17: 'white_margined_moth', 18: 'black_cutworm', 19: 'large_cutworm',
20: 'yellow_cutworm', 21: 'red_spider', 22: 'corn_borer', 23: 'army_worm',
24: 'aphids', 25: 'Potosiabre_vitarsis', 26: 'peach_borer', 27: 'english_grain_aphid',
28: 'green_bug', 29: 'bird_cherry-oataphid', 30: 'wheat_blossom_midge', 31: 'penthaleus_major',
32: 'longlegged_spider_mite', 33: 'wheat_phloeothrips', 34: 'wheat_sawfly', 35: 'cerodonta_denticornis',
36: 'beet_fly', 37: 'flea_beetle', 38: 'cabbage_army_worm', 39: 'beet_army_worm',
40: 'Beet_spot_flies', 41: 'meadow_moth', 42: 'beet_weevil', 43: 'sericaorient_alismots_chulsky',
44: 'alfalfa_weevil', 45: 'flax_budworm', 46: 'alfalfa_plant_bug', 47: 'tarnished_plant_bug',
48: 'Locustoidea', 49: 'lytta_polita', 50: 'legume_blister_beetle', 51: 'blister_beetle',
52: 'therioaphis_maculata_Buckton', 53: 'odontothrips_loti', 54: 'Thrips', 55: 'alfalfa_seed_chalcid',
56: 'Pieris_canidia', 57: 'Apolygus_lucorum', 58: 'Limacodidae', 59: 'Viteus_vitifoliae',
60: 'Colomerus_vitis', 61: 'Brevipoalpus_lewisi_McGregor', 62: 'oides_decempunctata', 63: 'Polyphagotars_onemus_latus',
64: 'Pseudococcus_comstocki_Kuwana', 65: 'parathrene_regalis', 66: 'Ampelophaga', 67: 'Lycorma_delicatula',
68: 'Xylotrechus', 69: 'Cicadella_viridis', 70: 'Miridae', 71: 'Trialeurodes_vaporariorum',
72: 'Erythroneura_apicalis', 73: 'Papilio_xuthus', 74: 'Panonchus_citri_McGregor', 75: 'Phyllocoptes_oleiverus_ashmead',
76: 'Icerya_purchasi_Maskell', 77: 'Unaspis_yanonensis', 78: 'Ceroplastes_rubens', 79: 'Chrysomphalus_aonidum',
80: 'Parlatoria_zizyphus_Lucus', 81: 'Nipaecoccus_vastalor', 82: 'Aleurocanthus_spiniferus', 83: 'Tetradacus_c_Bactrocera_minax',
84: 'Dacus_dorsalis(Hendel)', 85: 'Bactrocera_tsuneonis', 86: 'Prodenia_litura', 87: 'Adristyrannus',
88: 'Phyllocnistis_citrella_Stainton', 89: 'Toxoptera_citricidus', 90: 'Toxoptera_aurantii', 91: 'Aphis_citricola_Vander_Goot',
92: 'Scirtothrips_dorsalis_Hood', 93: 'Dasineura_sp', 94: 'Lawana_imitata_Melichar', 95: 'Salurnis_marginella_Guerr',
96: 'Deporaus_marginatus_Pascoe', 97: 'Chlumetia_transversa', 98: 'Mango_flat_beak_leafhopper', 99: 'Rhytidodera_bowrinii_white',
100: 'Sternochetus_frigidus', 101: 'Cicadellidae'
}
具体图片见如下:


第一步:YOLO26介绍
YOLO26采用了端到端无NMS推理,直接生成预测结果,无需非极大值抑制(NMS)后处理。这种设计减少了延迟,简化了集成,并提高了部署效率。此外,YOLO26移除了分布焦点损失(DFL),从而增强了硬件兼容性,特别是在边缘设备上的表现。
模型还引入了ProgLoss和小目标感知标签分配(STAL),显著提升了小目标检测的精度。这对于物联网、机器人技术和航空影像等应用至关重要。同时,YOLO26采用了全新的MuSGD优化器,结合了SGD和Muon优化技术,提供更稳定的训练和更快的收敛速度。
第二步:YOLO26网络结构

第三步:代码展示
# 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界面代码

项目完整文件下载请见演示与介绍视频的简介处给出:➷➷➷
https://www.bilibili.com/video/BV1Lm8s6GEn6/

更多推荐



所有评论(0)