gdussert's picture
Update README.md
4a2ba9f verified
|
Raw History Blame Contribute Delete
10.3 kB
---
license: cc-by-sa-4.0
pipeline_tag: image-classification
---
This model was trained by [the DeepFaune initiative](https://www.deepfaune.cnrs.fr/en/).
### Performances
We measured the performance (recall per class) of the classification model:
| Classes | Out-of-sample Test | Out-of-sample Test Support | Training Support |
|------------------------------|--------------------|----------------------------|------------------|
| bison / bison | 99,89% | 4604 | 56951 |
| badger / blaireau | 98,10% | 7490 | 110525 |
| ibex / bouquetin | NA | 0 | 10918 |
| beaver / castor | 98,57% | 70 | 7542 |
| red deer / cerf | 97,77% | 126686 | 370657 |
| golden jackal / chacal doré | 89,39% | 2394 | 10449 |
| chamois / chamois | 97,07% | 5674 | 139071 |
| cat / chat | 97,81% | 22263 | 63646 |
| goat / chèvre | 79,06% | 1237 | 6442 |
| roe deer / chevreuil | 98,57% | 63557 | 484170 |
| dog / chien | 95,75% | 2422 | 54548 |
| raccoon dog / chien viverrin | 92,27% | 556 | 19099 |
| fallow deer / daim | 94,30% | 1176 | 244446 |
| squirrel / écureuil | 98,93% | 2426 | 28598 |
| moose / élan | 97,41% | 6372 | 61257 |
| equid / équidé | 90,53% | 2966 | 43051 |
| genet / genette | 97,27% | 2524 | 917 |
| wolverine / glouton | 92,94% | 5465 | 3494 |
| hedgehog / hérisson | 95,38% | 65 | 718 |
| lagomorph / lagomorphe | 99,16% | 17980 | 76669 |
| wolf / loup | 97,10% | 2383 | 37136 |
| otter / loutre | 100,00% | 2 | 2325 |
| lynx / lynx | 99,90% | 1047 | 10741 |
| mongoose / mangouste | 100,00% | 23 | 616 |
| marmot / marmotte | 94,88% | 1485 | 2338 |
| micromammal / micromammifère | 97,27% | 403 | 35596 |
| mouflon / mouflon | 88,97% | 7848 | 4758 |
| sheep / mouton | 99,24% | 6546 | 197586 |
| mustelid / mustélidé | 97,94% | 5836 | 31782 |
| bird / oiseau | 99,65% | 57884 | 213822 |
| bear / ours | 96,09% | 3732 | 13091 |
| porcupine / porc-épic | 97,59% | 581 | 9319 |
| nutria / ragondin | 88,18% | 770 | 1303 |
| muskrat / rat musqué | 75,00% | 24 | 1444 |
| racoon / raton laveur | 93,56% | 11419 | 60071 |
| fox / renard | 97,43% | 49444 | 254662 |
| arctic fox / renard arctique | 94,37% | 8746 | 336544 |
| reindeer / renne | 98,84% | 518 | 11906 |
| wild boar / sanglier | 99,05% | 172129 | 292876 |
| cow / vache | 98,16% | 7647 | 148429 |
### Minimal inference code
```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import requests
import timm
import torch
import torch.nn as nn
from PIL import Image
from torchvision.transforms import InterpolationMode, transforms
classes = ['bison', 'badger', 'ibex', 'beaver', 'red deer', 'golden jackal',
'chamois', 'cat', 'goat', 'roe deer', 'dog', 'raccoon dog',
'fallow deer', 'squirrel', 'moose', 'equid', 'genet', 'wolverine',
'hedgehog', 'lagomorph', 'wolf', 'otter', 'lynx', 'mongoose',
'marmot', 'micromammal', 'mouflon', 'sheep', 'mustelid', 'bird',
'bear', 'porcupine', 'nutria', 'muskrat', 'raccoon', 'fox',
'arctic fox', 'reindeer', 'wild boar', 'cow']
# Important: DeepFaune is fine-tuned at 224x224 (rather than DINOv3's default 256x256)
# for faster inference with equivalent accuracy.
CROP_SIZE = 224
def cropSquareCVtoPIL(imagecv, box):
# cropped PIL image, as squared as possible (rectangle if close to the borders)
x1, y1, x2, y2 = box
xsize = (x2 - x1)
ysize = (y2 - y1)
if xsize > ysize:
y1 = y1 - int((xsize - ysize) / 2)
y2 = y2 + int((xsize - ysize) / 2)
if ysize > xsize:
x1 = x1 - int((ysize - xsize) / 2)
x2 = x2 + int((ysize - xsize) / 2)
height, width, _ = imagecv.shape
croppedimagecv = imagecv[max(0, int(y1)):min(int(y2), height), max(0, int(x1)):min(int(x2), width)]
croppedimage = Image.fromarray(croppedimagecv[:, :, (2, 1, 0)]) # converted to PIL RGB image
return croppedimage
class Model(nn.Module):
def __init__(self):
super().__init__()
# Important: the model is trained with global_pool = 'token'
# (i.e. using the class token for final classification).
# This is not the default behavior with timm's dinov3 and thus
# should be specified when using timm.create_model:
self.base_model = timm.create_model("vit_large_patch16_dinov3.lvd1689m",
pretrained=False,
num_classes=40,
global_pool="token")
self.transforms = transforms.Compose([
transforms.Resize(size=(CROP_SIZE, CROP_SIZE), interpolation=InterpolationMode.BICUBIC, max_size=None, antialias=None),
transforms.ToTensor(),
transforms.Normalize(mean=[0.4850, 0.4560, 0.4060], std=[0.2290, 0.2240, 0.2250])])
def loadWeights(self, path, device):
params = torch.load(path, weights_only=False, map_location=device)
self.load_state_dict(params['state_dict'])
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Loading the model
model = Model().to(device)
model.loadWeights("deepfaune-vit_large_patch16_dinov3.lvd1689m.pt", device=device)
model.eval()
# Downloading a test image
img_data = requests.get("https://huggingface.co/spaces/DeepFaune/README/resolve/main/chamois1.JPG", timeout=10).content
with open('chamois1.JPG', 'wb') as f:
f.write(img_data)
# Loading the image and cropping using bounding box coordinates in xyxy format
imagecv = cv2.imread("chamois1.JPG")
box = [0.0, 76.54496002197266, 436.7752990722656, 714.8261108398438]
crop = cropSquareCVtoPIL(imagecv, box)
# Preprocessing
preprocessed_crop = model.transforms(crop).unsqueeze(0).to(device)
# Inference
with torch.inference_mode():
embeddings = model.base_model.forward_features(preprocessed_crop)
logits = model.base_model.forward_head(embeddings)
scores = logits.softmax(dim=1).cpu().numpy()
best_idx = scores[0].argmax()
print(classes[best_idx], scores[0, best_idx]) # chamois 0.9999325
```
### Licensing
`DeepFaune` software is released under the [CeCILL](http://www.cecill.info) license, compatible with [GNU GPL](http://www.gnu.org/licenses/gpl-3.0.html).
Our model weights are distributed under [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.en) license not allowing using them without citing and referencing the name `DeepFaune`.
Please cite `DeepFaune` whenever you use it.
```bibtex
@article{rigoudy_deepfaune_2023,
title = {The DeepFaune initiative: a collaborative effort towards the automatic identification of European fauna in camera trap images},
url = {https://doi.org/10.1007/s10344-023-01742-7},
doi = {10.1007/s10344-023-01742-7},
journal = {European Journal of Wildlife Research},
author = {Rigoudy, Noa and Dussert, Gaspard and Benyoub, Abdelbaki and Besnard, Aurélien and Birck, Carole and Boyer, Jérome and Bollet, Yoann and Bunz, Yoann and Caussimont, Gérard and Chetouane, Elias and Carriburu, Jules Chiffard and Cornette, Pierre and Delestrade, Anne and De Backer, Nina and Dispan, Lucie and Le Barh, Maden and Duhayer, Jeanne and Elder, Jean-François and Fanjul, Jean-Baptiste and Fonderflick, Jocelyn and Froustey, Nicolas and Garel, Mathieu and Gaudry, William and Gérard, Agathe and Gimenez, Olivier and Hemery, Arzhela and Hemon, Audrey and Jullien, Jean-Michel and Knitter, Daniel and Malafosse, Isabelle and Marginean, Mircea and Ménard, Louise and Ouvrier, Alice and Pariset, Gwennaelle and Prunet, Vincent and Rabault, Julien and Randon, Malory and Raulet, Yann and Régnier, Antoine and Ribière, Romain and Ricci, Jean-Claude and Ruette, Sandrine and Schneylin, Yann and Sentilles, Jérôme and Siefert, Nathalie and Smith, Bethany and Terpereau, Guillaume and Touchet, Pierrick and Thuiller, Wilfried and Uzal, Antonio and Vautrain, Valentin and Vimal, Ruppert and Weber, Julian and Spataro, Bruno and Miele, Vincent and Chamaillé-Jammes, Simon},
year = {2023}
}
```