0

For Image Classification in Mxnet using GluonCV Model, I am using a transformed image through the network to obtain predicted probabilities for all ImageNet classes.

def predict_probabilities(network, data):
    """
    Should return the predicted probabilities of ImageNet classes for the given image.

    :param network: pre-trained image classification model
    :type network: mx.gluon.Block
    :param data: batch of transformed images of shape (1, 3, 224, 224)
    :type data: mx.nd.NDArray

    :return: array of probabilities of shape (1000,)
    :rtype: mx.nd.NDArray
    """
    # YOUR CODE HERE
    data=transform_image("")
    pred_probas= network(data)
    pred_probas=pred_probas[0]
    return pred_probas

I have to satisfy these assertions:

assert pred_probas.shape == (1000,)
np.testing.assert_almost_equal(pred_probas.sum().asscalar(), 1, decimal=5)
assert pred_probas.dtype == np.float32

Although I am getting this error:

AssertionError                            Traceback (most recent call last)
<ipython-input-10-70779066d528> in <module>
      1 pred_probas =
predict_probabilities(network, transformed_test_output)
      2 assert
pred_probas.shape == (1000,)
----> 3
np.testing.assert_almost_equal(pred_probas.sum().asscalar(), 1, decimal=5)
4 assert pred_probas.dtype == np.float32

/usr/local/lib/python3.7/dist-
packages/numpy/testing/_private/utils.py in assert_almost_equal(actual, desired,
decimal, err_msg, verbose)
    599         pass
    600     if abs(desired -
actual) >= 1.5 * 10.0**(-decimal):
--> 601         raise
AssertionError(_build_err_msg())
    602 
    603 

AssertionError: 
Arrays are
not almost equal to 5 decimals
 ACTUAL: 314.64026
 DESIRED: 1

How can I overcome this?

1 Answers1

0

After you do a forward pass of the image data through your pre-trained network i.e. pred_probas= network(data)

You have to map predicted values to probability by softmax

prob = mx.nd.softmax(pred_probas)

and then return prob[0]

Since softmax is applied, the outputs would be in the range of 0:1, and hence your np.testing.assert_almost_equal(pred_probas.sum().asscalar(), 1, decimal=5) would satisfy as pred_probas.sum().asscalar() would be almost equal to 1