遥感类_遥感图像农田分割数据集![]()
2890张,yolo和voc标注
标注“farms”1类目标
image num: 2890
使用遥感图像农田分割数据集进行目标检测任务,我们将采用YOLOv5作为模型框架。尽管该数据集的目标是“农田分割”,这里我们任务变更为基于YOLO格式的农田(farms)目标检测任务。以下是详细的实现步骤指南,包括环境设置、数据准备、模型训练、优化以及结果可视化。
1. 环境配置
首先,确保安装了必要的依赖项,并克隆YOLOv5的仓库。
# 克隆YOLOv5仓库gitclone https://github.com/ultralytics/yolov5.gitcdyolov5# 安装依赖项pipinstall-rrequirements.txt2. 数据准备
创建一个farms.yaml文件来描述你的数据集路径和类别信息。假设你的数据集结构如下:
/path/to/farms/ ├── images │ ├── train │ └── val └── labels ├── train └── valfarms.yaml示例内容如下:
train:/path/to/farms/images/train/val:/path/to/farms/images/val/nc:1# 类别数量names:['farms']# 类别名称3. 数据集划分
假设你有2890张图片,并且已经按照YOLO格式标注好。我们可以按比例将数据集划分为训练集和验证集,例如80%用于训练,20%用于验证。
如果你的数据集没有预先划分,可以使用以下Python脚本进行随机划分:
importosimportrandomimportshutil source_dir='/path/to/farms/images/'label_dir='/path/to/farms/labels/'os.makedirs(source_dir+'train/',exist_ok=True)os.makedirs(source_dir+'val/',exist_ok=True)os.makedirs(label_dir+'train/',exist_ok=True)os.makedirs(label_dir+'val/',exist_ok=True)all_images=[fforfinos.listdir(source_dir)iff.endswith('.jpg')]random.shuffle(all_images)split_ratio=0.8split_index=int(len(all_images)*split_ratio)train_images=all_images[:split_index]val_images=all_images[split_index:]forimageintrain_images:shutil.move(os.path.join(source_dir,image),os.path.join(source_dir+'train/',image))label=image.replace('.jpg','.txt')shutil.move(os.path.join(label_dir,label),os.path.join(label_dir+'train/',label))forimageinval_images:shutil.move(os.path.join(source_dir,image),os.path.join(source_dir+'val/',image))label=image.replace('.jpg','.txt')shutil.move(os.path.join(label_dir,label),os.path.join(label_dir+'val/',label))请根据实际目录结构调整路径。
4. 模型训练
在准备好数据之后,可以开始训练YOLOv5模型。我们使用预训练权重进行微调以加速训练过程并提高性能。
训练命令
# 训练命令python train.py--img640--batch16--epochs100--datafarms.yaml--weightsyolov5s.pt--cache参数解释:
--img 640: 设置输入图像尺寸为640x640(或根据实际情况调整)。--batch 16: 批大小设置为16(根据你的GPU内存调整)。--epochs 100: 设置训练周期数为100。--data farms.yaml: 指定数据集配置文件。--weights yolov5s.pt: 使用YOLOv5的小型版本作为预训练权重。--cache: 缓存图像以加速训练过程。
5. 模型评估
训练完成后,使用以下命令对验证集进行评估,查看模型性能。
# 评估命令python val.py--weightsruns/train/exp/weights/best.pt--datafarms.yaml--img640--taskval这将输出一系列指标,如mAP@0.5、mAP@0.5:0.95等,帮助你了解模型在验证集上的表现。
6. 结果可视化
为了更直观地理解模型的表现,运行以下命令对验证集中的部分图像进行预测并保存结果图像。
# 检测命令python detect.py--weightsruns/train/exp/weights/best.pt--img640--conf0.25--source/path/to/farms/images/val/ --save-txt --save-conf此命令将在runs/detect/exp目录下生成包含边界框和置信度分数的预测图像。
7. 可视化界面
为了创建一个简单的可视化界面,可以使用Flask或Streamlit等工具来展示模型的预测结果。
使用Streamlit创建可视化界面
首先,安装Streamlit:
pipinstallstreamlit然后,创建一个名为app.py的文件:
importstreamlitasstfromPILimportImageimporttorch# 加载训练好的模型model=torch.hub.load('ultralytics/yolov5','custom',path='runs/train/exp/weights/best.pt')st.title("Farms Dataset Object Detection")uploaded_file=st.file_uploader("Choose an image...",type=["jpg","jpeg","png"])ifuploaded_fileisnotNone:image=Image.open(uploaded_file)st.image(image,caption='Uploaded Image.',use_column_width=True)st.write("")st.write("Detecting...")# 进行推理results=model(image)# 显示结果st.image(results.render(),caption='Detected Image.',use_column_width=True)运行Streamlit应用:
streamlit run app.py