diff --git a/evaluate_efficientnet.ipynb b/evaluate_efficientnet.ipynb new file mode 100644 index 0000000..2946e64 --- /dev/null +++ b/evaluate_efficientnet.ipynb @@ -0,0 +1,952 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[31mmenpo 0.8.1 has requirement matplotlib<2.0,>=1.4, but you'll have matplotlib 3.0.2 which is incompatible.\u001b[0m\n", + "\u001b[31mmenpo 0.8.1 has requirement pillow<5.0,>=3.0, but you'll have pillow 5.4.1 which is incompatible.\u001b[0m\n", + "\u001b[31mmenpo 0.8.1 has requirement scipy<1.0,>=0.16, but you'll have scipy 1.2.0 which is incompatible.\u001b[0m\n", + "\u001b[33mYou are using pip version 10.0.1, however version 19.2.2 is available.\n", + "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install -q -r requirements.txt" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", + "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5" + }, + "outputs": [], + "source": [ + "import sys\n", + "import os\n", + "import numpy as np\n", + "import pandas as pd\n", + "\n", + "from PIL import Image\n", + "\n", + "import torch\n", + "import torch.nn as nn\n", + "import torch.utils.data as D\n", + "from torch.optim.lr_scheduler import ExponentialLR\n", + "import torch.nn.functional as F\n", + "from torch.autograd import Variable\n", + "\n", + "from torchvision import transforms\n", + "\n", + "from ignite.engine import Events\n", + "from scripts.ignite import create_supervised_evaluator, create_supervised_trainer\n", + "from ignite.metrics import Loss, Accuracy\n", + "from ignite.contrib.handlers.tqdm_logger import ProgressBar\n", + "from ignite.handlers import EarlyStopping, ModelCheckpoint\n", + "from ignite.contrib.handlers import LinearCyclicalScheduler, CosineAnnealingScheduler\n", + "\n", + "from tqdm import tqdm_notebook\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "from efficientnet_pytorch import EfficientNet\n", + "\n", + "from scripts.evaluate import eval_model \n", + "\n", + "import warnings\n", + "warnings.filterwarnings('ignore')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Define dataset and model" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0", + "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a" + }, + "outputs": [], + "source": [ + "img_dir = '../input/rxrxairgb'\n", + "path_data = '../input/rxrxaicsv'\n", + "device = 'cuda'\n", + "batch_size = 32\n", + "torch.manual_seed(0)\n", + "model_name = 'efficientnet-b0'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "jitter = (0.6, 1.4)\n", + "class ImagesDS(D.Dataset):\n", + " # taken textbook from https://arxiv.org/pdf/1812.01187.pdf\n", + " transform_train = transforms.Compose([\n", + " transforms.RandomResizedCrop(224),\n", + " transforms.ColorJitter(brightness=jitter, contrast=jitter, saturation=jitter, hue=.1),\n", + " transforms.RandomHorizontalFlip(p=0.5),\n", + " # PCA Noise should go here,\n", + " transforms.ToTensor(),\n", + " transforms.Normalize(mean=(123.68, 116.779, 103.939), std=(58.393, 57.12, 57.375))\n", + " ])\n", + " \n", + " transform_validation = transforms.Compose([\n", + " transforms.CenterCrop(224),\n", + " transforms.ToTensor(),\n", + " transforms.Normalize(mean=(123.68, 116.779, 103.939), std=(58.393, 57.12, 57.375))\n", + " ])\n", + "\n", + " def __init__(self, df, img_dir=img_dir, mode='train', validation=False, site=1):\n", + " self.records = df.to_records(index=False)\n", + " self.site = site\n", + " self.mode = mode\n", + " self.img_dir = img_dir\n", + " self.len = df.shape[0]\n", + " self.validation = validation\n", + " \n", + " @staticmethod\n", + " def _load_img_as_tensor(file_name, validation):\n", + " with Image.open(file_name) as img:\n", + " if not validation:\n", + " return ImagesDS.transform_train(img)\n", + " else:\n", + " return ImagesDS.transform_validation(img)\n", + "\n", + " def _get_img_path(self, index, site=1):\n", + " experiment, well, plate = self.records[index].experiment, self.records[index].well, self.records[index].plate\n", + " return f'{self.img_dir}/{self.mode}/{experiment}_{plate}_{well}_s{site}.jpeg'\n", + " \n", + " def __getitem__(self, index):\n", + " img1, img2 = [self._load_img_as_tensor(self._get_img_path(index, site), self.validation) for site in [1,2]]\n", + " if self.mode == 'train':\n", + " return img1, img2, int(self.records[index].sirna)\n", + " else:\n", + " return img1, img2, self.records[index].id_code\n", + "\n", + " def __len__(self):\n", + " return self.len" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# dataframes for training, cross-validation, and testing\n", + "df_test = pd.read_csv(path_data+'/test.csv')\n", + "\n", + "# pytorch test dataset & loader\n", + "ds_test = ImagesDS(df_test, mode='test', validation=True)\n", + "tloader = D.DataLoader(ds_test, batch_size=batch_size, shuffle=False, num_workers=4)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Downloading: \"http://storage.googleapis.com/public-models/efficientnet/efficientnet-b0-355c32eb.pth\" to /root/.cache/torch/checkpoints/efficientnet-b0-355c32eb.pth\n", + "100%|██████████| 21388428/21388428 [00:00<00:00, 48917479.03it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loaded pretrained weights for efficientnet-b0\n" + ] + }, + { + "data": { + "text/plain": [ + "EfficientNetTwoInputs(\n", + " (resnet): EfficientNet(\n", + " (_conv_stem): Conv2dStaticSamePadding(\n", + " 3, 32, kernel_size=(3, 3), stride=(2, 2), bias=False\n", + " (static_padding): ZeroPad2d(padding=(0, 1, 0, 1), value=0.0)\n", + " )\n", + " (_bn0): BatchNorm2d(32, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_blocks): ModuleList(\n", + " (0): MBConvBlock(\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 32, 32, kernel_size=(3, 3), stride=[1, 1], groups=32, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 1, 1, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(32, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 32, 8, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 8, 32, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 32, 16, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(16, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (1): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 16, 96, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 96, 96, kernel_size=(3, 3), stride=[2, 2], groups=96, bias=False\n", + " (static_padding): ZeroPad2d(padding=(0, 1, 0, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(96, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 96, 4, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 4, 96, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 96, 24, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(24, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (2): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(144, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 144, 144, kernel_size=(3, 3), stride=(1, 1), groups=144, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 1, 1, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(144, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 144, 6, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 6, 144, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 144, 24, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(24, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (3): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(144, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 144, 144, kernel_size=(5, 5), stride=[2, 2], groups=144, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 2, 1, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(144, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 144, 6, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 6, 144, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 144, 40, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(40, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (4): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(240, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 240, 240, kernel_size=(5, 5), stride=(1, 1), groups=240, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(240, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 240, 10, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 10, 240, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 240, 40, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(40, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (5): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(240, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 240, 240, kernel_size=(3, 3), stride=[2, 2], groups=240, bias=False\n", + " (static_padding): ZeroPad2d(padding=(0, 1, 0, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(240, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 240, 10, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 10, 240, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 240, 80, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(80, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (6): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(480, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 480, 480, kernel_size=(3, 3), stride=(1, 1), groups=480, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 1, 1, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(480, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 480, 20, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 20, 480, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(80, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (7): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(480, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 480, 480, kernel_size=(3, 3), stride=(1, 1), groups=480, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 1, 1, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(480, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 480, 20, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 20, 480, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(80, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (8): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(480, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 480, 480, kernel_size=(5, 5), stride=[1, 1], groups=480, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(480, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 480, 20, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 20, 480, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 480, 112, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(112, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (9): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(672, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 672, 672, kernel_size=(5, 5), stride=(1, 1), groups=672, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(672, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 672, 28, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 28, 672, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(112, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (10): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(672, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 672, 672, kernel_size=(5, 5), stride=(1, 1), groups=672, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(672, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 672, 28, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 28, 672, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(112, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (11): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(672, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 672, 672, kernel_size=(5, 5), stride=[2, 2], groups=672, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 2, 1, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(672, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 672, 28, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 28, 672, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 672, 192, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (12): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 1152, 1152, kernel_size=(5, 5), stride=(1, 1), groups=1152, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 1152, 48, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 48, 1152, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (13): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 1152, 1152, kernel_size=(5, 5), stride=(1, 1), groups=1152, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 1152, 48, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 48, 1152, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (14): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 1152, 1152, kernel_size=(5, 5), stride=(1, 1), groups=1152, bias=False\n", + " (static_padding): ZeroPad2d(padding=(2, 2, 2, 2), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 1152, 48, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 48, 1152, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(192, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " (15): MBConvBlock(\n", + " (_expand_conv): Conv2dStaticSamePadding(\n", + " 192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn0): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_depthwise_conv): Conv2dStaticSamePadding(\n", + " 1152, 1152, kernel_size=(3, 3), stride=[1, 1], groups=1152, bias=False\n", + " (static_padding): ZeroPad2d(padding=(1, 1, 1, 1), value=0.0)\n", + " )\n", + " (_bn1): BatchNorm2d(1152, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_se_reduce): Conv2dStaticSamePadding(\n", + " 1152, 48, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_se_expand): Conv2dStaticSamePadding(\n", + " 48, 1152, kernel_size=(1, 1), stride=(1, 1)\n", + " (static_padding): Identity()\n", + " )\n", + " (_project_conv): Conv2dStaticSamePadding(\n", + " 1152, 320, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn2): BatchNorm2d(320, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (_conv_head): Conv2dStaticSamePadding(\n", + " 320, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False\n", + " (static_padding): Identity()\n", + " )\n", + " (_bn1): BatchNorm2d(1280, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)\n", + " (_fc): Identity()\n", + " )\n", + " (fc): Linear(in_features=2560, out_features=1108, bias=True)\n", + ")" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class EfficientNetTwoInputs(nn.Module):\n", + " def __init__(self):\n", + " super(EfficientNetTwoInputs, self).__init__()\n", + " self.classes = 1108\n", + " \n", + " model = model = EfficientNet.from_pretrained(model_name, num_classes=1108) \n", + " model._fc = nn.Identity()\n", + " \n", + " self.resnet = model\n", + " self.fc = nn.Linear(2560, self.classes)\n", + "\n", + " def forward(self, x1, x2):\n", + " x1_out = self.resnet(x1)\n", + " x2_out = self.resnet(x2)\n", + " \n", + " N, _, _, _ = x1.size()\n", + " x1_out = x1_out.view(N, -1)\n", + " x2_out = x2_out.view(N, -1)\n", + " \n", + " out = torch.cat((x1_out, x2_out), 1)\n", + " out = self.fc(out)\n", + "\n", + " return out \n", + " \n", + "model = EfficientNetTwoInputs()\n", + "model.cuda()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Evaluate" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ff6b97b85a174dbd877db9a2547d9ea3", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(IntProgress(value=0, max=622), HTML(value='')))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "eval_model(model, tloader, 'models/Model_efficientnet-b0_63.pth', path_data)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": { + "2b62ae829edc4d60acf1d9a9e1d598d8": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.4.0", + "model_name": "DescriptionStyleModel", + "state": { + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.4.0", + "_model_name": "DescriptionStyleModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.1.0", + "_view_name": "StyleView", + "description_width": "" + } + }, + "7740dfb227e54da8b1510dac2d094406": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.1.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.1.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.1.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + }, + "921a9c670b6e4a2db86c75a7ff5d9ee6": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.1.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.1.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.1.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + }, + "9dfcb7497f8842af817750eec565b8b9": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.4.0", + "model_name": "HTMLModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.4.0", + "_model_name": "HTMLModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.4.0", + "_view_name": "HTMLView", + "description": "", + "description_tooltip": null, + "layout": "IPY_MODEL_921a9c670b6e4a2db86c75a7ff5d9ee6", + "placeholder": "​", + "style": "IPY_MODEL_2b62ae829edc4d60acf1d9a9e1d598d8", + "value": " 94% 2151/2283 [22:45<01:23, 1.58it/s]" + } + }, + "d2df0eb5abab4e3895ec792681cfa8d2": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.4.0", + "model_name": "ProgressStyleModel", + "state": { + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.4.0", + "_model_name": "ProgressStyleModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.1.0", + "_view_name": "StyleView", + "bar_color": null, + "description_width": "initial" + } + }, + "e3ff3ae302394523bb5b28ee009842d5": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.4.0", + "model_name": "HBoxModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.4.0", + "_model_name": "HBoxModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.4.0", + "_view_name": "HBoxView", + "box_style": "", + "children": [ + "IPY_MODEL_ff74a4321a59419cb24e116db9dd1e3e", + "IPY_MODEL_9dfcb7497f8842af817750eec565b8b9" + ], + "layout": "IPY_MODEL_7740dfb227e54da8b1510dac2d094406" + } + }, + "fad7703039454db7af5d7fb4bce65003": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.1.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.1.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.1.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + }, + "ff74a4321a59419cb24e116db9dd1e3e": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.4.0", + "model_name": "IntProgressModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.4.0", + "_model_name": "IntProgressModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.4.0", + "_view_name": "ProgressView", + "bar_style": "", + "description": "Loss: 128.54232788085938", + "description_tooltip": null, + "layout": "IPY_MODEL_fad7703039454db7af5d7fb4bce65003", + "max": 2283, + "min": 0, + "orientation": "horizontal", + "style": "IPY_MODEL_d2df0eb5abab4e3895ec792681cfa8d2", + "value": 2151 + } + } + }, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}