Tag Archives: tensorflow

Tensorflow installation and uninstall (Anaconda version)

TensorFlow Installation and Uninstallation (Anaconda version)
Anaconda can easily configure and set up the virtual environment. Generally, we can choose PIP Install or Conda Install to download and install the package. Here are the steps of Anaconda installation:
1, anaconda search-t conda tensorflow
looks for the available tensorflow source. After this command is run, the terminal will output all available sources

2, anaconda show name
. Here the name is the name of the tensorflow column of the output source in 1, and the show command will output the specific information of the source and download the required instructions at the terminal.

such as:

conda install --channel https://conda.anacoda.org/HCC tensorflow-gpu=1.6

1.6 Is the version number, which can be set according to its own requirements.
In addition, you can choose the available channel channel without so much trouble to search package name, and you can directly use the conda command:

Conda install tensorflow – gpu = 1.6

Conda automatically selects the available channels.

1, conda list/ PIP list view package installation information in the environment
2, PIP uninstall ‘name’, name is the name of the specified package
for example:

pip uninstall tensorflow-gpu=1.6

Exception ignored in: bound method basesession__ del__ Of

Error message

Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x000000001AB286D8>>
Traceback (most recent call last):
  File "python3.5.2\lib\site-packages\tensorflow\python\client\session.py", line xxx, in __del__
TypeError: 'NoneType' object is not callable

Reason: When the Python garbage collection mechanism collects the Session object, it finds that c_api_util or Tf_session has been collected, resulting in a null pointer. The following comment is the reason given when an exception is thrown in the source code of TensorFlow

# At shutdown, `c_api_util` or `tf_session` may have been garbage
# collected, causing the above method calls to fail. In this case,
# silently leak since the program is about to terminate anyway.

The solution
Method one:

import keras.backend as K

# your code

K.clear_session()

Method 2:

import gc

# your code

gc.collect()

reference
https://github.com/tensorflow/tensorflow/issues/3388
https://www.cnblogs.com/kaituorensheng/p/4449457.html

After tensorflow installation, an error occurred while importing: importerror: DLL load failed: the specified module could not be found

(author: Chen freebie)
share a friend’s artificial intelligence tutorial. Zero basis! Easy to understand! Funny humor! And dirty jokes! You can see if it is helpful to your http://www.captainbed.net/luanpeng
Tensorflow can be installed via PIP or anaconda, but once installed, it runs in a Python script

import tensorflow as tf

ImportError: DLL Load failed: : It failed to find the specified module.
Three solutions were tried:
1) Besides TensorFlow, tensorFlow-GPU should be installed
Later, I learned that the tensorFlow-GPU was installed mainly for acceleration, and the installation did not solve this problem.
2) The second is to uninstall and reinstall
So I upgraded the PIP, uninstall tensorflow by PIP uninstall tensorflow, and then reinstall it by PIP install tensorflow, but still couldn’t solve the problem.
3) Three said to update the pillow
Pillow is an image processing library in Python, which comes with Anaconda. But maybe because Pillow’s version is older, you need to update it.

conda uninstall pillow
conda update pip
pip install pillow

Through the above three lines of commands, first uninstall the PILLOW in Anaconda, then update the PIP, and then install the latest PILLOW through the upgraded PIP. The problem is solved. Hey, it is also amazing that the Python package conflicts with TensorFlow… However, the installation of strange problems are mostly version problems, can only check the version, but most of the time is to upgrade, and sometimes to downgrade is more headache.
Resources:
https://blog.csdn.net/blueheart20/article/details/79612985

TensorFlow tf.keras.losses .MeanSquaredError

Mean-square error (MSE)

mse = tf.keras.losses.MeanSquaredError()
loss = mse([0., 0., 1., 1.], [1., 1., 1., 0.])
print('Loss: ', loss.numpy())  # Loss: 0.75

 

__init__(
    reduction=losses_utils.ReductionV2.AUTO,
    name='mean_squared_error'
)

 

__call__(
    y_true,
    y_pred,
    sample_weight=None
)

 

Tensorflow training could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR error

Tensorflow1.10 Gpu: 2080 cuda: 9
Question:
Even if the batchsize is set to 1, there will be a problem when performing the training
could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
Solutions:
Searched a lot of methods, the following list of solutions
A,

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1.0
session = tf.Session(config=config, ...)

It doesn’t work for me…

config = tf.ConfigProto() 
config.gpu_options.allow_growth = True 
sess = tf.Session(config=config)

