Cannot run the event loop when another loop is running - tensorflow

RuntimeError: Cannot run the event loop while another loop is running
Trying to setup tensorflow_federated in my local. All import versions are right:
CUDA = 10.1,
python = 3.6.9,
tensorflow = 2.2.0,
tf_federated = latest
This error is not happening in google Colab. But, happens in my local machine when I am trying to do any federated computations. I get the runtime error:
RuntimeError: Cannot run the event loop while another loop is running
RuntimeError Traceback (most recent call last)
<ipython-input-5-9c097e9baec9> in <module>
----> 1 tff.federated_computation(lambda: 'hi')()
~\AppData\Local\Continuum\anaconda3\envs\tflocal\lib\asyncio\base_events.py in run_forever(self)
426 if events._get_running_loop() is not None:
427 raise RuntimeError(
--> 428 'Cannot run the event loop while another loop is running')
429 self._set_coroutine_wrapper(self._debug)
430 self._thread_id = threading.get_ident()
RuntimeError: Cannot run the event loop while another loop is running

I got a very similar error when copy+pasting the tensorflow-federated tutorial code into a local Jupyter notebook on Mac OS. I solved it by importing and applying nest_asyncio in addition to tf and tff:
import tensorflow as tf
import tensorflow_federated as tff
import nest_asyncio
nest_asyncio.apply()
...as suggested in step 2 of the separate tensorflow github tutorial that OP linked above in a comment. Step 1 did not help me at all (and I doubt it will be of much help to those who already managed to install tensorflow_federated using e.g. some combination of conda and pip, in that order).

Try to delete import nest-asyncio

Related

Did colab suspend tensorflow 1.x?

I tried
%tensorflow_version 1.15
I used this code a couple days ago, but it doesn't work anymore since today.
The outcomes are
ValueError Traceback (most recent call last)
<ipython-input-6-24c52e77c597> in <module>()
----> 1 get_ipython().magic('tensorflow_version 1.15')
2 frames
/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
2158 magic_name, _, magic_arg_s = arg_s.partition(' ')
2159 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2160 return self.run_line_magic(magic_name, magic_arg_s)
2161
2162 #-------------------------------------------------------------------------
/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
2079 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2080 with self.builtin_trap:
-> 2081 result = fn(*args,**kwargs)
2082 return result
2083
/usr/local/lib/python3.7/dist-packages/google/colab/_tensorflow_magics.py in _tensorflow_version(line)
39
40 Your notebook should be updated to use Tensorflow 2.
---> 41 See the guide at https://www.tensorflow.org/guide/migrate#migrate-from-tensorflow-1x-to-tensorflow-2."""
42 ))
43
ValueError: Tensorflow 1 is unsupported in Colab.
Your notebook should be updated to use Tensorflow 2.
See the guide at https://www.tensorflow.org/guide/migrate#migrate-from-tensorflow-1x-to-tensorflow-2.
Is there any method I can try to fix it or does it means that colab will not support tensorflow 1.x anymore?
Google Colab removed support for Tensorflow 1, and it is not possible to use %tensorflow_version 1.x magic anymore. You must remove this instruction from your code if you have it.
Also the default python version as I update this answer is python 3.8 which is not compatible with tensorflow 1.x.
To make everything work you first have to downgrade python. Python 3.6 should work. As suggested by #s-abbaasi here's a guide on how to do so:
%%bash
MINICONDA_INSTALLER_SCRIPT=Miniconda3-4.5.4-Linux-x86_64.sh
MINICONDA_PREFIX=/usr/local
wget https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT
chmod +x $MINICONDA_INSTALLER_SCRIPT
./$MINICONDA_INSTALLER_SCRIPT -b -f -p $MINICONDA_PREFIX
Then add to path:
import sys
_ = (sys.path.append("/usr/local/lib/python3.6/site-packages"))
At this point you can manually uninstall and re-install tensorflow through pip:
!pip uninstall tensorflow
!pip install tensorflow-gpu==1.15
Doing just so I sometimes encounter some errors due to the Cuda version. If this happens to you, you can execute the following:
!apt install --allow-change-held-packages libcudnn7=7.4.1.5-1+cuda10.0
The most appropriate version of cuda and libcudnn to use with the tensorflow version you want to install can be found here.
The versions available of libcudnn can be found with the following command:
!apt list -a libcudnn7
This will list all libcudnn7 versions available.
I was having the same problems while trying to use StyleGAN2-ADA, which only supports TensorFlow 1.
I found out that unfortunately Google Colab removed support for TensorFlow 1 in their latest release of 2022/8/11.
'Removed support for TensorFlow 1'
You can find more information in their notebook Release-Notes: https://colab.research.google.com/notebooks/relnotes.ipynb

