# Model Visualizer

This describes how to view an ML model in an interactive webpage using the MLTK's commands/APIs.


```{note}
_Any_  `.tflite` or `.h5` model file will work with the model visualizer.  
i.e. The model file does _not_ need to be generated by the MLTK to view the model.
```


## Quick Reference

- Command-line: [mltk view --help](../command_line/view.md)
- Python API: [view_model](mltk.core.view_model)
- Python API examples: [view_model.ipynb](../../mltk/examples/view_model.ipynb)


## Overview

Model visualization allows for viewing how the various layers of a model are connected.
Model visualization is enabled using the [Netron](https://github.com/lutzroeder/netron)
machine learning model viewer, a tool for viewing models in an interactive webpage.

```{note}
- Model visualization is done _entirely_ in the local web-browser. The model is _not_ uploaded to any remote servers
- Models may also be viewed by dragging and dropping a `.tflite` or `.h5` model file on the [http://netron.app](https://netron.app) webpage
```


## Command

Model visualization from the command-line is done using the `view` operation.

For more details on the available command-line options, issue the command:

```shell
mltk view --help
```

### Example 1: View Keras model

In this example, we view the trained `.h5` model file in the 
[image_classification](mltk.models.tinyml.image_classification) model's [archive](./model_archive.md).

__NOTE:__ The model graph will appear in your web-browser.

```shell
mltk view image_classification
```

### Example 2: View Tensorflow-Lite model

In this example, we view the trained `.tflite` model file in the 
[image_classification](mltk.models.tinyml.image_classification) model's [archive](./model_archive.md).

__NOTE:__ The model graph will appear in your web-browser.

```shell
mltk view image_classification --tflite
```

### Example 3: View external Tensorflow-Lite model

The given model need _not_ be generated by the MLTK. 
External models are also supported by the `view` command.

__NOTE:__ The model graph will appear in your web-browser.

```shell
mltk view ~/workspace/my_model.tflite
```

### Example 4: View model before training

Training a model can be very time-consuming, and it is useful to view a 
model before investing time and energy into training it.
For this reason, the MLTK `view` command features a `--build` flag to build a model
and view it _before_ the model is fully trained.

In this example, the [image_classification](mltk.models.tinyml.image_classification) model is built
at command-execution-time and this file is opened in the viewer.
Note that _only_ the model [specification](./model_specification.md) script is required, 
it does _not_ need to be trained first.

__NOTE:__ The model graph will appear in your web-browser.

```shell
mltk view image_classification --tflite --build
```


## Python API

Model visualization is accessible via the [view_model](mltk.core.view_model) API  

Examples using this API may be found in [view_model.ipynb](../../mltk/examples/view_model.ipynb)