简体   繁体   English

如何将带有tenserflow的python项目部署到exe文件?甚至有可能吗?

[英]How to deploy python project with tenserflow to exe file?And is it even possible?

I have a project based on ImageAI (the code is taken directly from the documentation), which uses tenserflow, keras and other dependencies, and it needs to be packed into an exe file.我有一个基于ImageAI的项目(代码直接取自文档),使用了tenserflow、keras等依赖,需要打包成exe文件。

The problem is that, so far, I haven't been able to do it, I've been using the pyinstaller library.But the problem I have with it is that I can not create a working exe file.问题是,到目前为止,我一直无法做到这一点,我一直在使用 pyinstaller 库。但我遇到的问题是我无法创建工作的 exe 文件。 Exe file closes in a fraction of a second.All hope is on you. exe文件在几分之一秒内关闭。所有希望都在你身上。

Now in more detail, here is the code itself:现在更详细地说,这里是代码本身:

from imageai.Detection import ObjectDetection
import easygui
import os


path = easygui.fileopenbox();
print(path);

exec_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(exec_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()


list = detector.detectObjectsFromImage(
    input_image = os.path.join(exec_path, path),
    output_image_path= os.path.join(exec_path, "new_objects.jpg"),
    minimum_percentage_probability = 60)

I use the virtual environment "virtual env", in order to store all dependencies there, and nothing extra.我使用虚拟环境“virtual env”,以便将所有依赖项存储在那里,没有额外的东西。 At first I tried packing like this: pyinstaller detect.py, that is the most usual packing, but on this I got warnings that the right tenserflow files were not found, such as these:起初我尝试像这样打包:pyinstaller detect.py,这是最常用的打包,但在此我收到警告说找不到正确的 tenserflow 文件,例如:

166260 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.mixed_precision" not found!
166440 WARNING: Hidden import "tensorflow_core._api.v1.compat.v2.keras.callbacks" not found!
166615 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.estimator" not found!
166626 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.estimator.tpu" not found!
168300 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.estimator" not found!
168307 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.applications.nasnet" not found!
168839 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.activations" not found!
168854 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.preprocessing.text" not found!
169538 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.metrics" not found!
169711 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.applications.inception_v3" not found!
169815 WARNING: Hidden import "tensorflow_core._api.v1.compat.v1.keras.datasets.cifar10" not found!

And also some problems in the syntax of the library (although the code works correctly):还有一些库的语法问题(尽管代码可以正常工作):

Traceback (most recent call last):
  File "<string>", line 21, in walk_packages
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\notebook\terminal\__init__.py", line 4, in <module>
    import terminado
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\terminado\__init__.py", line 7, in <module>
    from .websocket import TermSocket
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\terminado\websocket.py", line 18, in <module>
    import tornado.web
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\tornado\web.py", line 84, in <module>
    from tornado.concurrent import Future, future_set_result_unless_cancelled
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\tornado\concurrent.py", line 28, in <module>
    import asyncio
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\asyncio\__init__.py", line 21, in <module>
    from .base_events import *
  File "c:\users\sleping\appdata\local\programs\python\python37-32\lib\site-packages\asyncio\base_events.py", line 296
    future = tasks.async(future, loop=self)
                       ^
SyntaxError: invalid syntax

And everything in this spirit, after that I realized that it is necessary to connect the hidden imports and immediately connect the model (yolo.h5), which is actually what I did:而一切都是本着这种精神,后来才意识到需要连接隐藏的import,马上连接model(yolo.h5),其实我是这样做的:

pyinstaller --paths ..\env\Lib\site-packages --add-data yolo.h5;. --hidden-import=h5py;. --hidden-import=h5py.defs --hidden-import=h5py.utils --hidden-import=h5py.h5ac --hidden-import=h5py._proxy detect.py

Unfortunately the result is still disappointing, although there is no message about the lack of tenserflow, and some other errors are also gone, in their place appeared new ones, such as:不幸的是,结果仍然令人失望,虽然没有关于缺乏 tenserflow 的消息,并且其他一些错误也消失了,取而代之的是新的错误,例如:

241397 WARNING: Hidden import "pkg_resources.py2_warn" not found!
241417 WARNING: Hidden import "pkg_resources.markers" not found!

or:或者:

255797 WARNING: lib not found: libopenblas.QVLO2T66WEPI7JZ63PS3HMOHFEY472BC.gfortran-win_amd64.dll dependency of C:\bitwise\pyinstaller_demo\env\Lib\site-packages\numpy\core\_multiarray_umath.cp37-win_amd64.pyd

And the problems with invalid syntax are the same, exactly the same as above.和无效语法的问题是一样的,和上面一模一样。

To sum it up, I'm sorry the question got so big, I apologize for that.总结一下,很抱歉问题太大了,对此我深表歉意。 But I need this exe file badly, very badly.但是我非常非常非常需要这个exe文件。 What solutions are available?有哪些可用的解决方案? Is it at all possible to pack such a project into an exe file?是否有可能将这样的项目打包成 exe 文件? If yes, by what means is it possible?如果是,通过什么方式是可能的? Or is it possible to solve the problem with pyinstaller?还是可以用pyinstaller解决问题?

Here are the dependencies I put in: tensorflow==2.4.0, keras==2.4.3, numpy==1.19.3, pillow==7.0.0, scipy==1.4.1, h5py==2.10.0, matplotlib==3.3.2, opencv-python, keras-resnet==0.2.0, imageai.In the project folder, I have the model "yolo.h5", to run machine learning这是我放入的依赖项:tensorflow==2.4.0,keras==2.4.3,numpy==1.19.3,pillow==7.0.0,scipy==1.4.1,h5py==2.10.0, matplotlib==3.3.2,opencv-python,keras-resnet==0.2.0,imageai。在项目文件夹中,我有model“yolo.h5”,运行机器学习

1.first you need to save your tensorflow model weight ie (model.pb) 1.首先你需要保存你的 tensorflow model 重量即(model.pb)

  1. convert this into tensorflowlite (TF.lite) & you can use this in android phone将其转换为 tensorflowlite (TF.lite) & 你可以在 android 手机中使用它

    load and save model -tensorflow加载并保存 model -tensorflow

even you can refer this coursera course device-based-models-tensorflow甚至你可以参考这个 coursera 课程device-based-models-tensorflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM