ultralytics / yolov3

YOLOv3 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
10.16k stars 3.44k forks source link

Transfer learning/Custom data etc #907

Closed krishnak closed 4 years ago

krishnak commented 4 years ago

I have a query regarding this, most of the transfer learning & custom data training assumes a pre-trained model with 80 classes being already present.

My question is if a model is trained from scratch initially for 10 classes, then the detector in yolo v3 will be NxNx(5+10)*3

If now a 11th class is to be trained, how does the model cope? as the model has only been defined for NxNx45 but the 11th class requires the detector to be NxNx48

glenn-jocher commented 4 years ago

@krishnak I don't understand. You can train any number of classes either:

If you change your dataset in the future, you repeat the steps above, setting the output layers correctly according to the class count of course.

krishnak commented 4 years ago

Please refer here

When you create a model on day 1 with 10 classes and train from scratch

this yolo layer will have a dimension of NxNx(5+10)*number of detectors

You have finished or partially finished training on day 1 with 10 class images

On day 2 if you need to detect a class number 11, how is it done ?

Do you need to regenerate the model and re-run the whole thing again? If model is not modified how can NxNx(5+11)* number of detectors get satisfied.

glenn-jocher commented 4 years ago

@krishnak your question does not make any sense. You can train as many models with however many classes you want, training runs are completely separate and have no relation to each other. https://docs.ultralytics.com/yolov5/tutorials/train_custom_data

krishnak commented 4 years ago

May be you don't see it from where I see it. My question is not related to your code, it is a general implementation question. It is not about training different models with different classes.

If you refer to my question, on day 1 you have already trained a model with 10 classes. but that model has been created with a YOLO layer that can handle only 10 classes (i.e 45 filters) . So this model knows about 10 classes.

If you want to use this model to learn only the 11th class instead of re-learning all the previous 10 classes already learnt, then that involves changing the YOLO layer, this layer will now have 48 filters.

My question is how does this yolo layer load the previous values of the 45 filters that were learnt earlier.

glenn-jocher commented 4 years ago

@krishnak it doesn't work like that. Training the model twice are two separated things, they are not related.

You train with the number of classes in your dataset.

krishnak commented 4 years ago

ok, what you imply is I can't incrementally train the same model so that it detects more classes. Thanks

Lornatang commented 4 years ago

@krishnak Most objects can load pre trained Imagenet weights darknet53.conv.74. Unless you have a special kind of object, you can speed up your training. For example, I can't use the weight of the pre training model in mask detection.

krishnak commented 4 years ago

@Lornatang Thanks

joel5638 commented 4 years ago

@glenn-jocher DO we have to change the "p.requires.grad = False" to True here? or im confused. how do we change these to transfer learn on already generated weight file?

for p in model.parameters(): if opt.prebias and p.numel() == nf: # train (yolo biases) p.requires_grad = True elif opt.transfer and p.shape[0] == nf: # train (yolo biases+weights) p.requires_grad = True else: # freeze layer p.requires_grad = False

glenn-jocher commented 4 years ago

@joel5638 if you want to leverage pretrained weights as a starting point I would simply run this with no changes to the code. Freezing layers will always give you a suboptimal solution, it's only purpose that I can tell is for quick mediocre results or training on extremely limited hardware (mobile devices).

python3 train.py --weights your_weights.pt