Views
No views yet
vit_base_patch16_224_imagenet21k backbone using Keras 3.--region=GB), moderately rated images (--min-avg-rating=4), with a reasonable number of reviews (--min-reviews=2).model/final_224c_500i_GB_bird_vit.keras: The full Keras 3 model.model/224c_500i_GB_bird_vit_float32.tflite: Float32 TFLite model for mobile/edge deployment.model/224c_500i_GB_bird_vit_quantized.tflite: INT8 Quantized TFLite model for optimized edge deployment.model/224c_500i_GB_bird_classes.json: Ordered list of the 224 eBird taxon codes representing the classes.model/friendly_class_names.csv: Mapping of taxon codes to friendly human-readable bird names.training/british-birds-vit-training.ipynb: The Jupyter Notebook used to train the model.1import keras
2import numpy as np
3import json
4import csv
5from huggingface_hub import hf_hub_download
6
7REPO_ID = "rossheaton/british-birds-vit-base-patch16-224"
8
9# Download model, class codes, and the friendly names CSV
10model_path = hf_hub_download(repo_id=REPO_ID, filename="model/final_224c_500i_GB_bird_vit.keras")
11labels_path = hf_hub_download(repo_id=REPO_ID, filename="model/224c_500i_GB_bird_classes.json")
12csv_path = hf_hub_download(repo_id=REPO_ID, filename="model/friendly_class_names.csv")
13
14# Load the Keras model
15model = keras.models.load_model(model_path)
16
17# Load taxon codes (the raw array of classes)
18with open(labels_path, 'r') as f:
19 taxon_codes = json.load(f)
20
21# Build a dictionary mapping taxon codes to friendly names
22taxon_to_friendly = {}
23with open(csv_path, mode='r', encoding='utf-8') as f:
24 reader = csv.DictReader(f)
25 for row in reader:
26 # Maps e.g. "parjae" -> "Arctic Skua - Stercorarius parasiticus"
27 # Note: Change 'ebird_search_term' to 'rspb_name' if you prefer just the short English name
28 taxon_to_friendly[row['ebird_taxon_code']] = row['ebird_search_term']
29
30# Load and preprocess a local image
31image_path = "path/to/bird/image.jpg"
32img = keras.utils.load_img(image_path, target_size=(224, 224))
33img_array = keras.utils.img_to_array(img)
34img_array = np.expand_dims(img_array, axis=0)
35
36# Predict
37predictions = model.predict(img_array)
38predicted_index = np.argmax(predictions)
39confidence = predictions[0][predicted_index] * 100
40
41# Get the raw taxon code, then look up the friendly name
42predicted_taxon = taxon_codes[predicted_index]
43friendly_name = taxon_to_friendly.get(predicted_taxon, predicted_taxon) # Fallback to taxon code if missing
44
45print(f"Prediction: {friendly_name} ({confidence:.2f}% confidence)")