最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Training a ML agent using stable baselines3, python, and unity. Keep getting the same import error when trying to convert torch

programmeradmin14浏览0评论

I am currently training a ML agent to play a game I am developing in unity. Currently, I am having trouble converting the trained agent files to an ONNX format so that they can be integrated with unity. Every time I run my conversion script I get the following:

  File "C:\Users\alpha\Carrom_2Dnew\Lib\site-packages\onnx\__init__.py", line 77, in <module>
    from onnx.onnx_cpp2py_export import ONNX_ML
ImportError: DLL load failed while importing onnx_cpp2py_export: A dynamic link library (DLL) initialization routine failed.

Here is my code(this is the template from the stable-baselines3 website with a couple of edits, I have also tried making my own without any of the unity or environment packages and nothing changed):

import torch as th
from typing import Tuple
from my_env import MyEnv
#from onnx import onnx_cpp2py_export
import onnx
#from torch import onnx
from peaceful_pie.unity_comms import UnityComms
from my_env import MyEnv
from stable_baselines3mon.monitor import Monitor

from stable_baselines3 import PPO
from stable_baselines3mon.policies import BasePolicy

class OnnxableSB3Policy(th.nn.Module):
    def __init__(self, policy: BasePolicy):
        super().__init__()
        self.policy = policy

    def forward(self, observation: th.Tensor) -> Tuple[th.Tensor, th.Tensor, th.Tensor]:
        # NOTE: Preprocessing is included, but postprocessing
        # (clipping/inscaling actions) is not,
        # If needed, you also need to transpose the images so that they are channel first
        # use deterministic=False if you want to export the stochastic policy
        # policy() returns `actions, values, log_prob` for PPO
        return self.policy(observation, deterministic=True)


#model = PPO("MlpPolicy", "Pendulum-v1")
unity_comms = UnityComms(port=9000)
my_env = MyEnv(unity_comms=unity_comms)
my_env = Monitor(my_env)
PPO("MlpPolicy",env=my_env).save("C:/Users/alpha/Carrom_2Dnew/Carrom_2Dnew.zip")
model = PPO.load("C:/Users/alpha/Carrom_2Dnew/Carrom_2Dnew.zip", device="cpu")

onnx_policy = OnnxableSB3Policy(model.policy)

observation_size = model.observation_space.shape
dummy_input = th.randn(1, *observation_size)
th.onnx.export(
    onnx_policy,
    dummy_input,
    "my_ppo_model.onnx",
    opset_version=17,
    input_names=["input"],
)

##### Load and test with onnx

import onnx
import onnxruntime as ort
import numpy as np

onnx_path = "my_ppo_model.onnx"
onnx_model = onnx.load(onnx_path)
onnx.checker.check_model(onnx_model)

observation = np.zeros((1, *observation_size)).astype(np.float32)
ort_sess = ort.InferenceSession(onnx_path)
actions, values, log_prob = ort_sess.run(None, {"input": observation})

print(actions, values, log_prob)

# Check that the predictions are the same
with th.no_grad():
    print(model.policy(th.as_tensor(observation), deterministic=True))

Here are the packages I currently have installed:

certifi              2024.8.30
charset-normalizer   3.4.0
chili                1.8.0
cloudpickle          3.1.0
coloredlogs          15.0.1
contourpy            1.3.0
cycler               0.12.1
Farama-Notifications 0.0.4
filelock             3.16.1
flatbuffers          25.1.24
fonttools            4.54.1
fsspec               2024.10.0
gymnasium            0.29.1
humanfriendly        10.0
idna                 3.10
Jinja2               3.1.4
kiwisolver           1.4.7
MarkupSafe           3.0.2
matplotlib           3.9.2
mpmath               1.3.0
networkx             3.4.2
numpy                1.26.4
onnx                 1.17.0
onnxruntime          1.20.1
packaging            24.1
pandas               2.2.3
peaceful-pie         2.1.0
pillow               11.0.0
pip                  24.3.1
protobuf             5.29.3
pyparsing            3.2.0
pyreadline3          3.5.4
python-dateutil      2.9.0.post0
pytz                 2024.2
requests             2.32.3
setuptools           75.3.0
six                  1.16.0
stable_baselines3    2.3.2
sympy                1.13.1
torch                2.5.1
typing_extensions    4.12.2
tzdata               2024.2
urllib3              2.2.3
wheel                0.44.0

So far I have tried reinstalling every version combination of ONNX and onnxruntime, editing my environment variables to include the path to the DDL, disabling anti virus software, and even rewrote another script from scratch and still have the same result. The error always occurs when trying to export. Any help is appreciated and if you need anymore info I'd be happy to provide.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论