> For the complete documentation index, see [llms.txt](https://docs.tensorleap.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_load_model.md).

# @tensorleap\_load\_model

code\_loader.inner\_leap\_binder.leapbinder\_decorators.tensorleap\_load\_model

The `tensorleap_load_model` decorator registers the function that loads your model, and names the model's outputs.

```python
@tensorleap_load_model(prediction_types=[PredictionTypeHandler('classes', labels)])
def load_model():
    pass
```

| Args               |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `prediction_types` | *(List\[*[*PredictionTypeHandler*](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/predictiontypehandler.md)*], optional)* One entry per model output, **in output order**: its name, its labels and its channel axis. The number of entries must equal the number of model outputs, so for an output you don't use, still pass an entry. May be left out only for a model with a single output; leaving it out produces a warning in the local run. |

#### Function inputs

None.

#### Function output

One of:

* a Keras model (`keras.Model` or `tf.keras.Model`)
* an ONNX Runtime session (`onnxruntime.InferenceSession`)

Any other object raises an error. The function is called once; later calls return the same model.

### Calling the model in the integration test

Inside the [integration test](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_integration_test.md), call the returned object the same way for both model kinds:

* **Keras**: `model([input_1, input_2])`, or `model(input)` for a single input. Returns one array, or a list with one array per output.
* **ONNX**: `model.run(None, {name_1: input_1, name_2: input_2})`. The first argument must be `None` (all outputs). The dictionary keys must be the model's input names, in the model's input order. Inputs are cast to the dtypes the ONNX model declares.

The model may have at most 10 inputs and 10 outputs. To pick an output of a multi-output model, index the result by position (`preds[0]`).

If the model has a fixed batch size larger than 1, the local run repeats each sample to fill the batch and keeps the first result.

### Examples

#### Basic Usage

```python
import os
import tensorflow as tf
from code_loader.contract.datasetclasses import PredictionTypeHandler
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_load_model

CLASSES = PredictionTypeHandler(name='classes', labels=[str(i) for i in range(10)], channel_dim=-1)


@tensorleap_load_model(prediction_types=[CLASSES])
def load_model():
    dir_path = os.path.dirname(os.path.abspath(__file__))
    return tf.keras.models.load_model(os.path.join(dir_path, 'model/model.h5'))
```

#### ONNX model

```python
import os
import onnxruntime
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_load_model


@tensorleap_load_model(prediction_types=[CLASSES])
def load_model():
    dir_path = os.path.dirname(os.path.abspath(__file__))
    return onnxruntime.InferenceSession(os.path.join(dir_path, 'model/model.onnx'))
```

#### Several outputs (YOLO)

Four outputs, declared in output order: a concatenated prediction with `4 + number of classes` channels, then three heads with 20, 40 and 80 channels.

```python
prediction_types = [
    PredictionTypeHandler(name='object detection', labels=['x', 'y', 'w', 'h'] + class_names, channel_dim=1),
    PredictionTypeHandler(name='concatenate_20', labels=[str(i) for i in range(20)], channel_dim=-1),
    PredictionTypeHandler(name='concatenate_40', labels=[str(i) for i in range(40)], channel_dim=-1),
    PredictionTypeHandler(name='concatenate_80', labels=[str(i) for i in range(80)], channel_dim=-1),
]


@tensorleap_load_model(prediction_types=prediction_types)
def load_model():
    ...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_load_model.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
