YOLOv8小目标TinyPerson 行人数据集识别 行人小目标检测 包括数据准备、模型训练、评估和推理。小目标检测Tinyperson数据集。其train71748以及test78130处理好的yolo格式(txt)以及voc格式(xml)标签训练时可自行混合后按比例划分训练集和验证集。标签类别0:earth_person1:sea_person包括数据准备、模型训练、评估和可视化。整个代码块在一个 artifact 中方便一次性复制。完整代码importosimportcv2importnumpy as np from sklearn.model_selectionimporttrain_test_splitimportshutil from ultralyticsimportYOLOimportmatplotlib.pyplot as plt# Define pathsdata_pathpath_to_TinyPersonimages_train_pathos.path.join(data_path,images,train)images_test_pathos.path.join(data_path,images,test)labels_yolo_train_pathos.path.join(data_path,labels_yolo,train)labels_yolo_test_pathos.path.join(data_path,labels_yolo,test)# Create directories if they dont existos.makedirs(images_train_path,exist_okTrue)os.makedirs(images_test_path,exist_okTrue)os.makedirs(labels_yolo_train_path,exist_okTrue)os.makedirs(labels_yolo_test_path,exist_okTrue)# Combine train and test sets for splitting into train, validation, and testall_images[]all_labels[]forfilenameinos.listdir(images_train_path): image_filenamefilename label_filenameos.path.splitext(filename)[0].txtimage_pathos.path.join(images_train_path, image_filename)label_pathos.path.join(labels_yolo_train_path, label_filename)all_images.append(image_path)all_labels.append(label_path)forfilenameinos.listdir(images_test_path): image_filenamefilename label_filenameos.path.splitext(filename)[0].txtimage_pathos.path.join(images_test_path, image_filename)label_pathos.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_labelstrain_test_split(all_images, all_labels,test_size0.3,random_state42)val_images, test_images, val_labels, test_labelstrain_test_split(temp_images, temp_labels,test_size0.5,random_state42)# 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 modelmodelYOLO(yolov8n.pt)# You can choose other sizes like yolov8s, yolov8m, yolov8l, yolov8x# Modify the number of classes in the final layermodel.nc2# Training commandresultsmodel.train(dataos.path.join(data_path,dataset.yaml),imgsz640,epochs50,batch16,devicecudaiftorch.cuda.is_available()elsecpu,cacheTrue)# Evaluate the modelmetricsmodel.val()# Export the trained modelmodel.export(formatonnx)# Inference using the trained model# Load the trained modelinference_modelYOLO(runs/detect/train/weights/best.pt)# Path to your best weights# Perform inference on a sample imagesample_image_pathos.path.join(data_path,images,test,sample_image.jpg)# Replace with your sample image pathresultsinference_model(sample_image_path)# Visualize resultsdef plot_results(results, image_path): imagecv2.imread(image_path)imagecv2.cvtColor(image, cv2.COLOR_BGR2RGB)forresultinresults: boxesresult.boxes.cpu().numpy()forboxinboxes: rbox.xyxy[0].astype(int)clsint(box.cls[0])confbox.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 小目标检测系统。如果你有任何进一步的问题或需要更多帮助请随时提问自定义说明数据文件路径: 修改data_path变量以指向你的 TinyPerson 数据集路径。图像分辨率: 根据需要调整数据增强中的图像大小例如imgsz640。超参数调整: 根据需要调整训练参数如epochs,batch_size等。模型选择: 你可以选择不同的 YOLOv8 模型大小yolov8n,yolov8s,yolov8m,yolov8l,yolov8x以适应你的需求。推理样本路径: 修改sample_image_path变量以指向你要进行推理的图片路径。通过这些步骤你可以灵活地使用 TinyPerson 数据集进行小目标检测任务。