Tensorflow: How to load an object detection model and viewing the model's architecture? - tensorflow

I am trying to load a object detection model and viewing the architecture because I need to know what my input and output layers are in order to convert the model format to a different format.
Right now, I am trying to do:
model = tf.saved_model.load('/content/drive/MyDrive/my_ssd_mobnet_640x640_tuned/tfliteexport/saved_model')
model.summary()
And am getting this error:
AttributeError Traceback (most recent call last)
<ipython-input-18-5f15418b3570> in <module>()
----> 1 model.summary()
AttributeError: '_UserObject' object has no attribute 'summary'
My model is in .pb, but I do also have a .tflite version as well.

According to the documentation, you should load the model as keras model, like this:
model = keras.models.load_model("my/path/saved_model")
The .pb version should work.

Related

load TFlite model and quantizate

I would like to load a tflite model and quantize it but seems the model that is loaded does not have the right object class attributes.
here is my code
interpreter = tf.lite.TFLiteConverter.from_saved_model('efficientdet_d0_coco17_tpu-32/saved_model')
config = QuantizationConfig.for_float16()
interpreter.export(export_dir='.', tflite_filename='x_model_fp16.tflite', quantization_config=config)
error
AttributeError Traceback (most recent call last)
<ipython-input-14-19596da94f3d> in <module>
----> 1 interpreter.export(export_dir='.', tflite_filename='/content/drive/Othercomputers/intel16/botri/efficient_net/x_model_fp16.tflite', quantization_config=config)
AttributeError: 'TFLiteSavedModelConverterV2' object has no attribute 'export'
I also have tried to load tflite format file but the error stays the same.

Can not load saved model in keras / tensorflow?

I trained the model using autokeras with TensorFlow 2.5.
I saved the pre-trained model using both methods explained on Keras (TensorFlow) home page.
model.save(f'model_auto_keras{max_trials}.h5') model.save("keras_test_save_model")
again when I want to load the saved model using
model = tf.keras.models.load_model(f'model_auto_keras{max_trials}.h5')
and
model1 = tf.keras.models.load_model("keras_test_save_model/")
both methods are not doing well in my case.
saying ValueError: Unknown layer: Custom>
ValueError
ValueError: Unknown layer: Custom>MultiCategoryEncoding.
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.
the main problem is Custom layer >> MultiCategoryEncoding which is not available in keras.
RuntimeError
#krishna
You can try:
model = tf.keras.models.load_model('model.h5', custom_objects={'CategoryLayerName': tf.keras.layers.CategoryEncoding()})
In your model declaration use layer name for CategoryEncoding layer.
I'm not sure if it should be tf.keras.layers.CategoryEncoding() or tf.keras.layers.CategoryEncoding

What to use in place of predict_classes() in Tensorflow 2.7.0?

I am trying to train an NLP Neural Machine Translation model and in that code I'm using sequential model of Keras. I want to predict the output in the form of classes but as i am using Tensorflow 2.7.0 and the predict_classes() has now been depreciated, how should i go around it? Here's the code snippet -:
model = load_model('model.h1.24_jan_19')
preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))
And here's the error that i'm getting -:
AttributeError Traceback (most recent call last)
in ()
1 model = load_model('model.h1.24_jan_19')
----> 2 preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))
AttributeError: 'Sequential' object has no attribute 'predict_classes'
I dont think there is a direct replacement, you need to use the output of model.predict() and manually map them onto a list of class labels, then pick the one with highest confidence.
A simple example:
classes = ['a', 'b']
results = model.predict(testX.reshape((testX.shape[0],testX.shape[1])))
# the results here will be a n array of confidence level, if the last layer of your model uses the softmax activation function.
class_predicted = classes[np.argmax(predictions)] # this line gets the index of the highest confidence class and select from the classes list.
Beware that the order of labels in the classes list must be same as your input label, so you should double check on that. If you are using tensorflow APIs like tf.data.dataset, then you can use the dataset.class_names attribute to access the class list.

Custom optimiser implementation in Keras

