{"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":"!pip install efficientnet_pytorch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-29T02:41:13.817040Z","iopub.execute_input":"2021-09-29T02:41:13.818169Z","iopub.status.idle":"2021-09-29T02:41:24.091006Z","shell.execute_reply.started":"2021-09-29T02:41:13.818059Z","shell.execute_reply":"2021-09-29T02:41:24.090222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport torch\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset\nimport torch.nn as nn\nfrom torch.utils.tensorboard import SummaryWriter\nimport cv2\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport torchvision\nfrom torch.utils.data import Dataset, DataLoader, ConcatDataset\nfrom torchvision import models, transforms\nfrom efficientnet_pytorch import EfficientNet\nimport copy\nimport tqdm\nimport PIL\nfrom PIL import Image\nimport zipfile\n\nfrom sklearn.metrics import confusion_matrix\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))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-29T02:41:24.094723Z","iopub.execute_input":"2021-09-29T02:41:24.094936Z","iopub.status.idle":"2021-09-29T02:41:29.595673Z","shell.execute_reply.started":"2021-09-29T02:41:24.094909Z","shell.execute_reply":"2021-09-29T02:41:29.594972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_path = \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\"\ntest_data_path = \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\"\n\nwith zipfile.ZipFile(train_data_path, 'r') as zip_ref:\n    zip_ref.extractall(\".\")\n    \nwith zipfile.ZipFile(test_data_path, 'r') as zip_ref:\n    zip_ref.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:29.597030Z","iopub.execute_input":"2021-09-29T02:41:29.597278Z","iopub.status.idle":"2021-09-29T02:41:47.478584Z","shell.execute_reply.started":"2021-09-29T02:41:29.597246Z","shell.execute_reply":"2021-09-29T02:41:47.477871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\ntrain_dir = \"./train\"\ntest_dir = \"./test\"\ntrain_list = glob.glob(os.path.join(train_dir,'*.jpg'))\ntest_list = glob.glob(os.path.join(test_dir, '*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.479876Z","iopub.execute_input":"2021-09-29T02:41:47.480155Z","iopub.status.idle":"2021-09-29T02:41:47.602037Z","shell.execute_reply.started":"2021-09-29T02:41:47.480120Z","shell.execute_reply":"2021-09-29T02:41:47.601393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_list, val_list = train_test_split(train_list, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.604365Z","iopub.execute_input":"2021-09-29T02:41:47.604617Z","iopub.status.idle":"2021-09-29T02:41:47.634599Z","shell.execute_reply.started":"2021-09-29T02:41:47.604585Z","shell.execute_reply":"2021-09-29T02:41:47.633935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\ntrain_transforms =  transforms.Compose([\n        transforms.Resize((448, 448)),\n        transforms.RandomResizedCrop(224),\n        transforms.RandomHorizontalFlip(),\n        transforms.ToTensor(),\n        transforms.Normalize(mean, std),\n    ])\n\nval_transforms = transforms.Compose([\n        transforms.Resize((224, 224)),\n        transforms.RandomResizedCrop(224),\n        transforms.RandomHorizontalFlip(),\n        transforms.ToTensor(),\n        transforms.Normalize(mean, std),\n    ])\n\n\ntest_transforms = transforms.Compose([   \n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean, std),\n    ])","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.635829Z","iopub.execute_input":"2021-09-29T02:41:47.636262Z","iopub.status.idle":"2021-09-29T02:41:47.647216Z","shell.execute_reply.started":"2021-09-29T02:41:47.636229Z","shell.execute_reply":"2021-09-29T02:41:47.646592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataset(torch.utils.data.Dataset):\n    def __init__(self,file_list,transform=None):\n        self.file_list = file_list\n        self.transform = transform\n        \n    def __len__(self):\n        self.filelength = len(self.file_list)\n        return self.filelength\n    \n    #load an one of images\n    def __getitem__(self,idx):\n        img_path = self.file_list[idx]\n        img = Image.open(img_path)\n        img_transformed = self.transform(img)\n        \n        label = img_path.split('/')[-1].split('.')[0]\n        if label == 'dog':\n            label=1\n        elif label == 'cat':\n            label=0\n            \n        return img_transformed,label","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.648529Z","iopub.execute_input":"2021-09-29T02:41:47.648781Z","iopub.status.idle":"2021-09-29T02:41:47.659793Z","shell.execute_reply.started":"2021-09-29T02:41:47.648749Z","shell.execute_reply":"2021-09-29T02:41:47.659095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = dataset(train_list, transform=train_transforms)\ntest_data = dataset(test_list, transform=test_transforms)\nval_data = dataset(val_list, transform=test_transforms)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.661090Z","iopub.execute_input":"2021-09-29T02:41:47.661333Z","iopub.status.idle":"2021-09-29T02:41:47.668552Z","shell.execute_reply.started":"2021-09-29T02:41:47.661301Z","shell.execute_reply":"2021-09-29T02:41:47.667808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(dataset = train_data, batch_size=32, shuffle=True )\ntest_loader = torch.utils.data.DataLoader(dataset = test_data, batch_size=32, shuffle=True)\nval_loader = torch.utils.data.DataLoader(dataset = val_data, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.670769Z","iopub.execute_input":"2021-09-29T02:41:47.671420Z","iopub.status.idle":"2021-09-29T02:41:47.677509Z","shell.execute_reply.started":"2021-09-29T02:41:47.671326Z","shell.execute_reply":"2021-09-29T02:41:47.676639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# samples, labels = iter(train_loader).next()\n# fig = plt.figure(figsize=(24, 16))\n# fig.tight_layout()\n# ad = {0:'cat', 1:'dog'}\n# for num, sample in enumerate(samples[:24]):\n#     plt.subplot(4,6,num+1)\n#     plt.title(ad[labels[num].item()])\n#     plt.axis('off')\n#     sample = sample.cpu().numpy()\n#     plt.imshow(np.transpose(sample, (1,2,0)))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.679906Z","iopub.execute_input":"2021-09-29T02:41:47.680629Z","iopub.status.idle":"2021-09-29T02:41:47.686282Z","shell.execute_reply.started":"2021-09-29T02:41:47.680567Z","shell.execute_reply":"2021-09-29T02:41:47.685621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n\ntorch.manual_seed(42)\nif device =='cuda':\n    torch.cuda.manual_seed_all(42)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.687502Z","iopub.execute_input":"2021-09-29T02:41:47.687870Z","iopub.status.idle":"2021-09-29T02:41:47.751870Z","shell.execute_reply.started":"2021-09-29T02:41:47.687837Z","shell.execute_reply":"2021-09-29T02:41:47.751163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **CNN**","metadata":{}},{"cell_type":"code","source":"# class Cnn(nn.Module):\n#     def __init__(self):\n#         super(Cnn,self).__init__()\n        \n#         self.layer1 = nn.Sequential(\n#             nn.Conv2d(3,16,kernel_size=3, padding=0,stride=2),\n#             nn.BatchNorm2d(16),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2)\n#         )\n        \n#         self.layer2 = nn.Sequential(\n#             nn.Conv2d(16,32, kernel_size=3, padding=0, stride=2),\n#             nn.BatchNorm2d(32),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2)\n#             )\n        \n#         self.layer3 = nn.Sequential(\n#             nn.Conv2d(32,64, kernel_size=3, padding=0, stride=2),\n#             nn.BatchNorm2d(64),\n#             nn.ReLU(),\n#             nn.MaxPool2d(2)\n#         )\n        \n        \n#         self.fc1 = nn.Linear(3*3*64,10)\n#         self.dropout = nn.Dropout(0.5)\n#         self.fc2 = nn.Linear(10,2)\n#         self.relu = nn.ReLU()\n        \n        \n#     def forward(self,x):\n#         out = self.layer1(x)\n#         out = self.layer2(out)\n#         out = self.layer3(out)\n#         out = out.view(out.size(0),-1)\n#         out = self.relu(self.fc1(out))\n#         out = self.fc2(out)\n#         return out","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.754825Z","iopub.execute_input":"2021-09-29T02:41:47.755048Z","iopub.status.idle":"2021-09-29T02:41:47.760207Z","shell.execute_reply.started":"2021-09-29T02:41:47.755023Z","shell.execute_reply":"2021-09-29T02:41:47.759495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Cnn().to(device)\n# model.train()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.761356Z","iopub.execute_input":"2021-09-29T02:41:47.762013Z","iopub.status.idle":"2021-09-29T02:41:47.768387Z","shell.execute_reply.started":"2021-09-29T02:41:47.761892Z","shell.execute_reply":"2021-09-29T02:41:47.767692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# optimizer = optim.Adam(params = model.parameters(),lr=0.001)\n# criterion = nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.772999Z","iopub.execute_input":"2021-09-29T02:41:47.773205Z","iopub.status.idle":"2021-09-29T02:41:47.776889Z","shell.execute_reply.started":"2021-09-29T02:41:47.773170Z","shell.execute_reply":"2021-09-29T02:41:47.775930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **VGG16/19**","metadata":{}},{"cell_type":"code","source":"model = models.vgg19(pretrained=True)\n# model","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:41:47.778078Z","iopub.execute_input":"2021-09-29T02:41:47.778422Z","iopub.status.idle":"2021-09-29T02:42:07.114527Z","shell.execute_reply.started":"2021-09-29T02:41:47.778390Z","shell.execute_reply":"2021-09-29T02:42:07.113815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.classifier[6] = nn.Linear(in_features=4096, out_features=1)\n\nupdate_params = []\nparams_names = ['classifier.6.weight', 'classifier.6.bias']\n\nfor name, param in model.named_parameters():\n    if name in params_names:\n        param.requires_grad = True\n        update_params.append(param)\n    else:\n        param.requires_grad = False\n        \nmodel = model.to(device)\nsigmo = nn.Sigmoid()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:09:22.880479Z","iopub.execute_input":"2021-09-29T03:09:22.880771Z","iopub.status.idle":"2021-09-29T03:09:22.891356Z","shell.execute_reply.started":"2021-09-29T03:09:22.880738Z","shell.execute_reply":"2021-09-29T03:09:22.890519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# criterion = nn.CrossEntropyLoss()\n# criterion = nn.BCEWithLogitsLoss(reduction = 'mean').to(device)\ncriterion = nn.BCELoss(reduction = 'mean').to(device)\noptimizer = optim.SGD(params=update_params, lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:09:26.085815Z","iopub.execute_input":"2021-09-29T03:09:26.086504Z","iopub.status.idle":"2021-09-29T03:09:26.091849Z","shell.execute_reply.started":"2021-09-29T03:09:26.086463Z","shell.execute_reply":"2021-09-29T03:09:26.090757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **InceptionV3**","metadata":{}},{"cell_type":"code","source":"# model = models.inception_v3(pretrained=True, aux_logits=False)\n# model","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:42:12.564631Z","iopub.execute_input":"2021-09-29T02:42:12.565139Z","iopub.status.idle":"2021-09-29T02:42:13.043011Z","shell.execute_reply.started":"2021-09-29T02:42:12.565096Z","shell.execute_reply":"2021-09-29T02:42:13.042204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = models.inception_v3(pretrained=True, aux_logits=False)\n# model.fc = nn.Linear(in_features=2048, out_features=2)\n\n# update_params = []\n# params_names = ['fc.weight', 'fc.bias']\n\n# for name, param in model.named_parameters():\n#     if name in params_names:\n#         param.requires_grad = True\n#         update_params.append(param)\n#     else:\n#         param.requires_grad = False\n        \n# model = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:42:13.044764Z","iopub.execute_input":"2021-09-29T02:42:13.045532Z","iopub.status.idle":"2021-09-29T02:42:13.051038Z","shell.execute_reply.started":"2021-09-29T02:42:13.045482Z","shell.execute_reply":"2021-09-29T02:42:13.050270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# criterion = nn.CrossEntropyLoss()\n# optimizer = optim.SGD(params=update_params, lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:42:13.052184Z","iopub.execute_input":"2021-09-29T02:42:13.054423Z","iopub.status.idle":"2021-09-29T02:42:13.060001Z","shell.execute_reply.started":"2021-09-29T02:42:13.054395Z","shell.execute_reply":"2021-09-29T02:42:13.059357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **EffNet**","metadata":{}},{"cell_type":"code","source":"# model = EfficientNet.from_pretrained(\"efficientnet-b5\")\n# model","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:42:13.061259Z","iopub.execute_input":"2021-09-29T02:42:13.062003Z","iopub.status.idle":"2021-09-29T02:42:13.068531Z","shell.execute_reply.started":"2021-09-29T02:42:13.061915Z","shell.execute_reply":"2021-09-29T02:42:13.067806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model._fc = nn.Linear(in_features=2048, out_features=2)\n# update_params = []\n# params_names = ['_fc.weight', '_fc.bias']\n\n# for name, param in model.named_parameters():\n#     if name in params_names:\n#         param.requires_grad = True\n#         update_params.append(param)\n#     else:\n#         param.requires_grad = False\n        \n# model = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:42:13.069652Z","iopub.execute_input":"2021-09-29T02:42:13.070234Z","iopub.status.idle":"2021-09-29T02:42:13.077438Z","shell.execute_reply.started":"2021-09-29T02:42:13.070199Z","shell.execute_reply":"2021-09-29T02:42:13.076566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# criterion = nn.CrossEntropyLoss()\n# optimizer = optim.SGD(params=update_params, lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T02:42:13.080281Z","iopub.execute_input":"2021-09-29T02:42:13.080510Z","iopub.status.idle":"2021-09-29T02:42:13.085931Z","shell.execute_reply.started":"2021-09-29T02:42:13.080479Z","shell.execute_reply":"2021-09-29T02:42:13.085066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 6\n\ntrain_loss_list = []\ntrain_acc_list = []\nval_loss_list = []\nval_acc_list = []\n\nbest_loss = 1e10\nweights_path = \".\"\n\nfor epoch in range(epochs):\n    epoch_loss = 0\n    epoch_accuracy = 0\n#     print(\"#batches \", len(train_loader))\n    for batch_idx, (data, label) in enumerate(train_loader):\n#         if (batch_idx+1) % 100 == 0:\n#             print(batch_idx+1)\n        data = data.to(device)\n        label = label.to(device)\n        \n        output = sigmo(model(data))\n        loss = criterion(output, label.view(-1, 1).float())\n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n#         acc = ((output.argmax(dim=1) == label).float().mean())\n        acc = (((torch.flatten(output) > 0.5).int() == label).float().mean())\n        epoch_accuracy += acc/len(train_loader)\n        epoch_loss += loss/len(train_loader)\n        \n    print('Epoch : {}, train accuracy : {}, train loss : {}'.format(epoch+1, epoch_accuracy,epoch_loss))\n    train_loss_list.append(epoch_loss)\n    train_acc_list.append(epoch_accuracy)\n    \n    with torch.no_grad():\n        epoch_val_accuracy=0\n        epoch_val_loss =0\n        for data, label in val_loader:\n            data = data.to(device)\n            label = label.to(device)\n            \n            val_output = sigmo(model(data))\n            val_loss = criterion(val_output,label.view(-1, 1).float())\n            \n            \n#             acc = ((val_output.argmax(dim=1) == label).float().mean())\n            acc = (((torch.flatten(val_output) > 0.5).int() == label).float().mean())\n            epoch_val_accuracy += acc/ len(val_loader)\n            epoch_val_loss += val_loss/ len(val_loader)\n            \n        print('Epoch : {}, val_accuracy : {}, val_loss : {}'.format(epoch+1, epoch_val_accuracy,epoch_val_loss))\n        val_loss_list.append(epoch_val_loss)\n        val_acc_list.append(epoch_val_accuracy)\n        if epoch_val_loss < best_loss:\n            torch.save(model.state_dict(), os.path.join(weights_path, \"model.pt\"),)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:09:31.552006Z","iopub.execute_input":"2021-09-29T03:09:31.552546Z","iopub.status.idle":"2021-09-29T03:25:59.319187Z","shell.execute_reply.started":"2021-09-29T03:09:31.552509Z","shell.execute_reply":"2021-09-29T03:25:59.318279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.clf()\nx_axis = list(range(epochs))\nplt.plot(x_axis, train_loss_list, label=\"Train\")\nplt.plot(x_axis, val_loss_list, label=\"Validation\")\nplt.xlabel(\"#epoch\")\nplt.ylabel('loss')\nplt.title('Loss')\nplt.legend()\nplt.show()\n\nplt.clf()\nx_axis = list(range(epochs))\nplt.plot(x_axis, train_acc_list, label=\"Train\")\nplt.plot(x_axis, val_acc_list, label=\"Validation\")\nplt.xlabel(\"#epoch\")\nplt.ylabel('Accuracy')\nplt.title('accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:27:05.793508Z","iopub.execute_input":"2021-09-29T03:27:05.793780Z","iopub.status.idle":"2021-09-29T03:27:06.205580Z","shell.execute_reply.started":"2021-09-29T03:27:05.793751Z","shell.execute_reply":"2021-09-29T03:27:06.204917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\ntrue_labels = []\nwrong_answers = []\nepoch_val_loss = 0\nepoch_val_accuracy = 0\nfor data, label in val_loader:\n    data = data.to(device)\n    label = label.to(device)\n\n    val_output = sigmo(model(data))\n    val_output = torch.flatten(val_output)\n    for i in range(len(val_output)):\n        predictions.append((val_output[i] > 0.5).int().item())\n        true_labels.append(label[i].item())\n        if predictions[-1] != true_labels[-1]:\n            wrong_answers.append((data[i], predictions[-1]))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:27:17.257474Z","iopub.execute_input":"2021-09-29T03:27:17.257727Z","iopub.status.idle":"2021-09-29T03:27:52.732869Z","shell.execute_reply.started":"2021-09-29T03:27:17.257700Z","shell.execute_reply":"2021-09-29T03:27:52.732160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UnNormalize(object):\n    def __init__(self, mean, std):\n        self.mean = mean\n        self.std = std\n\n    def __call__(self, tensor):\n        for t, m, s in zip(tensor, self.mean, self.std):\n            t.mul_(s).add_(m)\n            # The normalize code -> t.sub_(m).div_(s)\n        return tensor","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:28:02.780661Z","iopub.execute_input":"2021-09-29T03:28:02.781456Z","iopub.status.idle":"2021-09-29T03:28:02.789277Z","shell.execute_reply.started":"2021-09-29T03:28:02.781408Z","shell.execute_reply":"2021-09-29T03:28:02.788187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(true_labels[:20])\nprint(predictions[:20])\nunorm = UnNormalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))\n\nfig = plt.figure(figsize=(24, 16))\nfig.tight_layout()\nad = {0:'cat', 1:'dog'}\nfor num, (sample, label) in enumerate(wrong_answers[:36]):\n    plt.subplot(6,6,num+1)\n    plt.title(ad[label])\n    plt.axis('off')\n    \n    sample = unorm(sample)\n    sample = sample.detach().cpu().numpy()\n    plt.imshow(np.transpose(sample, (1,2,0)))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:28:05.651252Z","iopub.execute_input":"2021-09-29T03:28:05.652325Z","iopub.status.idle":"2021-09-29T03:28:08.271828Z","shell.execute_reply.started":"2021-09-29T03:28:05.652260Z","shell.execute_reply":"2021-09-29T03:28:08.271205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_matrix = confusion_matrix(true_labels, predictions)\n\nfig, ax = plt.subplots(figsize=(7.5, 7.5))\nax.matshow(conf_matrix, cmap=plt.cm.Blues, alpha=0.3)\nfor i in range(conf_matrix.shape[0]):\n    for j in range(conf_matrix.shape[1]):\n        ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')\n \nplt.xlabel('Predictions', fontsize=18)\nplt.ylabel('Actuals', fontsize=18)\nplt.title('Confusion Matrix', fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:28:16.282783Z","iopub.execute_input":"2021-09-29T03:28:16.283688Z","iopub.status.idle":"2021-09-29T03:28:16.532342Z","shell.execute_reply.started":"2021-09-29T03:28:16.283638Z","shell.execute_reply":"2021-09-29T03:28:16.531252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load(weights_path + \"/model.pt\", map_location=device))\ndog_probs = []\nmodel.eval()\nwith torch.no_grad():\n    for data, fileid in test_loader:\n        data = data.to(device)\n        preds = sigmo(model(data))\n#         preds_list = F.softmax(preds, dim=1)[:, 1].tolist()\n#         preds_list = (torch.flatten(preds) > 0.5).detach().float().cpu() * 0.97 + 0.01\n        preds_list = torch.flatten(preds).detach().cpu().numpy()\n        dog_probs += list(zip(list(fileid), preds_list))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:28:23.708325Z","iopub.execute_input":"2021-09-29T03:28:23.709011Z","iopub.status.idle":"2021-09-29T03:29:53.304046Z","shell.execute_reply.started":"2021-09-29T03:28:23.708960Z","shell.execute_reply":"2021-09-29T03:29:53.303293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_probs.sort(key = lambda x : int(x[0]))\nidx = list(map(lambda x: x[0],dog_probs))\nprob = list(map(lambda x: x[1],dog_probs))\nsubmission = pd.DataFrame({'id':idx,'label':prob})\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:30:52.660803Z","iopub.execute_input":"2021-09-29T03:30:52.661077Z","iopub.status.idle":"2021-09-29T03:30:52.697406Z","shell.execute_reply.started":"2021-09-29T03:30:52.661045Z","shell.execute_reply":"2021-09-29T03:30:52.695722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('last.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T03:31:00.380579Z","iopub.execute_input":"2021-09-29T03:31:00.381266Z","iopub.status.idle":"2021-09-29T03:31:00.427159Z","shell.execute_reply.started":"2021-09-29T03:31:00.381230Z","shell.execute_reply":"2021-09-29T03:31:00.426483Z"},"trusted":true},"execution_count":null,"outputs":[]}]}