ubuntu下利用pyinstaller将tensorflow进行打包

项目中采用tensoflow进行目标检测,代码为https://github.com/yangxue0827/R-DFPN_FPN_Tensorflow

因为我的ubuntu16.04原本配置有detectron,采用cuda8.0和cudnn6.0,因此在配置tensorflow并没有用1.2.0(需要cudnn5.1),而使用的是1.4.0,可以利用现有的cudnn6.0

配置完毕后打包tensorflow:

import tensorflow as tf
import tensorflow.contrib.slim as slim

 首先是import tensorflow as tf出错,出现的问题主要是:

问题1:

Exception: Versioning for this project requires either an sdist tarball, or access to an upstream git repository. It's also pup.cfg and the argument given to pbr.version.VersionInfo. Project name mock was given, but was not able to be found.

解决办法:

(1)网上的,见https://blog.csdn.net/wuruiaoxue/article/details/78891935  但是我自己使用后发现并没有任何效果

(2)我自己的方法:首先pip list,查看自己的pbr版本:

ubuntu下利用pyinstaller将tensorflow进行打包_第1张图片.

然后在上述代码前添加:也就是强制使用本机拥有的pbr

import os
os.environ["PBR_VERSION"]='4.2.0'

其次是import tensorflow.contrib.slim as slim出错,出现的问题主要是:

问题2:

Traceback (most recent call last):
  File "lala.py", line 11, in 
    import tensorflow.contrib.slim as slim
  File "/tmp/pip-install-3Rwms2/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
  File "tensorflow/contrib/__init__.py", line 28, in 
  File "/tmp/pip-install-3Rwms2/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
  File "tensorflow/contrib/cudnn_rnn/__init__.py", line 33, in 
  File "/tmp/pip-install-3Rwms2/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
  File "tensorflow/contrib/cudnn_rnn/python/ops/cudnn_rnn_ops.py", line 21, in 
  File "/tmp/pip-install-3Rwms2/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
  File "tensorflow/contrib/rnn/__init__.py", line 83, in 
  File "/tmp/pip-install-3Rwms2/pyinstaller/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
  File "tensorflow/contrib/rnn/python/ops/gru_ops.py", line 33, in 
  File "tensorflow/contrib/util/loader.py", line 55, in load_op_library
  File "tensorflow/python/framework/load_library.py", line 56, in load_op_library
  File "tensorflow/python/framework/errors_impl.py", line 473, in __exit__
tensorflow.python.framework.errors_impl.NotFoundError: tensorflow/contrib/util/tensorflow/contrib/rnn/python/ops/_gru_ops.so: cannot open shared object file: No such file or directory

出现错误的原因是pyinstaller不能智能地跟踪到这写.so文件的位置,打包后默认的so位置还是存储在 tensorflow/contrib/util/tensorflow/contrib/rnn/python/ops/_gru_ops.so,因此我们就需在import tensorflow.contrib.slim as slim的时候,让代码能定位到这些.so的位置。由于pyinstaller打包后成为一个exe文件,因此我们只需要exe文件可以直接调用同目录下的这些so文件即可。

解决办法:

① 找到中如下函数,进行修改,这样一来在import tensorflow.contrib.slim as slim的时候,代码在exe的同目录下找到这些文件。

def get_path_to_datafile(path):
  """Get the path to the specified file in the data dependencies.

  The path is relative to tensorflow/

  Args:
    path: a string resource path relative to tensorflow/

  Returns:
    The path to the specified file present in the data attribute of py_test
    or py_binary.

  Raises:
    IOError: If the path is not found, or the resource can't be opened.
  """
  data_files_path = _os.path.dirname(_inspect.getfile(_sys._getframe(1)))
  #return _os.path.join(data_files_path, path)  #打包完成取消该行注释,把下面两行注释
  root = _os.path.dirname(sys.executable) #用于打包
  return _os.path.join(root, path)        #用于打包

② 在tensorflow/contrib文件夹下搜索.so文件,也就是tensorflow.contrib.slim所需要的库文件,如下,可以看到前面报错的修饰的文件_gru_ops.so就在其中。

ubuntu下利用pyinstaller将tensorflow进行打包_第2张图片

③ 对tensorflow进行打包,打包后生成的exe文件在dist中,此时将上面的.so库都拷贝到dist文件下,与exe文件同目录。这时候运行就会发现打包成功

你可能感兴趣的:(tensorflow,ubuntu)