Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.
6404 Discussions

Convert python script including openvino to exe

ahmed_hany
Beginner
713 Views

Hello,

I am trying to work on image classification python script that takes my own pre trained alexnet model and try to classify images.

I am trying first on Windows10 but the end goal is to make it work on a OpenVino Starter Platform.

The script works pretty well. But if I tried to make it executable and run it, it gives some errors.

The script is as follows:

import sys
import time
from pathlib import Path

import cv2
import matplotlib.pyplot as plt
#import mo_onnx
import numpy as np
import torch
from openvino.inference_engine import IECore
import argparse
import os
import openvino.inference_engine.constants

parser = argparse.ArgumentParser()
parser.add_argument("-m")
parser.add_argument("-i")

args = parser.parse_args()
model=os.path.abspath(args.m)
imageName=os.path.abspath(args.i)


BASE_MODEL_NAME = model
model_path = Path(BASE_MODEL_NAME).with_suffix(".pytorch")
onnx_path = model_path.with_suffix(".onnx")
ir_path = model_path.with_suffix(".xml")

model = torch.load(model_path).cpu().eval()

if not onnx_path.exists():
dummy_input = torch.randn(1, 3, 224, 224)
# For the Fastseg model, setting do_constant_folding to False is required
# for PyTorch>1.5.1
torch.onnx.export(
model,
dummy_input,
onnx_path,
opset_version=11,
do_constant_folding=False,
)
print(f"ONNX model exported to {onnx_path}.")
else:
print(f"ONNX model {onnx_path} already exists.")
#mo_path = str(Path(mo_onnx.__file__))

# Construct the command for Model Optimizer
#mo_command = f""""{sys.executable}"
# "{mo_path}"
# --input_model "{onnx_path}"
# --input_shape "[1,3, 224, 224]"
# --mean_values "[0.485, 0.456, 0.406]"
# --scale_values "[58.395, 57.12 , 57.375]"
# --data_type "FP16"
# --output_dir "{model_path.parent}"
# """

#print(model_path.parent)
#mo_command = " ".join(mo_command.split())
print("Model Optimizer command to convert the ONNX model to OpenVINO:")
#print(mo_command)

if not ir_path.exists():
print("Exporting ONNX model to IR... This may take a few minutes.")
# os.system(mo_command)
else:
print(f"IR model {ir_path} already exists.")


def normalize(image: np.ndarray) -> np.ndarray:
"""
Normalize the image to the given mean and standard deviation
for CityScapes models.
"""
image = image.astype(np.float32)
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
image /= 255.0
image -= mean
image /= std
return image

ie = IECore()
net_onnx = ie.read_network(model=onnx_path)
exec_net_onnx = ie.load_network(network=net_onnx, device_name="CPU")

input_layer_onnx = next(iter(exec_net_onnx.input_info))
output_layer_onnx = next(iter(exec_net_onnx.outputs))

# Run the Inference on the Input image...
res_onnx = exec_net_onnx.infer(
inputs={input_layer_onnx: normalized_input_image}
)
res_onnx = res_onnx[output_layer_onnx]

result_mask_onnx = np.squeeze(np.argmax(res_onnx, axis=1)).astype(np.uint8)
if (result_mask_onnx == 0):
print("Your image is not an RPW")
elif (result_mask_onnx == 1):
print("Your image is RPW")

I turn it into executable using the following commands;

>> <path>\openvino_2021\bin\setupvars.bat
>> pyinstaller --onefile --add-data "<path>\openvino_2021\deployment_tools\inference_engine\bin\intel64\Release\plugins.xml;." --paths "<path>\openvino_2021\python\python3\cv2" script.py

The error is as follows:

[14976] WARNING: file already exists but should not: <path>\Temp\_MEI149762\torch\_C.cp38-win_amd64.pyd                                                                                                                   Traceback (most recent call last):                                                                                        File "script.py", line 5, in <module>                                                                       import cv2                                                                                                            File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module                                               File "cv2\__init__.py", line 129, in <module>                                                                           File "cv2\__init__.py", line 72, in bootstrap                                                                           File "cv2\__init__.py", line 70, in load_first_config                                                                 ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.                       
[33260] Failed to execute script 'script' due to unhandled exception!     

Can you please help with this error and if you have a way to make this script working after that on linux machine that will have the FPGA connected to it?

0 Kudos
4 Replies
Wan_Intel
Moderator
655 Views

Hi Ahmed_hany,

Thanks for reaching out to us.

 

I encountered the same error when using OpenCV-Python version 4.5.3-openvino. As a workaround, the error can be solved by using OpenCV-Python Version other than 4.5.3-openvino.

 

 

 

"<INSTALL_DIR>\bin\setupvars.bat"

pyinstaller --onefile --add-data "<INSTALL_DIR>\inference_engine\bin\intel64\Release\plugins.xml;." --add-binary "<INSTALL_DIR>\inference_engine\bin\intel64\Release\inference_engine.dll;." ahmad.py

 

 

 

result.JPG

 

Regards,

Wan

 

0 Kudos
ahmed_hany
Beginner
625 Views
Hello
Thank you for the help
I have one question here, when I try to use pip install opencv-python == 4.5.5.64, it got installed but once I use the setupvars.bat and try to print (cv2.__version__), I got the 4.5.3-openvino
Can you help me remove the opencv 4.5.3-openvino version and install the right one ?
Thanks,
Ahmed
0 Kudos
Wan_Intel
Moderator
612 Views

Hi Ahmed_hany,

Starter Platform for OpenVINO™ Toolkit is a PCIe based FPGA.

 

For your information, Intel® is transitioning to the next-generation programmable deep learning solution, which will be called Intel® FPGA AI Suite and will support OpenVINO™ toolkit when productized.

 

As part of this transition, 2020.3.2 LTS was the final release to include support for Intel® Vision Accelerator Design with an Intel® Arria® 10 FPGA and the Intel® Programmable Acceleration Card with Intel® Arria® 10 GX FPGA. Now support offered in 2020.3.2 LTS for FPGA is coming to an end.

 

Any customer inquiries regarding Intel® FPGA AI Suite should be directed to your Intel Programmable Solutions Group account manager or subscribe to get notified with the latest updates.

This information is available in Release Notes for Intel® Distribution of OpenVINO™ Toolkit 2021.4 LTS under the “Support Change and Deprecation Notices” section.

 

Next, you may remove the OpenCV-Python version 4.5.3-openvino with the following steps:

 

  1. Run w_openvino_toolkit_p_2020.3.355.exe installer.
  2. Select Modify.
    untick_opencv_0.JPG
  3. Untick OpenCV.
    untick_opencv.JPG

 

 

Regards,

Wan

 

0 Kudos
Wan_Intel
Moderator
595 Views

Hi Ahmed_hany,

Thanks for your question.

This thread will no longer be monitored since we have provided suggestions. 

If you need any additional information from Intel, please submit a new question.

 

 

Best regards,

Wan


0 Kudos
Reply