{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":74752,"databundleVersionId":8237627,"sourceType":"competition"}],"dockerImageVersionId":30683,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Starter notebook for bird genus classification\n\nDescribes the dataset, shows an example torch dataloader and creates a random submission in the correct format.","metadata":{}},{"cell_type":"code","source":"# Imports\nimport os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport json\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:25.833358Z","iopub.execute_input":"2024-04-17T07:21:25.833827Z","iopub.status.idle":"2024-04-17T07:21:35.682625Z","shell.execute_reply.started":"2024-04-17T07:21:25.833752Z","shell.execute_reply":"2024-04-17T07:21:35.681846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Beg for GPU\n\nif torch.cuda.is_available():\n    print(\"GPU is available\")\n    device = torch.device(\"cuda\")\nelse:\n    print(\"GPU is not available, using CPU instead\")\n    device = torch.device(\"cpu\")","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:35.684200Z","iopub.execute_input":"2024-04-17T07:21:35.684825Z","iopub.status.idle":"2024-04-17T07:21:35.740711Z","shell.execute_reply.started":"2024-04-17T07:21:35.684774Z","shell.execute_reply":"2024-04-17T07:21:35.739648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Describe dataset\n\nimages = pd.read_csv('/kaggle/input/bird-genus-multi-class-image-classification/train.csv', header=0, names=['id', 'filename', 'genus'])\nclasses = images['genus'].unique()\n\nprint(f\"Classes {classes}\")\nprint(f\"Number of images {len(images)}\")\n\nplt.figure(figsize=(10, 10))\nplt.bar(classes, [images['genus'][images['genus'] == cls].count() for cls in classes])\nplt.xlabel('Class')\nplt.ylabel('Number of images')\nplt.title('Number of images per class')\nplt.xticks(rotation=90)\nplt.show()\n\nimages.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:35.742014Z","iopub.execute_input":"2024-04-17T07:21:35.742336Z","iopub.status.idle":"2024-04-17T07:21:36.291546Z","shell.execute_reply.started":"2024-04-17T07:21:35.742311Z","shell.execute_reply":"2024-04-17T07:21:36.290685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split to train and validation sets\n\ntrain, val = train_test_split(images, test_size=0.2)\ntrain.to_csv('train_labels.csv', index=False, index_label='id')\n!head train_labels.csv","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:36.293776Z","iopub.execute_input":"2024-04-17T07:21:36.294070Z","iopub.status.idle":"2024-04-17T07:21:37.271111Z","shell.execute_reply.started":"2024-04-17T07:21:36.294045Z","shell.execute_reply":"2024-04-17T07:21:37.269988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create torch dataset\n\nclass BirdsDataset(Dataset):\n  def __init__(self, images_dir, labels_file, img_width=300, img_height=300, augment=False):\n    super(BirdsDataset, self).__init__()\n    self.img_width = img_width\n    self.img_height = img_height\n    self.image_dir = images_dir\n\n    # dataframe of image file name and class label\n    self.df = pd.read_csv(labels_file, header=0, names=['id', 'filename', 'genus'])\n    # read all images to memory\n    self.images = [Image.open(os.path.join(self.image_dir, filename)) for filename in self.df['filename']]\n    # list of unique classes\n    self.classes = self.df['genus'].unique()\n    # number of classes\n    self.K = len(classes)\n\n    # example augmentations\n    if augment:\n        self.transform = transforms.Compose([\n            transforms.RandomHorizontalFlip(p=0.5),\n            transforms.ToTensor(),\n        ])\n    else:\n        self.transform = transforms.Compose([\n            transforms.ToTensor()\n        ])\n\n  def __len__(self):\n    return len(self.df)\n\n  def __getitem__(self, idx):\n    transformed_image = self.transform(self.images[idx])\n    label = self.df.iloc[idx]['genus']\n    return transformed_image, label","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:37.272640Z","iopub.execute_input":"2024-04-17T07:21:37.272971Z","iopub.status.idle":"2024-04-17T07:21:37.284496Z","shell.execute_reply.started":"2024-04-17T07:21:37.272942Z","shell.execute_reply":"2024-04-17T07:21:37.283568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dataset and loader\ntrain_batch_size = 16\n\ntrain_dataset = BirdsDataset(images_dir=\"/kaggle/input/bird-genus-multi-class-image-classification/images/train\", labels_file='train_labels.csv', augment=True)\ntrain_loader = DataLoader(train_dataset, batch_size=train_batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:37.285501Z","iopub.execute_input":"2024-04-17T07:21:37.285788Z","iopub.status.idle":"2024-04-17T07:21:52.988137Z","shell.execute_reply.started":"2024-04-17T07:21:37.285764Z","shell.execute_reply":"2024-04-17T07:21:52.987107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show example training batch\n\nplt.rcParams['figure.figsize'] = (10.0, 10.0)\nfor X_batch, y_batch in train_loader:\n    for i in range(0, train_batch_size):\n        plt.subplot(4, 4, 1 + i)\n        plt.imshow(X_batch[i].numpy().transpose(1, 2, 0))\n        plt.axis('off')\n        plt.title(y_batch[i])\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:52.989591Z","iopub.execute_input":"2024-04-17T07:21:52.990037Z","iopub.status.idle":"2024-04-17T07:21:54.861611Z","shell.execute_reply.started":"2024-04-17T07:21:52.989999Z","shell.execute_reply":"2024-04-17T07:21:54.860575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict random class\nimport random\n\ntest_images = pd.read_csv('/kaggle/input/bird-genus-multi-class-image-classification/test.csv', header=0, names=['id'], usecols=['id'])\npredictions = [random.choice(classes) for _ in range(len(test_images))]","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:54.862794Z","iopub.execute_input":"2024-04-17T07:21:54.863188Z","iopub.status.idle":"2024-04-17T07:21:54.878825Z","shell.execute_reply.started":"2024-04-17T07:21:54.863162Z","shell.execute_reply":"2024-04-17T07:21:54.877847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create sample submission file\ntest_images['genus'] = predictions\ntest_images.to_csv('sample_submission.csv', index=False, index_label='id')\ntest_images.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:21:54.879853Z","iopub.execute_input":"2024-04-17T07:21:54.880175Z","iopub.status.idle":"2024-04-17T07:21:54.892751Z","shell.execute_reply.started":"2024-04-17T07:21:54.880151Z","shell.execute_reply":"2024-04-17T07:21:54.891809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submit!\n#!kaggle competitions submit -c bird-genus-multi-class-image-classification -f sample_submission.csv -m \"Sample submission\"","metadata":{},"execution_count":null,"outputs":[]}]}