# Import Model

Tensorleap supports importing models exported to various standard formats, including standard TensorFlow (SaveMode&#x6C;*/*&#x48;5/JSON) and PyTorch (ONNX).

### Formats

#### JSON\_TF2 - JSON format (TensorFlow 2)

The JSON format serializes the model layers, their properties and connectivity. It does not hold the state (weights) of the model.

Below is a code snippet for saving a TensorFlow model as a JSON file:

```python
json_file = open("model.json", "w")
json_file.write(model.to_json())
json_file.close()
```

More info at <https://www.tensorflow.org/api_docs/python/tf/keras/models/model_from_json>.

#### H5\_TF2 - H5 format (TensorFlow 2)

The `h5` format serializes the model and model state (weights) as a **single** `h5` file.

Below is a code snippet for loading an exported `h5` file:

```python
model.save('model.h5')
```

More info at <https://www.tensorflow.org/tutorials/keras/save_and_load>.

#### ONNX format (PyTorch)

The `onnx` format is commonly used in PyTorch for serializing the model's layers and state (weights).

Below is a code snippet for exporting a model to an `onnx` file in PyTorch:

```python
import torch
import torchvision

dummy_input = torch.randn(10, 3, 224, 224, device="cuda")
model = torchvision.models.alexnet(pretrained=True).cuda()

input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
output_names = [ "output1" ]

torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)
```

More info at <https://pytorch.org/docs/stable/onnx.html>.

#### SavedModel\_TF2 - SaveModel serialization (TensorFlow 2)

Uses the TensorFlow 2 `SaveModel` to export a folder with files containing the serialized model layers and state.

Below is a code snippet to load the model from the extracted folder:

```python
model.save('model_folder')
```

The serialized data is stored to a folder with this directory structure:

```shell
assets/ (folder)
variables/ (folder)
saved_model.pb
```

More info at <https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model>.

When importing the folder generated by the `model.save()` format from Tensorleap, the exported folder needs to be contained in a `tar.gz` file.

&#x20;One way to do it is by using `tar`:

```shell
tar -zcvf model_folder.tar.gz  model_folder
```

### Import Model using UI

To import a model:

1. On the **Network** view, click <img src="https://3509361326-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UXeOlFqlw8pl79U2HGU%2Fuploads%2FZbix52umVdtURiu1YUQL%2Fimage.png?alt=media&#x26;token=3cfb8dbb-9446-45e9-a2a9-b84daab9aa09" alt="" data-size="line"> to open the **Import Model** panel.
2. Enter the revision name and model name, and select the import file format from the list.
3. Click and select the import file from your system.
4. Optional: Select a [Dataset](https://docs.tensorleap.ai/user-interface/resources-management/integration-scripts) to connect to your model

{% hint style="info" %}
If your model contains [Custom Layers](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/custom-layers) this step is mandatory. You must first add these to the dataset script and select the dataset before moving to the next step.
{% endhint %}

&#x20; 5\.  Click **Import**.

<figure><img src="https://3509361326-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UXeOlFqlw8pl79U2HGU%2Fuploads%2FGKkhTZusj3pacZVF7DMN%2Fimage.png?alt=media&#x26;token=071a4425-b48c-46a1-bcdf-b7ed284cd17f" alt=""><figcaption><p>Setting up the import</p></figcaption></figure>

6\. Once the import process is complete, the model is added to the [**Versions**](https://docs.tensorleap.ai/user-interface/project/versions) view.

7\. Position your cursor over the version and click <img src="https://3509361326-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UXeOlFqlw8pl79U2HGU%2Fuploads%2FOgTeHENlyECQTxXLUqHq%2Fimage.png?alt=media&#x26;token=ea4f3d55-cb32-485e-9c3d-af31b3d2aa88" alt="" data-size="line">, then **Open Commit**.&#x20;

8\. Back on the **Network** view, point the [**Dataset Block**](https://docs.tensorleap.ai/user-interface/project/network/network-mapping/create-a-mapping-deprecated/input-node) to the model's [**Dataset Instance**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code#dataset-instance) and connect it to the first layer in the network.

{% hint style="info" %}
Once a dataset has been integrated with Tensorleap, it becomes available for use with your models. More info about Dataset Integration can be found at [**Dataset**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code).
{% endhint %}

![Importing a model and pointing its Dataset Block to a dataset instance](https://3509361326-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UXeOlFqlw8pl79U2HGU%2Fuploads%2FR9nHt2e4TRXmtLnRsrhR%2Fimport-model.gif?alt=media\&token=b14f10bc-b33b-454c-8558-cd17a95b5ec7)

8\. Add a [**Loss and Optimizer**](https://docs.tensorleap.ai/user-interface/project/network/network-mapping/create-a-mapping-deprecated/loss-node) for the network, then set it up for [**training**](https://docs.tensorleap.ai/user-interface/project/menu-bar/evaluate-a-model).
