Can't load Keras model using RectifiedAdam optimizer - tensorflow

Trying to load a Keras model using tf.keras.models.load_model I get the following error:
import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam
model = tf.keras.models.load_model('model', custom_objects = {'RectifiedAdam' : RectifiedAdam})
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 150, in load_model
return saved_model_load.load(filepath, compile)
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 99, in load
training_config))
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/saving_utils.py", line 229, in compile_args_from_training_config
optimizer_config, custom_objects=custom_objects)
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/optimizers.py", line 819, in deserialize
printable_module_name='optimizer')
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 292, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 250, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown optimizer: RectifiedAdam
I can load the model with compile to False tf.keras.models.load_model('model', compile=False) and then compile it again with RectifiedAdam optimizer (as suggested here: https://stackoverflow.com/a/56565801) - however that is not ideal...
So any ideas on, what I'm doing wrong?

One quick hack around this is to manually assign RectifiedAdam to an object in the Tensorflow namespace:
import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam
tf.keras.optimizers.RectifiedAdam = RectifiedAdam
...

or do something like this:
models.load_model('myModel.h5', custom_objects={'MyOptimizer': MyOptimizer})

The model was trained on a tensorflow (Keras) version higher than the one you want to use to load the trained model. Find the compatible version of TensorFlow (Keras).

Related

How can make inference graph file using models/research/object_detection/export_inference_graph.py

python : 3.9.13
tensorflow : 2.9.1
I am making a custom dataset with 'tensorflow object detection'
The saved_model.pb file was generated by trainning with the FastRCNN dataset.
I took this file and applied it to Nuclio (Serverless function framework) but failed
It seems to have apply inference graph file type
I find export util pythonf file in models/research/object_detection directory "export_inference_graph.py"
But this file not working .
This is error message
Traceback (most recent call last):
File "export_inference_graph.py", line 211, in <module>
tf.app.run()
File "/home/namu/.local/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/home/namu/.local/lib/python3.8/site-packages/absl/app.py", line 308, in run
_run_main(main, args)
File "/home/namu/.local/lib/python3.8/site-packages/absl/app.py", line 254, in _run_main
sys.exit(main(argv))
File "export_inference_graph.py", line 199, in main
exporter.export_inference_graph(
File "/home/namu/myspace/data/models/research/object_detection/exporter.py", line 618, in export_inference_graph
_export_inference_graph(
File "/home/namu/myspace/data/models/research/object_detection/exporter.py", line 521, in _export_inference_graph
profile_inference_graph(tf.get_default_graph())
File "/home/namu/myspace/data/models/research/object_detection/exporter.py", line 649, in profile_inference_graph
contrib_tfprof.model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS)
NameError: name 'contrib_tfprof' is not defined
I knew from Google that this did not work on tensorflow 2.x
https://medium.com/#sebastingarcaacosta/how-to-export-a-tensorflow-2-x-keras-model-to-a-frozen-and-optimized-graph-39740846d9eb
I am working on it by referring to the above site
But
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
import numpy as np
#path of the directory where you want to save your model
frozen_out_path = "/home/namu/myspace/data/models/export_graph"
# name of the .pb file
frozen_model = "frozen_graph"
model = tf.keras.models.load_model('/home/namu/myspace/data/models/train_pb/saved_model') # tf_saved_model load
# model = tf.saved_model.load('/home/namu/myspace/data/models/train_pb/saved_model')
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
When i execute this code , error occurs
ValueError: Unable to create a Keras model from SavedModel at /home/namu/myspace/data/models/train_pb/saved_model. This SavedModel was exported with `tf.saved_model.save`, and lacks the Keras metadata file. Please save your Keras model by calling `model.save`or `tf.keras.models.save_model`. Note that you can still load this SavedModel with `tf.saved_model.load`.
How can i create inference graph pb file.

Unknown layer: KerasLayer. Please ensure this object is passed to the `custom_objects` argument

I have trained a model on google colab by adding a layer to resnet. Here is the model:
import tensorflow_hub as hub # Provides pretrained models
resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5"
# Download ResNet model and save as Keras layer
# Trainable : False means we don't want to train it further
resnet_layer = hub.KerasLayer(resnet_url,
trainable=False,
input_shape=(256,256,3))
# Create model
resnet_model=tf.keras.Sequential([
# Puts images through downloaded model first
resnet_layer,
# Define we will use 20 classes
Dense(2,
activation="softmax")
])
It works fine on colab notebook. But when I export and want to deploy it into a flask service using the code below:
import base64
import numpy as np
import io
import os
from PIL import Image
import keras
from keras import backend as K
from keras.models import Sequential
from keras.models import load_model
from keras.preprocessing.image import ImageDataGenerator
#from keras.preprocessing.image import img_to_array
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.utils import img_to_array
from flask import request
from flask import jsonify
from flask import Flask
app = Flask(__name__)
def get_model():
global model
model = load_model('resnet_model_1.h5')
print(" * Model loaded!")
def preprocess_image(image, target_size):
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize(target_size)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
return image
print(" * Loading Keras model...")
get_model()
#app.route("/predict", methods=["POST"])
def predict():
message = request.get_json(force=True)
encoded = message['image']
decoded = base64.b64decode(encoded)
image = Image.open(io.BytesIO(decoded))
processed_image = preprocess_image(image, target_size=(256, 256))
prediction = model.predict(processed_image).tolist()
response = {
'prediction': {
'dog': prediction[0][0],
'cat': prediction[0][1]
}
}
return jsonify(response)
I get this nasty error:
Traceback (most recent call last):
File "/usr/local/bin/flask", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 988, in main
cli.main()
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 579, in main
return super().main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 850, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 299, in __init__
self._load_unlocked()
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 333, in _load_unlocked
self._app = rv = self.loader()
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 389, in load_app
app = locate_app(import_name, name)
File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 234, in locate_app
__import__(module_name)
File "/home/pc3/dev/flaskservice/predict_app.py", line 39, in <module>
get_model()
File "/home/pc3/dev/flaskservice/predict_app.py", line 26, in get_model
model = load_model('resnet_model_1.h5')
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/utils/generic_utils.py", line 562, in class_and_config_for_serialized_keras_object
raise ValueError(
ValueError: Unknown layer: KerasLayer. 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.
my tensorflow version on colab is 2.8.2 and my local ubuntu machine on which I deply the model it is 2.9.1.
I read here that this error has been resolved by passing ,custom_objects={'KerasLayer':hub.KerasLayer} to load_model. But for me this results in all images are classifed as cat. So clearly it messes up somthing in the model.
So I'm left clueless and appreciate your hints.
Please ensure that these lines are added before calling your inference, as you need to call an argmax to specify which category is the dominant one in your prediction.
list_of_categories = ["dog", "cat"]
pred = list_of_categories[np.argmax(prediction)]
response = pred

Unable to import meta graph using tf.train.import_meta_graph, The name '' refers to an Operation not in the graph

OS Platform and Distribution: Ubuntu 18.04
tf.VERSION = 1.13.1
tf.GIT_VERSION = b'v1.13.1-0-g6612da8951'
tf.COMPILER_VERSION = b'v1.13.1-0-g6612da8951'
Python version: 3.6.10
GCC/Compiler version (if compiling from source): 4.8.5
CUDA/cuDNN version: 10.2
GPU model and memory: Nvidia Tesla V100, 16gb
I am not able to import meta graph.
Even If I define tf.placeholder(name="data", shape=(None,64), dtype=tf.float32), error comes for next layer.
I tried using tf2.0 also. But same issue there.
Code to reproduce the issue
import tensorflow as tf
model_dir="./model" # change this line to the directory where checkpoint and models are saved
checkpoint = tf.train.get_checkpoint_state(model_dir)
input_checkpoint = checkpoint.model_checkpoint_path
clear_devices = True
with tf.Session(graph=tf.Graph()) as sess:
saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)
Checkpoint files are attached in model.zip.
StackTrace
Traceback (most recent call last):
File "import_meta_graph.py", line 11, in <module>
saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)
File "/home/ubuntu/tf1.13/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1435, in import_meta_graph
meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
File "/home/ubuntu/tf1.13/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1457, in _import_meta_graph_with_return_elements
**kwargs))
File "/home/ubuntu/tf1.13/lib/python3.6/site-packages/tensorflow/python/framework/meta_graph.py", line 852, in import_scoped_meta_graph_with_return_elements
ops.prepend_name_scope(value, scope_to_prepend_to_names))
File "/home/ubuntu/tf1.13/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/home/ubuntu/tf1.13/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3538, in _as_graph_element_locked
"graph." % repr(name))
KeyError: "The name 'data' refers to an Operation not in the graph."
You should remove the graph=tf.Graph(), otherwise the import_meta_graph will import it into the wrong graph.

