
# Bird Classifier EfficientNet-B2 

## Model Description

Have you look at a bird and said "Boahh if only I know what bird that is". 
Unless you're an avid bird spotter (or just love birds in general), it's hard to differentiate some species of birds.
Well you're in luck, turns out you can use a image classifier to identify bird species!

This model is a fine-tuned version of [google/efficientnet-b2](https://huggingface.co/google/efficientnet-b2)
on the [gpiosenka/100-bird-species](https://www.kaggle.com/datasets/gpiosenka/100-bird-species) dataset available on Kaggle.
The dataset used to train the model was taken on September 24th, 2023. 

The original model itself was trained on ImageNet-1K, thus it might still have some useful features for identifying creatures like birds.

In theory, the accuracy for a random guess on this dataset is 0.0019047619 (essentially 1/525). 
The model performed significantly well on all three sets with result being:

- **Training**: 0.999480
- **Validation**: 0.985904
- **Test**: 0.991238

## Intended Uses

You can use the raw model for image classification.
Here is an example of the model in action using a picture of a bird

```python
# Importing the libraries needed
import torch
import urllib.request
from PIL import Image
from transformers import EfficientNetImageProcessor, EfficientNetForImageClassification

# Determining the file URL
url = 'some url'

# Opening the image using PIL
img = Image.open(urllib.request.urlretrieve(url)[0])

# Loading the model and preprocessor from HuggingFace
preprocessor = EfficientNetImageProcessor.from_pretrained("dennisjooo/Birds-Classifier-EfficientNetB2")
model = EfficientNetForImageClassification.from_pretrained("dennisjooo/Birds-Classifier-EfficientNetB2")

# Preprocessing the input
inputs = preprocessor(img, return_tensors="pt")

# Running the inference
with torch.no_grad():
    logits = model(**inputs).logits

# Getting the predicted label
predicted_label = logits.argmax(-1).item()
print(model.config.id2label[predicted_label])
```

Or alternatively you can streamline it using Huggingface's Pipeline

```python
# Importing the libraries needed
import torch
import urllib.request
from PIL import Image
from transformers import pipeline

# Determining the file URL
url = 'some url'

# Opening the image using PIL
img = Image.open(urllib.request.urlretrieve(url)[0])

# Loading the model and preprocessor using Pipeline
pipe = pipeline("image-classification", model="dennisjooo/Birds-Classifier-EfficientNetB2")

# Running the inference
result = pipe(img)[0]

# Printing the result label
print(result['label'])
```

## Training and Evaluation

### Data

The dataset was taken from [gpiosenka/100-bird-species](https://www.kaggle.com/datasets/gpiosenka/100-bird-species) on Kaggle. 
It contains a set of 525 bird species, with 84,635 training images, 2,625 each for validation and test images. 
Every image in the dataset is a 224 by 224 RGB image.

The training process used the same split provided by the author.
For more details, please refer to the [author's Kaggle page](https://www.kaggle.com/datasets/gpiosenka/100-bird-species).

### Training Procedure 

The training was done using PyTorch on Kaggle's free P100 GPU. The process also includes the usage of Lightning and Torchmetrics libraries.

### Preprocessing 
Each image is preprocessed according to the the orginal author's [config](https://huggingface.co/google/efficientnet-b2/blob/main/preprocessor_config.json).

The training set was also augmented using:

- Random rotation of 10 degrees with probability of 50%
- Random horizontal flipping with probability of 50%

### Training Hyperparameters

The following are the hyperparameters used for training:

- **Training regime:** fp32
- **Loss:** Cross entropy
- **Optimizer**: Adam with default betas (0.99, 0.999)
- **Learning rate**: 1e-3
- **Learning rate scheduler**: Reduce on plateau which monitors validation loss with patience of 2 and decay rate of 0.1
- **Batch size**: 64
- **Early stopping**: Monitors validation accuracy with patience of 10

### Results

The image below is the result of the training process both on the training and validation set:

![Training results](https://github.com/dennisjooo/Birds-Classifier-EfficientNetB2/raw/main/logs/metrics.png)


abbotts babbler abbotts booby abyssinian ground hornbill african crowned crane african emerald cuckoo african firefinch african oyster catcher african pied hornbill african pygmy goose albatross alberts towhee alexandrine parakeet alpine chough altamira yellowthroat american avocet american bittern american coot american dipper american flamingo american goldfinch american kestrel american pipit american redstart american robin american wigeon amethyst woodstar andean goose andean lapwing andean siskin anhinga anianiau annas hummingbird antbird antillean euphonia apapane apostlebird araripe manakin ashy storm petrel ashy thrushbird asian crested ibis asian dollard bird asian green bee eater asian openbill stork auckland shaq austral canastero australasian figbird avadavat azaras spinetail azure breasted pitta azure jay azure tanager azure tit baikal teal bald eagle bald ibis bali starling baltimore oriole bananaquit band tailed guan banded broadbill banded pita banded stilt bar-tailed godwit barn owl barn swallow barred puffbird barrows goldeneye bay-breasted warbler bearded barbet bearded bellbird bearded reedling belted kingfisher bird of paradise black and yellow broadbill black baza black breasted puffbird black cockato black faced spoonbill black francolin black headed caique black necked stilt black skimmer black swan black tail crake black throated bushtit black throated huet black throated warbler black vented shearwater black vulture black-capped chickadee black-necked grebe black-throated sparrow blackburniam warbler blonde crested woodpecker blood pheasant blue coau blue dacnis blue gray gnatcatcher blue grosbeak blue grouse blue heron blue malkoha blue throated piping guan blue throated toucanet bobolink bornean bristlehead bornean leafbird bornean pheasant brandt cormarant brewers blackbird brown crepper brown headed cowbird brown noody brown thrasher bufflehead bulwers pheasant burchells courser bush turkey caatinga cacholote cabots tragopan cactus wren california condor california gull california quail campo flicker canary canvasback cape glossy starling cape longclaw cape may warbler cape rock thrush capped heron capuchinbird carmine bee-eater caspian tern cassowary cedar waxwing cerulean warbler chara de collar chattering lory chestnet bellied euphonia chestnut winged cuckoo chinese bamboo partridge chinese pond heron chipping sparrow chucao tapaculo chukar partridge cinnamon attila cinnamon flycatcher cinnamon teal clarks grebe clarks nutcracker cock of the  rock cockatoo collared aracari collared crescentchest common firecrest common grackle common house martin common iora common loon common poorwill common starling coppersmith barbet coppery tailed coucal crab plover crane hawk cream colored woodpecker crested auklet crested caracara crested coua crested fireback crested kingfisher crested nuthatch crested oropendola crested serpent eagle crested shriketit crested wood partridge crimson chat crimson sunbird crow cuban tody cuban trogon curl crested aracuri d-arnauds barbet dalmatian pelican darjeeling woodpecker dark eyed junco daurian redstart demoiselle crane double barred finch double brested cormarant double eyed fig parrot downy woodpecker dunlin dusky lory dusky robin eared pita eastern bluebird eastern bluebonnet eastern golden weaver eastern meadowlark eastern rosella eastern towee eastern wip poor will eastern yellow robin ecuadorian hillstar egyptian goose elegant trogon elliots  pheasant emerald tanager emperor penguin emu enggano myna eurasian bullfinch eurasian golden oriole eurasian magpie european goldfinch european turtle dove evening grosbeak fairy bluebird fairy penguin fairy tern fan tailed widow fasciated wren fiery minivet fiordland penguin fire tailled myzornis flame bowerbird flame tanager forest wagtail frigate frill back pigeon gambels quail gang gang cockatoo gila woodpecker gilded flicker glossy ibis go away bird gold wing warbler golden bower bird golden cheeked warbler golden chlorophonia golden eagle golden parakeet golden pheasant golden pipit gouldian finch grandala gray catbird gray kingbird gray partridge great argus great gray owl great jacamar great kiskadee great potoo great tinamou great xenops greater pewee greater prairie chicken greator sage grouse green broadbill green jay green magpie green winged dove grey cuckooshrike grey headed chachalaca grey headed fish eagle grey plover groved billed ani guinea turaco guineafowl gurneys pitta gyrfalcon hamerkop harlequin duck harlequin quail harpy eagle hawaiian goose hawfinch helmet vanga hepatic tanager himalayan bluetail himalayan monal hoatzin hooded merganser hoopoes horned guan horned lark horned sungem house finch house sparrow hyacinth macaw iberian magpie ibisbill imperial shaq inca tern indian bustard indian pitta indian roller indian vulture indigo bunting indigo flycatcher inland dotterel ivory billed aracari ivory gull iwi jabiru jack snipe jacobin pigeon jandaya parakeet japanese robin java sparrow jocotoco antpitta kagu kakapo killdear king eider king vulture kiwi knob billed duck kookaburra lark bunting laughing gull lazuli bunting lesser adjutant lilac roller limpkin little auk loggerhead shrike long-eared owl looney birds lucifer hummingbird magpie goose malabar hornbill malachite kingfisher malagasy white eye maleo mallard duck mandrin duck mangrove cuckoo marabou stork masked bobwhite masked booby masked lapwing mckays bunting merlin mikado  pheasant military macaw mourning dove myna nicobar pigeon noisy friarbird northern beardless tyrannulet northern cardinal northern flicker northern fulmar northern gannet northern goshawk northern jacana northern mockingbird northern parula northern red bishop northern shoveler ocellated turkey oilbird okinawa rail orange breasted trogon orange brested bunting oriental bay owl ornate hawk eagle osprey ostrich ovenbird oyster catcher painted bunting palila palm nut vulture paradise tanager parakett  auklet parus major patagonian sierra finch peacock peregrine falcon phainopepla philippine eagle pink robin plush crested jay pomarine jaeger puffin puna teal purple finch purple gallinule purple martin purple swamphen pygmy kingfisher pyrrhuloxia quetzal rainbow lorikeet razorbill red bearded bee eater red bellied pitta red billed tropicbird red browed finch red crossbill red faced cormorant red faced warbler red fody red headed duck red headed woodpecker red knot red legged honeycreeper red naped trogon red shouldered hawk red tailed hawk red tailed thrush red winged blackbird red wiskered bulbul regent bowerbird ring-necked pheasant roadrunner rock dove rose breasted cockatoo rose breasted grosbeak roseate spoonbill rosy faced lovebird rough leg buzzard royal flycatcher ruby crowned kinglet ruby throated hummingbird ruddy shelduck rudy kingfisher rufous kingfisher rufous trepe rufuos motmot samatran thrush sand martin sandhill crane satyr tragopan says phoebe scarlet crowned fruit dove scarlet faced liocichla scarlet ibis scarlet macaw scarlet tanager shoebill short billed dowitcher smiths longspur snow goose snow partridge snowy egret snowy owl snowy plover snowy sheathbill sora spangled cotinga splendid wren spoon biled sandpiper spotted catbird spotted whistling duck squacco heron sri lanka blue magpie steamer duck stork billed kingfisher striated caracara striped owl stripped manakin stripped swallow sunbittern superb starling surf scoter swinhoes pheasant tailorbird taiwan magpie takahe tasmanian hen tawny frogmouth teal duck tit mouse touchan townsends warbler tree swallow tricolored blackbird tropical kingbird trumpter swan turkey vulture turquoise motmot umbrella bird varied thrush veery venezuelian troupial verdin vermilion flycather victoria crowned pigeon violet backed starling violet cuckoo violet green swallow violet turaco visayan hornbill vulturine guineafowl wall creaper wattled curassow wattled lapwing whimbrel white breasted waterhen white browed crake white cheeked turaco white crested hornbill white eared hummingbird white necked raven white tailed tropic white throated bee eater wild turkey willow ptarmigan wilsons bird of paradise wood duck wood thrush woodland kingfisher wrentit yellow bellied flowerpecker yellow breasted chat yellow cacique yellow headed blackbird zebra dove