Problem importing tensorflow / keras-rl from tutorial


#1

I’ve followed the tutorial for using tensorflow and installed keras-rl alongside it in the virtualenv

But I encountered the following problems with the import…

2018-02-14 21:44:03,954 [Thread-46   ] [hbp_nrp_cles] [ERROR]  Error in Transfer Function (Runtime): local variable 'Sequential' referenced before assignment
Traceback (most recent call last):
  File "/home/akshay/Dokumente/NRP/CLE/hbp_nrp_cle/hbp_nrp_cle/tf_framework/_TransferFunction.py", line 200, in run
    return self._func(*self._params)
  File "<string>", line 46, in init_DRLagent
UnboundLocalError: local variable 'Sequential' referenced before assignment
Using TensorFlow backend.
2018-02-14 21:44:05,848 [Thread-46   ] [hbp_nrp_cles] [CRITICAL]  Unhandled exception of type <type 'exceptions.RuntimeError'>: module compiled against API version 0xb but this version of numpy is 0xa
2018-02-14 21:44:05,849 [Thread-46   ] [hbp_nrp_cles] [ERROR]  None
Traceback (most recent call last):
  File "/home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 37, in <module>
    from tensorflow.python import pywrap_dlopen_global_flags
ImportError: cannot import name pywrap_dlopen_global_flags
2018-02-14 21:44:05,850 [Thread-46   ] [hbp_nrp_cles] [CRITICAL]  Unhandled exception of type <type 'exceptions.RuntimeError'>: module compiled against API version 0xb but this version of numpy is 0xa
2018-02-14 21:44:05,850 [Thread-46   ] [hbp_nrp_cles] [ERROR]  None
Traceback (most recent call last):
  File "/home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 37, in <module>
    from tensorflow.python import pywrap_dlopen_global_flags
ImportError: cannot import name pywrap_dlopen_global_flags
[libprotobuf FATAL external/protobuf_archive/src/google/protobuf/stubs/common.cc:68] This program requires version 3.5.0 of the Protocol Buffer runtime library, but the installed version is 3.4.0.  Please update your library.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.  (Version verification failed in "google/protobuf/descriptor.pb.cc".)
terminate called after throwing an instance of 'google::protobuf::FatalException'

using the following TF, which is pretty much the same as the TF in the advanced tensorflow tutorial for object detection

# internal keras-rl agent to persist
@nrp.MapVariable("agent", initial_value=None, scope=nrp.GLOBAL)
@nrp.MapVariable("bridge", initial_value=None, scope=nrp.GLOBAL)
# subscribe to images from the robot
@nrp.MapRobotSubscriber("camera", Topic('/husky/camera', sensor_msgs.msg.Image))
@nrp.MapRobotSubscriber("vel", Topic('/husky/cmd_vel', geometry_msgs.msg.Twist))
@nrp.Robot2Neuron()
def init_DRLagent(t, agent, bridge, camera, vel):
    
    # initialize the keras-rl agent
	if agent.value is None and camera.value is not None:
	    print("INITIALIZING AGENT") 
	    try:
	        # import keras-rl in NRP through virtual env
	        import site, os
	        site.addsitedir(os.path.expanduser('/home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages'))
	        
	        from keras.models import Model
	        from keras.models import Sequential
	        from keras.layers import Dense
	        from keras.layers import Activation
	        from keras.layers import Flatten
	        from keras.layers import Input
	        from keras.layers import concatenate
	        from keras.optimizers import Adam
	        from keras.optimizers import RMSprop
	        from rl.agents import DDPGAgent
	        from rl.memory import SequentialMemory
	        from rl.random import OrnsteinUhlenbeckProcess
	    except:
	        clientLogger.info("Unable to import keras-rl, did you change the path in the transfer function?")
	        raise
	    
	    import numpy as np
	    # OpenCV bridge for ROS <-> CV image conversion
	    from cv_bridge import CvBridge
	    bridge.value = CvBridge()
	    # convert the ROS image to an OpenCV image and Numpy array
	    cv_image = bridge.value.imgmsg_to_cv2(camera.value, "rgb8")
	    numpy_image = np.expand_dims(cv_image, axis=0)
	    obs_shape = numpy_image.shape
        nb_actions = 2
        
        # create the nets for rl agent
        # actor net
        #from keras.models import Sequential
        actor = Sequential()
        actor.add(Flatten(input_shape=(1,) + obs_shape))#camera.value.shape))
        actor.add(Dense(32))
        actor.add(Activation('relu'))
        actor.add(Dense(32))
        actor.add(Activation('relu'))
        actor.add(Dense(32))
        actor.add(Activation('relu'))
        actor.add(Dense(nb_actions))
        actor.add(Activation('sigmoid'))
        print(actor.summary())
...

Any help would be appreciated.


#2

There is a good chance that the TF is run before any camera image is transmitted (the camera plugin takes a while to load). In that case, that if block that would import Sequential is not executed.

Apart from that, I think the question should be tagged as Neurorobotics. For some reason, I do not have the permission to do that, so I recommend that you do it.


Nrp tensorflow tutorial confusion about virtualenv
#3

Hi Georg,

Thanks for your response. I have tagged the question for Neurorobotics.

Oh yes, the code I was using ‘looked’ properly indented in my editor and not like how it looks here - particularly - the assignment of Sequential was aligned inside the if statemtent along with the import of Sequential. That brought me to check the tabs and spacing in the editor - i read python doesnt work well with tabs - so I made sure identation was created only out of spaces and aligned everything under the if statement.
Now the error

