toco_from_protos: command not found - tensorflow

I'm using this following link to convert my Tensorflow model to tf lite model
https://www.tensorflow.org/lite/convert/python_api, In here i'm following instruction for 'Exporting a GraphDef from file'
But i'm getting following error
"TOCO failed. See console for info.\n%s\n%s\n" % (stdout, stderr))
tensorflow.lite.python.convert.ConverterError: TOCO failed. See console for info.
/bin/sh: toco_from_protos: command not found
I've installed latest tensorflow v1.13.1

The problem
Tensorflow calls to a specific binary file to convert the .pb file (stored by protobuf) in a tflite model. The binary file is 'toco_from_protos', and the error message suggests that the shell interpreter ('/bin/sh' in this case) is not able to find the binary file ('toco_from_proto').
You need to include the path to 'toco_from_proto' file in the PATH environment variable.
How to do this
First, check if the file exists. You can use the command 'locate' for example:
$ locate toco_from_proto
/home/user/anaconda3/envs/tensorflow/bin/toco_from_protos
/home/user/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/lite/toco/python/toco_from_protos.py
/home/user/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/lite/toco/python/__pycache__/toco_from_protos.cpython-36.pyc
In my case, I am using Anaconda to manage the environments. Thus, the binary is in the binary path ('bin' folder) of the environment container ('tensorflow' in this case).
To ensure the correct execution of the binary, include the path to 'toco_from_protos' file inside PATH environment variable. If you are using a Linux based system, you can do something like:
$ export PATH=$PATH:/home/user/anaconda3/envs/tensorflow/bin
If you are using an IDE program (e.g. Pycharm), you can call the IDE run script using the same console you used to export the PATH variable. For example:
$ export PATH=$PATH:/home/user/anaconda3/envs/tensorflow/bin
$ /opt/pycharm-community-2018.1.4/bin/pycharm.sh
The new PATH value change remains only in that console window, so if you want to make the change persistent, include the export sentence inside '~/.bashrc' file.

I had the same issue and solved by using an official docker image, host machine has a fresh Ubuntu 18.04.
docker run --runtime=nvidia -v /path/to/my/project:/mapped/docker/path -it tensorflow/tensorflow:latest-gpu bash
Then run the conversion script inside docker:
model = load_model() # keras model
output_names = [node.op.name for node in model.outputs]
input_names = [node.op.name for node in model.inputs]
with tf.keras.backend.get_session() as sess:
sess.run(tf.global_variables_initializer())
frozen_def = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, output_names)
converter = tf.lite.TFLiteConverter.from_session(sess, model.inputs, model.outputs)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
At the time of writing tensorflow/tensorflow:latest-gpu is version 1.13.1

I too got same error log in tensorflow 1.14. For me issue was not in converter, it was was related to path not getting resolved.
On running this before Python script, it worked for me
export PATH=$PATH:~/.local/bin

Related

Yolov2 Compiling and Training: Problems on Windows 10

I have been having issues setting up Darknet. I will be using yolov2 to detect cerebral microbleeds for a neuroscience project. After battling Darknet for a few days, I managed to install it and successfully download the train, test and validation Pascal VOC data by using the below general configuration/set up:
Cmake-GUI 3.2 (binary distributions, not source)
MSVS 2019
CUDA 11.1
cuDNN 8.0.5
OpenCV 4.2.0
I always get an error when running darknet.exe detector train data/voc.data yolo-voc.2.0.cfg darknet19_448.conv.23 in cmd:
'darknet.exe' is not recognized as an internal or external command,
operable program or batch file
I cannot seem to understand the reason why.
In addition, when following the pjreddie instructions to modify cfg for Pascal Data:
classes= 20
train = /train.txt
valid = 2007_test.txt
names = data/voc.names
backup = backup
I change the Notepad file and all / to backslash, does that make a difference?
Could anyone shed some light as to how to successfully train the data?
that's a generic error when you are trying to execute a program that is not in your current directory or not defined in PATH variable.
try adding the path to the darknet.exe file in your path variable:
\darknet\build\darknet\x64\

Gcloud ai-platform local predict Error: gcloud crashed (PermissionError): [WinError 5] Access is denied

I was trying to run a command to test local predict in my computer. However, the command failed every time with this error.
ERROR: gcloud crashed (PermissionError): [WinError 5] Access is denied
This is the command:
gcloud ai-platform local predict --model-dir model_final --json-instances image_b64.json --framework tensorflow
I am positive 101% positive that I have followed everything in the doc by Google.
First, the command required a model file to be saved in TensorFlow SavedModel format, which, since I use Keras, I can just do model.save("model_final").
If you have used Keras for training, use tf.keras.Model.save to export a SavedModel
So I did, at it only output a single file, so I can only assume it's the file to be placed in the --model-dir parameter. I admit doing model.save("model_final") created a file, not a dir, which is a bit weird but the document for Keras just said use that so there is no way I could be wrong.
And also:
If you export your SavedModel using tf.keras.Model.save, then you do not need to specify a serving input function.
If you export a SavedModel from tf.keras or from a TensorFlow estimator, the exported graph is ready for serving by default.
The "image_b64.json" file follows this format:
{"image_bytes":{"b64": base64_jpeg_data )}}
So after 3 hours and having followed everything required by Google, and somehow the gloud still throws me that error. And, yes, of course I have run the command line under Administrator Mode. I also tried it in two of my computers, and I got the same error. I am using Windows, Tensorflow 1.15.
Can anyone point out what is the problem with my implementation, or Google Doc/Keras is just lack lustering. Thank you.

