PyTorch Android Score

I follow the android demo app from https://github.com/pytorch/android-demo-app, and I have questions below:

From each prediction from the pytorch android, we get back score of each result. I know that the number that close to 0 is the best predicted result.

My questions are

  1. How is the score calculated internally ?
  2. Why the score is negative?
  3. How to get percentage of the result based on the score?

I also posted in github as well: https://github.com/pytorch/android-demo-app/issues/60

Thanks.

  1. Based on the demo code it looks like one demo is using a pretrained torchvision.models.resnet18.
    This model outputs logits for each class, such that the highest logit would correspond to the predicted class. You could apply a softmax, which is not necessary to get the prediction, but will give you probabilities, which might be easier to interpret.

  2. If I’m right in point 1, you should see positive and negative numbers as the logit output.

  3. You could add a softmax layer to your model.

@ptrblck Thanks for the explanation.

For 3, I already have a trained model and serialized it to be used in the mobile app, so by adding a softmax layer to my model, does it mean I have to re-train it since the original model doesn’t have a softmax layer. Are there anyway, that we can calculate percentage based on the result of the serialized model?

No, you don’t need to retrain the model if you just want to get the probabilities with a softmax layer.
I think the easiest way could be to pass your current model and the nn.Softmax layer to a new custom model or an nn.Sequential container and use this new combined model in your app.

Alternatively, you could compute the softmax on the mobile, as the formula is quite straightforward.

@ptrblck thanks.

I added:

Add softmax layer

mobilev2_loaded_model = nn.Sequential(
mobilev2_loaded_model,
nn.Softmax(1)
)

and serialized model again and now I can multiple 100 to the score and get the percentage of that class.

1 Like