In this Deep Neural Network project, we will be training a ResNet-n (n=9)
neural networks architecture with a different layers to classify a diverse set of 250 Bird Species from the Kaggle dataset with over 95% accuracy
. For this project, I used the 250 Birds Species Dataset, which consists of 250 bird species. 35215 training images, 1250 test images(5 per species) and 12500 validation images(5 per species. All images are 224 X 224 X 3 color images in jpg format. Also includes a “consolidated” image set that combines the training, test and validation images into a single data set.
!pip install jovian --upgrade --quiet
Let's begin by installing and importing the required libraries.
# Uncomment and run the appropriate command for your operating system, if required
# No installation is reqiured on Google Colab / Kaggle notebooks
# Linux / Binder / Windows (No GPU)
# !pip install numpy matplotlib torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# Linux / Windows (GPU)
# pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
# MacOS (NO GPU)
# !pip install numpy matplotlib torch torchvision torchaudio
!pip install opendatasets --upgrade --quiet
import opendatasets as od
import os
import torch
import torchvision
import tarfile
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torchvision.datasets.utils import download_url
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
import torchvision.transforms as tt
from torch.utils.data import random_split
from torchvision.utils import make_grid
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
matplotlib.rcParams['figure.facecolor'] = '#ffffff'
from PIL import Image
from pathlib import Path
import pandas as pd
import math
import cv2
from scipy import signal
import keras
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPool2D,Dropout,BatchNormalization,Flatten,Activation
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from keras.utils import plot_model
import seaborn as sns
project_name='Bird-Species-Classification'
jovian.commit(project=project_name, environment=None)
[jovian] Detected Colab notebook...
[jovian] Please enter your API key ( from https://jovian.ai/ ):
API KEY: ··········
[jovian] Uploading colab notebook to Jovian...
[jovian] Committed successfully! https://jovian.ai/aishwarya-mudaliar2019/bird-species-classification
from torchvision.datasets.utils import download_url
# Download the 250 Birds Species dataset
dataset_url = 'https://www.kaggle.com/gpiosenka/100-bird-species/download'
od.download(dataset_url)
Please provide your Kaggle credentials to download this dataset. Learn more: http://bit.ly/kaggle-creds
Your Kaggle username: aishaaam
Your Kaggle Key: ··········
0%| | 0.00/1.54G [00:00<?, ?B/s]
Downloading 100-bird-species.zip to ./100-bird-species
100%|██████████| 1.54G/1.54G [00:12<00:00, 134MB/s]
Data set of 250 bird species. 35215 training images, 1250 test images(5 per species) and 12500 validation images(5 per species. All images are 224 X 224 X 3 color images in jpg format. Also includes a "consolidated" image set that combines the training, test and validation images into a single data set.
print(os.listdir('.'))
['.config', '100-bird-species', 'sample_data']
# Look into the 250 Birds Species data directory
data_dir = './100-bird-species'
print(os.listdir(data_dir))
['consolidated', 'train', 'valid', 'test']
classes = os.listdir(data_dir + "/train")
print(len(classes))
print(classes)
250
['GILDED FLICKER', 'LARK BUNTING', 'GREAT POTOO', 'RED HONEY CREEPER', 'EVENING GROSBEAK', 'D-ARNAUDS BARBET', 'GREY PLOVER', 'ROADRUNNER', 'TAIWAN MAGPIE', 'INCA TERN', 'DARK EYED JUNCO', 'GOLDEN PIPIT', 'PURPLE MARTIN', 'MALACHITE KINGFISHER', 'OKINAWA RAIL', 'BROWN NOODY', 'CANARY', 'CASSOWARY', 'BLACK THROATED WARBLER', 'WHITE NECKED RAVEN', 'PUFFIN', 'CALIFORNIA GULL', 'MALABAR HORNBILL', 'SPANGLED COTINGA', 'EURASIAN GOLDEN ORIOLE', 'CURL CRESTED ARACURI', 'AFRICAN FIREFINCH', 'RED BEARDED BEE EATER', 'MIKADO PHEASANT', 'PURPLE FINCH', 'VULTURINE GUINEAFOWL', 'TREE SWALLOW', 'MOURNING DOVE', 'ANHINGA', 'AMERICAN REDSTART', 'YELLOW HEADED BLACKBIRD', 'COMMON HOUSE MARTIN', 'SHOEBILL', 'RING-NECKED PHEASANT', 'SCARLET MACAW', 'GLOSSY IBIS', 'SAMATRAN THRUSH', 'CASPIAN TERN', 'TAKAHE', 'BLACK FRANCOLIN', 'NORTHERN GOSHAWK', 'JAVAN MAGPIE', 'KIWI', 'EASTERN TOWEE', 'GRAY CATBIRD', 'ROUGH LEG BUZZARD', 'YELLOW CACIQUE', 'NORTHERN BALD IBIS', 'PHILIPPINE EAGLE', 'DOWNY WOODPECKER', 'ANTBIRD', 'VERMILION FLYCATHER', 'IMPERIAL SHAQ', 'EASTERN BLUEBIRD', 'ROCK DOVE', 'COMMON FIRECREST', 'TOWNSENDS WARBLER', 'MARABOU STORK', 'BALI STARLING', 'STEAMER DUCK', 'CAPUCHINBIRD', 'NORTHERN PARULA', 'CRESTED CARACARA', 'SPOONBILL', 'PEACOCK', 'GOLDEN CHEEKED WARBLER', 'BARN OWL', 'ROBIN', 'BALTIMORE ORIOLE', 'EASTERN ROSELLA', 'EURASIAN MAGPIE', 'AMERICAN COOT', 'RED HEADED DUCK', 'AMERICAN BITTERN', 'VARIED THRUSH', 'BAY-BREASTED WARBLER', 'FLAME TANAGER', 'CROWNED PIGEON', 'EASTERN MEADOWLARK', 'HAWAIIAN GOOSE', 'BLACK SKIMMER', 'WOOD DUCK', 'PARADISE TANAGER', 'HORNED SUNGEM', 'LONG-EARED OWL', 'KAKAPO', 'BELTED KINGFISHER', 'SPOON BILED SANDPIPER', 'ARARIPE MANAKIN', 'PURPLE GALLINULE', 'RED BELLIED PITTA', 'WATTLED CURASSOW', 'AMERICAN KESTREL', 'BLACK-THROATED SPARROW', 'GAMBELS QUAIL', 'PARUS MAJOR', 'MYNA', 'BIRD OF PARADISE', 'HOODED MERGANSER', 'HELMET VANGA', 'WHITE TAILED TROPIC', 'ROSY FACED LOVEBIRD', 'TRUMPTER SWAN', 'SWINHOES PHEASANT', 'HARPY EAGLE', 'NORTHERN CARDINAL', 'STRAWBERRY FINCH', 'BARN SWALLOW', 'RAINBOW LORIKEET', 'EMPEROR PENGUIN', 'INDIGO BUNTING', 'KING VULTURE', 'CHIPPING SPARROW', 'GYRFALCON', 'GOULDIAN FINCH', 'MALEO', 'COUCHS KINGBIRD', 'TURQUOISE MOTMOT', 'ANNAS HUMMINGBIRD', 'CARMINE BEE-EATER', 'COCKATOO', 'TOUCHAN', 'NORTHERN FLICKER', 'LEARS MACAW', 'FLAMINGO', 'WILSONS BIRD OF PARADISE', 'JABIRU', 'NICOBAR PIGEON', 'RUFUOS MOTMOT', 'SORA', 'MASKED BOOBY', 'GRAY PARTRIDGE', 'HOOPOES', 'TASMANIAN HEN', 'SRI LANKA BLUE MAGPIE', 'BLACKBURNIAM WARBLER', 'BARRED PUFFBIRD', 'TEAL DUCK', 'HORNBILL', 'CUBAN TODY', 'RAZORBILL', 'NORTHERN RED BISHOP', 'COCK OF THE ROCK', 'CINNAMON TEAL', 'GREATOR SAGE GROUSE', 'CRESTED AUKLET', 'NORTHERN JACANA', 'SAND MARTIN', 'AFRICAN CROWNED CRANE', 'PURPLE SWAMPHEN', 'STRIPPED SWALLOW', 'QUETZAL', 'WHIMBREL', 'PAINTED BUNTIG', 'BLUE GROUSE', 'WHITE CHEEKED TURACO', 'BLACK VULTURE', 'UMBRELLA BIRD', 'ASIAN CRESTED IBIS', 'BROWN THRASHER', 'LILAC ROLLER', 'RED WISKERED BULBUL', 'RED HEADED WOODPECKER', 'EMU', 'ALEXANDRINE PARAKEET', 'CALIFORNIA CONDOR', 'ELLIOTS PHEASANT', 'VENEZUELIAN TROUPIAL', 'SCARLET IBIS', 'CACTUS WREN', 'HOUSE FINCH', 'SMITHS LONGSPUR', 'PEREGRINE FALCON', 'RUBY THROATED HUMMINGBIRD', 'CAPE MAY WARBLER', 'RED WINGED BLACKBIRD', 'COMMON GRACKLE', 'CHARA DE COLLAR', 'SUPERB STARLING', 'OYSTER CATCHER', 'GO AWAY BIRD', 'PALILA', 'BALD EAGLE', 'RED FACED CORMORANT', 'BLUE HERON', 'FRIGATE', 'GOLDEN CHLOROPHONIA', 'AMERICAN PIPIT', 'COMMON STARLING', 'BEARDED BARBET', 'RED FACED WARBLER', 'GREEN JAY', 'COMMON LOON', 'KILLDEAR', 'MANDRIN DUCK', 'NORTHERN MOCKINGBIRD', 'SPLENDID WREN', 'BLACK SWAN', 'BLACK-CAPPED CHICKADEE', 'GOLD WING WARBLER', 'SHORT BILLED DOWITCHER', 'CROW', 'GUINEAFOWL', 'KOOKABURRA', 'YELLOW BELLIED FLOWERPECKER', 'BLACK-NECKED GREBE', 'MASKED LAPWING', 'BOBOLINK', 'ALBATROSS', 'GOLDEN EAGLE', 'CALIFORNIA QUAIL', 'GOLDEN PHEASANT', 'AMERICAN AVOCET', 'MALLARD DUCK', 'HORNED GUAN', 'BANDED BROADBILL', 'NORTHERN GANNET', 'HOATZIN', 'GUINEA TURACO', 'INDIAN PITTA', 'HOUSE SPARROW', 'TIT MOUSE', 'OSPREY', 'ELEGANT TROGON', 'WILD TURKEY', 'INDIAN BUSTARD', 'STORK BILLED KINGFISHER', 'BAR-TAILED GODWIT', 'VIOLET GREEN SWALLOW', 'TURKEY VULTURE', 'BANANAQUIT', 'PELICAN', 'OSTRICH', 'FIRE TAILLED MYZORNIS', 'GILA WOODPECKER', 'CRESTED NUTHATCH', 'SNOWY OWL', 'JAVA SPARROW', 'CHUKAR PARTRIDGE', 'AMERICAN GOLDFINCH', 'SNOWY EGRET', 'RUFOUS KINGFISHER', 'COMMON POORWILL', 'OCELLATED TURKEY', 'PINK ROBIN']
Let's look inside a couple of folders, one from the training set and another from the test set. As an analytic exploration, I can also verify the number of images for each class in the training set as well as in the test set.
SORA_files = os.listdir(data_dir + "/train/SORA")
print('No. of training examples for Bobolink:', len(SORA_files))
print(SORA_files[:10])
No. of training examples for Bobolink: 300
['073.jpg', '277.jpg', '157.jpg', '008.jpg', '033.jpg', '269.jpg', '211.jpg', '110.jpg', '007.jpg', '106.jpg']
baldEagle_test_files = os.listdir(data_dir + "/test/BALD EAGLE")
print("No. of test examples for ship:", len(baldEagle_test_files))
print(baldEagle_test_files[:5])
No. of test examples for ship: 5
['5.jpg', '4.jpg', '1.jpg', '2.jpg', '3.jpg']
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
I also loop through the 250 Bird Species Dataset to determine the number of images belonging to each class as follows:
for cls in classes:
train_images = os.listdir(data_dir + '/train/' + cls)
img_count = len(train_images)
print("Number of images belonging to {} is {}".format(cls, img_count))
Number of images belonging to GILDED FLICKER is 138
Number of images belonging to LARK BUNTING is 117
Number of images belonging to GREAT POTOO is 138
Number of images belonging to RED HONEY CREEPER is 132
Number of images belonging to EVENING GROSBEAK is 144
Number of images belonging to D-ARNAUDS BARBET is 233
Number of images belonging to GREY PLOVER is 120
Number of images belonging to ROADRUNNER is 107
Number of images belonging to TAIWAN MAGPIE is 136
Number of images belonging to INCA TERN is 118
Number of images belonging to DARK EYED JUNCO is 203
Number of images belonging to GOLDEN PIPIT is 113
Number of images belonging to PURPLE MARTIN is 109
Number of images belonging to MALACHITE KINGFISHER is 163
Number of images belonging to OKINAWA RAIL is 107
Number of images belonging to BROWN NOODY is 129
Number of images belonging to CANARY is 160
Number of images belonging to CASSOWARY is 114
Number of images belonging to BLACK THROATED WARBLER is 135
Number of images belonging to WHITE NECKED RAVEN is 112
Number of images belonging to PUFFIN is 124
Number of images belonging to CALIFORNIA GULL is 109
Number of images belonging to MALABAR HORNBILL is 130
Number of images belonging to SPANGLED COTINGA is 112
Number of images belonging to EURASIAN GOLDEN ORIOLE is 135
Number of images belonging to CURL CRESTED ARACURI is 137
Number of images belonging to AFRICAN FIREFINCH is 140
Number of images belonging to RED BEARDED BEE EATER is 197
Number of images belonging to MIKADO PHEASANT is 146
Number of images belonging to PURPLE FINCH is 128
Number of images belonging to VULTURINE GUINEAFOWL is 169
Number of images belonging to TREE SWALLOW is 181
Number of images belonging to MOURNING DOVE is 126
Number of images belonging to ANHINGA is 147
Number of images belonging to AMERICAN REDSTART is 139
Number of images belonging to YELLOW HEADED BLACKBIRD is 159
Number of images belonging to COMMON HOUSE MARTIN is 127
Number of images belonging to SHOEBILL is 175
Number of images belonging to RING-NECKED PHEASANT is 97
Number of images belonging to SCARLET MACAW is 105
Number of images belonging to GLOSSY IBIS is 175
Number of images belonging to SAMATRAN THRUSH is 128
Number of images belonging to CASPIAN TERN is 213
Number of images belonging to TAKAHE is 108
Number of images belonging to BLACK FRANCOLIN is 131
Number of images belonging to NORTHERN GOSHAWK is 112
Number of images belonging to JAVAN MAGPIE is 109
Number of images belonging to KIWI is 138
Number of images belonging to EASTERN TOWEE is 127
Number of images belonging to GRAY CATBIRD is 155
Number of images belonging to ROUGH LEG BUZZARD is 127
Number of images belonging to YELLOW CACIQUE is 155
Number of images belonging to NORTHERN BALD IBIS is 128
Number of images belonging to PHILIPPINE EAGLE is 154
Number of images belonging to DOWNY WOODPECKER is 127
Number of images belonging to ANTBIRD is 150
Number of images belonging to VERMILION FLYCATHER is 155
Number of images belonging to IMPERIAL SHAQ is 144
Number of images belonging to EASTERN BLUEBIRD is 128
Number of images belonging to ROCK DOVE is 132
Number of images belonging to COMMON FIRECREST is 139
Number of images belonging to TOWNSENDS WARBLER is 165
Number of images belonging to MARABOU STORK is 197
Number of images belonging to BALI STARLING is 132
Number of images belonging to STEAMER DUCK is 109
Number of images belonging to CAPUCHINBIRD is 133
Number of images belonging to NORTHERN PARULA is 196
Number of images belonging to CRESTED CARACARA is 146
Number of images belonging to SPOONBILL is 192
Number of images belonging to PEACOCK is 156
Number of images belonging to GOLDEN CHEEKED WARBLER is 176
Number of images belonging to BARN OWL is 119
Number of images belonging to ROBIN is 95
Number of images belonging to BALTIMORE ORIOLE is 137
Number of images belonging to EASTERN ROSELLA is 118
Number of images belonging to EURASIAN MAGPIE is 155
Number of images belonging to AMERICAN COOT is 158
Number of images belonging to RED HEADED DUCK is 103
Number of images belonging to AMERICAN BITTERN is 170
Number of images belonging to VARIED THRUSH is 193
Number of images belonging to BAY-BREASTED WARBLER is 143
Number of images belonging to FLAME TANAGER is 177
Number of images belonging to CROWNED PIGEON is 118
Number of images belonging to EASTERN MEADOWLARK is 190
Number of images belonging to HAWAIIAN GOOSE is 113
Number of images belonging to BLACK SKIMMER is 111
Number of images belonging to WOOD DUCK is 214
Number of images belonging to PARADISE TANAGER is 176
Number of images belonging to HORNED SUNGEM is 126
Number of images belonging to LONG-EARED OWL is 106
Number of images belonging to KAKAPO is 130
Number of images belonging to BELTED KINGFISHER is 125
Number of images belonging to SPOON BILED SANDPIPER is 144
Number of images belonging to ARARIPE MANAKIN is 105
Number of images belonging to PURPLE GALLINULE is 128
Number of images belonging to RED BELLIED PITTA is 151
Number of images belonging to WATTLED CURASSOW is 138
Number of images belonging to AMERICAN KESTREL is 130
Number of images belonging to BLACK-THROATED SPARROW is 168
Number of images belonging to GAMBELS QUAIL is 147
Number of images belonging to PARUS MAJOR is 122
Number of images belonging to MYNA is 141
Number of images belonging to BIRD OF PARADISE is 104
Number of images belonging to HOODED MERGANSER is 135
Number of images belonging to HELMET VANGA is 107
Number of images belonging to WHITE TAILED TROPIC is 175
Number of images belonging to ROSY FACED LOVEBIRD is 139
Number of images belonging to TRUMPTER SWAN is 137
Number of images belonging to SWINHOES PHEASANT is 217
Number of images belonging to HARPY EAGLE is 175
Number of images belonging to NORTHERN CARDINAL is 130
Number of images belonging to STRAWBERRY FINCH is 167
Number of images belonging to BARN SWALLOW is 132
Number of images belonging to RAINBOW LORIKEET is 141
Number of images belonging to EMPEROR PENGUIN is 129
Number of images belonging to INDIGO BUNTING is 147
Number of images belonging to KING VULTURE is 136
Number of images belonging to CHIPPING SPARROW is 115
Number of images belonging to GYRFALCON is 124
Number of images belonging to GOULDIAN FINCH is 130
Number of images belonging to MALEO is 120
Number of images belonging to COUCHS KINGBIRD is 140
Number of images belonging to TURQUOISE MOTMOT is 156
Number of images belonging to ANNAS HUMMINGBIRD is 139
Number of images belonging to CARMINE BEE-EATER is 121
Number of images belonging to COCKATOO is 166
Number of images belonging to TOUCHAN is 136
Number of images belonging to NORTHERN FLICKER is 139
Number of images belonging to LEARS MACAW is 131
Number of images belonging to FLAMINGO is 122
Number of images belonging to WILSONS BIRD OF PARADISE is 126
Number of images belonging to JABIRU is 143
Number of images belonging to NICOBAR PIGEON is 129
Number of images belonging to RUFUOS MOTMOT is 189
Number of images belonging to SORA is 300
Number of images belonging to MASKED BOOBY is 132
Number of images belonging to GRAY PARTRIDGE is 103
Number of images belonging to HOOPOES is 125
Number of images belonging to TASMANIAN HEN is 135
Number of images belonging to SRI LANKA BLUE MAGPIE is 161
Number of images belonging to BLACKBURNIAM WARBLER is 134
Number of images belonging to BARRED PUFFBIRD is 136
Number of images belonging to TEAL DUCK is 159
Number of images belonging to HORNBILL is 122
Number of images belonging to CUBAN TODY is 122
Number of images belonging to RAZORBILL is 194
Number of images belonging to NORTHERN RED BISHOP is 135
Number of images belonging to COCK OF THE ROCK is 124
Number of images belonging to CINNAMON TEAL is 117
Number of images belonging to GREATOR SAGE GROUSE is 184
Number of images belonging to CRESTED AUKLET is 106
Number of images belonging to NORTHERN JACANA is 156
Number of images belonging to SAND MARTIN is 95
Number of images belonging to AFRICAN CROWNED CRANE is 137
Number of images belonging to PURPLE SWAMPHEN is 154
Number of images belonging to STRIPPED SWALLOW is 120
Number of images belonging to QUETZAL is 152
Number of images belonging to WHIMBREL is 138
Number of images belonging to PAINTED BUNTIG is 163
Number of images belonging to BLUE GROUSE is 185
Number of images belonging to WHITE CHEEKED TURACO is 153
Number of images belonging to BLACK VULTURE is 126
Number of images belonging to UMBRELLA BIRD is 144
Number of images belonging to ASIAN CRESTED IBIS is 105
Number of images belonging to BROWN THRASHER is 99
Number of images belonging to LILAC ROLLER is 138
Number of images belonging to RED WISKERED BULBUL is 123
Number of images belonging to RED HEADED WOODPECKER is 133
Number of images belonging to EMU is 106
Number of images belonging to ALEXANDRINE PARAKEET is 165
Number of images belonging to CALIFORNIA CONDOR is 153
Number of images belonging to ELLIOTS PHEASANT is 148
Number of images belonging to VENEZUELIAN TROUPIAL is 127
Number of images belonging to SCARLET IBIS is 138
Number of images belonging to CACTUS WREN is 122
Number of images belonging to HOUSE FINCH is 249
Number of images belonging to SMITHS LONGSPUR is 116
Number of images belonging to PEREGRINE FALCON is 126
Number of images belonging to RUBY THROATED HUMMINGBIRD is 135
Number of images belonging to CAPE MAY WARBLER is 145
Number of images belonging to RED WINGED BLACKBIRD is 127
Number of images belonging to COMMON GRACKLE is 177
Number of images belonging to CHARA DE COLLAR is 104
Number of images belonging to SUPERB STARLING is 144
Number of images belonging to OYSTER CATCHER is 207
Number of images belonging to GO AWAY BIRD is 131
Number of images belonging to PALILA is 119
Number of images belonging to BALD EAGLE is 160
Number of images belonging to RED FACED CORMORANT is 127
Number of images belonging to BLUE HERON is 104
Number of images belonging to FRIGATE is 105
Number of images belonging to GOLDEN CHLOROPHONIA is 135
Number of images belonging to AMERICAN PIPIT is 179
Number of images belonging to COMMON STARLING is 141
Number of images belonging to BEARDED BARBET is 160
Number of images belonging to RED FACED WARBLER is 167
Number of images belonging to GREEN JAY is 156
Number of images belonging to COMMON LOON is 109
Number of images belonging to KILLDEAR is 175
Number of images belonging to MANDRIN DUCK is 130
Number of images belonging to NORTHERN MOCKINGBIRD is 140
Number of images belonging to SPLENDID WREN is 121
Number of images belonging to BLACK SWAN is 112
Number of images belonging to BLACK-CAPPED CHICKADEE is 133
Number of images belonging to GOLD WING WARBLER is 128
Number of images belonging to SHORT BILLED DOWITCHER is 164
Number of images belonging to CROW is 107
Number of images belonging to GUINEAFOWL is 104
Number of images belonging to KOOKABURRA is 143
Number of images belonging to YELLOW BELLIED FLOWERPECKER is 129
Number of images belonging to BLACK-NECKED GREBE is 105
Number of images belonging to MASKED LAPWING is 131
Number of images belonging to BOBOLINK is 157
Number of images belonging to ALBATROSS is 133
Number of images belonging to GOLDEN EAGLE is 123
Number of images belonging to CALIFORNIA QUAIL is 115
Number of images belonging to GOLDEN PHEASANT is 107
Number of images belonging to AMERICAN AVOCET is 179
Number of images belonging to MALLARD DUCK is 135
Number of images belonging to HORNED GUAN is 113
Number of images belonging to BANDED BROADBILL is 194
Number of images belonging to NORTHERN GANNET is 145
Number of images belonging to HOATZIN is 155
Number of images belonging to GUINEA TURACO is 162
Number of images belonging to INDIAN PITTA is 186
Number of images belonging to HOUSE SPARROW is 125
Number of images belonging to TIT MOUSE is 146
Number of images belonging to OSPREY is 127
Number of images belonging to ELEGANT TROGON is 144
Number of images belonging to WILD TURKEY is 144
Number of images belonging to INDIAN BUSTARD is 131
Number of images belonging to STORK BILLED KINGFISHER is 135
Number of images belonging to BAR-TAILED GODWIT is 114
Number of images belonging to VIOLET GREEN SWALLOW is 201
Number of images belonging to TURKEY VULTURE is 149
Number of images belonging to BANANAQUIT is 106
Number of images belonging to PELICAN is 118
Number of images belonging to OSTRICH is 123
Number of images belonging to FIRE TAILLED MYZORNIS is 150
Number of images belonging to GILA WOODPECKER is 146
Number of images belonging to CRESTED NUTHATCH is 163
Number of images belonging to SNOWY OWL is 161
Number of images belonging to JAVA SPARROW is 122
Number of images belonging to CHUKAR PARTRIDGE is 168
Number of images belonging to AMERICAN GOLDFINCH is 133
Number of images belonging to SNOWY EGRET is 132
Number of images belonging to RUFOUS KINGFISHER is 156
Number of images belonging to COMMON POORWILL is 161
Number of images belonging to OCELLATED TURKEY is 118
Number of images belonging to PINK ROBIN is 128
data_dir = './100-bird-species'
train_directory= data_dir + '/train'
test_directory= data_dir + '/test'
val_directory= data_dir + '/valid'
train_datagen=ImageDataGenerator(rescale=1/255)
val_datagen=ImageDataGenerator(rescale=1/255)
test_datagen=ImageDataGenerator(rescale=1/255)
train_generator=train_datagen.flow_from_directory(train_directory,
target_size=(224,224),
color_mode='rgb',
batch_size=256,
class_mode='sparse')
val_generator=val_datagen.flow_from_directory(val_directory,
target_size=(224,224),
batch_size=256,
color_mode='rgb',
class_mode='sparse')
test_generator=test_datagen.flow_from_directory(test_directory,
batch_size=256,
target_size=(224,224),
color_mode='rgb',
class_mode='sparse')
Found 35215 images belonging to 250 classes.
Found 1250 images belonging to 250 classes.
Found 1250 images belonging to 250 classes.
num_classes=(len(train_generator.class_indices))
print(num_classes)
250
train_generator.class_indices
{'AFRICAN CROWNED CRANE': 0,
'AFRICAN FIREFINCH': 1,
'ALBATROSS': 2,
'ALEXANDRINE PARAKEET': 3,
'AMERICAN AVOCET': 4,
'AMERICAN BITTERN': 5,
'AMERICAN COOT': 6,
'AMERICAN GOLDFINCH': 7,
'AMERICAN KESTREL': 8,
'AMERICAN PIPIT': 9,
'AMERICAN REDSTART': 10,
'ANHINGA': 11,
'ANNAS HUMMINGBIRD': 12,
'ANTBIRD': 13,
'ARARIPE MANAKIN': 14,
'ASIAN CRESTED IBIS': 15,
'BALD EAGLE': 16,
'BALI STARLING': 17,
'BALTIMORE ORIOLE': 18,
'BANANAQUIT': 19,
'BANDED BROADBILL': 20,
'BAR-TAILED GODWIT': 21,
'BARN OWL': 22,
'BARN SWALLOW': 23,
'BARRED PUFFBIRD': 24,
'BAY-BREASTED WARBLER': 25,
'BEARDED BARBET': 26,
'BELTED KINGFISHER': 27,
'BIRD OF PARADISE': 28,
'BLACK FRANCOLIN': 29,
'BLACK SKIMMER': 30,
'BLACK SWAN': 31,
'BLACK THROATED WARBLER': 32,
'BLACK VULTURE': 33,
'BLACK-CAPPED CHICKADEE': 34,
'BLACK-NECKED GREBE': 35,
'BLACK-THROATED SPARROW': 36,
'BLACKBURNIAM WARBLER': 37,
'BLUE GROUSE': 38,
'BLUE HERON': 39,
'BOBOLINK': 40,
'BROWN NOODY': 41,
'BROWN THRASHER': 42,
'CACTUS WREN': 43,
'CALIFORNIA CONDOR': 44,
'CALIFORNIA GULL': 45,
'CALIFORNIA QUAIL': 46,
'CANARY': 47,
'CAPE MAY WARBLER': 48,
'CAPUCHINBIRD': 49,
'CARMINE BEE-EATER': 50,
'CASPIAN TERN': 51,
'CASSOWARY': 52,
'CHARA DE COLLAR': 53,
'CHIPPING SPARROW': 54,
'CHUKAR PARTRIDGE': 55,
'CINNAMON TEAL': 56,
'COCK OF THE ROCK': 57,
'COCKATOO': 58,
'COMMON FIRECREST': 59,
'COMMON GRACKLE': 60,
'COMMON HOUSE MARTIN': 61,
'COMMON LOON': 62,
'COMMON POORWILL': 63,
'COMMON STARLING': 64,
'COUCHS KINGBIRD': 65,
'CRESTED AUKLET': 66,
'CRESTED CARACARA': 67,
'CRESTED NUTHATCH': 68,
'CROW': 69,
'CROWNED PIGEON': 70,
'CUBAN TODY': 71,
'CURL CRESTED ARACURI': 72,
'D-ARNAUDS BARBET': 73,
'DARK EYED JUNCO': 74,
'DOWNY WOODPECKER': 75,
'EASTERN BLUEBIRD': 76,
'EASTERN MEADOWLARK': 77,
'EASTERN ROSELLA': 78,
'EASTERN TOWEE': 79,
'ELEGANT TROGON': 80,
'ELLIOTS PHEASANT': 81,
'EMPEROR PENGUIN': 82,
'EMU': 83,
'EURASIAN GOLDEN ORIOLE': 84,
'EURASIAN MAGPIE': 85,
'EVENING GROSBEAK': 86,
'FIRE TAILLED MYZORNIS': 87,
'FLAME TANAGER': 88,
'FLAMINGO': 89,
'FRIGATE': 90,
'GAMBELS QUAIL': 91,
'GILA WOODPECKER': 92,
'GILDED FLICKER': 93,
'GLOSSY IBIS': 94,
'GO AWAY BIRD': 95,
'GOLD WING WARBLER': 96,
'GOLDEN CHEEKED WARBLER': 97,
'GOLDEN CHLOROPHONIA': 98,
'GOLDEN EAGLE': 99,
'GOLDEN PHEASANT': 100,
'GOLDEN PIPIT': 101,
'GOULDIAN FINCH': 102,
'GRAY CATBIRD': 103,
'GRAY PARTRIDGE': 104,
'GREAT POTOO': 105,
'GREATOR SAGE GROUSE': 106,
'GREEN JAY': 107,
'GREY PLOVER': 108,
'GUINEA TURACO': 109,
'GUINEAFOWL': 110,
'GYRFALCON': 111,
'HARPY EAGLE': 112,
'HAWAIIAN GOOSE': 113,
'HELMET VANGA': 114,
'HOATZIN': 115,
'HOODED MERGANSER': 116,
'HOOPOES': 117,
'HORNBILL': 118,
'HORNED GUAN': 119,
'HORNED SUNGEM': 120,
'HOUSE FINCH': 121,
'HOUSE SPARROW': 122,
'IMPERIAL SHAQ': 123,
'INCA TERN': 124,
'INDIAN BUSTARD': 125,
'INDIAN PITTA': 126,
'INDIGO BUNTING': 127,
'JABIRU': 128,
'JAVA SPARROW': 129,
'JAVAN MAGPIE': 130,
'KAKAPO': 131,
'KILLDEAR': 132,
'KING VULTURE': 133,
'KIWI': 134,
'KOOKABURRA': 135,
'LARK BUNTING': 136,
'LEARS MACAW': 137,
'LILAC ROLLER': 138,
'LONG-EARED OWL': 139,
'MALABAR HORNBILL': 140,
'MALACHITE KINGFISHER': 141,
'MALEO': 142,
'MALLARD DUCK': 143,
'MANDRIN DUCK': 144,
'MARABOU STORK': 145,
'MASKED BOOBY': 146,
'MASKED LAPWING': 147,
'MIKADO PHEASANT': 148,
'MOURNING DOVE': 149,
'MYNA': 150,
'NICOBAR PIGEON': 151,
'NORTHERN BALD IBIS': 152,
'NORTHERN CARDINAL': 153,
'NORTHERN FLICKER': 154,
'NORTHERN GANNET': 155,
'NORTHERN GOSHAWK': 156,
'NORTHERN JACANA': 157,
'NORTHERN MOCKINGBIRD': 158,
'NORTHERN PARULA': 159,
'NORTHERN RED BISHOP': 160,
'OCELLATED TURKEY': 161,
'OKINAWA RAIL': 162,
'OSPREY': 163,
'OSTRICH': 164,
'OYSTER CATCHER': 165,
'PAINTED BUNTIG': 166,
'PALILA': 167,
'PARADISE TANAGER': 168,
'PARUS MAJOR': 169,
'PEACOCK': 170,
'PELICAN': 171,
'PEREGRINE FALCON': 172,
'PHILIPPINE EAGLE': 173,
'PINK ROBIN': 174,
'PUFFIN': 175,
'PURPLE FINCH': 176,
'PURPLE GALLINULE': 177,
'PURPLE MARTIN': 178,
'PURPLE SWAMPHEN': 179,
'QUETZAL': 180,
'RAINBOW LORIKEET': 181,
'RAZORBILL': 182,
'RED BEARDED BEE EATER': 183,
'RED BELLIED PITTA': 184,
'RED FACED CORMORANT': 185,
'RED FACED WARBLER': 186,
'RED HEADED DUCK': 187,
'RED HEADED WOODPECKER': 188,
'RED HONEY CREEPER': 189,
'RED WINGED BLACKBIRD': 190,
'RED WISKERED BULBUL': 191,
'RING-NECKED PHEASANT': 192,
'ROADRUNNER': 193,
'ROBIN': 194,
'ROCK DOVE': 195,
'ROSY FACED LOVEBIRD': 196,
'ROUGH LEG BUZZARD': 197,
'RUBY THROATED HUMMINGBIRD': 198,
'RUFOUS KINGFISHER': 199,
'RUFUOS MOTMOT': 200,
'SAMATRAN THRUSH': 201,
'SAND MARTIN': 202,
'SCARLET IBIS': 203,
'SCARLET MACAW': 204,
'SHOEBILL': 205,
'SHORT BILLED DOWITCHER': 206,
'SMITHS LONGSPUR': 207,
'SNOWY EGRET': 208,
'SNOWY OWL': 209,
'SORA': 210,
'SPANGLED COTINGA': 211,
'SPLENDID WREN': 212,
'SPOON BILED SANDPIPER': 213,
'SPOONBILL': 214,
'SRI LANKA BLUE MAGPIE': 215,
'STEAMER DUCK': 216,
'STORK BILLED KINGFISHER': 217,
'STRAWBERRY FINCH': 218,
'STRIPPED SWALLOW': 219,
'SUPERB STARLING': 220,
'SWINHOES PHEASANT': 221,
'TAIWAN MAGPIE': 222,
'TAKAHE': 223,
'TASMANIAN HEN': 224,
'TEAL DUCK': 225,
'TIT MOUSE': 226,
'TOUCHAN': 227,
'TOWNSENDS WARBLER': 228,
'TREE SWALLOW': 229,
'TRUMPTER SWAN': 230,
'TURKEY VULTURE': 231,
'TURQUOISE MOTMOT': 232,
'UMBRELLA BIRD': 233,
'VARIED THRUSH': 234,
'VENEZUELIAN TROUPIAL': 235,
'VERMILION FLYCATHER': 236,
'VIOLET GREEN SWALLOW': 237,
'VULTURINE GUINEAFOWL': 238,
'WATTLED CURASSOW': 239,
'WHIMBREL': 240,
'WHITE CHEEKED TURACO': 241,
'WHITE NECKED RAVEN': 242,
'WHITE TAILED TROPIC': 243,
'WILD TURKEY': 244,
'WILSONS BIRD OF PARADISE': 245,
'WOOD DUCK': 246,
'YELLOW BELLIED FLOWERPECKER': 247,
'YELLOW CACIQUE': 248,
'YELLOW HEADED BLACKBIRD': 249}
breeds=list(train_generator.class_indices.keys())
x=list(train_generator.classes)
label=[]
for i in range (0, num_classes):
label.append(x.count(i))
fig_dims = (15, 4)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x=breeds[:60],y=label[:60])
plt.xticks(rotation=90)
plt.show()
fig_dims = (15, 4)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x=breeds[61:120],y=label[61:120])
plt.xticks(rotation=90)
plt.show()
fig_dims = (17, 5)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x=breeds[121:200],y=label[121:200])
plt.xticks(rotation=90)
plt.show()
fig_dims = (10, 4)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x=breeds[201:],y=label[201:])
plt.xticks(rotation=90)
plt.show()
image1 = np.array(Image.open('./100-bird-species/train/BALD EAGLE/122.jpg'))
plt.imshow(image1, cmap='gray')
<matplotlib.image.AxesImage at 0x7f9f35cd9c50>
image2 = np.array(Image.open('./100-bird-species/train/AFRICAN FIREFINCH/007.jpg'))
plt.imshow(image2, cmap='gray')
<matplotlib.image.AxesImage at 0x7f9f35c4ca20>
image3 = np.array(Image.open('./100-bird-species/train/FLAMINGO/003.jpg'))
plt.imshow(image3, cmap='gray')
<matplotlib.image.AxesImage at 0x7f9f35bb5860>
np.array(image2).shape
(224, 224, 3)
image2_1 = np.array(Image.open('./100-bird-species/train/AFRICAN FIREFINCH/007.jpg'))
image2_1 = cv2.Canny(image2_1,224,224)
plt.imshow(image2_1)
<matplotlib.image.AxesImage at 0x7f9f35b1c2b0>
We create training and validation datasets using the ImageFolder class from torchvision. In addition to the ToTensor transform, we also apply some other transforms to the images. The following changes are a few improvement changes we make while creating PyTorch datasets for training and validation:
Channel-wise data normalization
Randomized data augmentations
image_size = 32 # 32
import PIL.Image
# Data transforms (normalization & data augmentation) # tt.RandomCrop(64, padding=4, padding_mode='reflect')
stats = ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
randomrotate = 10 # 0.175 rad
train_tfms = tt.Compose([tt.Resize((image_size, image_size)),
tt.RandomCrop(image_size, padding=4, padding_mode='reflect'),
#tt.Resize(image_size),
#tt.CenterCrop(image_size),
tt.RandomHorizontalFlip(),
## tt.RandomRotation(randomrotate, resample=PIL.Image.NEAREST, expand=False, center=None, fill=None), ##
# tt.RandomResizedCrop(256, scale=(0.5,0.9), ratio=(1, 1)),
## tt.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1), ##
tt.ToTensor(),
tt.Normalize(*stats,inplace=True)])
valid_tfms = tt.Compose([tt.Resize((image_size, image_size)), tt.ToTensor(), tt.Normalize(*stats)])
test_tfms = tt.Compose([tt.Resize((image_size, image_size)), tt.ToTensor(), tt.Normalize(*stats)])
# PyTorch Training & Validation & Test Datasets
train_ds = ImageFolder(data_dir+'/train', train_tfms)
valid_ds = ImageFolder(data_dir+'/valid', valid_tfms)
test_ds = ImageFolder(data_dir+'/test', test_tfms)
Next, we can create data loaders for retrieving images in batches. I will use a relatively large batch size of 400 to utlize a larger portion of the GPU RAM. The, I try reducing the batch size & restarting the kernel if I face an "out of memory" error.
batch_size = 256 # 256
# PyTorch Training & Validation & Test Data Loaders
train_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=3, pin_memory=True)
valid_dl = DataLoader(valid_ds, batch_size*2, num_workers=3, pin_memory=True)
test_dl = DataLoader(test_ds, batch_size*2, num_workers=3, pin_memory=True)
Let's take a look at some sample images from the training dataloader. To display the images, I need to denormalize the pixels values to bring them back into the range (0,1).
def denormalize(images, means, stds):
means = torch.tensor(means).reshape(1, 3, 1, 1)
stds = torch.tensor(stds).reshape(1, 3, 1, 1)
return images * stds + means
def show_batch(dl):
for images, labels in dl:
fig, ax = plt.subplots(figsize=(12, 12))
ax.set_xticks([]); ax.set_yticks([])
denorm_images = denormalize(images, *stats)
ax.imshow(make_grid(denorm_images[:64], nrow=8).permute(1, 2, 0).clamp(0,1))
break
show_batch(train_dl)