{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Bird Classification using ResNet34","metadata":{}},{"cell_type":"markdown","source":"## Importing the Libraries","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torchvision import transforms as T\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport os\n\nBASE_DIR = \"/kaggle/input/100-bird-species\"\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(\"Device:\", device)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T14:04:36.472756Z","iopub.execute_input":"2023-01-18T14:04:36.473269Z","iopub.status.idle":"2023-01-18T14:04:36.482978Z","shell.execute_reply.started":"2023-01-18T14:04:36.473225Z","shell.execute_reply":"2023-01-18T14:04:36.481718Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stdout","text":"Device: cuda\n","output_type":"stream"}]},{"cell_type":"markdown","source":"Setting seed for reproducibility","metadata":{}},{"cell_type":"code","source":"seed = 42\ndef set_seed(seed):\n torch.manual_seed(seed)\n torch.backends.cudnn.deterministic = True\n torch.backends.cudnn.benchmark = False\n np.random.seed(seed)\n\nset_seed(seed)","metadata":{"execution":{"iopub.status.busy":"2023-01-18T14:04:36.485714Z","iopub.execute_input":"2023-01-18T14:04:36.486482Z","iopub.status.idle":"2023-01-18T14:04:36.492961Z","shell.execute_reply.started":"2023-01-18T14:04:36.486442Z","shell.execute_reply":"2023-01-18T14:04:36.491874Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"markdown","source":"## Loading the Dataset","metadata":{}},{"cell_type":"markdown","source":"The dataset is taken from [Kaggle](https://www.kaggle.com/datasets/gpiosenka/100-bird-species) and consists of 70,658 training, 2250 test and 2259 validation images of birds belonging to 450 different species. The images are of size 224x224x3(HxWxC). The `birds.csv` files consists of the class ID, file paths, labels, scientific label and the dataset[train, test, val] to which the image belongs to. The images are read using python `PIL` library and transforms are applied.","metadata":{}},{"cell_type":"code","source":"paths_df = pd.read_csv(os.path.join(BASE_DIR, \"birds.csv\"))\npaths_df.drop(46620, axis= 0, inplace=True) # The dataset as of 9-Jan-2023 contains a file that is \n# present at the given index whose dimension is not 224x224. Removing the file to avoid unnecessary complexity in the code\nlabels = paths_df[\"class id\"].unique()\nbird_name_map = {int(i): paths_df[paths_df[\"class id\"] == i][\"labels\"].values[0] for i in labels}","metadata":{"execution":{"iopub.status.busy":"2023-01-18T14:04:36.494984Z","iopub.execute_input":"2023-01-18T14:04:36.495763Z","iopub.status.idle":"2023-01-18T14:04:36.922346Z","shell.execute_reply.started":"2023-01-18T14:04:36.495724Z","shell.execute_reply":"2023-01-18T14:04:36.921376Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"paths_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-18T14:04:36.925039Z","iopub.execute_input":"2023-01-18T14:04:36.925610Z","iopub.status.idle":"2023-01-18T14:04:36.944752Z","shell.execute_reply.started":"2023-01-18T14:04:36.925570Z","shell.execute_reply":"2023-01-18T14:04:36.943733Z"},"trusted":true},"execution_count":6,"outputs":[{"execution_count":6,"output_type":"execute_result","data":{"text/plain":" class id filepaths labels \\\n0 0 train/ABBOTTS BABBLER/001.jpg ABBOTTS BABBLER \n1 0 train/ABBOTTS BABBLER/002.jpg ABBOTTS BABBLER \n2 0 train/ABBOTTS BABBLER/003.jpg ABBOTTS BABBLER \n3 0 train/ABBOTTS BABBLER/004.jpg ABBOTTS BABBLER \n4 0 train/ABBOTTS BABBLER/005.jpg ABBOTTS BABBLER \n\n scientific label data set \n0 Malacocincla abbotti train \n1 Malacocincla abbotti train \n2 Malacocincla abbotti train \n3 Malacocincla abbotti train \n4 Malacocincla abbotti train ","text/html":"
\n | class id | \nfilepaths | \nlabels | \nscientific label | \ndata set | \n
---|---|---|---|---|---|
0 | \n0 | \ntrain/ABBOTTS BABBLER/001.jpg | \nABBOTTS BABBLER | \nMalacocincla abbotti | \ntrain | \n
1 | \n0 | \ntrain/ABBOTTS BABBLER/002.jpg | \nABBOTTS BABBLER | \nMalacocincla abbotti | \ntrain | \n
2 | \n0 | \ntrain/ABBOTTS BABBLER/003.jpg | \nABBOTTS BABBLER | \nMalacocincla abbotti | \ntrain | \n
3 | \n0 | \ntrain/ABBOTTS BABBLER/004.jpg | \nABBOTTS BABBLER | \nMalacocincla abbotti | \ntrain | \n
4 | \n0 | \ntrain/ABBOTTS BABBLER/005.jpg | \nABBOTTS BABBLER | \nMalacocincla abbotti | \ntrain | \n