水面垃圾数据集 模型对水面及漂浮物垃圾进行检测 识别水面漂浮物13种垃圾
使用深度学习模型YOLOv8训练使用水面垃圾数据集 模型对水面及漂浮物垃圾进行检测 识别水面漂浮物13种垃圾文章目录使用深度学习模型YOLOv8训练使用水面垃圾数据集 模型对水面及漂浮物垃圾进行检测 识别水面漂浮物13种垃圾1. 数据集准备2. 安装依赖库3. 模型训练训练脚本4. 推理与结果可视化推理脚本5. 可视化界面6. 运行步骤总结水面垃圾数据集格式yolo数据集格式 voc格式类别can bad fruit bottle glass bottle plastic box plastic bag milk boxleaf branch paper ball grass garbage标注labelimg总共有3000张标记图像主要应用背景水面垃圾检测使用深度学习模型对水面垃圾进行检测采用YOLOv8作为目标检测框架。由于你提到数据集已经提供了YOLO和VOC两种格式的标注文件基于YOLO格式进行训练。以下是详细的步骤、关键文件及代码示例来指导你完成从准备数据集到训练模型的整个流程。1. 数据集准备假设你的数据集结构如下water_trash_detection/ ├── images/ │ ├── train/ │ │ ├── img1.jpg │ │ └── ... │ ├── val/ │ │ ├── img1.jpg │ │ └── ... │ └── test/ │ ├── img1.jpg │ └── ... └── labels/ ├── train/ │ ├── img1.txt │ └── ... ├── val/ │ ├── img1.txt │ └── ... └── test/ ├── img1.txt └── ... data_water_trash.yamldata_water_trash.yaml文件内容示例train:./water_trash_detection/images/train/val:./water_trash_detection/images/val/nc:12# 类别数量can, bad fruit, bottle, glass bottle, plastic box, plastic bag, milk box, leaf, branch, paper, ball, grass, garbagenames:[can,bad fruit,bottle,glass bottle,plastic box,plastic bag,milk box,leaf,branch,paper,ball,grass,garbage]确保每个标签文件的内容符合YOLO格式即每行包含一个对象的类别ID、中心点x, y坐标归一化到0-1宽度和高度同样归一化。2. 安装依赖库确保安装了必要的库pipinstallultralytics opencv-python-headless tensorboard3. 模型训练创建一个Python脚本来开始训练过程。这里我们以YOLOv8为例说明如何训练模型。训练脚本fromultralyticsimportYOLOdefmain_train():# 加载预训练的YOLOv8n模型或从头开始定义模型modelYOLO(yolov8n.yaml)# 或者直接加载预训练权重如 yolov8n.ptresultsmodel.train(data./data_water_trash.yaml,epochs100,# 根据需要调整imgsz640,batch16,project./runs/detect,namewater_trash_detection,optimizerSGD,device0,# 使用GPU编号saveTrue,cacheTrue,verboseTrue,)if__name____main__:main_train()4. 推理与结果可视化训练完成后我们可以利用训练好的模型对新图片进行预测并将结果可视化。推理脚本importcv2fromPILimportImagefromultralyticsimportYOLO modelYOLO(./runs/detect/water_trash_detection/weights/best.pt)defdetect_objects(image_path):resultsmodel.predict(sourceimage_path)imgcv2.imread(image_path)forresultinresults:boxesresult.boxes.numpy()forboxinboxes:rbox.xyxy x1,y1,x2,y2int(r[0]),int(r[1]),int(r[2]),int(r[3])label_idint(box.cls)labelresult.names[label_id]confidencebox.confifconfidence0.5:# 设置置信度阈值cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)# 绘制矩形框cv2.putText(img,f{label}{confidence:.2f},(x1,y1-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)returnimg# 示例调用result_imagedetect_objects(your_test_image.jpg)# 确保测试图像路径正确Image.fromarray(cv2.cvtColor(result_image,cv2.COLOR_BGR2RGB)).show()# 使用PIL显示图像5. 可视化界面为了监控训练过程可以使用TensorBoard。在训练脚本中添加tensorboardTrue参数然后运行以下命令启动TensorBoardtensorboard--logdirruns/然后在浏览器中访问http://localhost:6006查看训练进度和结果。6. 运行步骤总结数据集准备确认数据集已按要求组织好并创建data_water_trash.yaml。安装依赖通过提供的命令安装所需的Python库。训练模型运行训练脚本开始训练过程。推理与可视化使用训练好的模型进行推理并可视化结果。监控训练使用TensorBoard监控训练过程。使用YOLOv8对水面垃圾检测数据集进行目标检测任务。请根据实际需求调整相关参数和代码。如果有任何问题或需要进一步的帮助请随时提问此外希望将其转换为YOLO格式可以编写一个简单的脚本来实现这种转换。例如importxml.etree.ElementTreeasETimportosdefconvert_voc_to_yolo(xml_file,classes):treeET.parse(xml_file)roottree.getroot()sizeroot.find(size)wint(size.find(width).text)hint(size.find(height).text)yolo_lines[]forobjinroot.iter(object):difficultobj.find(difficult).text clsobj.find(name).textifclsnotinclassesorint(difficult)1:continuecls_idclasses.index(cls)xmlboxobj.find(bndbox)b(float(xmlbox.find(xmin).text),float(xmlbox.find(xmax).text),float(xmlbox.find(ymin).text),float(xmlbox.find(ymax).text))bbconvert((w,h),b)yolo_lines.append(f{cls_id}{ .join(map(str,bb))})txt_filexml_file.replace(.xml,.txt)withopen(txt_file,w)asf:f.write(\n.join(yolo_lines))defconvert(size,box):dw1./(size[0])dh1./(size[1])x(box[0]box[1])/2.0-1y(box[2]box[3])/2.0-1wbox[1]-box[0]hbox[3]-box[2]xx*dw ww*dw yy*dh hh*dhreturn(x,y,w,h)# 使用方法classes[can,bad fruit,bottle,glass bottle,plastic box,plastic bag,milk box,leaf,branch,paper,ball,grass,garbage]forxml_fileinos.listdir(path/to/voc/annotations):ifxml_file.endswith(.xml):convert_voc_to_yolo(os.path.join(path/to/voc/annotations,xml_file),classes)这个脚本可以帮助你快速地将VOC格式的标注文件转换为YOLO格式以