34data/animal-10
Viewer • Updated • 26.2k • 67
How to use jramduran/animals10-convnext-tiny with Keras:
# !pip install -U keras tensorflow huggingface_hub
# Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here;
# "jax" and "torch" also work for computation once TensorFlow is installed.
import os
os.environ["KERAS_BACKEND"] = "tensorflow"
import keras
model = keras.saving.load_model("hf://jramduran/animals10-convnext-tiny")
Classifies a photo into one of 10 animals: butterfly, cat, chicken, cow, dog, elephant, horse, sheep, spider, squirrel.
Transfer learning from ConvNeXt-Tiny pre-trained on ImageNet: the classification head is trained first with the backbone frozen, then the last 40 layers are fine-tuned at a low learning rate.
| Metric (held-out test, n=2618) | Value |
|---|---|
| Accuracy | 0.987 |
| F1 macro | 0.987 |
| Precision (macro) | 0.988 |
| Recall (macro) | 0.987 |
Data: Animals-10, 26,177 photos, stratified split 80/10/10 (train / validation for early stopping / untouched test). The four models of the project share exactly the same split, so their scores are directly comparable.
Training: input 224x224, 8 epochs, on NVIDIA GeForce RTX 5060 Laptop GPU (5.7 min).
Code: https://github.com/JTiiz/CNN-project (training/).
import os
os.environ["KERAS_BACKEND"] = "torch" # the backend the model was trained and tested with
import json, keras, numpy as np
from huggingface_hub import snapshot_download
from PIL import Image
path = snapshot_download("jramduran/animals10-convnext-tiny")
info = json.load(open(f"{path}/model_info.json"))
model = keras.saving.load_model(f"{path}/model.keras")
size = info["input_size"]
img = Image.open("cat.jpg").convert("RGB").resize((size, size))
x = np.asarray(img, dtype="float32")[None] # raw pixels 0-255, no normalisation
probs = model.predict(x, verbose=0)[0]
print(info["labels"][probs.argmax()], probs.max())