Tensorflow - import error in CIFAR tutorial - tensorflow

Recently, I installed tensorflow and got python import error in CIFAR tutorial.
I'm using Mac OS X, CPU only, Python 2.7.
$ python cifar10_train.py
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
Traceback (most recent call last):
File "cifar10_train.py", line 120, in
tf.app.run()
File "/Users/sunwoo/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 30, in run
sys.exit(main(sys.argv[:1] + flags_passthrough))
File "cifar10_train.py", line 116, in main
train()
File "cifar10_train.py", line 76, in train
class _LoggerHook(tf.train.SessionRunHook):
AttributeError: 'module' object has no attribute 'SessionRunHook'
How can I import tf.train.SessionRunHook?

It looks like you are using the master branch of cifar10_train.py, with an older installed version of TensorFlow (0.11 or earlier). The master branch was recently modified to use a new API, which wasn't available in TensorFlow 0.11 or earlier.
There are two ways to fix this problem. Either upgrade TensorFlow to version 0.12 or later, or check out the r0.11 branch of the TensorFlow source, and use the version of cifar10_train.py from that branch.

Related

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

Errors in converting Tensorflow Model to Tensorflow Lite Model

I wanted to use yolov4-tiny in the Tensorflow lite framework to count objects that cross a virtual line in a video.
I converted my darknet weights trained from AlexeyAB's repo using these commands:
python save_model.py --weights yolov4-tiny.weights --output ./checkpoints/yolov4-tiny-608-tf --input_size 608 --model yolov4 --tiny --framework tflite
python convert_tflite.py --weights ./checkpoints/yolov4-tiny-608-tf --output ./checkpoints/yolov4-tiny-608.tflite
You can find the convert_tflite.py here
The first command is successful using numpy==1.19.0. However, the second one shows these errors:
loc("batch_normalization/moving_mean"): error: is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 213, in toco_convert_protos
enable_mlir_converter)
File "C:\Python37\lib\site-packages\tensorflow\lite\python\wrap_toco.py", line 38, in wrapped_toco_convert
enable_mlir_converter)
Exception: <unknown>:0: error: loc("batch_normalization/moving_mean"): is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "convert_tflite.py", line 76, in <module>
app.run(main)
File "C:\Python37\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "C:\Python37\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "convert_tflite.py", line 71, in main
save_tflite()
File "convert_tflite.py", line 45, in save_tflite
tflite_model = converter.convert()
File "C:\Python37\lib\site-packages\tensorflow\lite\python\lite.py", line 762, in convert
result = _convert_saved_model(**converter_kwargs)
File "C:\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 648, in convert_saved_model
enable_mlir_converter=True)
File "C:\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 216, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc("batch_normalization/moving_mean"): is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable
I have tried other versions of Tensorflow (2.2, 2.3, 2.4) but I had no luck. What should I do?
There is a similar issue raised here: Tensorflow Issue 44790
Here are my system details:
Windows 10, x64
GeForce GTX 1060
NVIDIA Driver 460.89
CUDA 11.0.3
CuDNN 8.0.5.39
Python 3.7.2
pip install tensorflow==2.3.0rc0
and restart runtime before starting conversion
I resolved the problem by following a thread on Github issues.
In google colab, I had this issue if I used the default TF version, which was 2.4.0 or above.
Running !pip install tensorflow==2.3.0 and restarting the runtime, then converting corrected the issue.
For me this solved my problem :
import tensorflow as tf
if tf.__version__ != '2.3.0-rc0':
!pip uninstall -y tensorflow
!pip install tensorflow-gpu==2.3.0rc0
And restart runtime, in order to use newly installed versions.

Tensor2tensor: My customed problem never registered with registry problems

