https://github.com/filipspl/tf_consensus_score
Calculating consensus scores from multiple TensorFlow Lite classificaitons
https://github.com/filipspl/tf_consensus_score
consensus python3 tensorflow-lite
Last synced: 12 days ago
JSON representation
Calculating consensus scores from multiple TensorFlow Lite classificaitons
- Host: GitHub
- URL: https://github.com/filipspl/tf_consensus_score
- Owner: filipsPL
- License: apache-2.0
- Created: 2023-09-23T15:36:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-25T17:19:59.000Z (over 2 years ago)
- Last Synced: 2025-02-28T23:10:15.673Z (over 1 year ago)
- Topics: consensus, python3, tensorflow-lite
- Language: Python
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tf_consensus_score
This module contains a collection of functions for calculating consensus scores from multiple TensorFlow Lite classificaitons.
```text
┌─────────────┐
│ │
│ Image │ ┌─────────────────────────────────┐ ┌──────────────────────┐
│ ├──────►│ │ │ │
└─────────────┘ │ │ │ │
│ │ │ Output: │
┌─────────────┐ │ tf_consensus_score.py ├─────►│ │
│tfl model 1 ├──────►│ │ │ cat 0.9255041480 │
├─────────────┤ │ │ │ │
│tfl model 2 │ │ │ │ │
├─────────────┤ └─────────────────────────────────┘ └──────────────────────┘
│tfl model n │
└─────────────┘
```
Usage:
```python
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
```
## Installation
Just download `tf_consensus_score.py` and put in the program directory.
## Functions
### `calc_probabilities_for_image(image, local_model_file)`
Calculate probabilities for a given image using a specific model file.
#### Arguments
- `image`: The input image to be classified.
- `local_model_file`: The path to the local TensorFlow Lite model file.
#### Returns
A list of classification categories and their corresponding probabilities.
### `calculate_consensus_scores(ProbabilitiesForImage)`
Calculate consensus scores based on a list of probabilities for multiple models.
#### Arguments
- `ProbabilitiesForImage`: A list of probabilities for each model.
#### Returns
A list of consensus scores for each category based on the input probabilities.
### `return_best_consensus_category(consensus)`
Find the category with the highest consensus score.
#### Arguments
- `consensus`: A list of consensus scores for each category.
#### Returns
A tuple containing the best category name and its score.
### `calc_probabilities_for_all_models(image, local_model_files, models_path)`
Calculate probabilities for all models in a list.
#### Arguments
- `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.
#### Returns
A list of probabilities for each model in the input list.
### `calc_consensus(image, local_model_files, models_path)`
Calculate the consensus category and score for a given image.
#### Arguments
- `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.
#### Returns
A tuple containing the consensus category name and its score based on the input probabilities.