数据集目录结构在这里插入图片描述

深色版本
VLSDD/
├── images/
│ ├── train/
│ └── val/
├── labels/
│ ├── train/
│ └── val/
└── data.yaml
数据集配置文件
创建一个data.yaml文件,配置数据集的路径和类别信息:
在这里插入图片描述

yaml
深色版本
path: ./VLSDD # 数据集路径
train: images/train # 训练集图像路径
val: images/val # 验证集图像路径
在这里插入图片描述
在这里插入图片描述

nc: 7 # 类别数
names: [‘Cruise Ship’, ‘Bulk Carrier’, ‘Military Ship’, ‘Sailboat’, ‘Kayak’, ‘Container Ship’, ‘Fishing Boat’] # 类别名称
YOLOv8训练代码
安装YOLOv8库和依赖项:
bash
深色版本
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics
pip install -r requirements.txt
训练模型:
python
深色版本
from ultralytics import YOLO
在这里插入图片描述

def train_model(data_yaml_path, model_config, epochs, batch_size, img_size):
# 加载模型
model = YOLO(model_config)

# 训练模型
results = model.train(
    data=data_yaml_path,
    epochs=epochs,
    batch=batch_size,
    imgsz=img_size
)

# 保存模型
model.save("runs/train/visible_light_ship_detection/best.pt")

if name == “main”:
data_yaml_path = ‘VLSDD/data.yaml’
model_config = ‘yolov8s.yaml’
epochs = 100
batch_size = 16
img_size = 640

train_model(data_yaml_path, model_config, epochs, batch_size, img_size)

详细解释
安装YOLOv8和依赖项:
克隆YOLOv8仓库并安装所有必要的依赖项。
训练模型:
导入YOLOv8库。
加载模型配置文件。
调用model.train方法进行训练。
保存训练后的最佳模型。
运行训练脚本
将上述脚本保存为一个Python文件(例如train_yolov8_vlsdd.py),然后运行它。

bash
深色版本
python train_yolov8_vlsdd.py
评估模型
评估模型:
python
深色版本
from ultralytics import YOLO

def evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold):
# 加载模型
model = YOLO(weights_path)

# 评估模型
results = model.val(
    data=data_yaml_path,
    imgsz=img_size,
    conf=conf_threshold
)

# 打印评估结果
print(results)

if name == “main”:
data_yaml_path = ‘VLSDD/data.yaml’
weights_path = ‘runs/train/visible_light_ship_detection/best.pt’
img_size = 640
conf_threshold = 0.4

evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold)

详细解释
评估模型:
导入YOLOv8库。
加载训练好的模型权重。
调用model.val方法进行评估。
打印评估结果。
运行评估脚本
将上述脚本保存为一个Python文件(例如evaluate_yolov8_vlsdd.py),然后运行它。

bash
深色版本
python evaluate_yolov8_vlsdd.py
一键运行脚本
为了实现一键运行,可以将训练和评估脚本合并到一个主脚本中,并添加命令行参数来控制运行模式。

python
深色版本
import argparse
from ultralytics import YOLO

def train_model(data_yaml_path, model_config, epochs, batch_size, img_size):
# 加载模型
model = YOLO(model_config)

# 训练模型
results = model.train(
    data=data_yaml_path,
    epochs=epochs,
    batch=batch_size,
    imgsz=img_size
)

# 保存模型
model.save("runs/train/visible_light_ship_detection/best.pt")

def evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold):
# 加载模型
model = YOLO(weights_path)

# 评估模型
results = model.val(
    data=data_yaml_path,
    imgsz=img_size,
    conf=conf_threshold
)

# 打印评估结果
print(results)

def main(mode):
data_yaml_path = ‘VLSDD/data.yaml’
model_config = ‘yolov8s.yaml’
epochs = 100
batch_size = 16
img_size = 640
conf_threshold = 0.4

if mode == 'train':
    train_model(data_yaml_path, model_config, epochs, batch_size, img_size)
elif mode == 'eval':
    weights_path = 'runs/train/visible_light_ship_detection/best.pt'
    evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold)
else:
    print("Invalid mode. Use 'train' or 'eval'.")

if name == “main”:
parser = argparse.ArgumentParser(description=“Train or evaluate YOLOv8 on the VLSDD dataset.”)
parser.add_argument(‘mode’, type=str, choices=[‘train’, ‘eval’], help=“Mode: ‘train’ or ‘eval’”)
args = parser.parse_args()

main(args.mode)

详细解释
命令行参数:
使用argparse库添加命令行参数,控制脚本的运行模式(训练或评估)。
主函数:
根据传入的模式参数,调用相应的训练或评估函数。
运行主脚本
将上述脚本保存为一个Python文件(例如main_yolov8_vlsdd.py),然后运行它。

训练模型
bash
深色版本
python main_yolov8_vlsdd.py train
评估模型
bash
深色版本
python main_yolov8_vlsdd.py eval
总结
通过以上步骤,你可以准备好可见光船舶数据集,并使用YOLOv8进行训练和评估。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