Description
I am following a tutorial of microsoft from this website
to get a model to inference Chinese couplet.
Now I have trained the model on Google cloud and I can also get good inference.
Howerver, when I am constructing inference service, I found my function to communicate with tensorflowserverapi can't find my problem get registered.
I also have trained this model for one step and add t2t_trainer --registry_help, and I can see my problem is actually registered under problems Problems.
My code is just the same as the one in this repo script
And here is my test code:
from up2down_model.up2down_model import up2down
upper_couplet = input()
up2down.get_down_couplet([upper_couplet])
Environment information:
OS: Ubuntu 20.04
$ pip freeze | grep tensor
tensor2tensor 1.15.6
tensorboard 1.14.0
tensorflow 1.14.0
tensorflow-addons 0.10.0
tensorflow-datasets 1.3.0
tensorflow-estimator 1.14.0
tensorflow-gan 2.0.0
tensorflow-hub 0.8.0
tensorflow-metadata 0.22.0
tensorflow-probability 0.7.0
tensorflow-serving-api 1.14.0
$ python -3.7.7
Error logs:
raceback (most recent call last):
File "/home/enigma/anaconda3/envs/NLP/lib/python3.7/site-packages/tensor2tensor/utils/registry.py", line 509, in problem
return Registries.problems[spec.base_name](
File "/home/enigma/anaconda3/envs/NLP/lib/python3.7/site-packages/tensor2tensor/utils/registry.py", line 254, in __getitem__
(key, self.name, display_list_by_prefix(sorted(self), 4)))
KeyError: 'translate_up2down never registered with registry problems. Available:
All problems without my own
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 1, in <module>
from up2down_model.up2down_model import up2down
File "/home/enigma/Desktop/NLP/service/up2down_model/up2down_model.py", line 85, in <module>
up2down = up2down_class(FLAGS,server_address) # inference model
File "/home/enigma/Desktop/NLP/service/up2down_model/up2down_model.py", line 40, in __init__
self.problem = registry.problem(self.FLAGS.problem)
File "/home/enigma/anaconda3/envs/NLP/lib/python3.7/site-packages/tensor2tensor/utils/registry.py", line 513, in problem
return env_problem(problem_name, **kwargs)
File "/home/enigma/anaconda3/envs/NLP/lib/python3.7/site-packages/tensor2tensor/utils/registry.py", line 527, in env_problem
ep_cls = Registries.env_problems[env_problem_name]
File "/home/enigma/anaconda3/envs/NLP/lib/python3.7/site-packages/tensor2tensor/utils/registry.py", line 254, in __getitem__
(key, self.name, display_list_by_prefix(sorted(self), 4)))
KeyError: 'translate_up2down never registered with registry env_problems. Available:\n reacher:\n * reacher_env_problem\n tic:\n * tic_tac_toe_env_problem'

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.

Tensorflow running version with CUDA on CPU only

I am running tensorflow on a cluster. I installed the CUDA version. It works without any problem. To use GPU, I have to request resource. Now, I want to run only on CPU without requesting GPU resources.
On import tensorflow as tf, I get the error:
ImportError: /home/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: cuDevicePrimaryCtxRetain
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
I realized I had to run only on CPU and set environment variable CUDA_VISIBLE_DEVICES="". I did it through export on bash as well as on python script both. I still get the same error.
How can I use the GPU version of tensorflow on CPU only? Is it possible? Some other pages e.g. Run Tensorflow on CPU suggest to change session config parameter. But since I get the error on import itself, I don't think that is applicable or helpful.
Stack Trace:
File "<FileNameReplaced>", line 10, in <module>
import tensorflow as tf
File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import *
File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 51, in <module>
from tensorflow.python import pywrap_tensorflow
File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/***/.pyenv/versions/2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
Additional Info:
Version: 1.1.0
Take a look at issue #2175 in the TensorFlow repo, where this problem is discussed. What worked for me was to set CUDA_VISIBLE_DEVICES="-1", not "", following to the documentation of CUDA environment variables. It may produce some warnings when you first create a session, but the computation should work alright. If you are using Bash or similar, you can do this by exporting it before running the program, like you say, or just with:
$ CUDA_VISIBLE_DEVICES="-1" python my_program.py
Alternatively, a probably more portable solution is to have Python itself set the environment variable before TensorFlow is imported by any module:
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import tensorflow as tf
Another user suggests creating your session in the following way:
import tensorflow as tf
session_conf = tf.ConfigProto(
device_count={'CPU' : 1, 'GPU' : 0},
allow_soft_placement=True,
log_device_placement=False
)
with tf.Session(config=session_conf) as sess:
sess.run(...)
This should allow you for more fine-grained control too (e.g. I have two GPUs but only want TensorFlow to use one of them).