Comments (12)
Your imports look fine to me. Check the type of the model you generated: type(my_model) and lmk what the output is, it should be a tf.keras Model - it cannot be a Sequential Model, you need to convert it. This source code explains why you're getting a NoneType returned:
if isinstance(to_prune, list):
return _prune_list(to_prune, **params)
elif isinstance(to_prune, keras.Model):
return keras.models.clone_model(
to_prune, input_tensors=None, clone_function=_add_pruning_wrapper)
elif isinstance(to_prune, keras.layers.Layer):
params.update(kwargs)
return pruning_wrapper.PruneLowMagnitude(to_prune, **params)
Take a look at: https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/python/core/sparsity/keras/prune.py
Btw, just outta curiosity what do you plan on using pruning for? With this package as it is, you're only going to save footprint size when using a compression utility (e.g gzip). You're also not going to obtain any faster inference time, unless you've enabled some sort of meta parameter on your hardware device.
from model-optimization.
You can convert it like this:
from keras import layers, models
input_layer = layers.Input(batch_shape=seqmodel.layers[0].input_shape)
prev_layer = input_layer
for layer in seqmodel.layers:
prev_layer = layer(prev_layer)
funcmodel = models.Model([input_layer], [prev_layer])
from model-optimization.
Your imports look fine to me. Check the type of the model you generated: type(my_model) and lmk what the output is, it should be a tf.keras Model - it cannot be a Sequential Model, you need to convert it. This source code explains why you're getting a NoneType returned:
if isinstance(to_prune, list): return _prune_list(to_prune, **params) elif isinstance(to_prune, keras.Model): return keras.models.clone_model( to_prune, input_tensors=None, clone_function=_add_pruning_wrapper) elif isinstance(to_prune, keras.layers.Layer): params.update(kwargs) return pruning_wrapper.PruneLowMagnitude(to_prune, **params)
Take a look at: https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/python/core/sparsity/keras/prune.py
Btw, just outta curiosity what do you plan on using pruning for? With this package as it is, you're only going to save footprint size when using a compression utility (e.g gzip). You're also not going to obtain any faster inference time, unless you've enabled some sort of meta parameter on your hardware device.
This revealed the problem to me. I was returning my model incorrectly. I was not using the getter method in my custom class that contained my model. Thank you so much!
from model-optimization.
How do you solve it? I encounter the similar problem and could you help me?
from model-optimization.
Make sure you're using Keras from Tensorflow-Python. Try this:
from tensorflow.python import keras
# Important: TensorFlow-Optimization uses this version of Keras. And then from tensorflow_model_optimization.sparsity import keras as sparsity
. Your model must be built using the right version of Keras, otherwise it will return a Null model as per their source code. Let me know if this works, I had a similar issue a while ago.
from model-optimization.
Make sure you're using Keras from Tensorflow-Python. Try this:
from tensorflow.python import keras
# Important: TensorFlow-Optimization uses this version of Keras. And thenfrom tensorflow_model_optimization.sparsity import keras as sparsity
. Your model must be built using the right version of Keras, otherwise it will return a Null model as per their source code. Let me know if this works, I had a similar issue a while ago.
This did not seem to work for me. What version of Keras is considered correct?
from model-optimization.
I am trying to prune a trained model , but model is saved a model_architecture.json and weights.h5 and model is loaded as follows:
with open(modeljson) as f:
self.model = model_from_json(f.read())
self.model.load_weights(modelfile)But whenever I try to prune it the output from:
new_pruned_model = sparsity.prune_low_magnitude(model, **new_pruning_params)
is always none so it crashes at the model.compile part as type none has no attribute compile
Any help please
Did you ever find a solution to this problem?
from model-optimization.
considered
You want to be using tf.keras for all sequential models. from tensorflow.python import keras
, this effectively does the same thing, it's up to you as to whether you want to be explicit or not. What in specific isn't working for you? Are you getting a NoneType returned from the prune_low_magnitude
wrapper?
from model-optimization.
Yes, I am getting a NoneType returned from prune_low_magnitude.
Here is my output:
[info] applying pruning techniques to the provided model Traceback (most recent call last): File "train.py", line 72, in <module> main() File "train.py", line 55, in main student = prune(teacher, X_train, Y_train, X_test, Y_test, len(X_train), 256, 100, logdir) File "PrunePipeline.py", line 10, in prune studentOne = sparsePrune(model, X_train, Y_train, X_test, Y_test, numTrainingSamples, batchSize, epochs, logdir) File "PrunePipeline.py", line 24, in sparsePrune new_model.compile( AttributeError: 'NoneType' object has no attribute 'compile'
Here is the pruning code I am working with:
end_step = np.ceil(1.0 * num_train_samples / batch_size).astype(np.int32) * epochs new_pruning_params = { 'pruning_schedule': sparsity.PolynomialDecay(initial_sparsity=0.01, final_sparsity=0.1, begin_step=0, end_step=end_step, frequency=100) } new_model = sparsity.prune_low_magnitude(model, **new_pruning_params) new_model.compile( loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
When putting a breakpoint on the compile, where I am getting the error, I see that the new_model is None.
These are the imports I am using for the model I am training and attempting to prune:
import os import tensorflow as tf from tensorflow.python.keras.layers import MaxPooling2D, Dropout, Dense, Flatten, Activation, Conv2D from tensorflow.python.keras.models import Model, Sequential, model_from_json import numpy as np from HelperUtil import HelpfulFunctions import datetime helpful = HelpfulFunctions()
Is this the incorrect way to go about it?
from model-optimization.
@blakeedwards823 : it works with pruning a Sequential model. Sequential is an instance of keras.Model (see these docs).
from model-optimization.
The pruned model type is <class 'NoneType'>
Hence, it does not work with other calls such as complie or fit or evaluate.
Suggest any solution.
from model-optimization.
Closing this issue since Blake has found a resolution here.
From my guesses, Blake did something effectively like this:
model_wrapper = ModelWrapper()
model_wrapper.model = model_from_json(...)
sparsity.prune(model_wrapper)
instead of
sparsity.prune(model_wrapper.model)
In other words, the actual Keras model object was not passed. I have filled an issue to add an error message when a Keras model object is not passed.
@blakeedwards823 : if I'm wrong, please correct me and share the before/after code that demonstrates your resolution.
@bothrasumit: let's move the discussion to your issue and see if Blake's problem is the same one you ran into from his response.
from model-optimization.
Related Issues (20)
- QAT aware training for mobilenetV2 not working HOT 1
- Determinism is not yet supported in GPU implementation of FakeQuantWithMinMaxVarsGradient HOT 2
- batch norm layer quantization error HOT 2
- 16x8 Quantization fails for RNN model - Max and min for dynamic tensors should be recorded during calibration HOT 4
- float16 quantization runs out of memory for LSTM model HOT 3
- float16 quantization runs out of memory for LSTM model HOT 1
- Add a default PruningPolicy that filters out any layers not supported by the API HOT 1
- Add batch norm to default_n_bit_quantize_registry and default_8_bit_quantize_registry HOT 2
- Quant aware training in tensorflow model optimization HOT 4
- [COLAB] No module named 'tensorflow_model_optimization' HOT 1
- Module Import Error HOT 2
- A error about quantization aware training HOT 2
- MobileNetV3 QAT TFLite Conversion Issue HOT 4
- Error in MovingAverageQuantizer with per_axis=True due to missing parameters in _add_range_weights
- strange behavior when quantizing a model. HOT 2
- Support for Recurrent layers for Quantization Aware Training. HOT 1
- Can't use TFMOT version 0.8.0 due to missing dependency HOT 1
- Any plans to support keras3? HOT 3
- Cannot use Quantize layer and use abstract class and methods HOT 1
- Custom layer with Concat afterwards causes an error during QAT modeling HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from model-optimization.