Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom model train gives error: You are trying to load a weight file containing 116 layers into a model with 106 layers #737

Closed
sb3114 opened this issue Feb 13, 2022 · 1 comment

Comments

@sb3114
Copy link

sb3114 commented Feb 13, 2022

hi,
i am new to learning AI/ML using imageai, tensorflow etc. I am testing custom training using transfer learning as i have 5 images only ( 3 for train and 2 for test), i get below error. what does this mean really?

below is my system settings:
raspberry Pi OS 64 bullseye
Keras 2.4.3
Keras-Preprocessing 1.1.2
python 3.7.7

My code - same error with resnet50_coco_best_v2.1.0.h5 , resnet50_imagenet_tf.2.0.h5:

(virtenv3.7) pi@pi:~/ml/detection $ cat customtrain.py
from imageai.Classification.Custom import ClassificationModelTrainer
model_trainer = ClassificationModelTrainer()
model_trainer.setModelTypeAsResNet50()
model_trainer.setDataDirectory("mask")
model_trainer.trainModel(num_objects=2, num_experiments=10, enhance_data=True, batch_size=2, show_network_summary=True, transfer_from_model="resnet50_coco_best_v2.0.1.h5", initial_num_objects=1000)
(virtenv3.7) pi@pi:~/ml/detection $

Error:
(virtenv3.7) pi@pi:~/ml/detection $ python customtrain.py Traceback (most recent call last): File "customtrain.py", line 5, in <module> model_trainer.trainModel(num_objects=2, num_experiments=10, enhance_data=True, batch_size=2, show_network_summary=True, transfer_from_model="resnet50_coco_best_v2.0.1.h5", initial_num_objects=1000) File "/home/pi/virtenv3.7/lib/python3.7/site-packages/imageai/Classification/Custom/__init__.py", line 225, in trainModel include_top=False, pooling="avg") File "/home/pi/virtenv3.7/lib/python3.7/site-packages/tensorflow/python/keras/applications/resnet.py", line 472, in ResNet50 input_tensor, input_shape, pooling, classes, **kwargs) File "/home/pi/virtenv3.7/lib/python3.7/site-packages/tensorflow/python/keras/applications/resnet.py", line 221, in ResNet model.load_weights(weights) File "/home/pi/virtenv3.7/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2234, in load_weights hdf5_format.load_weights_from_hdf5_group(f, self.layers) File "/home/pi/virtenv3.7/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 688, in load_weights_from_hdf5_group ' layers.') ValueError: You are trying to load a weight file containing 116 layers into a model with 106 layers. (virtenv3.7) pi@pi:~/ml/detection $

@sb3114
Copy link
Author

sb3114 commented Feb 18, 2022

changed to using DetectionModelTrainer instead of Classification and used yolo model which solved the issue. Not sure why
`from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="./imageai/data/")
trainer.setTrainConfig(object_names_array=["with_mask", "without_mask","mask_weared_incorrect"],
batch_size=8,
num_experiments=10,
train_from_pretrained_model="pretrained-yolov3.h5")

trainer.trainModel()
`

@sb3114 sb3114 closed this as completed Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant