keras icon indicating copy to clipboard operation
keras copied to clipboard

ValueError: Unknown layer: PruneLowMagnitude. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

Open MiraGaikwad opened this issue 3 years ago • 8 comments

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): I have used pre-trained model for InceptionNetV3 and last layer was train and then doing pruning on it.
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Google colab
  • TensorFlow installed from (source or binary): on colab
  • TensorFlow version (use command below): 2.8
  • Python version: Python 3.7.13
  • Bazel version (if compiling from source):
  • GPU model and memory: Colab Pro +
  • Exact command to reproduce:

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the problem.

  1. I have used pre-trained model for InceptionNetV3 and only trained last layer. I could save load that model.
  2. I then pruned that model and saved. But when I tried loading thar model I got error as

'ValueError: Unknown layer: PruneLowMagnitude. Please ensure this object is passed to the custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.'

It worked well on MobileNetV2, InceptionResNet but not working on VGG19 and InceptionnetV3

I have used below code to prun the model

import tensorflow_model_optimization as tfmot prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude #Compute end step to finish pruning after 2 epochs. batch_size = 10 epochs = 2

end_step = np.ceil(train_imgs.shape[0]//batch_size).astype(np.int32) * epochs

#Define model for pruning. pruning_params = { 'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.50, final_sparsity=0.80, begin_step=0, end_step=end_step) }

model_for_pruning = prune_low_magnitude(InceptionnetV3_model_base, **pruning_params) #prune_low_magnitude requires a recompile. model_for_pruning.compile(optimizer='adam', # loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), loss=['categorical_crossentropy'], metrics=['accuracy'])

model_for_pruning.summary()

Describe the problem clearly here. Be sure to convey here why it's a bug in Keras or why the requested feature is needed.

Describe the current behavior.

Describe the expected behavior.

Contributing.

  • Do you want to contribute a PR? (yes/no):
  • If yes, please read this page for instructions
  • Briefly describe your candidate solution(if contributing):

Standalone code to reproduce the issue.

Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook.

Source code / logs.

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

MiraGaikwad avatar Aug 06 '22 02:08 MiraGaikwad

@MiraGaikwad, I ran the code and faced a different error, please find the gist here and share all dependencies to replicate the issue or share a colab gist with the reported error. Thank you!

tilakrayal avatar Aug 08 '22 10:08 tilakrayal

@tilakrayal Could you please share your email id ? I can give access to google drive where I have dataset to replicate the process ?

Cheers Mira

MiraGaikwad avatar Aug 10 '22 23:08 MiraGaikwad

@MiraGaikwad, Could you please attach the code here or provide the colab gist to reproduce the issue. Thank you!

tilakrayal avatar Aug 12 '22 10:08 tilakrayal

@tilakrayal - Please find the attached code -

after the pruned code while loading pruned model you can see the error in below fine - ( Pruned_Formatted_InceptionnetV3_01.zip )

Dataset I used is from this link - https://www.kaggle.com/code/brsdincer/land-use-scene-classification-all-process/data

MiraGaikwad avatar Aug 13 '22 01:08 MiraGaikwad

@gadagashwini, I was able to reproduce the issue on tensorflow v2.8, v2.9. Kindly find the gist of it here.

tilakrayal avatar Aug 17 '22 09:08 tilakrayal

@LukeWood , @rchao , @tilakrayal Could you please let me know if there is any update on this issue ?

MiraGaikwad avatar Sep 08 '22 22:09 MiraGaikwad

Thanks for checking in @MiraGaikwad - if you could re-build tensorflow_model_optimization library, can you follow what the error message suggests, "Please ensure this object is passed to the custom_objects argument" and see if it helps in any way?

rchao avatar Sep 08 '22 22:09 rchao

@rchao - Yeah, I already tried that but no luck..

MiraGaikwad avatar Sep 08 '22 22:09 MiraGaikwad