getImagePredictionListProbabilitiesFromBytesList method

Future<List<double?>?> getImagePredictionListProbabilitiesFromBytesList(
  1. List<Uint8List> imageAsBytesList,
  2. int imageWidth,
  3. int imageHeight, {
  4. List<double> mean = TORCHVISION_NORM_MEAN_RGB,
  5. List<double> std = TORCHVISION_NORM_STD_RGB,
})

Runs batch image classification and returns softmax probabilities.

Processes multiple images in a single batch and returns probability distributions. All probabilities for each image sum to 1.0.

@param imageAsBytesList List of raw image bytes @param imageWidth The width to resize images to @param imageHeight The height to resize images to @param mean Optional normalization mean values (default: ImageNet means) @param std Optional normalization std values (default: ImageNet stds) @return A Future that completes with a list of prediction probabilities

Implementation

Future<List<double?>?> getImagePredictionListProbabilitiesFromBytesList(
    List<Uint8List> imageAsBytesList, int imageWidth, int imageHeight,
    {List<double> mean = TORCHVISION_NORM_MEAN_RGB,
    List<double> std = TORCHVISION_NORM_STD_RGB}) async {
  // Assert mean std
  assert(mean.length == 3, "Mean should have size of 3");
  assert(std.length == 3, "STD should have size of 3");
  List<double?>? prediction = await ModelApi().getImagePredictionList(
      _index, null, imageAsBytesList, imageWidth, imageHeight, mean, std);
  List<double?>? predictionProbabilities = [];

  //Getting sum of exp
  double? sumExp;
  for (var element in prediction) {
    if (sumExp == null) {
      sumExp = exp(element!);
    } else {
      sumExp = sumExp + exp(element!);
    }
  }
  for (var element in prediction) {
    predictionProbabilities.add(exp(element!) / sumExp!);
  }

  return predictionProbabilities;
}