|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +A script to evaluate the accuracy of a detector on a given dataset. |
| 4 | +It will upload the images to the detector and compare the predicted labels with the ground truth labels. |
| 5 | +You can specify the delay between uploads. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import PIL |
| 11 | +import time |
| 12 | +import PIL.Image |
| 13 | +import pandas as pd |
| 14 | +import logging |
| 15 | + |
| 16 | +from groundlight import Groundlight, Detector, BinaryClassificationResult |
| 17 | +from tqdm.auto import tqdm |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | +logging.basicConfig(level=logging.INFO) |
| 21 | + |
| 22 | + |
| 23 | +def upload_image(gl: Groundlight, detector: Detector, image: PIL) -> BinaryClassificationResult: |
| 24 | + """ |
| 25 | + Upload a image with a label to a detector. |
| 26 | +
|
| 27 | + Args: |
| 28 | + gl: The Groundlight object. |
| 29 | + detector: The detector to upload to. |
| 30 | + image: The image to upload. |
| 31 | + Returns: |
| 32 | + The predicted label (YES/NO). |
| 33 | + """ |
| 34 | + |
| 35 | + # Convert image to jpg if not already |
| 36 | + if image.format != "JPEG": |
| 37 | + image = image.convert("RGB") |
| 38 | + |
| 39 | + # Use ask_ml to upload the image and then add the label to the image query |
| 40 | + iq = gl.ask_ml(detector=detector, image=image) |
| 41 | + return iq.result |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + parser = argparse.ArgumentParser(description="Evaluate the accuracy of a detector on a given dataset.") |
| 46 | + parser.add_argument("--detector-id", type=str, required=True, help="The ID of the detector to evaluate.") |
| 47 | + parser.add_argument("--dataset", type=str, required=True, help="The folder containing the dataset.csv and images folder") |
| 48 | + parser.add_argument("--delay", type=float, required=False, default=0.1, help="The delay between uploads.") |
| 49 | + args = parser.parse_args() |
| 50 | + |
| 51 | + gl = Groundlight() |
| 52 | + detector = gl.get_detector(args.detector_id) |
| 53 | + |
| 54 | + # Load the dataset from the CSV file and images from the images folder |
| 55 | + # The CSV file should have two columns: image_name and label (YES/NO) |
| 56 | + |
| 57 | + dataset = pd.read_csv(os.path.join(args.dataset, "dataset.csv")) |
| 58 | + images = os.listdir(os.path.join(args.dataset, "images")) |
| 59 | + |
| 60 | + logger.info(f"Evaluating {len(dataset)} images on detector {detector.name} with delay {args.delay}.") |
| 61 | + |
| 62 | + # Record the number of correct predictions |
| 63 | + # Also record the number of false positives and false negatives |
| 64 | + correct = 0 |
| 65 | + total_processed = 0 |
| 66 | + false_positives = 0 |
| 67 | + false_negatives = 0 |
| 68 | + average_confidence = 0 |
| 69 | + |
| 70 | + for image_name, label in tqdm(dataset.values): |
| 71 | + if image_name not in images: |
| 72 | + logger.warning(f"Image {image_name} not found in images folder.") |
| 73 | + continue |
| 74 | + |
| 75 | + if label not in ["YES", "NO"]: |
| 76 | + logger.warning(f"Invalid label {label} for image {image_name}. Skipping.") |
| 77 | + continue |
| 78 | + |
| 79 | + image = PIL.Image.open(os.path.join(args.dataset, "images", image_name)) |
| 80 | + result = upload_image(gl=gl, detector=detector, image=image) |
| 81 | + |
| 82 | + if result.label == label: |
| 83 | + correct += 1 |
| 84 | + elif result.label == "YES" and label == "NO": |
| 85 | + false_positives += 1 |
| 86 | + elif result.label == "NO" and label == "YES": |
| 87 | + false_negatives += 1 |
| 88 | + |
| 89 | + average_confidence += result.confidence |
| 90 | + total_processed += 1 |
| 91 | + |
| 92 | + time.sleep(args.delay) |
| 93 | + |
| 94 | + # Calculate the accuracy, precision, and recall |
| 95 | + accuracy = correct / total_processed if total_processed > 0 else 0 |
| 96 | + precision = correct / (correct + false_positives) if correct + false_positives > 0 else 0 |
| 97 | + recall = correct / (correct + false_negatives) if correct + false_negatives > 0 else 0 |
| 98 | + |
| 99 | + logger.info(f"Processed {total_processed} images.") |
| 100 | + logger.info(f"Correct: {correct}/{total_processed}") |
| 101 | + logger.info(f"Average Confidence: {average_confidence / total_processed:.2f}") |
| 102 | + logger.info(f"False Positives: {false_positives}") |
| 103 | + logger.info(f"False Negatives: {false_negatives}") |
| 104 | + logger.info(f"Accuracy: {accuracy:.2f}") |
| 105 | + logger.info(f"Precision: {precision:.2f}") |
| 106 | + logger.info(f"Recall: {recall:.2f}") |
0 commit comments