How to use a string placeholder for the model_dir in tf.contrib.factorization.KMeansClustering?

I'm using TF version 1.12 with conda and python 3.
My question concerns the model_dir value of tf.contrib.factorization.KMeansClustering : how to use a string placeholder for the model_dir value?
Here is the context: I have pretrained KMeans in different situation, checkpoints are in different model_dir.
I want to use predictions of these pretrained models inside a graph, depending on each situation, so I need to put in this graph the KMeansClustering which can accept different model_dirs.
In the graph I defined :
ckpt_ph = tf.placeholder(tf.string)
...
kmeans = KMeansClustering(5, model_dir=ckpt_ph,distance_metric='cosine')
def input_fn():
return tf.train.limit_epochs(tf.convert_to_tensor(x, dtype=tf.float32), num_epochs=1)
centers_idx = list(kmeans.predict(input_fn,predict_keys='cluster_index',checkpoint_path=ckpt_ph,yield_single_examples=False))[0]['cluster_index']
centers_val = kmeans.cluster_centers()
...
And I run it with:
...
for ind in range(nb_cases):
...
sess.run([...], feed_dict={..., ckpt_ph: km_ckpt[ind]})
...
Where km_ckpt is the list of pretrained KMeansClustering checkpoints pathes that I want to use for each situations.
The error that I get is:
Traceback (most recent call last):
File "main.py", line 28, in <module>
tf.app.run()
File "C:\Users\Denis\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
_sys.exit(main(argv))
File "main.py", line 23, in main
launch_training()
File "main.py", line 14, in launch_training
train_mnist.train_model()
File "C:\Users\Denis\ML\ScatteringReconstruction\src\model\train_mnist.py", line 355, in train_model
X_r = SR(X_tensor)
File "C:\Users\Denis\ML\ScatteringReconstruction\src\model\train_mnist.py", line 316, in __call__
kmeans = KMeansClustering(FLAGS.km_k, model_dir=ckpt_ph, distance_metric='cosine')
File "C:\Users\Denis\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\factorization\python\ops\kmeans.py", line 423, in __init__
config=config)
File "C:\Users\Denis\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\estimator\estimator.py", line 189, in __init__
model_dir)
File "C:\Users\Denis\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1665, in maybe_overwrite_model_dir_and_session_config
if model_dir:
File "C:\Users\Denis\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 671, in __bool__
raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
I think that the problem is that in KMeansClustering and KMeansClustering.predict, model_dir is expecting a Python bool or string, and I'm giving him a Tensor, but then I don't see hos to use pretrained KMeansClustering inside a graph.
Thanks in advance for the help!

tensorflow keras conv1d : ValueError: len(dilation_rate)=1 but should be -1

I try to build a custom Keras regularize with tensorflow as backend.
Executing the following piece of code gives me an exception :
import tensorflow as tf
from tensorflow import keras
inputs = keras.Input(shape=(10,))
x = keras.backend.conv1d(inputs, tf.constant([-1,1]), padding = 'same', dilation_rate=None)
x = keras.backend.conv1d(inputs, tf.constant([-1,1]), padding = 'same', dilation_rate=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/keras/backend.py", line 3775, in conv1d
data_format=tf_data_format)
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 779, in convolution
data_format=data_format)
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 842, in __init__
num_spatial_dims, strides, dilation_rate)
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 625, in _get_strides_and_dilation_rate
(len(dilation_rate), num_spatial_dims))
ValueError: len(dilation_rate)=1 but should be -1
i can't understand what i am doing wrong.
Thank you.
I think the problem is in tf.constant([-1,1]). This is a place for kernel which should have dimension like input_length,in_channel,out_channel.