# How to load PyTorch weights from KNIME Hub without converting to ONNX?

**URL:** <https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782>\
**Category:** KNIME Server\
**Tags:** machine-learning, database, python, big-data\
**Created:** [January 24, 2024, 3:15pm UTC](https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782 "2024-01-24T15:15:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qzhang1](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/q/8e7dd6/32.png) [@qzhang1](https://forum.knime.com/u/qzhang1)\
**Post date:** [January 24, 2024, 3:15pm UTC](https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782/1 "2024-01-24T15:15:33Z")

</div>

I’m trying to have my Python node load some pretrained PyTorch weights hosted on a KNIME Hub server. As you know, KNIME currently has poor PyTorch support. I’m aware of people using the following workarounds, but I ran into problems with each one:

1. Convert the PyTorch weights into ONNX format and then back again - First of all, PyTorch has poor support for converting ONNX files back into PyTorch models. Secondly, my models have many features from other deep learning libraries, that would be lost during the conversion.
2. [Use the URL to File Path (Variable) Node as suggested in this thread](https://forum.knime.com/t/loading-python-model-pkl-from-server-with-python-script-source-node) - this approach only applies to locally hosted files identified by a URL

 ![Screenshot 2024-01-24 150629](https://forum-cdn.knime.com/uploads/default/original/3X/e/4/e48d94802155fc06f8d50ad915027dff38514b02.png)

1. Fetch the weight files from the Python Node directly using a URL, i.e. using `torch.hub.load_state_dict_from_url("/http/url/to/hosted/weight/files/")` - I get a HTTP 403 Access Denied Error when using the https:// url, and the function doesn’t recognize a knime:// url.

2. I successfully managed to convert a directory of PyTorch weights on the server into a table of binary objects, using the following setup. However, I’m stuck on how to convert the binary objects into PyTorch weights or models.

![Screenshot 2024-01-24 151338](https://forum-cdn.knime.com/uploads/default/original/3X/d/4/d4c8745907604c734ad440c376f2e9e3ea977e01.png)

---

<div class="post-metadata">

**Author:** ![armingrudd](https://forum-cdn.knime.com/user_avatar/forum.knime.com/armingrudd/32/84633_2.png) [@armingrudd](https://forum.knime.com/u/armingrudd)\
**Post date:** [January 25, 2024, 9:11am UTC](https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782/2 "2024-01-25T09:11:42Z")

</div>

Hi @qzhang1,

Have you tried to transfer the files to a temporary folder under workflow data area and read them in your Python Script node?

Here are the nodes you can use:

If you are using KNIME Business Hub:

> **[KNIME Hub Authenticator](https://hub.knime.com/knime/extensions/com.knime.features.enterprise.client/latest/com.knime.hub.nodes.authentication.HubAuthenticationNodeFactory)**
>
> Authenticates against a KNIME Hub. The resulting output port allows compatible nodes, such as the Space Connector, to access the configured Hub.

> **[Space Connector](https://hub.knime.com/knime/extensions/com.knime.features.enterprise.client/latest/com.knime.hub.nodes.filehandling.space.connector.SpaceConnectorNodeFactory)**
>
> Provides a file system connection with access to a KNIME Hub Space, for example the "Private" Space on "My-KNIME-Hub". It is also possible to a…

If you are using KNIME Server:

> **[KNIME Server Connector](https://hub.knime.com/knime/extensions/com.knime.features.explorer.serverspace/latest/com.knime.server.nodes.filehandling.connector.ServerConnectorNodeFactory)**
>
> This node connects to a KNIME Server via REST. The resulting output port allows downstream nodes to access the Server workflow repository as a file system, e.g…

To create a temp folder:

> **[Create Temp Folder](https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.filehandling.utility.nodes.tempdir.CreateTempDir2NodeFactory)**
>
> Creates a temporary folder upon execute and exposes its path as flow variable. This can be useful in (a) demo applications where the actual path of the output …

To transfer files:

> **[Transfer Files](https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.filehandling.utility.nodes.transfer.filechooser.TransferFilesFileChooserNodeFactory)**
>
> This node copies or moves files from a source (folder or file) to another folder. The node offers options to either copy or move a single file, a complete fold…

To list files and transfer them:

> **[List Files/Folders](https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.filehandling.utility.nodes.listpaths.ListFilesAndFoldersNodeFactory)**
>
> List files and/or folders contained in the selected folder. This node can access a variety of different file systems. More information about file handling in K…

> **[Transfer Files (Table)](https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.filehandling.utility.nodes.transfer.table.TransferFilesTableNodeFactory)**
>
> This node copies or moves files from a source (folder or file) to another folder. The node offers options to either specify the destination based on a column i…

---

<div class="post-metadata">

**Author:** ![qzhang1](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/q/8e7dd6/32.png) [@qzhang1](https://forum.knime.com/u/qzhang1)\
**Post date:** [January 25, 2024, 11:35am UTC](https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782/3 "2024-01-25T11:35:47Z")

</div>

Hello @armingrudd,

Thank you for your swift and helpful response! I thought about creating temp files, but wasn’t aware of the temp folder node.

I have one question however - When using that node in a remote workflow, and writing relative to the current workflow data area, are we writing to a local temp folder, or directly to the server?

 ![Screenshot 2024-01-25 113351](https://forum-cdn.knime.com/uploads/default/original/3X/1/2/12d6c441faa9b2cf96250e0fe243ce4d565edf8d.png)

---

<div class="post-metadata">

**Author:** ![armingrudd](https://forum-cdn.knime.com/user_avatar/forum.knime.com/armingrudd/32/84633_2.png) [@armingrudd](https://forum.knime.com/u/armingrudd)\
**Post date:** [January 25, 2024, 12:59pm UTC](https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782/4 "2024-01-25T12:59:02Z")

</div>

Relative to current workflow data area points to the current workflow which is executing. So, when an instance of the workflow is running on Hub, then the temp folder is also created on Hub under the running workflow’s data folder.

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [February 1, 2024, 12:59pm UTC](https://forum.knime.com/t/how-to-load-pytorch-weights-from-knime-hub-without-converting-to-onnx/76782/5 "2024-02-01T12:59:41Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