Universal Sentence Encoder load error "Error: SavedModel file does not exist at..."

I installed Uiniversal Sentence Encoder (Tensorflow 2) in 2 virtual environment with Ananconda. One is on Mac, anther is on Ubuntu.
All worked with following:
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
model = hub.load(module_url)
Installed with:
conda create -n my-tf2-env python=3.6 tensorflow
conda init bash
conda activate my-tf2-env
conda install -c conda-forge tensorflow-hub
But, for unknown reason after 3 weeks, Mac does not work with following error which fails at:
model = hub.load(module_url)
Error: SavedModel file does not exist at: /var/folders/99/8rwn_9hx3jj9x3qz6yf0j2f00000gp/T/tfhub_modules/063d866c06683311b44b4992fd46003be952409c/{saved_model.pbtxt|saved_model.pb}
On Mac, I recreated new env with same procedure but has same error.
On Ubuntu, all works well.
I want to know how to fix Mac. Thank you for help.
What I attempted on Mac is that I tried to download "https://tfhub.dev/google/universal-sentence-encoder/4" to local drive and load it from local drive in future, not from web url. This process was not finished and not successful yet. I don't remember if there is anything downloaded to Mac with this attempt, that might corrupted Tensorflow-hub on login user account of my Mac.
This error usually occurs when the saved_model.pb is not present in the path specified in the module_url.
For example, if we consider the Folder structure as shown in the screenshot below,
The code,
import tensorflow_hub as hub
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
model = hub.load(module_url)
and
import tensorflow_hub as hub
module_url = "/home/mothukuru/Downloads/Hub"
model = hub.load(module_url)
work successfully.
But if saved_model.pb is not present in that Folder as shown below,
Executing the code,
import tensorflow_hub as hub
module_url = "/home/mothukuru/Downloads/Hub"
model = hub.load(module_url)
results in the below error,
OSError: SavedModel file does not exist at: /home/mothukuru/Downloads/Hub/{saved_model.pbtxt|saved_model.pb}
In your specific case, executing the code while the Download of the Model was in progress might have resulted in the error.
As stated in the comment, deleting the Downloaded File can fix the problem.
Please let me know if this answer has not resolved your issue and I will be happy to modify it accordingly.
TF Published some additional guidelines on caching models apparently in response to questions about this issue.
In my case, I was running this locally on Mac via a jupyter notebook.
I was not sure how to "Delete the download file" as suggest in the other answer, but I found this resolved my issue:
https://www.tensorflow.org/hub/caching#reading_from_remote_storage
Reading from remote storage
Users can instruct the tensorflow_hub
library to directly read models from remote storage (GCS) instead of
downloading the models locally with
os.environ["TFHUB_MODEL_LOAD_FORMAT"] = "UNCOMPRESSED"
or by setting the command-line flag --tfhub_model_load_format to UNCOMPRESSED. This way, no caching directory is needed, which is especially helpful in environments that provide little disk space but a fast internet connection.
I ran that command in my notebook, and then the error was immediately resolved.
Note: I assume this is slower, especially if you do not have a fast internet connection, since what you are doing is telling the program to not locally cache (store) a copy and to just download it on demand.

How do you install modules within sagemaker training jobs?

