ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.32k stars 16.24k forks source link

How to customize file model.yaml ? #7718

Closed hoangkhoiLE closed 2 years ago

hoangkhoiLE commented 2 years ago

Search before asking

Question

Hello,

I have some idea about changing the head or the backbone for some use case specific. For example, in my dataset, there is 0 large object, so I would like to remove the layer P5 in head and backbone for this use case with the target to train model concentrating on another kind of object.

https://github.com/ultralytics/yolov5/blob/master/models/hub/yolov5-p2.yaml

But I'm not sure which line to remove in the file model.yaml. Please correct me if I'm wrong. In my guess, for backbone that would be line 19 and 20

[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 [-1, 3, C3, [1024]],

In head that would be line 49, 50, 51

[-1, 1, Conv, [512, 3, 2]], [[-1, 10], 1, Concat, [1]], # cat head P5 [-1, 3, C3, [1024, False]], # 30 (P5/32-large)

I would like to know more the principe of changing file model yaml too ! Please help me to do that if there is document about it !

If this approach worked well, do I need to change the calculation of autoanchor ? The calculation of autoanchor is adapting with only file model.yaml in https://github.com/ultralytics/yolov5/tree/master/models or it works with https://github.com/ultralytics/yolov5/tree/master/models/hub too or all kind of file model.yaml ?

Thank a lot for your support ! Best regards

Additional

No response

glenn-jocher commented 2 years ago

@hoangkhoiLE 👋 Hello! Thanks for asking about model anchors. YOLOv5 🚀 uses a new Ultralytics algorithm called AutoAnchor for anchor verification and generation before training starts.

Autoanchor will analyse your anchors against your dataset and training settings (like --img-size), and will adjust your anchors as necessary if it determines the original anchors are a poor fit, or if an anchor count was specified in your model.yaml rather than anchor values, i.e.

# Specify anchor count (per layer)
anchors: 3

# --OR-- Specify anchor values manually
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

When generating new anchors, autoanchor first applies a kmeans function against your dataset labels (scaled to your training --img-size), and uses kmeans centroids as initial conditions for a Genetic Evolution (GE) algorithm. The GE algorithm will evolve all anchors for 1000 generations under default settings, using CIoU loss (same regression loss used during training) combined with Best Possible Recall (BPR) as its fitness function.

Notebook example: Open In Colab Open In Kaggle

Screenshot 2022-03-24 at 10 58 28

No action is required on your part to use autoanchor. If you would like to force manual anchors for any reason, you can skip autoanchor with the --noautoanchor flag:

python train.py --noautoanchor

For more details on AutoAnchor see: https://github.com/ultralytics/yolov5/blob/master/utils/autoanchor.py

Good luck 🍀 and let us know if you have any other questions!

glenn-jocher commented 2 years ago

@hoangkhoiLE you can use this yaml directly to train a YOLOv5l model with only P3 and P4 outputs (no P5): https://github.com/ultralytics/yolov5/blob/master/models/hub/yolov5-p34.yaml

hoangkhoiLE commented 2 years ago

@glenn-jocher thank for your answer very detail !

Can you explain me the final line of "head" in file https://github.com/ultralytics/yolov5/blob/master/models/hub/yolov5-p34.yaml: [ [ 17, 20 ], 1, Detect, [ nc, anchors ] ], # Detect(P3, P4) which is different from https://github.com/ultralytics/yolov5/blob/master/models/yolov5l.yaml

As I understand we remove "23" at this line. So this mean 17, 20, 23 match with P3, P4, P5 one by one. But I take a look at another file https://github.com/ultralytics/yolov5/blob/master/models/hub/yolov5-p2.yaml, we have [[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(P2, P3, P4, P5) There is a difference. So how do I get this number to customize the head part ( for ex: another random combination of P2,P3,P4,P5) ?

Thank in advance ! Best regard

glenn-jocher commented 2 years ago

@hoangkhoiLE you can pass any layer to Detect. The layers passed correspond to the YOLOv5 architecture I've designed.

hoangkhoiLE commented 2 years ago

@glenn-jocher so can you please explain the logic behind the list [21, 24, 27, 30] at the final line in the file https://github.com/ultralytics/yolov5/blob/master/models/hub/yolov5-p2.yaml

and the list [17, 20, 23] # Detect(P3, P4, P5) at the final line in the file https://github.com/ultralytics/yolov5/blob/master/models/yolov5s.yaml

It would help me to understand more !

Thank in advance ! Best regard

glenn-jocher commented 2 years ago

These are are the indices that input the last layers for strides 8, 16, 32, and 64

hoangkhoiLE commented 2 years ago

Where I can find these indices ? Please help me to know it ! Big thanks !

glenn-jocher commented 11 months ago

@hoangkhoiLE you can find these indices in the modules dictionary of the model.yaml file. Each entry specifies the layer index for a particular desired feature map.