ModuleNotFoundError: No module named 'tensorflow.contrib.lite.toco.python' - tensorflow

Exact command to reproduce: toco --help
I am trying to run the codelab tutorial of tensorflow lite. After installing tf-nightly, when I try to run the command "toco --help", I get the error ModuleNotFoundError: No module named 'tensorflow.contrib.lite.toco.python'.
I have tried this on 3 computers( all Windows) and the same problem persists.
Source code / logs
C:\Users\HP\Downloads>toco --help
Traceback (most recent call last):
File "c:\programdata\anaconda3\lib\runpy.py", line 193, in
_run_module_as_main "main", mod_spec)
File "c:\programdata\anaconda3\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\Scripts\toco.exe_main.py", line 5, in 
ModuleNotFoundError: No module named
'tensorflow.contrib.lite.toco.python'

I was getting the same error and apparently TOCO doesn't work on Windows machines,
https://github.com/tensorflow/tensorflow/issues/16374
My alternative to optimize the model was to user TensorFlow Mobile, instead of TensorFlow Lite, and use 'optimize_for_inference'. You could also try on a linux environment.

I solved it by downgrading tensorflow to 1.7
pip install --upgrade "tensorflow==1.7.*"

Issue Solved
Clone the tensorflow repository an copy the lite folder from tensorflow\tensorflow\contrib
and paste it in the C:\Users\$USERNAME$\AppData\Local\Programs\Python\Python36\Lib\site-packages\tensorflow\contrib\lite

The tensorflow library in Python36 has some missing files. My Python36 folder is in c:\Python36. So the toco will load "C:\Python36\Lib\site-packages\tensorflow\contrib\lite\python". Whoever coded the toco forgot to copy the whole folder into there.
You need to copy from your tensorflow folder to the lite folder. My tensorflow is at
"c:\tensorflow". The lite folder looks like this:
Copy all files from "C:\tensorflow\tensorflow\contrib\lite\python" to "C:\Python36\Lib\site-packages\tensorflow\contrib\lite\python".
Now, you need to make a test, "toco --help"

Related

Tensorflow docker installation problem. (undefined symbol: _ZN10tensorflow8OpKernel11TraceStringEPNS_15OpKernelContextEb)

