{"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\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai import *\n\nimport matplotlib.pyplot as plt\n\nplt.style.use('ggplot')\nPATH = Path('/kaggle/input/plant-pathology-2021-fgvc8/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(PATH/'train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.describe(include='all')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.labels.value_counts().plot(kind='bar', figsize=(16,6));","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Count files in train directory & test directory\n!ls ../input/plant-pathology-2021-fgvc8/train_images/ | wc -l\n!ls ../input/plant-pathology-2021-fgvc8/test_images/ | wc -l","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path= '../input/plant-pathology-2021-fgvc8/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# High-Level API\n# dls = ImageDataLoaders.from_df(df, path, folder='train_images', label_delim=' ',\n#                                item_tfms=Resize(460), batch_tfms=aug_transforms(size=224))\n\n# # DataBlock\n# dblock = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),\n#                    get_x=ColReader('image', pref=str(path+'train_images') + os.path.sep),\n#                    get_y=ColReader('labels', label_delim=' '),\n#                    item_tfms = Resize(16),\n#                    batch_tfms=aug_transforms(mult=2, size=128, flip_vert=True))\n\n# dls = dblock.dataloaders(df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ImageDataLoaders.from_df??\n# dls.valid.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def get_dls(bs, size):\n#     dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n#                    get_items=get_image_files,\n#                    get_y=parent_label,\n#                    item_tfms=Resize(128),\n#                    batch_tfms=[*aug_transforms(size=size, min_scale=0.75),\n#                                Normalize.from_stats(*imagenet_stats)])\n#     return dblock.dataloaders(path, bs=bs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# cnn_learner?","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn = cnn_learner(dls, resnet50, metrics=partial(accuracy_multi, thresh=0.5))\n# learn.lr_find()\n\n# dls = get_dls(64, 128)\n\n# model = xresnet50()\n# learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)\n# learn.fit_one_cycle(5, 3e-3)\n\n# learn = cnn_learner(dls, resnet50, metrics=F1ScoreMulti, loss_func=BCEWithLogitsLossFlat())\n# learn.fine_tune(1,0.001)\n\n# # learn.fit_one_cycle(1, 0.01)\n# = Path('/kaggle/input/model4/')\n#learn = load_learner(MODEL_PATH/'serial_learner_export_3.pkl')\nMODEL_PATH = Path('/kaggle/input/model4/')\nlearn = load_learner(MODEL_PATH/'serial_learner_export_3.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The way to load learners\n# learn.model_dir='../input/model' \n# learn.model_dir='/kaggle/working'\n# # # Saving serializing\n# learn.export('/kaggle/working/serial_learner_export_4.pkl')\n# # # loading learner\n# learn = load_learner('/kaggle/working/serial_learner_export_3.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Checking loaded model with trained model\n\n# print(f'{learn.dls.vocab}')\n# print(f'{export_learner.dls.vocab}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Potentially used in prediciting images work\n\n# test_dl = dls.test_dl(get_image_files('../input/plant-pathology-2021-fgvc8/test_images').sorted())\n# learn.get_preds(dl=test_dl)\n# export_learner.model.cpu()\n\n# With imported model\n\n# Had an error around Tensorfloats and cuda.Tensorfloats\n# This was because the data was trained on GPU and was trying to run on CPU \n\n# learn.model.cpu()\nlearn.model.cpu()\ndls = learn.dls\n\n# Used in prediciting images\ntest_dl = dls.test_dl(get_image_files('../input/plant-pathology-2021-fgvc8/test_images')\n                          .sorted()\n                         )\n\nlearn.get_preds(dl=test_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.dls.vocab","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.predict(PATH/'test_images/85f8cb619c66b863.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.predict(PATH/'test_images/ad8770db05586b59.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.predict(PATH/'test_images/c7b03e718489f3ca.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds_2 = learn.get_preds(dl=test_dl)\narr = preds_2[0].numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(data=arr, \n                index=pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')['image'], \n                columns=['cider_apple_rust', 'complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab'])\n\n\ndf['labels'] = df[['cider_apple_rust', 'complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']].idxmax(axis=1)\n\ndf.drop(columns=['cider_apple_rust', 'complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab'], inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}