{"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)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor 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-01-05T05:48:48.264169Z","iopub.execute_input":"2022-01-05T05:48:48.264570Z","iopub.status.idle":"2022-01-05T05:48:57.116959Z","shell.execute_reply.started":"2022-01-05T05:48:48.264453Z","shell.execute_reply":"2022-01-05T05:48:57.116229Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport random\nimport tqdm\nimport cv2\nimport os\nimport copy\nimport torchvision\nimport PIL\nimport matplotlib.pyplot as plt\n\nsubmit = pd.read_csv('../input/2022-rcv-winter-urp/sample-submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:48:57.119854Z","iopub.execute_input":"2022-01-05T05:48:57.120056Z","iopub.status.idle":"2022-01-05T05:48:58.802868Z","shell.execute_reply.started":"2022-01-05T05:48:57.120028Z","shell.execute_reply":"2022-01-05T05:48:58.802107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed=42):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n    \nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:49:02.169724Z","iopub.execute_input":"2022-01-05T05:49:02.170381Z","iopub.status.idle":"2022-01-05T05:49:02.178640Z","shell.execute_reply.started":"2022-01-05T05:49:02.170344Z","shell.execute_reply":"2022-01-05T05:49:02.177745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reverse_map(x):\n    reverse_label_map = {\n    0:\"buildings\",\n    1:\"forest\",\n    2:\"glacier\",\n    3:\"mountain\",\n    4:\"sea\",\n    5:\"street\"}\n    return reverse_label_map[x]","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:49:02.720113Z","iopub.execute_input":"2022-01-05T05:49:02.720359Z","iopub.status.idle":"2022-01-05T05:49:02.725362Z","shell.execute_reply.started":"2022-01-05T05:49:02.720332Z","shell.execute_reply":"2022-01-05T05:49:02.724678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed_everything()\n\nclass Dataset(torch.utils.data.Dataset):\n    def __init__(self,split,path,hash_dict,transform):\n        super(Dataset,self).__init__()\n        self.split = split\n        self.root_path = path\n        self.transform = transform\n        self.video_list = list()\n        self.mapping_label_to_int = hash_dict\n        \n        if split == 'train':\n            self.labels = list()\n            for cls in os.listdir(path):\n                self.video_list.extend(os.listdir(os.path.join(path,cls)))\n                self.labels.extend(np.full((len(os.listdir(os.path.join(path,cls)))),cls))\n \n        elif split == 'test':\n            self.video_list = os.listdir(path)\n            self.video_list.sort()\n            \n    def __len__(self):\n        return len(self.video_list)\n    \n    def __getitem__(self,index):\n        if self.split == 'train':\n            img = PIL.Image.open(os.path.join(self.root_path,str(self.labels[index]),self.video_list[index]))\n            img = img.convert('RGB')\n            img = self.transform(img)\n            return img,self.mapping_label_to_int[self.labels[index]]\n        \n        elif self.split == 'test':\n            img = PIL.Image.open(os.path.join(self.root_path,self.video_list[index]))\n            img = img.convert('RGB')\n            img = self.transform(img)\n            return img","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:49:11.261100Z","iopub.execute_input":"2022-01-05T05:49:11.261363Z","iopub.status.idle":"2022-01-05T05:49:11.275572Z","shell.execute_reply.started":"2022-01-05T05:49:11.261335Z","shell.execute_reply":"2022-01-05T05:49:11.274719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\n\nlabel_map = {\n    \"buildings\":0,\n    \"forest\":1,\n    \"glacier\":2,\n    \"mountain\":3,\n    \"sea\":4,\n    \"street\":5\n}\n\nseed_everything()\n\ntransform = transforms.Compose([\n        transforms.ToTensor(),\n        transforms.Resize((224,224)),\n        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])\n\n\ntrain_dataset = Dataset(path='../input/2022-rcv-winter-urp/TRAIN',\n                        hash_dict = label_map,\n                        split = 'train',\n                        transform = transform,\n                        )\n\ntrain_loader = torch.utils.data.DataLoader(dataset=train_dataset,\n                                           batch_size=64,\n                                           shuffle=True,\n                                           num_workers=4,\n                                           drop_last = True\n                                          )\n\ntest_dataset = Dataset(path='../input/2022-rcv-winter-urp/TEST',\n                       hash_dict = label_map,\n                       split = 'test',\n                       transform = transform\n                      )\n\n\ntest_loader = torch.utils.data.DataLoader(dataset=test_dataset,\n                                          batch_size=64,\n                                          shuffle=False,\n                                          num_workers=0\n                                         )","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:49:43.146317Z","iopub.execute_input":"2022-01-05T05:49:43.146992Z","iopub.status.idle":"2022-01-05T05:49:43.185607Z","shell.execute_reply.started":"2022-01-05T05:49:43.146955Z","shell.execute_reply":"2022-01-05T05:49:43.184912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class VGG16(torch.nn.Module):\n    def __init__(self,pretrained,n_classes):\n        super(VGG16,self).__init__()\n        self.pretrained = pretrained\n        \n        self.conv1_1 = torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv1_2 = torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        \n        self.conv2_1 = torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv2_2 = torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        \n        self.conv3_1 = torch.nn.Conv2d(in_channels=128, out_channels=256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv3_2 = torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv3_3 = torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        \n        self.conv4_1 = torch.nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv4_2 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv4_3 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        \n        self.conv5_1 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv5_2 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        self.conv5_3 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        \n        self.activation = torch.nn.ReLU()\n        self.dropout = torch.nn.Dropout(p=0.5)\n        self.maxpool = torch.nn.MaxPool2d(kernel_size=2,stride=2)\n        \n        self.fc1 = torch.nn.Linear(7*7*512,4096,bias=True)\n        self.fc2 = torch.nn.Linear(4096,4096,bias=True)\n        self.fc3 = torch.nn.Linear(4096,n_classes,bias=True)\n        \n        self.weight_init()\n        \n        \n    def weight_init(self):\n        if self.pretrained==True:\n            state_dict = self.state_dict()\n            param_names = list(state_dict.keys())\n            \n            pretrained_state_dict = torchvision.models.vgg16(pretrained=True).state_dict()\n            pretrained_param_names = list(pretrained_state_dict.keys())\n         \n            for i, param in enumerate(param_names[:-2]):  \n                state_dict[param] = pretrained_state_dict[pretrained_param_names[i]]\n                \n            self.load_state_dict(state_dict)\n            \n            print(\"VGG16 Pretrained Weight Init Finish!!\")\n                \n    def forward(self,x):\n                \n        out = self.activation(self.conv1_1(x))\n        out = self.activation(self.conv1_2(out))\n        out = self.maxpool(out)\n    \n        out = self.activation(self.conv2_1(out))\n        out = self.activation(self.conv2_2(out))\n        out = self.maxpool(out)\n    \n        out = self.activation(self.conv3_1(out))\n        out = self.activation(self.conv3_2(out))\n        out = self.activation(self.conv3_3(out))\n        out = self.maxpool(out)\n        \n        out = self.activation(self.conv4_1(out))\n        out = self.activation(self.conv4_2(out))\n        out = self.activation(self.conv4_3(out))\n        out = self.maxpool(out)\n        \n        out = self.activation(self.conv5_1(out))\n        out = self.activation(self.conv5_2(out))\n        out = self.activation(self.conv5_3(out))\n        out = self.maxpool(out)\n        \n        out = out.view(-1,out.shape[1]*out.shape[2]*out.shape[3])\n        \n        out = self.dropout(self.activation(self.fc1(out)))\n        out = self.dropout(self.activation(self.fc2(out)))\n        out = self.fc3(out)\n        \n        return out","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:49:44.989440Z","iopub.execute_input":"2022-01-05T05:49:44.989929Z","iopub.status.idle":"2022-01-05T05:49:45.017592Z","shell.execute_reply.started":"2022-01-05T05:49:44.989894Z","shell.execute_reply":"2022-01-05T05:49:45.016724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if __name__ == '__main__':\n    seed_everything()\n\n    model = VGG16(pretrained=True,n_classes=6)\n    model.train()\n    model = model.cuda()\n\n    Loss = torch.nn.CrossEntropyLoss()\n    optimizer = torch.optim.SGD(model.parameters(),lr=0.001,momentum=0.9)\n\n    Total_Epoch = 3\n    \n    for epoch in range(Total_Epoch):\n        epoch_loss = 0\n        epoch_accuracy = 0\n        for X,Y in tqdm.tqdm(train_loader):\n            X = torch.FloatTensor(X).cuda()\n            Y = torch.LongTensor(Y).cuda()\n\n            output = model(X)\n            Cost = Loss(output,Y)\n\n            optimizer.zero_grad()\n            Cost.backward()\n            optimizer.step()\n\n            hypothesis = torch.nn.functional.softmax(output,dim=1)\n            predict = torch.argmax(hypothesis,dim=1)\n            correct_predict = predict == Y\n\n            epoch_accuracy = epoch_accuracy + correct_predict.sum()\n            epoch_loss = epoch_loss + Cost.item()\n\n        if epoch % 1 == 0:\n            print(\" Epoch : {:4d} , Cost : {:.2f} , Accuracy : {:.4f}\".format(epoch,epoch_loss,epoch_accuracy/len(train_loader.dataset)))\n","metadata":{"execution":{"iopub.status.busy":"2022-01-05T05:49:51.484323Z","iopub.execute_input":"2022-01-05T05:49:51.484871Z","iopub.status.idle":"2022-01-05T05:55:21.320229Z","shell.execute_reply.started":"2022-01-05T05:49:51.484837Z","shell.execute_reply":"2022-01-05T05:55:21.319378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed_everything()\n\ntotal_prediction = list()\nmodel.eval()\nwith torch.no_grad():\n    for X in tqdm.tqdm(test_loader):\n        X = torch.FloatTensor(X).cuda()\n        output = model(X)\n        hypothesis = torch.nn.functional.softmax(output,dim=1)\n        predict = torch.argmax(hypothesis,dim=1)\n        total_prediction.extend(predict.cpu().numpy())\n    submit['label'] = list(map(reverse_map, total_prediction))\n    submit.to_csv('baseline.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:05:07.514758Z","iopub.execute_input":"2022-01-05T06:05:07.515159Z","iopub.status.idle":"2022-01-05T06:05:34.543152Z","shell.execute_reply.started":"2022-01-05T06:05:07.515123Z","shell.execute_reply":"2022-01-05T06:05:34.542478Z"},"trusted":true},"execution_count":null,"outputs":[]}]}