ultralytics / ultralytics

NEW - YOLOv8 πŸš€ in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
25.95k stars 5.17k forks source link

How many layers is the backbone in the segmentation model of YoloV8? #13180

Closed nachoogriis closed 1 day ago

nachoogriis commented 1 month ago

Search before asking

Question

I am working in a person segmentation problem with a personal dataset. To obtain good results I want to do some fine-tuning. As the amount of data I have is not extremely high, I would want to perform fine-tuning while freezing some layers.

For freezing, I was considering to freeze the backbone, while fine-tuning the heads. My questions are:

  1. Which has to be the value of the freeze parameter in training in order to freeze the backbone of the model? The freeze parameter freezes the first $N$ layers.

  2. Is there any better place to place the freezing. This is, which might be a good value for the freeze parameter?

Additional

No response

github-actions[bot] commented 1 month ago

πŸ‘‹ Hello @nachoogriis, thank you for your interest in Ultralytics YOLOv8 πŸš€! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 month ago

@nachoogriis hello,

Thank you for your question! The backbone in the YOLOv8 segmentation model consists of 12 layers. To freeze the backbone during fine-tuning, you can set the freeze parameter to 12. This will freeze the first 12 layers, allowing you to fine-tune the head layers.

Here's an example of how to set the freeze parameter:

yolo segment train data=your_dataset.yaml model=yolov8n-seg.pt epochs=100 freeze=12

Freezing the backbone while fine-tuning the head layers is a common approach, especially when working with a limited amount of data. It helps in retaining the learned features from the backbone while adapting the head layers to your specific dataset.

If you have any further questions or need additional assistance, feel free to ask. Happy training! πŸš€

nachoogriis commented 1 month ago

Thank you so much. Moreover, I have been trying to train the YoloV8-seg model (the same than in the previous question) on my new dataset. To work with different experiments, I wanted to try to execute, at first, the model with random weight inizialization. I have done the following code.

model_file_path = "/kaggle/input/segmentation-models/yolov8m-seg.pt"
config_file_path = "/kaggle/input/config-data/config.yaml"
hyps_file_path = "/kaggle/input/config-data/hyps.yaml"

model = YOLO(model_file_path)  

model.train(data = config_file_path, epochs = 150, patience = 50, imgsz = 640, pretrained = False, name = "YOLO random weights base hyps")

However, this gives exactly the same results than executing the model with the pretrained weights. The code done to execute pretrained weights is the following.

model_file_path = "/kaggle/input/segmentation-models/yolov8m-seg.pt"
config_file_path = "/kaggle/input/config-data/config.yaml"
hyps_file_path = "/kaggle/input/config-data/hyps.yaml"

model = YOLO(model_file_path)  

model.train(data = config_file_path, epochs = 150, patience = 50, imgsz = 640, name = "YOLO random weights base hyps")

What can be happening? Are both the pretrained version or are both the random version?

glenn-jocher commented 1 month ago

Hello @nachoogriis,

Thank you for reaching out with your query. It seems there might be a misunderstanding in how the model initialization is handled in your code.

When you specify pretrained=False, it should indeed initialize the model with random weights rather than loading pretrained weights. If you're observing that the results are the same in both cases, it's possible that the model is not correctly recognizing the pretrained flag due to how the model is instantiated.

To ensure that the model starts with random weights, you might want to explicitly load a model configuration file (YAML) rather than a pre-trained model file (PT). Here’s how you can modify your code:

model = YOLO('path/to/model.yaml')  # Ensure this is a YAML file, not a PT file

model.train(data=config_file_path, epochs=150, patience=50, imgsz=640, pretrained=False, name="YOLO random weights base hyps")

This approach ensures that the model is initialized as defined in the YAML configuration, without loading any pretrained weights. Please try this and let me know if it resolves the issue!

github-actions[bot] commented 1 week ago

πŸ‘‹ Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO πŸš€ and Vision AI ⭐