I am trying to learn more about tensorflow and building a custom model with it.
I follow an instruction in which i install a docker container with the following steps:
git clone https://github.com/tensorflow/models.git
cd models
docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od .
docker run -it od
All works ok.
Next step is to run a test in the container script:
python object_detection/builders/model_builder_tf2_test.py
This fails and outputs:
Traceback (most recent call last):
File "object_detection/builders/model_builder_tf2_test.py", line 22, in <module>
import tensorflow.compat.v1 as tf
File "/home/tensorflow/.local/lib/python3.6/site-packages/tensorflow/__init__.py", line 438, in <module>
_ll.load_library(_main_dir)
File "/home/tensorflow/.local/lib/python3.6/site-packages/tensorflow/python/framework/load_library.py", line 154, in load_library
py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: /usr/local/lib/python3.6/dist-packages/tensorflow/core/kernels/libtfkernel_sobol_op.so: undefined symbol: _ZN10tensorflow8OpKernel11TraceStringEPNS_15OpKernelContextEb
So there is a question about this in stackoverflow see:
Also a followup at:
It says, if i understand correctly, to uninstall and install tensorflow again, but it does not work.
The version is 2.6.1. If i try to downgrade i run into all kind of other problems.
I am stuck ;-(, any clues ?
I am working on a VPS, is it possible to run the tensorflow docker on 'normal' hardware ?

ModuleNotFoundError: No module named 'official.modeling.optimization'

Please give solution on the following error. The tricky part is that the verification and testing is running "OK" but when I run command for training it throws following error.
The system details are as follows
Win10 - python 3.7.6
cuda 10.1 cudnn - 7.6.5
opencv - 4.5.1.48
tensorflow / tensorflow-gpu ==2.2.0
object_detection-0.1
protoc - 3.17.3
tf-models-official
tensorflow-model-optimization
Paths are already created
(Working on jupyter notebook on TFOD-API)
Traceback (most recent call last): File
"Tensorflow\models\research\object_detection\model_main_tf2.py", line
32, in
from object_detection import model_lib_v2 File "D:\Code_kida\ANPR\Tensorflow\models\research\object_detection\model_lib_v2.py",
line 31, in
from object_detection import model_lib File "D:\Code_kida\ANPR\Tensorflow\models\research\object_detection\model_lib.py",
line 34, in
from object_detection.builders import optimizer_builder File "D:\Code_kida\ANPR\Tensorflow\models\research\object_detection\builders\optimizer_builder.py",
line 25, in
from official.modeling.optimization import ema_optimizer ModuleNotFoundError: No module named 'official.modeling.optimization'
Make sure that the folder "D:\Code_kida\ANPR\Tensorflow\models\official" is exist and it has "modeling/optimization/ema_optimizer.py" inside.
I had the same error and that was the fix for me. might work for you too. or whoever comes here in the future

How can I install the tensorflow object detection API on Google colab?

I tried running tensorflow object detection API on Colab according to
Inline Link
I got such an error at the first Install required packages.
How can I solve it?
Background : Python2 , GPU
/root
fatal: destination path 'models' already exists and is not an empty directory.
/root/models/research
Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 23, in <module>
from object_detection.builders import model_builder
ImportError: No module named object_detection.builders
I'm not clear about from which directory you are executing the command.
if you executing it from content directory then go to model and then to research directory.
%cd ~/models/research
!python object_detection/builders/model_builder_test.py
If you don't have model directory clone it by using
!git clone --quiet https://github.com/tensorflow/models.git

Error while tensorflow training on gcloud ml engine

I am following this ml-engine guide. I did setup my gcloud and created vm also. For tensorflow, I am using Anaconda 3 to create my python environment. I created new environment with python=3.6. But when I fire this
gcloud ml-engine local train --module-name trainer.task --package-path trainer -- --train-files c:\Anaconda3\mytensorflowcode\cloudml-samples-master\census\estimator\data\adult.data.csv --eval-files c:\Anaconda3\mytensorflowcode\cloudml-samples-master\census\estimator\data\adult.test.csv --train-steps 1000 --job-dir c:\Anaconda3\mytensorflowcode\cloudml-samples-master\census\estimator\output --eval-steps 100
I am getting following error
Traceback (most recent call last):
File "D:\gcsdk174\google-cloud-sdk\platform\bundledpython\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "D:\gcsdk174\google-cloud-sdk\platform\bundledpython\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Anaconda3\mytensorflowcode\cloudml-samples-master\census\estimator\trainer\task.py", line 4, in <module>
import model
File "trainer\model.py", line 20, in <module>
import tensorflow as tf
ImportError: No module named tensorflow
I could able to install tensorflow successfully with pip install -r ../requirements.txt command as per the guide.
Can anybody point out, what I am doing wrong?
Update: this issue should now be fixed with the most recent version of gcloud. Can you give it a try and see if it works for you? First do:
gcloud components update
What's happening is that gcloud is (silently) requiring py2.7, which is causing your import error. This is a bug that we will fix soon. (It's particularly problematic for Windows, since TF doesn't support a 2.7 install for windows). We'll update here when it's fixed.
In the meantime, the best option is probably to test locally by just running your python script directly (unless you are trying to test distributed training locally).
If you are trying to test distributed training locally, then your best temporary option is probably to use Docker and the TensorFlow docker container.

While running TensorFlow's classify_image, getting AttributeError: 'module' object has no attribute 'gfile'

I am getting this error while running the sample file given with TensorFlow, in the imagenet model,
File "classify_image.py", line 154, in run_inference_on_image
if not tf.gfile.Exists(image):
AttributeError: 'module' object has no attribute 'gfile'
I have tried installing using both, from pip as well as source, on virtualserver as well, still I get this error.
There are two ways to go about solving this problem:
Option 1
This is an expansion on Yaroslav's comment above, and is easier than option 2.
Modify classify_image.py as follows:
Replace all instances of tf.gfile.Exists to os.path.exists, and
replace all instances of tf.gfile.GFile and tf.gfile.FastGFile to open
Then run the modified classify_image.py, and it should work.
Option 2
Update tensorflow to the latest version that includes gfile as described here
However, after you do that, you might encounter the following error when you try to run classify_image.py:
$ python classify_image.py
>> Downloading inception-2015-12-05.tgz 100.0%
Succesfully downloaded inception-2015-12-05.tgz 88931400 bytes.
[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A protocol message was rejected because it was too big (more than 67108864 bytes). To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
Traceback (most recent call last):
File "classify_image.py", line 213, in <module>
tf.app.run()
File "/Users/USERNAME/.virtualenvs/mlnd/lib/python2.7/site-packages/tensorflow/python/platform/default/_app.py", line 30, in run
sys.exit(main(sys.argv))
File "classify_image.py", line 209, in main
run_inference_on_image(image)
File "classify_image.py", line 159, in run_inference_on_image
create_graph()
File "classify_image.py", line 141, in create_graph
graph_def.ParseFromString(f.read())
google.protobuf.message.DecodeError: Error parsing message
To fix this, you can change a line in the source code as described here and then recompile tensorflow on your machine.
Option 2 might be a bit of work, especially if you're on a Mac.
The following steps helped me resolve the issue
update tensorflow as described here https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html
2.For me this resulted in protobuf error. To resolve I ran
$sudo pip uninstall protobuf
$sudo pip install --upgrade
https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp27-none-any.whl