{"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":"markdown","source":"## Beginner notebook for a baseline using fastai \n\n* [EDA](#EDA)\n* [DataBlock & dataloaders](#section-two)\n* [Training a model](#section-3)\n* [Predict & submit](#submit)","metadata":{}},{"cell_type":"code","source":"#!pip install -qq fastai --upgrade","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:41:38.434196Z","iopub.execute_input":"2021-08-23T17:41:38.434579Z","iopub.status.idle":"2021-08-23T17:41:38.439055Z","shell.execute_reply.started":"2021-08-23T17:41:38.434497Z","shell.execute_reply":"2021-08-23T17:41:38.438232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nprint(torch.__version__)\nif torch.cuda.is_available():\n    print(torch.cuda.get_device_name(0), torch.cuda.is_available())\n\nimport fastai; print(fastai.__version__)\n!nvcc --version","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:41:38.45066Z","iopub.execute_input":"2021-08-23T17:41:38.45092Z","iopub.status.idle":"2021-08-23T17:41:41.763145Z","shell.execute_reply.started":"2021-08-23T17:41:38.450896Z","shell.execute_reply":"2021-08-23T17:41:41.762186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA <a id=\"EDA\"></a>","metadata":{}},{"cell_type":"code","source":"import os\nfor root, dirs, files in os.walk('/kaggle/input', topdown=False):\n    for name in dirs: print (os.path.join(root, name))","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:41:41.765082Z","iopub.execute_input":"2021-08-23T17:41:41.765471Z","iopub.status.idle":"2021-08-23T17:42:10.461338Z","shell.execute_reply.started":"2021-08-23T17:41:41.765425Z","shell.execute_reply":"2021-08-23T17:42:10.460444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Path.cwd()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:10.463319Z","iopub.execute_input":"2021-08-23T17:42:10.46367Z","iopub.status.idle":"2021-08-23T17:42:10.474512Z","shell.execute_reply.started":"2021-08-23T17:42:10.463632Z","shell.execute_reply":"2021-08-23T17:42:10.473431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tree ../input/ -d","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:10.476402Z","iopub.execute_input":"2021-08-23T17:42:10.476925Z","iopub.status.idle":"2021-08-23T17:42:11.201835Z","shell.execute_reply.started":"2021-08-23T17:42:10.476886Z","shell.execute_reply":"2021-08-23T17:42:11.200864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = Path(\"../input/cassava-leaf-disease-classification\")\nTRAIN_DIR = DATA_DIR/\"train_images\"\nTEST_DIR = DATA_DIR/\"test_images\"\nPath.BASE_PATH = DATA_DIR\nDATA_DIR, TRAIN_DIR, TEST_DIR","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.204898Z","iopub.execute_input":"2021-08-23T17:42:11.205204Z","iopub.status.idle":"2021-08-23T17:42:11.215904Z","shell.execute_reply.started":"2021-08-23T17:42:11.20517Z","shell.execute_reply":"2021-08-23T17:42:11.215066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(DATA_DIR/\"train.csv\")\ntrain_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.217431Z","iopub.execute_input":"2021-08-23T17:42:11.217951Z","iopub.status.idle":"2021-08-23T17:42:11.263777Z","shell.execute_reply.started":"2021-08-23T17:42:11.217909Z","shell.execute_reply":"2021-08-23T17:42:11.262987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_id'] = train_df['image_id'].apply(lambda x: f'train_images/{x}')\ntrain_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.264936Z","iopub.execute_input":"2021-08-23T17:42:11.265295Z","iopub.status.idle":"2021-08-23T17:42:11.284632Z","shell.execute_reply.started":"2021-08-23T17:42:11.265258Z","shell.execute_reply":"2021-08-23T17:42:11.28359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx2lbl = {0:\"Cassava Bacterial Blight (CBB)\",\n          1:\"Cassava Brown Streak Disease (CBSD)\",\n          2:\"Cassava Green Mottle (CGM)\",\n          3:\"Cassava Mosaic Disease (CMD)\",\n          4:\"Healthy\"}\n\ntrain_df['label'].replace(idx2lbl, inplace=True)\ntrain_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.2883Z","iopub.execute_input":"2021-08-23T17:42:11.288572Z","iopub.status.idle":"2021-08-23T17:42:11.304617Z","shell.execute_reply.started":"2021-08-23T17:42:11.288548Z","shell.execute_reply":"2021-08-23T17:42:11.303904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.306283Z","iopub.execute_input":"2021-08-23T17:42:11.306526Z","iopub.status.idle":"2021-08-23T17:42:11.320511Z","shell.execute_reply.started":"2021-08-23T17:42:11.306503Z","shell.execute_reply":"2021-08-23T17:42:11.319441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR.ls()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.322064Z","iopub.execute_input":"2021-08-23T17:42:11.322519Z","iopub.status.idle":"2021-08-23T17:42:11.376615Z","shell.execute_reply.started":"2021-08-23T17:42:11.32248Z","shell.execute_reply":"2021-08-23T17:42:11.375659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_image_files(TRAIN_DIR)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.378116Z","iopub.execute_input":"2021-08-23T17:42:11.378463Z","iopub.status.idle":"2021-08-23T17:42:11.697428Z","shell.execute_reply.started":"2021-08-23T17:42:11.378426Z","shell.execute_reply":"2021-08-23T17:42:11.696289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.698962Z","iopub.execute_input":"2021-08-23T17:42:11.69935Z","iopub.status.idle":"2021-08-23T17:42:11.704981Z","shell.execute_reply.started":"2021-08-23T17:42:11.69931Z","shell.execute_reply":"2021-08-23T17:42:11.704155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating DataBlock and a dataloader <a id=\"section-two\"></a>","metadata":{}},{"cell_type":"code","source":"# When using file path for inputs when creating datasets from a DataBlock:\ndef get_label(path):\n    fname = path.name\n    row = train_df.loc[train_df.image_id == str(\"train_images/\" + fname)]\n    value = row.label.values[0]\n    return (value)\n\n# When using pandas datafragme as input\ndef get_x(row): return DATA_DIR / row.image_id\ndef get_y(row): return row.label","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.706403Z","iopub.execute_input":"2021-08-23T17:42:11.706956Z","iopub.status.idle":"2021-08-23T17:42:11.714445Z","shell.execute_reply.started":"2021-08-23T17:42:11.706917Z","shell.execute_reply":"2021-08-23T17:42:11.713664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_image_files(TRAIN_DIR)[0:5].map(get_label)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.715748Z","iopub.execute_input":"2021-08-23T17:42:11.716217Z","iopub.status.idle":"2021-08-23T17:42:11.898437Z","shell.execute_reply.started":"2021-08-23T17:42:11.716182Z","shell.execute_reply":"2021-08-23T17:42:11.897349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datablock = DataBlock(\n    blocks=(ImageBlock, CategoryBlock), \n    get_x=lambda row: DATA_DIR / row.image_id, #get_image_files, \n    get_y=lambda row: row.label, #get_label,\n    splitter=RandomSplitter(valid_pct=0.2, seed=42),\n    item_tfms=Resize(460),\n    batch_tfms=[RandomResizedCropGPU(224), *aug_transforms(size = 224, min_scale = 0.75), Normalize.from_stats(*imagenet_stats)]\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:11.899988Z","iopub.execute_input":"2021-08-23T17:42:11.900403Z","iopub.status.idle":"2021-08-23T17:42:15.760195Z","shell.execute_reply.started":"2021-08-23T17:42:11.900363Z","shell.execute_reply":"2021-08-23T17:42:15.75932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = datablock.dataloaders(train_df)\nlen(dls.train.dataset), len(dls.valid.dataset)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:15.7614Z","iopub.execute_input":"2021-08-23T17:42:15.761737Z","iopub.status.idle":"2021-08-23T17:42:20.461561Z","shell.execute_reply.started":"2021-08-23T17:42:15.761702Z","shell.execute_reply":"2021-08-23T17:42:20.460791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(figsize=(15,10))","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:20.462862Z","iopub.execute_input":"2021-08-23T17:42:20.463229Z","iopub.status.idle":"2021-08-23T17:42:22.585456Z","shell.execute_reply.started":"2021-08-23T17:42:20.463192Z","shell.execute_reply":"2021-08-23T17:42:22.5845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training  <a id=\"section-3\"></a>","metadata":{}},{"cell_type":"code","source":"if not os.path.exists('/root/.cache/torch/hub/checkpoints/'):\n        os.makedirs('/root/.cache/torch/hub/checkpoints/')\n!cp '../input/resnet50/resnet50.pth' '/root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth'","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:22.586583Z","iopub.execute_input":"2021-08-23T17:42:22.586933Z","iopub.status.idle":"2021-08-23T17:42:26.030391Z","shell.execute_reply.started":"2021-08-23T17:42:22.586899Z","shell.execute_reply":"2021-08-23T17:42:26.029308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner = cnn_learner(dls, resnet151, loss_func=CrossEntropyLossFlat(), metrics=accuracy)\nprint(\"cbs: \", learner.cbs)\nprint(\"loss: \", learner.loss_func)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:26.032084Z","iopub.execute_input":"2021-08-23T17:42:26.032437Z","iopub.status.idle":"2021-08-23T17:42:26.885811Z","shell.execute_reply.started":"2021-08-23T17:42:26.032392Z","shell.execute_reply":"2021-08-23T17:42:26.884975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learner.lr_find(num_it=100)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:42:26.88738Z","iopub.execute_input":"2021-08-23T17:42:26.887728Z","iopub.status.idle":"2021-08-23T17:44:02.943912Z","shell.execute_reply.started":"2021-08-23T17:42:26.887687Z","shell.execute_reply":"2021-08-23T17:44:02.943082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install wandb --upgrade -qq","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:44:02.945296Z","iopub.execute_input":"2021-08-23T17:44:02.94581Z","iopub.status.idle":"2021-08-23T17:44:13.960179Z","shell.execute_reply.started":"2021-08-23T17:44:02.945764Z","shell.execute_reply":"2021-08-23T17:44:13.959149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.callback.wandb import *\nimport wandb\nrun_name = \"resnet151 5-10 wd+CutMix\"\nsettings = wandb.Settings(silent=\"True\")\nrun = wandb.init(name=run_name, project='Kaggle - Cassava Leaf Disease Classification', settings=settings)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-23T17:44:13.961883Z","iopub.execute_input":"2021-08-23T17:44:13.96233Z","iopub.status.idle":"2021-08-23T17:55:59.583668Z","shell.execute_reply.started":"2021-08-23T17:44:13.962222Z","shell.execute_reply":"2021-08-23T17:55:59.582659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.01\nwd = 0.01\nfreeze_epochs = 5\nepochs = 10\n\ncbs = [WandbCallback(), CutMix(1.)]\n#cbs = [WandbCallback()]\n\nlearner.fine_tune(epochs, base_lr = lr, wd=wd, freeze_epochs=freeze_epochs, cbs=cbs)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T17:55:59.591109Z","iopub.execute_input":"2021-08-23T17:55:59.591576Z","iopub.status.idle":"2021-08-23T18:51:24.415315Z","shell.execute_reply.started":"2021-08-23T17:55:59.591498Z","shell.execute_reply":"2021-08-23T18:51:24.41401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learner.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:24.421376Z","iopub.execute_input":"2021-08-23T18:51:24.421758Z","iopub.status.idle":"2021-08-23T18:51:24.430566Z","shell.execute_reply.started":"2021-08-23T18:51:24.421713Z","shell.execute_reply":"2021-08-23T18:51:24.429812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict and submit <a id=\"submit\"></a>","metadata":{}},{"cell_type":"code","source":"get_image_files(TEST_DIR)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:24.43625Z","iopub.execute_input":"2021-08-23T18:51:24.446294Z","iopub.status.idle":"2021-08-23T18:51:24.470081Z","shell.execute_reply.started":"2021-08-23T18:51:24.446214Z","shell.execute_reply":"2021-08-23T18:51:24.467853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv(DATA_DIR/'sample_submission.csv')\nsample_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:24.471421Z","iopub.execute_input":"2021-08-23T18:51:24.47178Z","iopub.status.idle":"2021-08-23T18:51:24.557862Z","shell.execute_reply.started":"2021-08-23T18:51:24.471733Z","shell.execute_reply":"2021-08-23T18:51:24.554547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_copy = sample_df.copy()\nsample_copy['image_id'] = sample_copy['image_id'].apply(lambda x: f'test_images/{x}')\nsample_copy.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:24.570631Z","iopub.execute_input":"2021-08-23T18:51:24.586288Z","iopub.status.idle":"2021-08-23T18:51:24.703215Z","shell.execute_reply.started":"2021-08-23T18:51:24.586187Z","shell.execute_reply":"2021-08-23T18:51:24.702353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = learner.dls.test_dl(sample_copy)\ntest_dl.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:24.709675Z","iopub.execute_input":"2021-08-23T18:51:24.711866Z","iopub.status.idle":"2021-08-23T18:51:25.253412Z","shell.execute_reply.started":"2021-08-23T18:51:24.711825Z","shell.execute_reply":"2021-08-23T18:51:25.252146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, _ = learner.tta(dl=test_dl)\npreds","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:25.257027Z","iopub.execute_input":"2021-08-23T18:51:25.25744Z","iopub.status.idle":"2021-08-23T18:51:26.82869Z","shell.execute_reply.started":"2021-08-23T18:51:25.257397Z","shell.execute_reply":"2021-08-23T18:51:26.827814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['label'] = preds.argmax(dim=-1).numpy()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:26.831699Z","iopub.execute_input":"2021-08-23T18:51:26.831979Z","iopub.status.idle":"2021-08-23T18:51:26.839667Z","shell.execute_reply.started":"2021-08-23T18:51:26.831946Z","shell.execute_reply":"2021-08-23T18:51:26.838945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:26.841669Z","iopub.execute_input":"2021-08-23T18:51:26.842015Z","iopub.status.idle":"2021-08-23T18:51:26.858482Z","shell.execute_reply.started":"2021-08-23T18:51:26.841981Z","shell.execute_reply":"2021-08-23T18:51:26.857538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-23T18:51:26.859807Z","iopub.execute_input":"2021-08-23T18:51:26.86029Z","iopub.status.idle":"2021-08-23T18:51:26.872969Z","shell.execute_reply.started":"2021-08-23T18:51:26.860232Z","shell.execute_reply":"2021-08-23T18:51:26.872135Z"},"trusted":true},"execution_count":null,"outputs":[]}]}