{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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        pass\n        \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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys, cv2, torch\nimport numpy as np\nsys.path.append(\"../input/efficient-net-deps-1\")\nfrom efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import torch\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\ndef Load_Model():\n    num_gpus = torch.cuda.device_count()\n    device, model = {}, {}\n    models = []\n    device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n    model = torch.load(\"../input/models/efficientnet-b0-CL.pt\", map_location=device).eval()\n    models.append(model)\n    return models\n\nmodel = Load_Model()[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_path = \"../input/cassava-leaf-disease-classification/test_images\"\ndf = pd.read_csv(\"../input/cassava-leaf-disease-classification/sample_submission.csv\")\nfull_image_paths = [os.path.join(image_path, x) for x in df.image_id.values]\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# utility functions\n\ndef image_preprocess(file_path, img_size=224):\n    image = cv2.imread(file_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = cv2.resize(image, (img_size, img_size))\n    image = image.astype(np.float32)\n    image /= 255.\n    # print(\"Shape {} and Type {} and Data-Type {}\".format(image.shape, type(image), image.dtype))\n    image = np.array(image)\n    image = torch.from_numpy(image)\n    #plt.imshow(image)\n    #plt.show()\n    return image\n\ndef inference(file_path, model):\n    image = image_preprocess(file_path)\n    img = (image).to(device)\n    img = img.unsqueeze(0)\n    img = img.permute(0,3,1,2) # (bs, width, height, channels) --> (bs, channels, width, height)\n    output = model(img)\n    return output.argmax(1).item()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_prediction = inference(full_image_paths[0], model)\ndf.label = final_prediction","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}