Search code examples
python-3.xkeraspyinstallertensorflow2.0onnx

PyInstaller .exe file plugin could not be extracted (with Keras and ONNX converter libraries and modules)


I have a python script that takes an ONNX Neural Network and converts it to a keras (.h5) model to be trained and exported back into ONNX as a newly trained model to be deployed later. The problem is that I am required to create a python .exe file from the python script as the goal is to deploy the deep learning model in C Plus Plus. Currently, the Python script does a great job of altering the onnx program for the C Plus Plus program to deploy the trained model, and successfully creates a .exe file with Pyinstaller with the following command:

pyinstaller --onefile Material_Classifier.py

However, the problem is that when I click on the .exe file in File Explorer, the console renders empty for about half a minute, and then stops working. When I use cmd, it gives the following error:

Failed to decode wchar_t from UTF-8
MultiByteToWideChar: The data area passed to a system call is too small.
share\jupyter\lab\staging\node_modules\.cache\terser-webpack-plugin\content-v2\sha512\2e\ba\cfce62ec1f408830c0335f2b46219d58ee5b068473e7328690e542d2f92f2058865c600d845a2e404e282645529eb0322aa4429a84e189eb6b58c1b97c1a could not be extracted!
fopen: No such file or directory

My first hunch was that my input ONNX folder was not in the same folder as the .exe file as the script requires it for input, but this did nothing to fix the .exe file. And now, I'm leaning towards the fact that PyInstaller may not like some of the following libraries that I used in my script:

import os
import onnx
import keras
import keras2onnx
from onnx2keras import onnx_to_keras

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model

from tensorflow.python.keras.models import load_model

import cv2
from tqdm import tqdm
import pickle

My second hunch is that because my program requires an external file and outputs a new file, I would need to specify this somehow before creating a .exe with Pyinstaller. But I currently have no way of knowing either way.

TLDR; I'm wondering if PyInstaller has trouble processing deep learning libraries if there is something else I'm doing incorrectly, and if there are any alternatives to creating .exe files from python scripts for deep learning. Thanks!


Solution

  • Answering my own question,

    This process had something to do with PyInstaller not working with the Anaconda environment upon creating the .exe executable. When uninstalling Anaconda and reinstalling Python 3.7.x, I was able to successfully able to do this with no external environment when copying the tensorflow, onnx, and onnx.dist folders into the same directory as my script, then writing

    pyinstaller --onefile --add-data C:\path\to\onnx;onnx. --add-data C:\path\to\tensorflow;tensorflow. --add-data C:\path\to\onnx-dist;onnx-dist. script.py