Trouble running converted custom trained model on RPI4 using TFLite - tensorflow

I am trying to run a mask detecting model on my RPI4.
I followed this Roboflow tutorial:
https://www.youtube.com/watch?v=pXLLNa4IrmM&list=LL&index=1&t=1169s.
However, after converting the darknet model to a TFLite model, I am getting this error:
2021-01-30 21:42:03.351149: E
tensorflow/core/platform/hadoop/hadoop_file_system.cc:132]
HadoopFileSystem load error: libhdfs.so: cannot open shared object
file: No such file or directory Traceback (most recent call last):
File "TFLite_detection_webcam.py", line 138, in
interpreter = Interpreter(model_path=PATH_TO_CKPT) File
"/home/pi/.local/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter.py",
line 207, in init model_path, self._custom_op_registerers))
ValueError: Didn't find op for builtin opcode 'RESIZE_BILINEAR'
version '3' Registration failed.
I know I am probably missing information that could help solve this problem, but I am not sure what else I should be including.
I was able to run premade object models, however the custom model I made using the YouTube guide did not work.
How do I solve this issue? Or, is there another way to train a custom model to work on the RPI4?

I think you are using a too old version of Tensorflow. Can you try the latest version of nightly?

Related

SavedModel file does not exist error while converting vgg16

I want to convert rcmalli_vggface_tf_vgg16.h5 pre-trained model to js format to use in a tensorflow-js project.
to convert this file I tried different ways but yet I could not to solve the problem.
I'm using converters.convert_tf_saved_model method to load and then convert it to a json file.
converters.convert_tf_saved_model('rcmalli_vggface_tf_vgg16.h5','web_model')
But every time following error is shown:
SavedModel file does not exist at: rcmalli_vggface_tf_vgg16.h5
While I am sure the h5 file is next to the file that is running the program.
I try full path address but same error occured. I do not know what is problem
You are converting a keras saved_model but the model is a keras layers_model because it is stored in h5 file.
I find this way the simplest to convert the model. And it did it in about 2 seconds.
Go to TensorflowJS converter Github
Follow installation instructions
Download the model you point to from the Github repo.
The model is written in the HDF5 file format
By the way, this is the oldest of the models, why not to download a new one ?
Also, this is a huge model, unless runs on the server it is of no use for a browser (produces 50 shard files of 4MB.)
Perform the conversion*
tensorflowjs_converter --input_format=keras rcmalli_vggface_tf_vgg16.h5 ./converted
The output will then be a layers model, and this has to be loaded with the tf.loadLayers API.
For example, use const model = await tf.loadGraphModel('path/to/model.json');
Note*: ./converted is the output directory, be sure not to overwrite your own stuff.

Deploy a Tensorflow model built using TF2 in TF1 format (no SavedModel bundles found!)

I have used Recommenders https://github.com/microsoft/recommenders library to train an NCF recommendation model. Currently I'm getting issues in deployment through Amazon TensorflowModel library
Model is saved using the following code
def save(self, dir_name):
"""Save model parameters in `dir_name`
Args:
dir_name (str): directory name, which should be a folder name instead of file name
we will create a new directory if not existing.
"""
# save trained model
if not os.path.exists(dir_name):
os.makedirs(dir_name)
saver = tf.compat.v1.train.Saver()
saver.save(self.sess, os.path.join(dir_name, MODEL_CHECKPOINT))
Files exported in the process are 'checkpoint', 'model.ckpt.data-00000-of-00001', 'model.ckpt.index', 'model.ckpt.meta'
They follow the structure of
- model.tar.gz
- 00000000
- checkpoint
- model.ckpt.data-00000-of-00001
- model.ckpt.index
- model.ckpt.meta
I have tried various deployment processes, however they all give the same error. Here's the latest one that I implemented following this example https://github.com/aws/amazon-sagemaker-examples/blob/main/sagemaker-script-mode/pytorch_bert/code/inference_code.py
from sagemaker.tensorflow.model import TensorFlowModel
model = TensorFlowModel(
entry_point="tf_inference.py",
model_data=zipped_model_path,
role=role,
model_version='1',
framework_version="2.7"
)
predictor = model.deploy(
initial_instance_count=1, instance_type="ml.g4dn.2xlarge", endpoint_name='endpoint-name3'
)
All Solutions end with the same error over and over again
Traceback (most recent call last):
File "/sagemaker/serve.py", line 502, in <module>
ServiceManager().start()
File "/sagemaker/serve.py", line 482, in start
self._create_tfs_config()
File "/sagemaker/serve.py", line 153, in _create_tfs_config
raise ValueError("no SavedModel bundles found!")
These 2 links helped me resolve the issue
https://github.com/aws/sagemaker-python-sdk/issues/599
https://www.tensorflow.org/guide/migrate/saved_model#1_save_the_graph_as_a_savedmodel_with_savedmodelbuilder
Sagemaker has weird directory structure that you need to strictly follow. The first one shares the starting directories and 2nd one shares the process of saving the model for TF1 and TF2

