OlafenwaMoses / ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
https://www.genxr.co/#products
MIT License
8.48k stars 2.18k forks source link

Pretrained weight loading failed #792

Closed LOActualControl closed 1 year ago

LOActualControl commented 1 year ago

Hi - not sure if this is, strictly speaking, a bug, but I noticed that when attempting to train a custom model from a pre-trained YoloV3 model, the terminal output notes that loading the weights from the pretrained model failed. The model goes on to train successfully, but I assume not as accurately as if the pretrained weight loading was successful. The terminal output and my code are below:

Terminal output:

`Generating anchor boxes for training images... thr=0.25: 1.0000 best possible recall, 8.84 anchors past thr n=9, img_size=416, metric_all=0.637/0.896-mean/best, past_thr=0.645-mean: pretrained weight loading failed. Defaulting to using random weight.

Pretrained YOLOv3 model loaded to initialize weights

Epoch 1/200

Train: 49it [00:35, 1.38it/s] box loss-> 0.08040, object loss-> 0.25415, class loss-> 0.00000 Validation: 25it [01:31, 3.66s/it] recall: 0.139568 precision: 0.202396 mAP@0.5: 0.095452, mAP@0.5-0.95: 0.017927 `

`from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer() trainer.setModelTypeAsYOLOv3() trainer.setDataDirectory(data_directory="/content/gdrive/MyDrive/shipspotting_revision/rostock") trainer.setTrainConfig(object_names_array=["ship"], batch_size=16, num_experiments=200, train_from_pretrained_model="/content/gdrive/MyDrive/shipspotting_revision/yolov3.pt") trainer.trainModel()`

I'm working in a Colab notebook, by the way. Thanks for any help you can provide!

Edwin-Aguirre92 commented 1 year ago

Hi LOActualControl,

I'm also getting the same error. What is weird though is that the code says "Defaulting to using random weight", but then it says "Pretrained Yolov3 model loaded to initialize weights" . Have you gotten any further regarding this issue ?

Edwin-Aguirre92 commented 1 year ago

Actually, I checked the banckend code and it prints that when this happens: image Which leads me to believe that when you load the model state using the load model function, the model does not have the right amount of weights on it ? Or something along these lines.

What do you think ? Any help would be appreciated

Edwin-Aguirre92 commented 1 year ago

Actually I fix the issue, the issue for me was that the path that I had put in the train_from_pretrained_model method, in your case ("/content/gdrive/MyDrive/shipspotting_revision/yolov3.pt" ), did not match with where my actual yolov3.pt file path was located. Hopefully this helps you out !

Best,

Edwin Aguirre

Yodeman commented 1 year ago

Actually, I checked the banckend code and it prints that when this happens: image Which leads me to believe that when you load the model state using the load model function, the model does not have the right amount of weights on it ? Or something along these lines.

What do you think ? Any help would be appreciated

Hi... Since care has been taken to only load weights of modules that are both present in the weights to be loaded and the weights of the model i.e. new_state_dict = {k:v for k,v in state_dict.items() if k in self.__model.state_dict().keys() and v.shape==self.__model.state_dict()[k].shape} the only ways for weight loading to fail are:

But from the code, I think what the maintainer was trying to achieve was to provide a message if the weights were loaded successfully and also if weight loading failed, the only issue is that the code responsible for successful weight loading message is placed at the wrong place (i.e. outside the try block), causing it to be printed whether the weights are successfully loaded or not.

797 PR tries to fix this.

OlafenwaMoses commented 1 year ago

797 has been merged and this issue addressed.