{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom torch.utils.data import DataLoader\nimport torch\nfrom torch.utils.data import Dataset\nfrom torchvision.transforms import ToTensor\nimport matplotlib.pyplot as plt\nfrom torchvision.io import read_image\nfrom torchvision import datasets, models, transforms\nimport torch.nn as nn\nimport os\nimport json\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-12T08:02:06.791032Z","iopub.execute_input":"2022-10-12T08:02:06.791397Z","iopub.status.idle":"2022-10-12T08:02:08.953033Z","shell.execute_reply.started":"2022-10-12T08:02:06.79135Z","shell.execute_reply":"2022-10-12T08:02:08.952055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_dir = '/kaggle/input/herbarium-2022-fgvc9/'\n\ntrain_images_dir = os.path.join(path_dir, 'train_images/')\ntest_images_dir = os.path.join(path_dir, 'test_images/')\n\ntrain_metadata_path = os.path.join(path_dir, 'train_metadata.json')\nwith open(train_metadata_path) as jfile:\n    train_metadata = json.load(jfile)\ntrain_metadata.keys()\n#test_metadata_path = os.path.join(dataset_dir, 'test_metadata.json')","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:08.95542Z","iopub.execute_input":"2022-10-12T08:02:08.956018Z","iopub.status.idle":"2022-10-12T08:02:21.031763Z","shell.execute_reply.started":"2022-10-12T08:02:08.95598Z","shell.execute_reply":"2022-10-12T08:02:21.030669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_metadata['annotations'][0])\nprint(train_metadata['images'][0])\nprint(train_metadata['categories'][0])","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:21.033635Z","iopub.execute_input":"2022-10-12T08:02:21.034072Z","iopub.status.idle":"2022-10-12T08:02:21.040256Z","shell.execute_reply.started":"2022-10-12T08:02:21.034035Z","shell.execute_reply":"2022-10-12T08:02:21.03909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id_to_filename = {item['image_id']:item['file_name'] for item in train_metadata[\"images\"]}\n","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:21.043319Z","iopub.execute_input":"2022-10-12T08:02:21.043739Z","iopub.status.idle":"2022-10-12T08:02:21.383259Z","shell.execute_reply.started":"2022-10-12T08:02:21.043688Z","shell.execute_reply":"2022-10-12T08:02:21.381901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_Train = [\n    os.path.join(train_images_dir, img_id_to_filename[item_dict['image_id']]) for item_dict in train_metadata['annotations']]\nX_Train[0]\n","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:21.386264Z","iopub.execute_input":"2022-10-12T08:02:21.387285Z","iopub.status.idle":"2022-10-12T08:02:22.891555Z","shell.execute_reply.started":"2022-10-12T08:02:21.387241Z","shell.execute_reply":"2022-10-12T08:02:22.890451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_Train = [item_dict['category_id'] for item_dict in train_metadata['annotations']]\nY_Train[100]","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:22.892864Z","iopub.execute_input":"2022-10-12T08:02:22.893646Z","iopub.status.idle":"2022-10-12T08:02:22.965843Z","shell.execute_reply.started":"2022-10-12T08:02:22.893606Z","shell.execute_reply":"2022-10-12T08:02:22.964828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IM_SIZE = 224\n\nTransform = transforms.Compose(\n    [#transforms.ToTensor(),\n    transforms.Resize((IM_SIZE, IM_SIZE)),\n    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:22.967549Z","iopub.execute_input":"2022-10-12T08:02:22.967943Z","iopub.status.idle":"2022-10-12T08:02:22.973938Z","shell.execute_reply.started":"2022-10-12T08:02:22.967907Z","shell.execute_reply":"2022-10-12T08:02:22.972758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HerbariumDataset(Dataset):\n    def __init__(self, img_paths, img_labels, transform=None, target_transform=None):\n        self.img_labels = img_labels\n        self.img_paths = img_paths\n        self.transform = transform\n        self.target_transform = target_transform\n\n    def __len__(self):\n        return len(self.img_labels)\n\n    def __getitem__(self, idx):\n        img_path = self.img_paths[idx]\n        image = read_image(img_path)\n        image = image.to(dtype = torch.float32)\n        label = self.img_labels[idx]\n        if self.transform:\n            image = self.transform(image)\n        if self.target_transform:\n            label = self.target_transform(label)\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:22.975161Z","iopub.execute_input":"2022-10-12T08:02:22.976097Z","iopub.status.idle":"2022-10-12T08:02:23.014187Z","shell.execute_reply.started":"2022-10-12T08:02:22.976062Z","shell.execute_reply":"2022-10-12T08:02:23.013268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data = HerbariumDataset(X_Train, Y_Train, Transform)\ntraining_data","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.015533Z","iopub.execute_input":"2022-10-12T08:02:23.015965Z","iopub.status.idle":"2022-10-12T08:02:23.025875Z","shell.execute_reply.started":"2022-10-12T08:02:23.015929Z","shell.execute_reply":"2022-10-12T08:02:23.024748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)\ntrain_dataloader","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.02982Z","iopub.execute_input":"2022-10-12T08:02:23.030156Z","iopub.status.idle":"2022-10-12T08:02:23.039583Z","shell.execute_reply.started":"2022-10-12T08:02:23.030128Z","shell.execute_reply":"2022-10-12T08:02:23.038687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage = read_image(X_Train[5])\nplt.imshow(image.permute(1, 2, 0))","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.041054Z","iopub.execute_input":"2022-10-12T08:02:23.041431Z","iopub.status.idle":"2022-10-12T08:02:23.416974Z","shell.execute_reply.started":"2022-10-12T08:02:23.041393Z","shell.execute_reply":"2022-10-12T08:02:23.416012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\ndevice\n","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.417922Z","iopub.execute_input":"2022-10-12T08:02:23.418238Z","iopub.status.idle":"2022-10-12T08:02:23.488574Z","shell.execute_reply.started":"2022-10-12T08:02:23.418196Z","shell.execute_reply":"2022-10-12T08:02:23.487538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_classes = len(set(Y_Train))\nn_classes","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.490357Z","iopub.execute_input":"2022-10-12T08:02:23.491102Z","iopub.status.idle":"2022-10-12T08:02:23.522201Z","shell.execute_reply.started":"2022-10-12T08:02:23.491062Z","shell.execute_reply":"2022-10-12T08:02:23.521354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(set(Y_Train))[-1]","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.52432Z","iopub.execute_input":"2022-10-12T08:02:23.525017Z","iopub.status.idle":"2022-10-12T08:02:23.556292Z","shell.execute_reply.started":"2022-10-12T08:02:23.52498Z","shell.execute_reply":"2022-10-12T08:02:23.555327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet50(pretrained=True).to(device)","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:23.558407Z","iopub.execute_input":"2022-10-12T08:02:23.559114Z","iopub.status.idle":"2022-10-12T08:02:29.591421Z","shell.execute_reply.started":"2022-10-12T08:02:23.559079Z","shell.execute_reply":"2022-10-12T08:02:29.590415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor param in model.parameters():\n    param.requires_grad = False   \n    \nmodel.fc = nn.Sequential(\n               nn.Linear(2048, 15504+1).to(device)#,\n               #nn.ReLU(inplace=True),\n               #nn.Linear(15501*2, n_classes)).to(device\n                )","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:29.592927Z","iopub.execute_input":"2022-10-12T08:02:29.59328Z","iopub.status.idle":"2022-10-12T08:02:29.923072Z","shell.execute_reply.started":"2022-10-12T08:02:29.593243Z","shell.execute_reply":"2022-10-12T08:02:29.92208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.fc.parameters())","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:02:29.924694Z","iopub.execute_input":"2022-10-12T08:02:29.92508Z","iopub.status.idle":"2022-10-12T08:02:29.931259Z","shell.execute_reply.started":"2022-10-12T08:02:29.925042Z","shell.execute_reply":"2022-10-12T08:02:29.930079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model, criterion, optimizer, num_epochs=3):\n    for epoch in range(num_epochs):\n        print('Epoch {}/{}'.format(epoch+1, num_epochs))\n        print('-' * 10)\n        \n        for phase in ['train', 'validation']:\n            i=0\n            if phase == 'train':\n                model.train()\n            else:\n                model.eval()\n\n            running_loss = 0.0\n            running_corrects = 0\n            \n            for inputs, labels in train_dataloader:\n                print(i)\n                i=i+1\n                inputs = inputs.to(device)\n                labels = labels.to(device)\n\n                outputs = model(inputs)\n                loss = criterion(outputs, labels)\n\n                if phase == 'train':\n                    optimizer.zero_grad()\n                    loss.backward()\n                    optimizer.step()\n\n                _, preds = torch.max(outputs, 1)\n                running_loss += loss.item() * inputs.size(0)\n                running_corrects += torch.sum(preds == labels.data)\n\n            epoch_loss = running_loss / len(image_datasets[phase])\n            epoch_acc = running_corrects.double() / len(image_datasets[phase])\n\n            print('{} loss: {:.4f}, acc: {:.4f}'.format(phase,\n                                                        epoch_loss,\n                                                        epoch_acc))\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:10:37.231557Z","iopub.execute_input":"2022-10-12T08:10:37.231911Z","iopub.status.idle":"2022-10-12T08:10:37.241335Z","shell.execute_reply.started":"2022-10-12T08:10:37.231881Z","shell.execute_reply":"2022-10-12T08:10:37.240347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_trained = train_model(model, criterion, optimizer, num_epochs=3)\nmodel_trained","metadata":{"execution":{"iopub.status.busy":"2022-10-12T08:10:40.509435Z","iopub.execute_input":"2022-10-12T08:10:40.510348Z"},"trusted":true},"execution_count":null,"outputs":[]}]}