Error when "import tensorflow as tf" in colab

When I "import tensorflow as tf" in google colab I am getting the following error. Any idea how to fix it ?
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-37-07e0f991eb97> in <module>()
----> 1 import tensorflow as tf
2
3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/client/pywrap_tf_session.py in <module>()
17 # pylint: disable=invalid-import-order,g-bad-import-order, wildcard-import, unused-import
18 from tensorflow.python import pywrap_tensorflow
---> 19 from tensorflow.python.client._pywrap_tf_session import *
20 from tensorflow.python.client._pywrap_tf_session import _TF_SetTarget
21 from tensorflow.python.client._pywrap_tf_session import _TF_SetConfig
ImportError: SystemError: <built-in method __contains__ of dict object at 0x7f79368db0a0> returned a result with an error set
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
This issue has been fixed now in 2.8.0 stable version. If it still persists ,please install Numpy 1.21.5 manually and Try again.
I've post a similar error on tensorflow github issue.
Tensorflow 2.8.0 | Error Message: ImportError: SystemError.
My solution (since I'm using conda environment on Windows 10), is to downgrade the Tensorflow version to 2.7.0. Hope help helps.

CuPy on AMD GPU causing an ImportError

I'm trying to use/test the (experimental) AMD GPU support of CuPy (8.1.0). In short: I'm having an ImportError.
I followed the instructions for installing ROCm on a Ubuntu focal machine (with a gfx906/Radeon VII (Vega 20) card).
From the doc I set the mentioned variables (with adjusted HCC_AMDGPU_TARGET), the ROCM_HOME and export PATH=$PATH:/opt/rocm/bin:/opt/rocm/profiler/bin:/opt/rocm/opencl/bin.
pip3 install -v --no-cache-dir cupy succeeded without any complaints. Just a few compiler notes and warnings because of the -v option
Now when I try to import cupy it's throwing an ImportError.
In [1]: import cupy
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/cupy/__init__.py in <module>
19 message='can\'t resolve package from __spec__')
---> 20 from cupy import core # NOQA
21 except ImportError as e:
/usr/local/lib/python3.8/dist-packages/cupy/core/__init__.py in <module>
----> 1 from cupy.core import core # NOQA
2 from cupy.core import internal # NOQA
3
cupy/core/core.pyx in init cupy.core.core()
cupy/core/_routines_manipulation.pyx in init cupy.core._routines_manipulation()
cupy/core/_routines_indexing.pyx in init cupy.core._routines_indexing()
cupy/core/_routines_math.pyx in init cupy.core._routines_math()
cupy/core/_reduction.pyx in init cupy.core._reduction()
cupy/core/_cub_reduction.pyx in init cupy.core._cub_reduction()
ModuleNotFoundError: No module named 'cupy.cuda.cub'
The above exception was the direct cause of the following exception:
ImportError Traceback (most recent call last)
<ipython-input-1-329ec5cf1bc8> in <module>
----> 1 import cupy
/usr/local/lib/python3.8/dist-packages/cupy/__init__.py in <module>
39 original error: {}'''.format(_exc_info[1])) # NOQA
40
---> 41 raise ImportError(_msg) from e
42
43
ImportError: CuPy is not correctly installed.
If you are using wheel distribution (cupy-cudaXX), make sure that the version of CuPy you installed matches with the version of CUDA on your host.
Also, confirm that only one CuPy package is installed:
$ pip freeze
If you are building CuPy from source, please check your environment, uninstall CuPy and reinstall it with:
$ pip install cupy --no-cache-dir -vvvv
Check the Installation Guide for details:
https://docs.cupy.dev/en/latest/install.html
original error: No module named 'cupy.cuda.cub'
What should I do next? What should I look for in the compilation process? Thanks in advance
EDIT On request here the complete stdout and stderr of the installation process and a few io's of the console: https://seafile.cloud.uni-hannover.de/d/68862cd1057e47d180aa/
To summarize the discussion in the comments so far:
For CuPy v8.x (the latest stable releases): Up to v8.1 the offending cupy.cuda.cub module is not built in ROCm/HIP environments, which will hopefully be fixed in v8.2 (see ticket).
For CuPy v9.x (the master branch): It should just work as long as rocPRIM and hipCUB are correctly installed. It's likely that the OP's local environment is not correctly set up, leading to cupy.cuda.cub module not built and thus ImportError.

Tensorflow with R and Anaconda - error "Could not import PIL.Image. The use of `load_img` requires PIL"

There are some answers to this question in a Python environment, but the solutions did not work for my RStudio environment. Here is my code:
library(keras)
library(tensorflow)
use_condaenv("tf")
train_dir = "C:/training_images/"
train_datagen <- image_data_generator(rescale = 1/255)
validation_datagen <- image_data_generator(rescale = 1/255)
train_generator <- flow_images_from_directory(
train_dir,
train_datagen,
target_size = c(150, 150),
batch_size = 20,
class_mode = "binary"
)
batch <- generator_next(train_generator)
The code works until the last "batch" line where it explodes like this:
Error in py_iter_next(it, completed) :
ImportError: Could not import PIL.Image. The use of `load_img` requires PIL.
Detailed traceback:
File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\iterator.py", line 104, in __next__
return self.next(*args, **kwargs)
File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\iterator.py", line 116, in next
return self._get_batches_of_transformed_samples(index_array)
File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\iterator.py", line 230, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\utils.py", line 108, in load_img
raise ImportError('Could not import PIL.Image. '
R version 3.6.1
Conda version 4.7
Python version 3.7
I had this same problem
After a few hours of looking, I came up with a solution that worked for me.
I used this code for solving the PIL problem. I tried using anaconda prompt but this code worked in r for me...
reticulate::py_install("pillow",env=tf)
I came up with this error next...
loaded runtime CuDNN library: 7.4.2 but source was compiled with: 7.6.0.
Make sure you have the correct cudnn version installed. For me it was CUDA 10 with 7.6.0 cudnn with 10. The output of the error will tell you which one to use.
Make sure you have cleaned any extra path variables that are in your environmental variables from installing previous versions.
I'm using windows 10
gpu = GeForce GTX 1060 with Max-Q Design
R - 3.6.1
tensorflow = 1.13
python = 3.7
anaconda = Anaconda3–2019.03-Windows-x86_64.exe
I ended up uninstalling Anaconda altogether, which made troubleshooting the remaining errors in the Python connection to R much simpler.
I had same problem with "Deep Learning with R" CNN example on Win7. I solved it like this:
I added Anaconda3 paths to PATH. In my case it was Windows so paths were like that:
C:\Anaconda3\Scripts;C:\Anaconda3\Library\bin By default there were no paths to conda in $PATH.
installed pillow (it contains PIL) to python with:
pip install pillow
configured r-reticulate.
This answer Could not import PIL.Image even if Pillow already installed? helped me. I had pillow already but conda environment wasn't configured properly so pillow wasn't visible.
Also install Nvidia CUDA if you don't have it - you need it too for tensorflow.

Tensorflow importing error in Jupyter notebook

So I managed all other packages (such as cv2, pandas, matplotlib..) work well in the default kernel or Python 3 kernel, except Tensorflow.
Tensorflow does not import with Kernel: "Python 3" or "Python default".
import tensorflow as tf
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-41389fad42b5> in <module>()
----> 1 import tensorflow as tf
ImportError: No module named tensorflow
When I choose "env: tensorflow" kernel, my other packages such as pandas, cv2 all collapse. Thus, I try to use "default" kernel or "Python 3" kernel.
Thanks a lot.
You haven't loaded the tensorflow kernel, please follow instructions here.