{"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 os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport torch\nimport torchvision\n\nfrom torch.utils.data import DataLoader, Dataset, random_split\nfrom torchvision import datasets, transforms\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\n# \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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH = '../input/cassava-leaf-disease-classification/test_images/'\nfiles = os.listdir(TEST_PATH)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalize = transforms.Normalize(\n    mean=[0.485, 0.456, 0.406],\n    std=[0.229, 0.224, 0.225]\n)\npreprocess = transforms.Compose([\n    #transforms.Scale(256),\n    #transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    normalize\n])\n\ndef default_loader(path):\n    img_pil =  Image.open(path)\n    img_pil = img_pil.resize((400,300))\n    img_tensor = preprocess(img_pil)\n    return img_tensor","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyTestDataset(Dataset):\n    def __init__(self, files, loader=default_loader):\n        self.files = files\n        self.loader = loader\n        \n    def __getitem__(self, index):\n        img_path = os.path.join(TEST_PATH, self.files[index])\n        img = self.loader(img_path)\n        \n        return self.files[index], img\n    \n    def __len__(self):\n        return len(self.labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset = MyTestDataset(files)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_PATH = '../input/cassavaleaffsolmodels/resnet1.mdl'\nmodel = torchvision.models.resnet34()\nmodel.load_state_dict(torch.load(MODEL_PATH))\nmodel.eval()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\nfor file in files:\n    img_path = os.path.join(TEST_PATH, file)\n    img = default_loader(img_path)\n    \n    logits = model(img.unsqueeze(0))\n    _, predicted_label = torch.max(logits, 1)\n    labels.append(int(predicted_label))\nsubmission = pd.DataFrame({'image_id':files, 'label':labels})","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}