I am trying to use a custom optimiser to train a NN in Keras. The original algorithm has been developed to train CNNs and is based on using different adaptive learning rates for every weight of the network. The algorithm is called WAME (weight-wise adaptive learning rates with moving average estimator).
The optimiser has been developed by a former University of London student and can be found in this GitHub repo (lines 54 to 153).
As you can see in the code, it is built as a subclass of the optimizer_v2 superclass available in TensorFlow. This class is called WAMEprop.
What I am trying to do is simply:
pasting the code that defines the class into my Google Colab notebook
using the new optimizer as follows:
#building the model
wame_model = models.Sequential()
wame_model.add(layers.Dense(32, activation='relu', input_shape=(11,)))
wame_model.add(layers.Dense(32, activation='relu'))
wame_model.add(layers.Dense(1))
#compiling the model using WAMEprop as optimizer
wame_model.compile(optimizer=WAMEprop(name='wame'), loss='mse', metrics=['mae'])
#fitting the model
history = wame_model.fit(train_features, train_targets,
validation_data=(test_features, test_targets),
epochs=50, batch_size=1, verbose=1)
Now, I get the following error:
ValueError Traceback (most recent call last)
<ipython-input-55-a13c54fc61f2> in <module>()
5 wame_model.add(layers.Dense(32, activation='relu'))
6 wame_model.add(layers.Dense(1))
----> 7 wame_model.compile(optimizer=WAMEprop(name='wame'), loss='mse', metrics=['mae'])
8
9 history = wame_model.fit(white_train_features_df_s.to_numpy(), white_train_targets.to_numpy(),
5 frames
/usr/local/lib/python3.7/dist-packages/keras/optimizers.py in get(identifier)
131 else:
132 raise ValueError(
--> 133 'Could not interpret optimizer identifier: {}'.format(identifier))
ValueError: Could not interpret optimizer identifier: <__main__.WAMEprop object at 0x7fd4bc1d01d0>
Since I am new to Keras and I, unfortunately, don't know Tensorflow, I am not sure what exactly it is not able to find.
Did I get some import wrong?
Did I use the keras compile() method in the wrong way?
Also, if I don't pass the parameter name='wame' to the WAMEprop() call, I get an error message saying that the positional argument 'name' is required. Strangely enough, there is no parameter 'name' in the class constructor. Does this depend on the interaction with the superclass?
Thank you very much a lot in advance for any help you could offer!
Cheers!
UPDATE:
the error message refers to a method get() that takes as input identifier in the optimizers.py file that must have been installed with TensorFlow. Now, this function is expecting to get a string (I guess fr the readily available optimizers), a configuration dictionary, an optimizer_v2.OptimizerV2 object or a tf.compat.v1.train.Optimizer object.
I think the object I am passing as an optimizer is no one of these.
If I run:
my_optimizer = WAMEprop(name='wame')
print(type(my_optimizer))
I get <class '__main__.WAMEprop'>.
So, I suspect the object I am dealing with is something different from what Keras is expecting.
UPDATE2: it runs on my laptop, I have tensorflow installed within an Anaconda environment. Now, I am convinced there is some installation or import problem in Google Colab
Problem solved. I needed to uninstall tensorflow 2.6.0 from Google Colab and install tensorflow 2.0.0 instead.

Input tensor ValueError when loading model BertClassifier Model

--INTRO
I am training a BERT classifier using the google code from tf-models-official==2.3.0.
Everything works like charm: training, evaluating, saving checkpoints.
--THE PROBLEM
My problem arises when I save the whole model and then trying to reload it, I get this error.
I load my checkpoints
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = bert.bert_models.classifier_model(FLAGS_pretrained_bert["bert_config"], 2)[0]
print(f"Loading the checkpoint in {PATH_ckpt}")
model.load_weights(PATH_ckpt)
I save it as a whole model:
model.save("gs://MY_PATH")
Then reloading the model:
reloaded_model = tf.keras.models.load_model("gs://MY_PATH")
And I am getting this error:
ValueError: Input tensors to a Text>BertClassifier must come from `tf.keras.Input`. Received: None (missing previous layer metadata).