This module contains a collection of functions for calculating consensus scores from multiple TensorFlow Lite classificaitons.
┌─────────────┐
│ │
│ Image │ ┌─────────────────────────────────┐ ┌──────────────────────┐
│ ├──────►│ │ │ │
└─────────────┘ │ │ │ │
│ │ │ Output: │
┌─────────────┐ │ tf_consensus_score.py ├─────►│ │
│tfl model 1 ├──────►│ │ │ cat 0.9255041480 │
├─────────────┤ │ │ │ │
│tfl model 2 │ │ │ │ │
├─────────────┤ └─────────────────────────────────┘ └──────────────────────┘
│tfl model n │
└─────────────┘
Usage:
import cv2
from tf_consensus_score import *
# Initialize a dictionary for local model files
model_files = {}
# Add model files to the dictionary
model_files = [
"animals1.tflite",
"animals2.tflite"
]
# Define the path where models are located
models_path = "models/"
# Read the image the cv2 way
image = cv2.imread("cat.jpg", cv2.IMREAD_COLOR)
# Calc consensus score and return the highest scored class
category_name, score = calc_consensus(image, model_files, models_path)
print(category_name, score)
# cat 0.9255041480
Just download tf_consensus_score.py
and put in the program directory.
Calculate probabilities for a given image using a specific model file.
image
: The input image to be classified.local_model_file
: The path to the local TensorFlow Lite model file.
A list of classification categories and their corresponding probabilities.
Calculate consensus scores based on a list of probabilities for multiple models.
ProbabilitiesForImage
: A list of probabilities for each model.
A list of consensus scores for each category based on the input probabilities.
Find the category with the highest consensus score.
consensus
: A list of consensus scores for each category.
A tuple containing the best category name and its score.
Calculate probabilities for all models in a list.
image
: The input image to be classified.models_path
: The path where model files are located.local_model_files
: A list of model file names for a specific category.
A list of probabilities for each model in the input list.
Calculate the consensus category and score for a given image.
image
: The input image to be classified.models_path
: The path where model files are located.local_model_files
: A list of model file names for a specific category.
A tuple containing the consensus category name and its score based on the input probabilities.