I don't think I'm asking this question right but I have jupyter notebook that launches a Tensorflow training job with a python training script I wrote.
That training script requires certain modules. Seems my sagemaker training job is failing because some of the modules don't exist.
How can I ensure that my training job script has all the modules it needs?
Edit
An example of one of these modules is keras.
The odd thing is, I can import keras in the jupyter notebook, but when that import statement is in my training script then I get the No module named keras error
If you want to install multiple packages, one way is to upgrade to Sagemaker Python SDK v2. With this, you can create a requirements.txt in the same directory as your notebook, and run the training. Sagemaker will automatically take care of the installation.
If you want to stay on v1 SDK, you can add the following snippet to your entry_point script.
import subprocess
import sys
def install(package):
subprocess.check_call([sys.executable, "-q", "-m", "pip", "install", package])
install('keras')
The module script runs within a docker container which obviously does not have the dependency installed. Jupyter notebook on the other hand has keras pre-installed.
Easy way to do this is to have a requirements.txt file with all the requirements and then pass that on when creating your model.
env = {
'SAGEMAKER_REQUIREMENTS': 'requirements.txt', # path relative to `source_dir` below.
}
sagemaker_model = TensorFlowModel(model_data = 's3://mybucket/modelTarFile,
role = role,
entry_point = 'entry.py',
code_location = 's3://mybucket/runtime-code/',
source_dir = 'src',
env = env,
name = 'model_name',
sagemaker_session = sagemaker_session,
)
You can upload your requirements.txt file to s3 bucket which can be
accessible by sagemaker and download the file to your working
directory of the container using boto3. Install the libraries from
requirements.txt the entry file.
import os
import boto3
s3 = boto3.client('s3')
s3.download_file('BUCKET_NAME', 'OBJECT_NAME', '/opt/ml/code/requirements.txt')
os.command('pip install -r /opt/ml/code/requirements.txt')
The other way you can do it is by building your own container using
bring your own algorithm option provided by aws.
Ref-links:
https://github.com/awslabs/amazon-sagemaker-examples/blob/master/advanced_functionality/scikit_bring_your_own/scikit_bring_your_own.ipynb
The EstimatorBase class (and TensorFlow class) accept the parameter dependencies which you can use as follows to pass your requirements.txt:
estimator = TensorFlow(
dependencies=['requirements.txt'], # copies this file
)
e.g.
estimator = TensorFlow(
entry_point='src/train.py',
dependencies=['requirements.txt'], # copies this file
)
or
estimator = TensorFlow(
source_dir='src', # this copies the entire src folder
entry_point='train.py', # when using source_dir has to be directly under that dir
dependencies=['requirements.txt'], # copies this file
)
This copies the requirements.txt file into your sourcedir.tar.gz along with the training code.
This may only work on newer image versions. I read that in older versions you may need to put the requirements.txt file in the same folder as your training code.
If this doesn't work, you can use pip download to download your dependencies defined in requirements.txt locally, then use the dependencies parameter to specify the folder to which you downloaded your dependencies.
Another option is in your entry_point .py file you can add
import os
if __name__ == "__main__":
os.system('pip install mymodule')
import mymodule
# rest of code goes here
This worked for me for simple modules such as pyparsing, but I think with keras you better just use a Tensorflow container that has keras preinstalled, as mentioned above.
The environment on your notebook instance is exclusive from the environment of your training job on SageMaker, unless it is local mode.
If you're using a custom docker image, then most likely your docker image doesn't have Keras installed.
If you are using the SageMaker predefined TensorFlow container, which is most likely invoked through the following code:
https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/estimator.py#L170
TensorFlow(entry_point='training_code.py',
blah,
blah
)
Then you will need to install your dependencies within that container. There are currently two modes for training for TensorFlow on SageMaker, "framework" and "script" mode.
If training through "framework" mode, which is only available with 1.12 and below, then you will be limited to using a keras_model_fn defined here:
https://github.com/aws/sagemaker-python-sdk/tree/v1.12.0/src/sagemaker/tensorflow#preparing-the-tensorflow-training-script
Installing your dependencies would be done by passing in a requirements.txt.
On "script mode", which is introduced with TensorFlow 1.11 and above:
https://github.com/aws/sagemaker-python-sdk/tree/master/src/sagemaker/tensorflow#training-with-tensorflow
Requirements.txt is not supported for "script" mode and instead it is recommended to install your dependencies within your user script, which would be your Python file that contains all of your Keras code.
Please let me know if there is anything I can clarify.
For examples:
https://github.com/awslabs/amazon-sagemaker-examples/tree/master/sagemaker-python-sdk/tensorflow_script_mode_quickstart
https://github.com/awslabs/amazon-sagemaker-examples/tree/master/sagemaker-python-sdk/tensorflow_iris_dnn_classifier_using_estimators

Tensorflow Tensorboard on Windows shows a blank page

I'm using Tensorflow on Windows but when I try to launch Tensorboard opening http://localhost:6006 the browser shows a blank page
I have added the codeline
writer = tf.train.SummaryWriter('mypath/my_graph', sess.graph)
to my Tensorflow model and launched tensorboard with
tensorboard --logdir="mypath/my_graph"
Here the console output:
Following mrry suggestion I have updated to 0.12.0rc1 and now the Tensorboard page is shown but unfortunately I cannot see any graph, and is missing also the left panel to upload a graph file manually that I can see in some screenshots of the official guide.
Tried also to use
writer = tf.summary.FileWriter('mypath/my_graph', sess.graph)
following the deprecation hint
EDIT
I have found the problem. If I launch tensorboard --logdir="mypath/my_graph" TensorBoard is unable to load the path and looks always for the graph files in the default user path C:\Users\andrew\mygraph\ if I run console as user or C:\Windows\System32 if I run console as administrator. This is a bug and should be fixed.
The 0.12.0rc0 (Release Candidate 0) release of TensorFlow on Windows contains a broken version of TensorBoard. We recently made a new release (0.12.0rc1, Release Candidate 1) that contains a fix for TensorBoard on Windows. You can upgrade by following the instructions for installing the latest release on Windows, or simply typing pip install --upgrade tensorflow at the command prompt.
In ubuntu we can use:
tensorboard --logdir=/home/user/graph/
In Windows we have to change the command prompt to the directory in which the graph file is placed and then use:
tensorboard --logdir=\home\user\graph\