Skip to content

Any way to load or convert a .pth? #1789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bassmit opened this issue Mar 5, 2019 · 10 comments
Closed

Any way to load or convert a .pth? #1789

bassmit opened this issue Mar 5, 2019 · 10 comments
Assignees
Labels
discussion Issue contains general discussion.

Comments

@bassmit
Copy link

bassmit commented Mar 5, 2019

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 bassmit changed the title Any way to load a .pth? Any way to load or convert a .pth? Mar 5, 2019
@sam598
Copy link

sam598 commented Mar 5, 2019

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.

@bassmit
Copy link
Author

bassmit commented Mar 5, 2019 via email

@eshvk eshvk added the discussion Issue contains general discussion. label Mar 5, 2019
@bassmit
Copy link
Author

bassmit commented Mar 6, 2019

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

@mantasp mantasp self-assigned this Mar 13, 2019
@mantasp
Copy link
Contributor

mantasp commented Mar 13, 2019

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?

@bassmit
Copy link
Author

bassmit commented Mar 14, 2019

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

@mantasp
Copy link
Contributor

mantasp commented Mar 14, 2019

Ok, lets move it to the email! :)

@mantasp mantasp closed this as completed Mar 14, 2019
@kumarict
Copy link

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.

@bassmit
Copy link
Author

bassmit commented Aug 19, 2019 via email

@ghost
Copy link

ghost commented Feb 9, 2020

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 !

@chriselion
Copy link
Contributor

I'm closing and locking this issue in favor of #3398

@Unity-Technologies Unity-Technologies locked as resolved and limited conversation to collaborators Feb 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
discussion Issue contains general discussion.
Projects
None yet
Development

No branches or pull requests

6 participants