How to obtain the class labels in the prediction of the model?

I trained the object detection model into 3 classes using CreateML. Loaded the model into coremltools and performed the prediction, got 'coordinates' and 'confidence' but got no class labels. How can I get class labels?



{'coordinates': array([[0.31552333, 0.30995899, 0.38225624, 0.59993058],
[0.41404313, 0.5812282 , 0.07037259, 0.06291649],
[0.23581643, 0.08297428, 0.18884215, 0.12770388],
[0.42899013, 0.18289472, 0.06921174, 0.06600466]]), 'confidence': array([[9.92818241e-06, 9.99987483e-01, 8.74077966e-09],
[1.00150883e-05, 1.29828220e-06, 9.98730123e-01],
[9.98335183e-01, 1.02320407e-03, 2.00047023e-09],
[6.40532759e-04, 1.93859320e-04, 8.27081084e-01]])}



Code Block language
def get_feature_size(spec):
input_description = spec.description.input[0]
inputSizeImg = input_description.type.imageType.imageSizeRange.widthRange.lowerBound, \
input_description.type.imageType.imageSizeRange.heightRange.lowerBound
return inputSizeImg
loaded_model = coremltools.models.MLModel(modelPath)
inputSize_img = get_feature_size(loaded_model.get_spec())
with Image.open(imgPath).resize(input_size_img) as img:
out_dict = model.predict({'image':img})


The confidence array has the confidence of every class for each detected object. Some objects it may identify as 90% sure it's a cat, but also 50% sure it's a dog. To find the class with the highest confidence, just find the element in the confidence array and the index of that item is the class.

So, here's how your objects break out:

  • Object 1:
    • Coordinates: [0.31552333, 0.30995899, 0.38225624, 0.59993058] (percentage of the width/height)
    • Confidences: [9.92818241e-06, 9.99987483e-01, 8.74077966e-09] (0.0-1.0 where the index in the array is the 0-based class index)
    • Most confident class: 1 (it has essentially 0% on classes 0 and 2, but 99.99% on class 1)
  • Object 2:
    • Coordinates: [0.41404313, 0.5812282 , 0.07037259, 0.06291649]
    • Confidences: [1.00150883e-05, 1.29828220e-06, 9.98730123e-01]
    • Most confident class: 2 (it's 99.87% confident for class 2)

In python you might do something like this (using numpy to get the index):

coordinatesOfObject0 = out_dict['coordinates'][0]
confidenceArrayOfObject0 = out_dict['confidence'][0]
highestConfidenceClassOfObject0 = np.argmax(confidenceArrayOfObject0)

This was really confusing to me too :)

How to obtain the class labels in the prediction of the model?
 
 
Q