ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

Any way to load or convert a .pth?

Open bassmit opened this issue 7 years ago • 10 comments

Hi, sorry to ask this here, but I can find very little on google. We have a trained agent that we would like to run in unity. Currently this is trained with pytorch and is in the form of a .pth file. Is there any way I can import this in to unity, perhaps by converting it to a .nn file?

Thanks, Bas

bassmit avatar Mar 05 '19 11:03 bassmit

You might be able to convert your pytorch model to ONNX and then to Barracuda: https://github.com/Unity-Technologies/ml-agents/blob/master/UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda.md

It looks like the Barracuda converter is still a work in progress and only supports some layers/nodes.

EDIT

It looks like the script "onnx_to_barracuda.py" mentioned in that readme has not been written yet. Sorry for the confusion.

sam598 avatar Mar 05 '19 17:03 sam598

Thanks, I'll check it out

On Tue, 5 Mar 2019, 18:35 sam598, [email protected] wrote:

You might be able to convert your pytorch model to ONNX and then to Barracuda:

https://github.com/Unity-Technologies/ml-agents/blob/master/UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda.md

It looks like the Barracuda converter is still a work in progress and only supports some layers/nodes.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Unity-Technologies/ml-agents/issues/1789#issuecomment-469776403, or mute the thread https://github.com/notifications/unsubscribe-auth/AEW2W5gbknH22sF0cDeUGI-2vedHO5heks5vTqrKgaJpZM4beaiG .

bassmit avatar Mar 05 '19 18:03 bassmit

EDIT

It looks like the script "onnx_to_barracuda.py" mentioned in that readme has not been written yet. Sorry for the confusion.

Ok no worries, I found this script tensorflow_to_barracuda.py, I'll try to use mmdnn to convert .pth to tensorflow then run that script. Fwiw I think the md linked is out of date, the .bytes format has been dropped afaict

bassmit avatar Mar 06 '19 12:03 bassmit

Hi @bassmit! At the moment we do not support Pytorch models, but in the future we might support it via ONNX. Though we are interested to hear more details on what kind of models would you like to run in Unity?

mantasp avatar Mar 13 '19 19:03 mantasp

Hi Mantas, I tried getting to a tensorflow model through ONNX, but that was way too optimistic :) We have since abandoned pytorch, and Im currently looking through the ml-agent python scripts to figure out how to convert our agents to .nn's. I'll share some more details over email

bassmit avatar Mar 14 '19 09:03 bassmit

Ok, lets move it to the email! :)

mantasp avatar Mar 14 '19 10:03 mantasp

Hi! Are there any updates on this? I have generated pytorch models as .pt or .pth files and would like to load them into Unity. It would be great to convert them into .nn files or import them directly or at least know when they will be supported. Thank you.

kumarict avatar Aug 16 '19 18:08 kumarict

We convert to pytorch to onnx https://pytorch.org/docs/stable/onnx.html then to nn with onnx_to_barracuda https://github.com/mantasp/barracuda-release/tree/release/0.2.4/Tools

On Fri, Aug 16, 2019 at 8:36 PM kumarict [email protected] wrote:

Hi! Are there any updates on this? I have generated pytorch models as .pt or .pth files and would like to load them into Unity. It would be great to convert them into .nn files or import them directly or at least know when they will be supported. Thank you.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Unity-Technologies/ml-agents/issues/1789?email_source=notifications&email_token=ABC3MW6A63K3NZHFPELHJSDQE3XUVA5CNFSM4G3ZVCDKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4PMGYI#issuecomment-522109793, or mute the thread https://github.com/notifications/unsubscribe-auth/ABC3MWYQET4TV4HAXGNFLODQE3XUVANCNFSM4G3ZVCDA .

bassmit avatar Aug 19 '19 08:08 bassmit

Hello @bassmit !

I followed your steps but when I have to drag my model into the agent in Unity I have this following error:

"NullReferenceException: Object reference not set to an instance of an object"

Captura

Versions: · Windows 10 · ML-Agents 0.13.1 · Unity 2018.4.16f1 · Barracuda 0.3.2 *but to convert from onnx to nn I use the scripts of barracuda 0.2.4 because if I use 0.3.2 or higher I have this error:

Traceback (most recent call last): File "onnx_to_barracuda.py", line 20, in onnx2bc.convert(args.source_file, args.target_file, args.trim_unused_by_output, args) File "C:\Users\Ocin\Desktop\pytorch to unity model\onnx_to_barracuda.py", line 570, in convert lambda tensor: get_tensor_data(tensor)) File "C:\Users\Ocin\Desktop\pytorch to unity model\barracuda.py", line 221, in setup_constants data = np.reshape(get_tensor_data_lambda(tensor), shape).astype(np.float32))] File "C:\Users\Ocin\Desktop\pytorch to unity model\onnx_to_barracuda.py", line 570, in lambda tensor: get_tensor_data(tensor)) File "C:\Users\Ocin\Desktop\pytorch to unity model\onnx_to_barracuda.py", line 336, in get_tensor_data floats = struct.unpack('<'+str(int(elems))+'f', tensor.raw_data) struct.error: unpack requires a buffer of 4 bytes

Did you do any more step?

Thank you !

ghost avatar Feb 09 '20 08:02 ghost

I'm closing and locking this issue in favor of https://github.com/Unity-Technologies/ml-agents/issues/3398

chriselion avatar Feb 10 '20 01:02 chriselion