news 2026/8/13 8:56:15

YOLOv8小目标TinyPerson 行人数据集识别 行人小目标检测 包括数据准备、模型训练、评估和推理。

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
YOLOv8小目标TinyPerson 行人数据集识别 行人小目标检测 包括数据准备、模型训练、评估和推理。

YOLOv8小目标TinyPerson 行人数据集识别 行人小目标检测 包括数据准备、模型训练、评估和推理。


小目标检测,Tinyperson数据集。

其train717+48以及test781+30处理好的yolo格式(txt)以及voc格式(xml)标签,训练时可自行混合后按比例划分训练集和验证集。标签类别:
0:earth_person
1:sea_person

,包括数据准备、模型训练、评估和可视化。整个代码块在一个 artifact 中,方便一次性复制。

完整代码

importosimportcv2importnumpy as np from sklearn.model_selectionimporttrain_test_splitimportshutil from ultralyticsimportYOLOimportmatplotlib.pyplot as plt# Define pathsdata_path='path_to_TinyPerson'images_train_path=os.path.join(data_path,'images','train')images_test_path=os.path.join(data_path,'images','test')labels_yolo_train_path=os.path.join(data_path,'labels_yolo','train')labels_yolo_test_path=os.path.join(data_path,'labels_yolo','test')# Create directories if they don't existos.makedirs(images_train_path,exist_ok=True)os.makedirs(images_test_path,exist_ok=True)os.makedirs(labels_yolo_train_path,exist_ok=True)os.makedirs(labels_yolo_test_path,exist_ok=True)# Combine train and test sets for splitting into train, validation, and testall_images=[]all_labels=[]forfilenameinos.listdir(images_train_path): image_filename=filename label_filename=os.path.splitext(filename)[0]+'.txt'image_path=os.path.join(images_train_path, image_filename)label_path=os.path.join(labels_yolo_train_path, label_filename)all_images.append(image_path)all_labels.append(label_path)forfilenameinos.listdir(images_test_path): image_filename=filename label_filename=os.path.splitext(filename)[0]+'.txt'image_path=os.path.join(images_test_path, image_filename)label_path=os.path.join(labels_yolo_test_path, label_filename)all_images.append(image_path)all_labels.append(label_path)# Split data into train, validation, and test setstrain_images, temp_images, train_labels, temp_labels=train_test_split(all_images, all_labels,test_size=0.3,random_state=42)val_images, test_images, val_labels, test_labels=train_test_split(temp_images, temp_labels,test_size=0.5,random_state=42)# Move files to respective foldersdef move_files(images, labels, dest_image_folder, dest_label_folder):forimage_path, label_pathinzip(images, labels): shutil.move(image_path, dest_image_folder)shutil.move(label_path, dest_label_folder)move_files(train_images, train_labels, os.path.join(data_path,'images','train'), os.path.join(data_path,'labels_yolo','train'))move_files(val_images, val_labels, os.path.join(data_path,'images','val'), os.path.join(data_path,'labels_yolo','val'))move_files(test_images, test_labels, os.path.join(data_path,'images','test'), os.path.join(data_path,'labels_yolo','test'))# Create dataset.yaml file for YOLOv8dataset_yaml_content=""" train: ./images/train val: ./images/val test: ./images/test nc:2names:['earth_person','sea_person']""" with open(os.path.join(data_path,'dataset.yaml'),'w')as f: f.write(dataset_yaml_content)# Step 3: Train YOLOv8 Model# Load a pre-trained YOLOv8 modelmodel=YOLO('yolov8n.pt')# You can choose other sizes like yolov8s, yolov8m, yolov8l, yolov8x# Modify the number of classes in the final layermodel.nc=2# Training commandresults=model.train(data=os.path.join(data_path,'dataset.yaml'),imgsz=640,epochs=50,batch=16,device='cuda'iftorch.cuda.is_available()else'cpu',cache=True)# Evaluate the modelmetrics=model.val()# Export the trained modelmodel.export(format='onnx')# Inference using the trained model# Load the trained modelinference_model=YOLO('runs/detect/train/weights/best.pt')# Path to your best weights# Perform inference on a sample imagesample_image_path=os.path.join(data_path,'images','test','sample_image.jpg')# Replace with your sample image pathresults=inference_model(sample_image_path)# Visualize resultsdef plot_results(results, image_path): image=cv2.imread(image_path)image=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)forresultinresults: boxes=result.boxes.cpu().numpy()forboxinboxes: r=box.xyxy[0].astype(int)cls=int(box.cls[0])conf=box.conf[0]cv2.rectangle(image,(r[0], r[1]),(r[2], r[3]),(0,255,0),2)cv2.putText(image, f'{result.names[cls]} {conf:.2f}',(r[0], r[1]-10), cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)plt.figure(figsize=(10,10))plt.imshow(image)plt.axis('off')plt.show()plot_results(results, sample_image_path)

