{"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\ncount = 0\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input/hpa-cell-tiles-test-with-enc-dataset/cells'):\n    for filename in filenames:\n        count = count + 1\nprint(count)\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell = pd.read_csv('../input/hpa-cell-tiles-test-with-enc-dataset/cell_df.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = cell[cell['image_id'] == '0040581b-f1f2-4fbe-b043-b6bfea5404bb']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = cell['image_id']\nimage_id = set(image_id)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport os\nimport re\n\nimport base64\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\n\nimport cv2\nfrom tqdm import tqdm_notebook\nimport typing as t\nimport zlib","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self, device):\n        super().__init__()\n\n        self.build_model()\n\n        self.criterion = nn.CrossEntropyLoss()\n\n        self.optimizer = optim.Adam(self.parameters(), lr=4e-5)\n\n        self.device = device\n\n    def build_model(self):\n        self.conv0 = nn.Conv2d(3, 64, (5, 5), padding=2)\n        self.pool0 = nn.AvgPool2d((2, 2), 2)\n        self.conv1 = nn.Conv2d(64, 256, (5, 5), padding=2)\n        self.pool1 = nn.AvgPool2d((2, 2), 2)\n        self.conv2 = nn.Conv2d(256, 512, (5, 5), padding=2)\n        self.pool2 = nn.MaxPool2d((2, 2), 2)\n        self.fc = nn.Sequential(\n            nn.Linear(65536*8, 19),\n        )\n\n    def forward(self, x):\n        x = self.conv0(x)\n        x = self.pool0(x)\n        x = self.conv1(x)\n        x = self.pool1(x)\n        x = self.conv2(x)\n        x = self.pool2(x)\n        x = x.reshape(x.shape[0], -1)\n        x = self.fc(x)\n        return x\n\n\n    def fit(self, image, label):\n        image, label = torch.FloatTensor(image).to(device), torch.LongTensor(label).to(device)\n        image = image.permute(0, 3, 1, 2)\n        self.zero_grad()\n        pred = self(image)\n        loss = self.criterion(pred, label)\n        loss.backward()\n        self.optimizer.step()\n        return loss.item()\n\n    \nclass ResNet(CNN):\n    def __init__(self, device):\n        super().__init__(device)\n        \n        self.build_model()\n\n        self.criterion = nn.CrossEntropyLoss()\n\n        self.optimizer = optim.Adam(self.parameters(), lr=4e-5)\n\n        self.device = device\n\n    def build_model(self):\n        self.model = torchvision.models.resnet18(pretrained=True)\n        self.model.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3, bias=False)\n        self.fc_features = self.model.fc.in_features\n        self.OUT_CLASSES = 19\n        self.model.fc = nn.Linear(self.fc_features, self.OUT_CLASSES)\n        \n        \n    def forward(self, x):\n        return self.model(x)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet18(CNN): \n    def __init__(self, device, model_path=None):\n        super().__init__(device)\n        self.model = ResNet(device).to(device)\n        \n        if model_path is not None:\n            self.model.load_state_dict(torch.load(model_path))\n        \n        self.criterion = nn.CrossEntropyLoss()\n    \n        self.optimizer = optim.Adam(self.parameters(), lr=1e-4)\n        \n        self.device = device\n\n    def forward(self, x):\n        return self.model(x)\n    \n    def pred(self, cell_batch):\n        pred = self(cell_batch)\n        pred = torch.softmax(pred, -1)\n        label = torch.argmax(pred, -1)\n        confidence, _ = torch.max(pred, -1)\n        return label, confidence\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\ndsize = (256, 256)\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = ResNet18(device, model_path=\"/kaggle/input/resnet18/ResNet18_para.pkl\").to(device)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('../input/hpa-cell-tiles-test-with-enc-dataset/cells/0040581b-f1f2-4fbe-b043-b6bfea5404bb_1.jpg')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.resize(img,(224,224))\nimg.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg_t = torch.FloatTensor(img_t).to(device).permute(0,3,1,2)\nimg_t.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = model.pred(img_t)\nprint(res)\nres = model(img_t)\nres","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv('../input/hpa-single-cell-image-classification/sample_submission.csv')\nsample_sub\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx in sample_sub['ID']:\n    cell_image = cell[cell['image_id'] == idx]\n    cell_fnames = cell_image['fname']\n    lab, conf, predstr = [], [], []\n    for cell_fname in cell_fnames:\n        file = f'/kaggle/input/hpa-cell-tiles-test-with-enc-dataset/cells/{cell_fname}'\n        img = cv2.imread(file)\n        img = cv2.resize(img, (256,256))\n        img_t = np.expand_dims(img, 0)\n        img_t = torch.FloatTensor(img_t).to(device).permute(0,3,1,2)\n        label, confidence = model.pred(img_t)\n        label, confidence = label.cpu().detach().numpy(), confidence.cpu().detach().numpy()\n        lab = np.concatenate([lab, label], 0)\n        print(lab)\n        print(confidence)\n    if label.shape[0] > 30: break","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}