{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip3 install --upgrade fastai","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The aim of this notebook is to get started as quickly as possible and make a submission to Kaggle.\n\n* Loading Data\n* Data Augmentation (Mixup)\n* Fix Precision training\n* Model Training\n* Model Confusion Matrix\n* Predictions"},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.callback.mixup import MixUp\nimport torch\nimport numpy as np\nimport random, os","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"files = glob.glob('/kaggle/input/cassava-leaf-disease-classification/train_images/*.jpg')\nf, plots = plt.subplots(2, 5, sharex='col', sharey='row', figsize=(19, 7),  constrained_layout=True)\nim_plot = []\n\nfor j in files[:10]:\n    im = Image.open(j)\n    im_plot.append(im)\n        \nfor i in range(10):\n    plots[i // 5, i % 5].axis('off')\n    plots[i // 5, i % 5].imshow(im_plot[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def seed_everything(seed=0):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_everything()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('/kaggle/input/cassava-leaf-disease-classification');","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"with open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json', 'r') as handle:\n    parsed = json.load(handle)\nparsed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = path / \"train.csv\"\ntrain_df = pd.read_csv(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Looks like we have a class imbalance probelm"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = ImageDataLoaders.from_df(train_df, path/\"train_images\", item_tfms=Resize(256),\n                              bs=64, num_workers=4, \n                              label_col=\"label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(dls.vocab); print(dls.c)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics=[error_rate, accuracy], cbs=MixUp, model_dir=\"/tmp/model/\").to_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(15, lr_max=1e-2, cbs=EarlyStoppingCallback(patience=3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(15, lr_max=slice(1e-7, 1e-3), cbs=EarlyStoppingCallback(patience=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df = pd.read_csv(path/'sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_items = get_image_files(path/\"test_images\")\ndl = learn.dls.test_dl(test_items, rm_type_tfms=1, bs=64)\ny_pred, _ = learn.get_preds(dl=dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df['label'] = y_pred.argmax(dim=-1).numpy()\nsample_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df.to_csv('submission.csv',index=False)","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}