GantMan / nsfw_model

Keras model of NSFW detector
Other
1.75k stars 273 forks source link

Finetune error: layer keras_layer expects 262 weights, but the saved weights have 260 elements #77

Open chinalu opened 3 years ago

chinalu commented 3 years ago

I want to feed more samples base on trained model, to do some fine tune. But I got Error:

Traceback (most recent call last): File "make_nsfw_model.py", line 273, in <module> run_main() File "make_nsfw_model.py", line 269, in run_main app.run(main) File "/Users/bill/lab/py/nsfw_model/venv/lib/python3.7/site-packages/absl/app.py", line 300, in run _run_main(main, args) File "/Users/bill/lab/py/nsfw_model/venv/lib/python3.7/site-packages/absl/app.py", line 251, in _run_main sys.exit(main(argv)) File "make_nsfw_model.py", line 197, in main FLAGS.tfhub_module, image_dir, hparams, FLAGS.image_size, FLAGS.saved_model_dir) File "/Users/bill/lab/py/nsfw_model/training/make_nsfw_model_lib.py", line 427, in make_image_classifier model.load_weights(existingWeightsPath) File "/Users/bill/lab/py/nsfw_model/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 234, in load_weights return super(Model, self).load_weights(filepath, by_name, skip_mismatch) File "/Users/bill/lab/py/nsfw_model/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py", line 1222, in load_weights hdf5_format.load_weights_from_hdf5_group(f, self.layers) File "/Users/bill/lab/py/nsfw_model/venv/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 701, in load_weights_from_hdf5_group str(len(weight_values)) + ' elements.') ValueError: Layer #0 (named "keras_layer" in the current model) was found to correspond to layer keras_layer in the save file. However the new layer keras_layer expects 262 weights, but the saved weights have 260 elements.

My command is: python3 make_nsfw_model.py --image_dir /nsfw/training --image_size 224 --saved_model_dir /Users/bill/lab/nsfw/mobilenet_v2_140_224 --labels_output_file /nsfw/mobilenet_v2_140_224/class_labels.txt --tfhub_module https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/4 --tflite_output_file /nsfw/saved_model.tflite --train_epochs 3 --batch_size 32 --do_fine_tuning --learning_rate 0.001 --dropout_rate 0.0 --momentum 0.9

kim333 commented 3 years ago

Same error happening to me. I can fine tune other models like Keras 299x299 Image Model, but I can't make it work on Latest Models Zip (v1.1) The only way I can make this v1.1 version model working is if I drop the option do_fine_tuning but it drops the model weights and starts everything all over from scratch.