UnboundLocalError: local variable 'Sequential' referenced before assignment

is gone.- Thanks!

Lesson learned - python is tricky in that sense. One should be careful only to use spaces for identation, even if it looks properly indented in the editor

Unfortunately I still get the other errors

 Using TensorFlow backend.
2018-02-15 18:55:01,909 [Thread-46   ] [hbp_nrp_cles] [CRITICAL]  Unhandled exception of type <type 'exceptions.RuntimeError'>: module compiled against API version 0xb but this version of numpy is 0xa
2018-02-15 18:55:01,909 [Thread-46   ] [hbp_nrp_cles] [ERROR]  None
Traceback (most recent call last):
  File "/home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 37, in <module>
    from tensorflow.python import pywrap_dlopen_global_flags
ImportError: cannot import name pywrap_dlopen_global_flags
2018-02-15 18:55:01,910 [Thread-46   ] [hbp_nrp_cles] [CRITICAL]  Unhandled exception of type <type 'exceptions.RuntimeError'>: module compiled against API version 0xb but this version of numpy is 0xa
2018-02-15 18:55:01,910 [Thread-46   ] [hbp_nrp_cles] [ERROR]  None
Traceback (most recent call last):
  File "/home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 37, in <module>
    from tensorflow.python import pywrap_dlopen_global_flags
ImportError: cannot import name pywrap_dlopen_global_flags
[libprotobuf FATAL external/protobuf_archive/src/google/protobuf/stubs/common.cc:68] This program requires version 3.5.0 of the Protocol Buffer runtime library, but the installed version is 3.4.0.  Please update your library.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.  (Version verification failed in "google/protobuf/descriptor.pb.cc".)
terminate called after throwing an instance of 'google::protobuf::FatalException'

with the code changed to

# internal keras-rl agent to persist
@nrp.MapVariable("agent", initial_value=None, scope=nrp.GLOBAL)
@nrp.MapVariable("bridge", initial_value=None, scope=nrp.GLOBAL)
# subscribe to images from the robot
@nrp.MapRobotSubscriber("camera", Topic('/husky/camera', sensor_msgs.msg.Image))
@nrp.MapRobotSubscriber("vel", Topic('/husky/cmd_vel', geometry_msgs.msg.Twist))
@nrp.Robot2Neuron()
def init_DRLagent(t, agent, bridge, camera, vel):
    # initialize the keras-rl agent
    if agent.value is None and camera.value is not None:
        # import keras-rl in NRP through virtual env
        import site, os
        site.addsitedir(os.path.expanduser('/home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages'))
        from keras.models import Model, Sequential
        from keras.layers import Dense, Activation, Flatten, Input, concatentate
        from keras.optimizers import Adam, RMSprop
        from rl.agents import DDPGAgent
        from rl.memory import SequentialMemory
        from rl.random import OrnsteinUhlenbeckProcess
        import numpy as np
        # OpenCV bridge for ROS <-> CV image conversion
        from cv_bridge import CvBridge
        bridge.value = CvBridge()
        # convert the ROS image to an OpenCV image and Numpy array
        cv_image = bridge.value.imgmsg_to_cv2(camera.value, "rgb8")
        numpy_image = np.expand_dims(cv_image, axis=0)
        obs_shape = numpy_image.shape
        nb_actions = 2
        # create the nets for rl agent
        # actor net
        #from keras.models import Sequential
        actor = Sequential()
        actor.add(Flatten(input_shape=(1,) + obs_shape))
        actor.add(Dense(32))
        actor.add(Activation('relu'))
        actor.add(Dense(32))
        actor.add(Activation('relu'))
        actor.add(Dense(32))
        actor.add(Activation('relu'))
        actor.add(Dense(nb_actions))
        actor.add(Activation('sigmoid'))
        print(actor.summary())
        #...rest of code

-There is an Importerror regarding tensorflow

this is the exception from tensorflow/tensorflow/python/pywrap_tensorflow.py

...
try:
  # This import is expected to fail if there is an explicit shared object
  # dependency (with_framework_lib=true), since we do not need RTLD_GLOBAL.
  from tensorflow.python import pywrap_dlopen_global_flags
  _use_dlopen_global_flags = True
except ImportError:
  _use_dlopen_global_flags = False
...

-and protobuf - i have checked profobuf in the virtualenv - it is indeed Version 3.5.1 and not 3.4.0 like the error says

(kerasrl_venv) akshay@akshay-mbp:~/.opt/kerasrl_venv$ pip show protobuf
Name: protobuf
Version: 3.5.1
Summary: Protocol Buffers
Home-page: https://developers.google.com/protocol-buffers/
Author: protobuf@googlegroups.com
Author-email: protobuf@googlegroups.com
License: 3-Clause BSD License
Location: /home/akshay/.opt/kerasrl_venv/lib/python2.7/site-packages
Requires: setuptools, six

-And somehow it doesnt like a mismatch with numpy

but the tensorflow tutorial object detection also imports numpy within the TF


Could it have something to do with my python path? Any ideas?


#4

Update :
When I run ipython in the virtual env and run the same imports and use them there is no error
-Does the venv need to be activated when the TF imports from it?

Have cleared the PYTHONPATH and ran the configure_nrp script again
Same error

Have removed keras-rl and Keras from the venv, then simply tried the TF from the tensorflow tutorial where a simple message is printed to the clientlogger.
Same error


#5

Hi Akshay

Concerning the python virtualenv, I replied to your other thread there: