# Semantic Segmentation task

***Sample inference script for torchscript exported semantic segmentation model***

```python
 import torch
import numpy as np
from PIL import Image
import torchvision
import json
import matplotlib.pyplot as plt
import cv2

with open('class_mapping.json') as data:
    mappings = json.load(data)

class_mapping = {item['model_idx']: item['class_name'] for item in mappings}

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

model = torch.jit.load('model.pt').to(device)

image_path = '/path/to/your/image'
image = Image.open(image_path)
# Transform your image if the config.yaml shows
# you used any image transforms for validation data
image = np.array(image)
h, w = image.shape[:2]
# Convert to torch tensor
x = torch.from_numpy(image).to(device)
with torch.no_grad():
    # Convert to channels first, convert to float datatype
    x = x.permute(2, 0, 1).unsqueeze(dim=0).float()
    y = model(x)
    mask = torch.argmax(y, dim=1).squeeze()

# Overlay predicted mask on image and display
plt.imshow(image)
plt.imshow(mask, alpha=0.5)
plt.show()
    
```

The script above should produce outputs that look like this:

<figure><img src="https://wiki.cloudfactory.com/media/pages/docs/userdocs/model-playground/torchscript-sample-inference-scripts/semantic-segmentation/e97b426de8-1684131944/semseg.webp" alt=""><figcaption><p>Example output from the semseg inference script, yellow highlights the present class.</p></figcaption></figure>


---

# 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.cloudfactory.com/model-playground/torchscript-sample-inference-scripts/semantic-segmentation-task.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.
