How to save a YOLOv8 model after some training on a custom dataset to continue the training later?

huangapple go评论94阅读模式
英文:

How to save a YOLOv8 model after some training on a custom dataset to continue the training later?

问题

我在Colab上训练自定义数据集的YOLOv8。如何在一些时期后保存模型并在稍后继续训练。我像这样做了第一个时期:

import torch

model = YOLO("yolov8x.pt")
model.train(data="/image_datasets/Website_Screenshots.v1-raw.yolov8/data.yaml", epochs=1)


在寻找选项时,似乎YOLOv5可以保存模型或权重字典。我尝试了这些,但在这种情况下,保存或加载似乎无效:

torch.save(model, 'yolov8_model.pt')
torch.save(model.state_dict(), 'yolov8x_model_state.pt')

英文:

I'm training YOLOv8 in Colab on a custom dataset. How can I save the model after some epochs and continue the training later. I did the first epoch like this:

import torch

model = YOLO("yolov8x.pt")
model.train(data="/image_datasets/Website_Screenshots.v1-raw.yolov8/data.yaml", epochs=1)

While looking for the options it seems that with YOLOv5 it would be possible to save the model or the weights dict. I tried these but either the save or load doesn't seem to work in this case:

torch.save(model, 'yolov8_model.pt')
torch.save(model.state_dict(), 'yolov8x_model_state.pt')

答案1

得分: 1

"我目前正在使用 YOLOv8 进行项目开发。在用自定义数据集训练后,最佳权重会自动存储在 runs/detect/train/weights 目录下,命名为 best.pt。当我重新训练模型时,我会使用 best.pt 来代替 yolov8x.pt 来训练模型。"

英文:

"I am currently working on a project using YOLOv8.
After training on a custom dataset, the best weight is automatically stored in the runs/detect/train/weights directory as best.pt. When I retrain the model, I use the best.pt weight instead of yolov8x.pt to train the model."

答案2

得分: 0

我认为只需将"yolov8x.pt"替换为您训练好的模型即可
训练好的模型将保存在工作目录中的results/run文件夹中。

英文:

I think just replacing the "yolov8x.pt" to your trained model will do the work
The trained model will be saved in the results/run folder in the working dir.

model = YOLO('yolov8x.yaml').load('yolov8x.pt') # build from YAML and transfer weights
model.train(data="/image_datasets/Website_Screenshots.v1-raw.yolov8/data.yaml", epochs=1)

check this -> https://docs.ultralytics.com/modes/train/

答案3

得分: 0

问题是runs/detect不是始终可用。例如,如果超过了GPU限制,环境将停止并移除GPU后端,重新启动后,挂载到驱动器时将找不到runs目录。该问题在yolov5中通过save_dir参数解决,但对于yolov8,我找到的唯一解决方案是将训练时期划分,以确保不超过使用限制,并在我的驱动器中备份runs目录。


结果是,我可以基于备份目录中的best.ptlast.pt进行预测或继续训练。

英文:

Well the problem is that runs/detect is not always available.
For example if you exceed GPU limit the environment will stop and remove the GPU backend, after restarting you won't find runs directory when mounting to the drive. The problem is solved in yolov5 with save_dir parameter but for yolov8 the only solution that I found is dividing the training epochs so that usage limits won't be reached and I make a backup of runs directory in my drive.


As a result, I can make my predictions or continue training based on the best.pt or the last.pt from the backup directory.

huangapple
  • 本文由 发表于 2023年4月11日 16:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定