# Model Integration

In this section, we will set up our classification model by either **importing** or **building** it.

### Project Setup

{% tabs %}
{% tab title="UI" %}
First step is to [**Create a Project**](/user-interface/project.md) with the name`MNIST`, and the description `Digit Classifier.`
{% endtab %}

{% tab title="CLI" %}
The project name, MNIST, is set with the `leap init` command, as discussed in [**Dataset Integration**](#dataset-integration) above.
{% endtab %}
{% endtabs %}

The project page contains the **Network** and **Dashboard** views. To toggle them, click the ![](/files/bm5Du4oaCbW1aSTqF1og) buttons at the top.

### Dataset Block Setup

We'll start by pointing the model's dataset block to our `mnist` dataset. This will update the **Dataset Block** with the relevant **input**.

In the **Network** view, click the **Dataset Block** to display the **Dataset Details** panel on the right, then click **Connect Dataset** and select the `mnist` dataset from the list.&#x20;

A more detailed explanation about this step can be found at [**Dataset Block**](/user-interface/project/network/network-mapping/create-a-mapping-deprecated/input-node.md).

### Set Model Layers

{% tabs %}
{% tab title="Importing a Model" %}

#### Importing a Model from Tensorflow or PyTorch

Tensorleap can import a model saved by Tensorflow (PB/H5/JSON\_TF2) and PyTorch (ONNX). For more information, see [**Import Model**](/user-interface/project/versions/import-model.md).

In this sample code, a simple CNN model is created using Tensorflow and then saved as a file named `mnist-cnn.h5`.

```python
import tensorflow as tf

input = tf.keras.layers.Input(shape=(28, 28, 1))
layer = tf.keras.layers.Conv2D(32, [3, 3], activation='relu')(input)
layer = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(layer)
layer = tf.keras.layers.Conv2D(64, [3, 3], activation='relu')(layer)
layer = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(layer)
layer = tf.keras.layers.Flatten()(layer)
layer = tf.keras.layers.Dropout(0.5)(layer)
output = tf.keras.layers.Dense(10, activation='softmax')(layer)

model = tf.keras.Model(inputs=input, outputs=output)
model.save('mnist-cnn.h5')
```

For your convenience, you can also download this file here:

{% file src="/files/2uMqvOXKaAO5t2uZXqad" %}

#### Import Model

1. In the **Network** view, click the <img src="/files/sUoaqbThWCuTOiENJUE2" alt="" data-size="line"> button to open the `Import Model` panel.&#x20;
2. Set the **Revision Name** to `mnist-cnn` and the **Model Name** to `pre-trained`.
3. For **File Type**, select `H5_TF2,` then choose `mnist-cnn.h5` in the **Upload File** field.
4. Click the **Import** button.&#x20;
5. Once completed, the imported model, `mnist-cnn,` is added to the [**Versions**](/user-interface/project/versions.md) view. Position your cursor over that version, click ![](/files/6EdLuMuhWyAGYbh9aHzc) to **Open Commit**.
6. Back in the **Network** view, set the **Dataset** **Block** to point to the `mnist` **Dataset**.

![Importing a Model and Dataset Block Setup (click-to-zoom)](/files/zdkfT2d5FNtMCqFelxwp)
{% endtab %}

{% tab title="Building a Model" %}

#### Building our Classification Model using UI

In this section, we will add layers, update their properties, and connect them together to form the model.&#x20;

The model we'll build is based on a small CNN (Convolutional Neural Network):

<table><thead><tr><th>Layers</th><th>Properties</th><th data-hidden></th></tr></thead><tbody><tr><td>Convo2D</td><td>filters=32, kernel_size=(3, 3), activation="relu" </td><td></td></tr><tr><td>MaxPooling2D</td><td>pool_size=(2, 2)</td><td></td></tr><tr><td>Conv2D </td><td>filters=64, kernel_size=(3, 3), activation="relu"</td><td></td></tr><tr><td>MaxPooling2D</td><td>pool_size=(2, 2) </td><td></td></tr><tr><td> Flatten </td><td></td><td></td></tr><tr><td>Dropout </td><td>rate=0.5</td><td></td></tr><tr><td>Dense </td><td>units=10, activation="softmax"</td><td></td></tr></tbody></table>

#### Adding Layers and Their Properties

In the [Network](/user-interface/project/network.md) view, follow these steps for each layer:

1. **Right-click** and add the corresponding layer. More info at [**Add Layers**](/user-interface/project/network/network-mapping/create-a-mapping-deprecated/layers.md).
2. Update the corresponding properties in the **Layer Properties** view on the right. More info at [**Layer Properties**](/user-interface/project/network/network-mapping/create-a-mapping-deprecated/layers.md#layer-properties).
3. Connect the **Dataset** to the first layer, and then connect all the layers in order. More info at [**Connections**](/user-interface/project/network/network-mapping/create-a-mapping-deprecated/layers.md#connections).

All steps are illustrated below.

![Add Layers and Connect them](/files/zjY0ROcAc0X2FTW3sBVm)
{% endtab %}

{% tab title="CLI" %}

#### Build Model

Copy the `mnist_cnn.py` file to the `mnist` folder we created in [**Dataset Integration**](#dataset-integration). This file defines the CNN model, and can be found here:

{% file src="/files/uiOKfLL6robBtW2w3E2N" %}

#### Tensorleap Model Integration Script

Next, edit the `.tensorleap/model.py` file and point it to the model defined in `mnist_cnn.py`:

```python
from pathlib import Path
from mnist_cnn import build_model


def leap_save_model(target_file_path: Path):
    # Load your model
    model = build_model()
    # Save it to the path supplied as an arugment (has a .h5 suffix)
    model.save(target_file_path)

```

The function `leap_save_model` gets called by the CLI on `leap push`, with the argument `target_file_path`, which is the location where the model is to be saved.

To validate the correctness of our code, run the following command:

```
leap check --all
```

Upon a successful check, we can continue to pushing the model:

```
leap push --model --description=mnist-cnn --model-name=pre-trained --branch-name=master
```

#### Opening the Model Version in the UI and Set the Dataset Block

Once `leap push` completes, open the [**Versions**](/user-interface/project/versions.md) view in the UI, where you should now see the imported model, `mnist_cnn`.&#x20;

* Position your cursor over `mnist_cnn` and **click** ![](/files/uG4lQceLsLKBgiqjLMTA) to **Open Commit**.
* Back on the **Network** view, set the **Dataset** **Block** to point to the `mnist` **Dataset Instance**, and connect it to the first layer.
  {% endtab %}
  {% endtabs %}

Once all layers have been connected to each other, the model should look like this:

![Connected Layers and Dataset](/files/L5lHRXVc98LDYKH8vR2K)

{% hint style="info" %}
Each block shows the calculated output shape affected by the preceding layers.
{% endhint %}

### Add Loss and Optimizer

In this section, we will set the **Categorical Crossentropy** loss function, and connect it to both the dataset's ground truth and the last layer in our model. We'll then add an **Adam** Optimizer block and connect it to the loss block. For more information, see [**Loss and Optimizer**](/user-interface/project/network/network-mapping/create-a-mapping-deprecated/loss-node.md).

After completing this section, our model will be ready for training.

Within the [**Network**](/user-interface/project/network.md) view:

1. Right-click and add the following:
   * **Loss** -> **CategoricalCrossentropy**
   * **GroundTruth**, and set it to `Ground Truth - classes`
   * **Optimizer** -> **Adam**
2. Connect the last **Dense** layer and the **GroundTruth** to the **CategoricalCrossentropy** block. Additionally, connect the **Loss** block to the **Adam** optimizer.

All steps are illustrated below.

![Add Loss and Optimizer](/files/3ZcuAxfgTH8AU9b6T1zl)

### Add **Visualizers**

Visualizers define how to display tensors within the model graph. For more info, see [**Visualizers**](/user-interface/project/network/network-mapping/create-a-mapping-deprecated/visualizer-node.md).&#x20;

#### Dataset Input **Visualizer**

There must be at least **one** visualizer connected to the model's input for analysis. To add the **Visualizer** for the **input,** follow the steps below.

Within the [Network](/user-interface/project/network.md) view:

1. Right-click and choose **Visualizer** to add it.
2. Click the **Visualizer** node to open up the **Visualizer Details** on the right.
3. Choose `Image` from the `Selected Visualizer` list.
4. Connect the **Dataset** node output to the input of the **Visualizer** node.

#### Prediction and Ground Truth **Visualizer**

Additional Visualizers will be connected to the prediction and ground truth, in order to visualize the model's prediction output. Follow the steps below:

Within the [Network](/user-interface/project/network.md) view:

1. Right-click and choose **Visualizer** to add it.
2. Click the **Visualizer** node to open up the **Visualizer Details** on the right.
3. Choose `HorizontalBar` from the `Selected Visualizer` list.
4. Connect the last Dense layer output to the input of the **Visualizer** node.
5. Repeat the steps, and connect the second visualizer to the **GroundTruth** node's output.

![Add Visualizers](/files/lODIun8IbhNTqLo0GMq3)

### Save Network Version

Great! Your first version is ready to be saved.

![Full Model with Dataset, Visualizers, Layers, Loss and an Optimizer](/files/3Tn2e4tv0axHgcVH6fmg)

Click the <img src="/files/KufD8iWbuVi1Smd314P4" alt="" data-size="line"> button and set the `Revision Name` to `cnn-2` (Convolution Neural Network with 2 convolutional layers). This adds the new version to the [**Versions**](/user-interface/project/versions.md) view. For more information, see [**Save a Version**](/user-interface/project/versions.md#save-a-version).

![The newly-saved version appears on the Versions view](/files/9ogriFZUzJY8ULnKPZRo)

## Training

Tensorleap can import trained and untrained models. In our case, the model was created from scratch and needs to be trained.&#x20;

To train the model, click <img src="/files/EjRb7Kqxt5ZYUOF3Xy3L" alt="" data-size="line"> from the top bar. Let's set the `Number of Epochs` to `10` and click <img src="/files/Is9sXxG5yWqlFXZb0k6P" alt="" data-size="line">. For more information, see [**Evaluate/Train Model**](/user-interface/project/menu-bar/evaluate-a-model.md).

![Train Model Dialog](/files/76f0dPvvSon9Zekl4iWy)

### Metrics

Once training begins, you can start tracking metrics in real-time.

#### Add a Dashboard and Dashlets

To add a new [**Metrics Dashboard**](/user-interface/project/dashboards/dashlets/metrics-dashboard.md), click <img src="/files/lyozzPr085ixIbOIJrj7" alt="" data-size="line"> and fill in the dashboard name - `mnist`.

Make sure that the model is selected from the [**Versions**](/user-interface/project/versions.md) view, and add a new **Dashlet**.

Click <img src="/files/4FjDzyhmVL4CO6YI03bY" alt="" data-size="line"> for a [**Line Dashlet**](/user-interface/project/dashboards/dashlets/metrics-dashboard.md#line), set the name to `Loss`, and turn on `Split series by subset` to separate `training` and `validation` metrics.

![Add a Dashboard and a Loss Dashlet](/files/GR9we44tqAZJVW6rBecA)

Next, add another [**Line Dashlet**](/user-interface/project/dashboards/dashlets/metrics-dashboard.md#line) for the accuracy. Follow all the previous steps to add it and set the **Dashlet Name** to `Accuracy` and set the **Y-Axis** to `metrics.Accuracy`. Do not forget turn on  `Split series by subset`.

#### Overview

As training progresses, you should see **loss** values declining and **accuracy** values increasing.\
When training is completed, the model achieved an accuracy of 98%.

![Loss vs Batch](/files/KAwRShG6M1OWWVmpEG5J) ![Accuracy vs Batch - Reaching 98% Accuracy](/files/hH4lMjrP17u9R63AZIp5)

## Up Next - Model Perception Analysis

Tensorleap provides a host of innovative tools for model analysis and debugging. It tracks how each sample flows through each layer, and how each learned feature flows through the model. It also stores metrics in a big dataset to analyze the model's response to data.

We've discussed integrating and training our model. It's now time to analyze it. The [**next**](/guides/full-guides/mnist-guide/model-perception-analysis.md) part of this tutorial will demonstrate various analyses of the model and data.

Once you are ready, proceed to [**Model Perception Analysis**](/guides/full-guides/mnist-guide/model-perception-analysis.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tensorleap.ai/guides/full-guides/mnist-guide/model-integration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