It doesn’t work for me…
Three,
Execute SUdo RM-f ~/.nv to solve the problem
It doesn’t work for me…
Four,
Set existing automatic growth in this way; That’s how I solved it,
Note that this is different from the second one, the green one, depending on the tensorFlow version, the import is different, and I’ve listed two;

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
From tensorflow import ConfigProto
from tensorflow import InteractiveSession
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

How to Fix distributed training report terminate called after throwing an instance of’std::length_error’

In conducting the training in a distributed fashion.
INFO: sensorflow:Reduce to /replica:0/task:0/device:CPU:0 then broadcast to (‘/replica:0/task:0/device:CPU:0’,)
I0408 04:01 41.507015 140706188736256 cross_device_ops. reduce to /replica:0/task:0/device:CPU:0 then broadcast to (‘/replica:0/task:0/device:CPU:0’ ,).
INFO: tensorflow:Create CheckpointSaverHook.
I0408 04:01 44.424420 140706188736256 basic_session_run_hooks. py: 541] to create CheckpointSaverHook.
Call termination after throwing the instance ‘std::length_error’
what(): basic_string::append
Fatal Python Error: Abort
I’ve spared a lot of troubleshooting, and by reducing the number of GPUs, I can run it normally!

tensorflow import error: DLL load failed: The specified module could not be found (DLL load failed: The specified module could not be found)

The background,
Recently, after my tensorFlow version was updated, there was a problem, and the error was also very vague: DLL Load failed: the specified module could not be found. Here is my environment first:

win10 + pycharm
Anaconda3 (python3.6)
tensorflow1.9

Ii. Problem description
My own version of TensorFlow, 1.9, has been in use for almost a year and has been fine. Later, I saw that the version of TensorFlow was updated to 1.12, so I thought I would update it. However, after the update, the error of importing tensorFlow was reported. Later, even if the tensorFlow version was reduced to 1.2, the error was still reported:

When importing TensorFlow, an error message will be sent:

D:\python\anaconda\python.exe D:*****.py
Traceback (most recent call last):
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "D:\python\anaconda\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "D:\python\anaconda\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module cannot be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python/item/64-MARTAGAN/train_marta_gan.py", line 7, in <module>
    import tensorflow as tf
  File "D:\python\anaconda\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\python\anaconda\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "D:\python\anaconda\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "D:\python\anaconda\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module cannot be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

Process finished with exit code 1

My error message is in Chinese: DLL Load failed: the specified module cannot be found; Specified Module Could not be Found DLL Load Failed: The Specified Module could not be found
3. Solutions
The problem is new, the current solution is less online, after a search, finally found a similar problem and I post on making the first link: https://github.com/tensorflow/tensorflow/issues/25597, are described in detail below.
The situation of paster is similar to mine. The environment of paster is given first:

You can see that the sponsor also updated the TensorFlow version to 1.12 before encountering this problem. The following is a detailed description of how to configure your environment, and mentions that this error still occurs with CUDA9.2 and CUDA10.0 versions.
To address this problem, it has been proposed to install CUDA9.0:

The user then reconfigured his environment to CUDA9.0 and cuDNN7.05

About how to view your CUDA version, you can open CMD and type NVCC — Version in CMD to view:

Here are some of the approaches I tried, but none of them solved the problem:
(1) Install other versions of CUDA. But it didn’t solve the problem…
(2) Update VS2015, reference blog: TensorFlow installation issues and Github. But it doesn’t solve the problem…

(3) Continue to read the post on Github, found that many people are thanking the layer owner FO40255, which is the above (2) layer owner message, reinstall the.WHL file, but I did not try this method, I will mention my method later, here is a random post:

To sum up my own approach to this problem:
(1) Open CMD and enter PIP Uninstall tensorFlow, that is, uninstall TensorFlow.
(2) Re-install after uninstalling and enter PIP Install TensorFlow.
(3) After the installation of a simple test, there is no error. In short, if it breaks, reinstall it… (That’s how I got it back.)

Of course, if reinstall will not solve the problem, can consider fo40255 layer above the main method, cover again. WHL file, the file link here also is given, by the way: https://github.com/fo40225/tensorflow-windows-wheel/tree/master/1.6.0/py36/CPU/sse2

InternalError: Failed to create session. Error and solution

InternalError: Failed to create session. Errors and solutions
Introduction Error message solution

preface
This error occurred at the beginning of training Keras (using the TensorFlow as Backend) model.
Python version: 3.5.2
Keras version: 2.1.3
TensorFlow version: 1.9.0
Error message