TypeError: weight_decay is not a valid argument, kwargs should be empty for `optimizer_experimental.Optimizer`

I am getting the above error when running tensorflow.keras.models.load_model('')
I was working on tensorflow in the Spyder environment and I had no issues.
Since I kept getting prompts (whenever launched Spyder) that the new version 5 of Spyder is available, I uninstalled the existing Spyder version and installed the latest version. In fact I uninstalled Anaconda itself and reinstalled it all over again (since the above problem persisted).
However when running the same programs with included the code for loading an existing saved LSTM model, I got the below error.
prediction_model = tensorflow.keras.models.load_model('')
The model is saved as a .h5 file.
the complete error string when running the load_model command is as follows:
File "C:\Users\ayapp\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\ayapp\anaconda3\lib\site-packages\keras\optimizers\optimizer_experimental\optimizer.py", line 94, in _process_kwargs
raise TypeError(f"{k} is not a valid argument, kwargs should be empty "
TypeError: weight_decay is not a valid argument, kwargs should be empty for `optimizer_experimental.Optimizer`.
This issue was never faced when I was working in my earlier version of Spyder.
Can anybody suggest a solution?
use
model = tensorflow.keras.models.load_model(fileName, compile=False)
then
model.compile(whatever settings here)
This worked for me
Rewriting #Christian Velez's answer for more clarity.
Example code:
model = Sequential()
...
model.compile(//some arguments) #Copy This Line
model.save(//path)
Solution:
model = load_model(//path, compile=False)
model.compile(//some arguments) #Paste it here
make sure the version of tensorflow is same for trained model and the env in which you are trying to predict. that worked for me

Can't save save/export and load a keras model that uses eager execution

I'm following the RNN text-generation tutorial with eager execution pretty much line for line. I've trained the model with my own data set and have saved a low loss checkpoint. I'm able to load the weights and generate text but I want to export/save the model so that I can learn how to deploy one using flask. However I can't figure out how. The version I'm using is '1.14.0-rc1'.
The tutorial: https://www.tensorflow.org/tutorials/sequences/text_generation
I have been able to save the model as an HDF5 file but I cannot load it. I've also disabled eager execution but that causes problems with running the code later on. I have tried the following and a few more snippets but those led to nothing as well:
new_model = keras.models.load_model("/content/gdrive/My Drive/ColabNotebooks/ckpt4/my_model.h5")
How ever I get
RuntimeError: tf.placeholder() is not compatible with eager execution.
Lastly I found this in another post and tried it as well but was met with another error:
tf.saved_model.save(model, "/content/gdrive/My Drive/Colab Notebooks/ckpt4/my_model.h5")
error:
AssertionError: Tried to export a function which references untracked object Tensor("StatefulPartitionedCall/args_2:0", shape=(), dtype=resource).TensorFlow objects (e.g. tf.Variable) captured by functions must be tracked by assigning them to an attribute of a tracked object or assigned to an attribute of the main object directly.

Toolkit Error: Stage Details Not Supported: VarHandleOp

I have trained a model using keras and saved to json file and .h5 weight file. Then I need to put this run on intel Neural Compute stick. So I converted these two model and weight files to .meta file for tensorflow using my repo: https://github.com/anuragcp/to_ncs_graph.git
Prediction working without NCS. On creating graph from this meta file using mvNCCompile command it make an error:
[Error 5] Toolkit Error: Stage Details Not Supported: VarHandleOp
I Have checked on both ncsdk v1 & v2 - same result.
Any idea how to solve this?