运行脚本

在终端中运行以下命令来执行整个流程:

python main.py

总结

以上文档包含了从数据加载、预处理、模型构建到训练、评估和可视化的所有步骤。希望这些详细的信息和代码能够帮助你顺利实施和优化你的 TinyPerson 小目标检测系统。如果你有任何进一步的问题或需要更多帮助,请随时提问!

自定义说明

  1. 数据文件路径: 修改data_path变量以指向你的 TinyPerson 数据集路径。
  2. 图像分辨率: 根据需要调整数据增强中的图像大小(例如,imgsz=640)。
  3. 超参数调整: 根据需要调整训练参数,如epochs,batch_size等。
  4. 模型选择: 你可以选择不同的 YOLOv8 模型大小(yolov8n,yolov8s,yolov8m,yolov8l,yolov8x)以适应你的需求。
  5. 推理样本路径: 修改sample_image_path变量以指向你要进行推理的图片路径。

通过这些步骤,你可以灵活地使用 TinyPerson 数据集进行小目标检测任务。


版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/13 8:52:39

有没有实测靠谱的 AI 论文工具,能够让枯燥的学术写作变简单?

每一位经历过毕业论文、课程作业、期刊文稿的同学,大都体会过学术写作的煎熬:苦思冥想搭建大纲、耗费整日搜集参考文献、反复修改标红段落、调整繁杂的论文格式,漫长枯燥的流程很容易消耗掉全部耐心。 伴随着 AI 科研辅助工具成熟落地&#x…

作者头像 李华
网站建设 2026/8/13 8:51:51

从零配置 xv6-RISC-V 的 VSCode 开发与调试环境

从零配置 xv6-RISC-V 的 VSCode 开发与调试环境 1. 环境概览宿主机:Ubuntu(虚拟机)目标系统:xv6-RISC-V(MIT 6.S081)开发工具:VSCode 插件调试工具链:QEMU gdb-multiarch2. 安装基…

作者头像 李华
网站建设 2026/8/13 8:51:43

智能问数,正在杀死整个数据团队

数据团队最近过得不太好。需求单越来越少,不是业务不需要数据了,而是他们开始直接问 AI 了。一句自然语言,十秒出图表。过去要排期三天的取数需求,现在被一个对话框替代。没有人宣布裁员,但每个数据分析师都感受到了那…

作者头像 李华
网站建设 2026/8/13 8:51:34

AI智能体时代:数据团队如何从报表交付转向业务价值创造

1. 从“看板”到“行动者”:数据团队的范式危机 最近和几个不同公司的数据负责人聊天,发现一个挺有意思的现象:大家普遍焦虑,但焦虑的源头出奇地一致。过去,数据团队的KPI是“看板数”、“报表覆盖率”、“数据需求响应…

作者头像 李华
网站建设 2026/8/13 8:50:59

2026秋人教版语文典中点上册 1-6 年级配基础 8分钟同步全套

新学期语文学习想要夯实基础、稳步提升,既要同步跟进课堂内容,也要兼顾日常积累与阶段检测,一套配套完整的同步教辅能省去零散搜集资料的精力。这套 2026 秋季人教版《典中点》语文抢先版资料,覆盖一年级到六年级上册全学段&#…

作者头像 李华
网站建设 2026/8/13 8:47:36

SCP文件传输权限问题深度解析:从Permission denied到三层权限壁垒

1. 从一次文件传输失败说起:为什么权限是SCP的“隐形门槛” 那天下午,我正急着把一台测试服务器上的日志文件拉到本地分析。服务器是CentOS,本地是Ubuntu,心想这还不简单,打开终端,手指飞舞敲下熟悉的 scp…

作者头像 李华