InternalErrorTraceback (most recent call last)
< ipython-input-4-d4cc2ca313a3> in < module>
10 model.compile(loss= ‘mse’, optimizer= ‘Adam’)
11 # fit network
— > 12 history = model.fit(X_train, y_train, epochs=3000, batch_size=16, validation_data=(x_test, y_test), verbose=2, shuffle=False)
13 #history = model.fit(X,y, epochs=3000, batch_size=16, Verbose = 2, shuffle = False)
14 # plot history
/usr/local/lib/python3.5/dist – packages/keras/models. Py fit in (self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
963 initial_epoch=initial_epoch,
964 steps_per_epoch=steps_per_epoch,
— > 965 validation_steps = validation_steps)

967 966 def the evaluate (self, x = None, y = None,
/usr/local/lib/python3.5/dist – packages/keras/engine/training. Py fit in (self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1667 initial_epoch=initial_epoch,
1668 steps_per_epoch=steps_per_epoch,
-> 1669 validation_steps = validation_steps)

1671 1670 def the evaluate (self, x = None, y = None,
/usr/local/lib/python3.5/dist – packages/keras/engine/training. Py in _fit_loop (self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
1204 ins_batch[i] = ins_batch[i].toarray()
1205
-> 1206 outs = f (ins_batch)
1207 if not isinstance (outs, a list) :
1208 outs = [outs]
/usr/local/lib/python3.5/dist – packages/keras/backend/tensorflow_backend py in the call (self, inputs)
2471 feed_dict[tensor] = value
2472 fetches = self.outputs + [self.updates_op] + self.fetches
-> 2473 session = get_session()
2474 updated = session.run(fetches=fetches, Feed_dict = feed_dict,
2475 * * self session_kwargs)
/usr/local/lib/python3.5/dist – packages/keras/backend/tensorflow_backend py in get_session ()
config = 174 Tf. ConfigProto (intra_op_parallelism_threads = num_thread,
175 allow_soft_placement = True)
– & gt; 176 _SESSION = tf. The Session (config = config)
177 Session = _SESSION 178 if not _MANUAL_VAR_INIT:

/usr/local/lib/python3.5/dist – packages/tensorflow/python/client/Session. Py in init (self, Target, graph, config)
1561
1562 “”
-& gt; 1563 super(Session, self).init(target, graph, config=config)
1564 # NOTE(mrry): Create these on first __enter__ to avoid a reference cycle.
1565 self._default_graph_context_manager = None
/usr/local/lib/python3.5/dist – packages/tensorflow/python/client/session. Py in init (self, target, graph, the config).
631 if self _created_with_new_api:
632 # pylint: Disable = protected – access
– & gt; 633 self._session = tf_session.TF_NewSession(self._graph._c_graph, opts)
634 # pylint: enable=protected-access
635 else:
InternalError: Failed to create session.

The solution


found that GPU memory has been occupied by other programs.
try to close these programs, then reruns the code (no need to restart kernel) and find the problem solved!

Failed to load the native TensorFlow runtime.

 
Failed to load the native TensorFlow runtime.
 
Running CPU version of object Detection’s sample program, which appears when importing tensorFlow:
Failed to load the native TensorFlow runtime.
The solution is: PIP install –upgrade –ignore — Installed tensorFlow
Update tensorflow good

the original link: https://blog.csdn.net/sy20173081277/article/details/82977893
 

Importerror using tensorflow: DLL load failed: the specified program cannot be found

introduction

will encounter a variety of problems in the process of using tensorflow, which will be recorded here for my review and later learning.

problem description

has tensorflow installed in anaconda, and the call has this problem:

>>> import tensorflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda\envs\dl\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "D:\Anaconda\envs\dl\lib\site-packages\tensorflow\python\__init__.py", line 59, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "D:\Anaconda\envs\dl\lib\site-packages\tensorflow\core\framework\graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
  File "D:\Anaconda\envs\dl\lib\site-packages\google\protobuf\descriptor.py", line 47, in <module>
    from google.protobuf.pyext import _message
ImportError: DLL load failed: 找不到指定的程序。

solution

through careful review, yesterday I upgraded to protobuf when installing object-detection, so, call back the protobuf version, it should be ok.

pip install protobuf==3.6.0
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190708150939546.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x2c2VoYWl5YW5nMTk5Mw==,size_16,color_FFFFFF,t_70)