Tutorial 2: Predicting with your model

In this tutorial we are going to show you how to run prediction using the NPU API. All you need is, a model, some data and the following line of code:

pred = npu.predict(model, x)

First, let’s get the data that we will be using for prediction. We can either pass 1 sample or multiple samples as a batch to be predicted. We will be using the FashionMNIST testing dataset for this. We can easily get this data from our global datasets on the NPU library but for the sake of teaching more closely to a real world scenario, let’s get it from torchvision package and do the required pre-processing.

import torch.nn as nn
import torchvision.datasets as dset
import os
from pathlib import Path

# Create folder to save Datasets if it doesn't exist already
CWD = os.getcwd()
DATA_PATH = CWD + '/datasets'
Path(DATA_PATH).mkdir(parents=True, exist_ok=True)

test_ds = dset.FashionMNIST(DATA_PATH, train=False, download=True)

# Get 100 samples, reshape to [N,C,H,W] and transform to numpy.
max = 100
x = test_ds.data.unsqueeze(1).numpy()[0:max]
y = test_ds.targets.numpy()[0:max]

Let’s now get the model. As we saw on the previous tutorial, we can compile the model from 4 formats and then use it for prediction. We will be using the id from one of the models trained on the previous tutorial:

trained_model = "5efdfa49e8479257197d54c0"


The output of npu.train() provides you the id of the trained model and you can directly refer to this output. You can also access to the id of the trained model from your dashboard.

Now that we have the data and the model, let’s run a batch of predictions and check the accuracy of our model.

import npu
import numpy as np


# Get a batch of results
predictions = npu.predict(trained_model, x)

accuracy = 0
for i, result in enumerate(predictions.get_result()):
    predicted = np.argmax(result)
    if y[i] == predicted:
print(accuracy, "correct out of", max)


Use method .get_result() to retrieve the result from the cloud.


78 correct out of 100