{"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 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        os.path.join(dirname, filename)\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","execution":{"iopub.status.busy":"2022-08-15T23:03:54.288687Z","iopub.execute_input":"2022-08-15T23:03:54.288950Z","iopub.status.idle":"2022-08-15T23:04:23.941985Z","shell.execute_reply.started":"2022-08-15T23:03:54.288923Z","shell.execute_reply":"2022-08-15T23:04:23.941077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nimport wandb\nfrom fastai.callback.wandb import *\nimport matplotlib.pyplot as plt\nimport seaborn as sn","metadata":{"execution":{"iopub.status.busy":"2022-08-16T00:52:32.271178Z","iopub.execute_input":"2022-08-16T00:52:32.271541Z","iopub.status.idle":"2022-08-16T00:52:32.376273Z","shell.execute_reply.started":"2022-08-16T00:52:32.271508Z","shell.execute_reply":"2022-08-16T00:52:32.375043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Monitor gradients and training through weights and biases\nwandb.login()\ncassava_path = Path('/kaggle/input/cassava-leaf-disease-classification')\ncassava_path","metadata":{"execution":{"iopub.status.busy":"2022-08-15T23:04:29.866789Z","iopub.execute_input":"2022-08-15T23:04:29.867123Z","iopub.status.idle":"2022-08-15T23:05:17.313476Z","shell.execute_reply.started":"2022-08-15T23:04:29.867089Z","shell.execute_reply":"2022-08-15T23:05:17.312419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv(cassava_path/'train.csv')\nimgs = get_image_files(cassava_path/'train_images')\nimgs,train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-15T23:05:17.318379Z","iopub.execute_input":"2022-08-15T23:05:17.320504Z","iopub.status.idle":"2022-08-15T23:05:22.778311Z","shell.execute_reply.started":"2022-08-15T23:05:17.320461Z","shell.execute_reply":"2022-08-15T23:05:22.777242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 4))\nsn.countplot(y=\"label\", data=train_csv);","metadata":{"execution":{"iopub.status.busy":"2022-08-16T00:52:59.936285Z","iopub.execute_input":"2022-08-16T00:52:59.936680Z","iopub.status.idle":"2022-08-16T00:53:00.082758Z","shell.execute_reply.started":"2022-08-16T00:52:59.936647Z","shell.execute_reply":"2022-08-16T00:53:00.081789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_csv[train_csv['label'] == 4]))","metadata":{"execution":{"iopub.status.busy":"2022-08-16T01:33:33.064103Z","iopub.execute_input":"2022-08-16T01:33:33.064445Z","iopub.status.idle":"2022-08-16T01:33:33.072051Z","shell.execute_reply.started":"2022-08-16T01:33:33.064413Z","shell.execute_reply":"2022-08-16T01:33:33.071032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gety(path):\n    name = path.name\n    return train_csv[train_csv['image_id'] == name].label.values[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-15T23:12:55.651673Z","iopub.execute_input":"2022-08-15T23:12:55.652075Z","iopub.status.idle":"2022-08-15T23:12:55.664100Z","shell.execute_reply.started":"2022-08-15T23:12:55.652041Z","shell.execute_reply":"2022-08-15T23:12:55.662935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preparing data loaders which converts images to tensor for training\n# uses \ndblock = DataBlock(blocks    = (ImageBlock, CategoryBlock),\n                   get_items = get_image_files,\n                   get_y = gety,\n                  splitter = RandomSplitter(seed = 42),\n                  item_tfms = Resize(460),\n                  batch_tfms = [*aug_transforms(size = 224,min_scale = 0.75),Normalize.from_stats(*imagenet_stats)]\n                  )","metadata":{"execution":{"iopub.status.busy":"2022-08-15T23:12:57.993673Z","iopub.execute_input":"2022-08-15T23:12:57.994053Z","iopub.status.idle":"2022-08-15T23:12:58.008416Z","shell.execute_reply.started":"2022-08-15T23:12:57.994011Z","shell.execute_reply":"2022-08-15T23:12:58.007076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = dblock.dataloaders(cassava_path/'train_images')","metadata":{"execution":{"iopub.status.busy":"2022-08-15T23:13:00.328487Z","iopub.execute_input":"2022-08-15T23:13:00.328826Z","iopub.status.idle":"2022-08-15T23:13:40.160250Z","shell.execute_reply.started":"2022-08-15T23:13:00.328796Z","shell.execute_reply":"2022-08-15T23:13:40.159324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ds.valid.dataset) + len(ds.dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T18:24:34.121229Z","iopub.execute_input":"2022-08-15T18:24:34.121604Z","iopub.status.idle":"2022-08-15T18:24:34.130213Z","shell.execute_reply.started":"2022-08-15T18:24:34.121568Z","shell.execute_reply":"2022-08-15T18:24:34.129371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T00:42:41.197785Z","iopub.execute_input":"2022-08-16T00:42:41.198160Z","iopub.status.idle":"2022-08-16T00:42:43.116637Z","shell.execute_reply.started":"2022-08-16T00:42:41.198125Z","shell.execute_reply":"2022-08-16T00:42:43.115587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(\n    project='Ml Report 2',\n    name='resnet'\n)\nlearner_resnet = cnn_learner(ds,resnet34,metrics=[error_rate,accuracy],cbs=WandbCallback())\nlearner_resnet.lr_find()\nlearner_resnet.fine_tune(10,base_lr = 1e-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T18:24:34.132003Z","iopub.execute_input":"2022-08-15T18:24:34.132724Z","iopub.status.idle":"2022-08-15T19:30:29.022715Z","shell.execute_reply.started":"2022-08-15T18:24:34.132680Z","shell.execute_reply":"2022-08-15T19:30:29.021713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner_resnet.recorder.plot_loss()\ninterp = ClassificationInterpretation.from_learner(learner_resnet)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T19:30:29.024231Z","iopub.execute_input":"2022-08-15T19:30:29.024801Z","iopub.status.idle":"2022-08-15T19:31:41.875532Z","shell.execute_reply.started":"2022-08-15T19:30:29.024760Z","shell.execute_reply":"2022-08-15T19:31:41.874667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(\n    project='Ml Report 2',\n    name='densenet121'\n)\nlearner_densenet = cnn_learner(ds,densenet121,metrics=[error_rate,accuracy],cbs=WandbCallback())\nlearner_densenet.lr_find()\nlearner_densenet.fine_tune(10,base_lr = 1e-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T19:31:41.877080Z","iopub.execute_input":"2022-08-15T19:31:41.877448Z","iopub.status.idle":"2022-08-15T20:44:32.166003Z","shell.execute_reply.started":"2022-08-15T19:31:41.877408Z","shell.execute_reply":"2022-08-15T20:44:32.165066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner_densenet.recorder.plot_loss()\ninterp = ClassificationInterpretation.from_learner(learner_densenet)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T20:44:32.167634Z","iopub.execute_input":"2022-08-15T20:44:32.168018Z","iopub.status.idle":"2022-08-15T20:45:45.884587Z","shell.execute_reply.started":"2022-08-15T20:44:32.167974Z","shell.execute_reply":"2022-08-15T20:45:45.883387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(\n    project='Ml Report 2',\n    name='squeezenet'\n)\nlearner_squeezenet = cnn_learner(ds,squeezenet1_1,metrics=[error_rate,accuracy],cbs=WandbCallback())\nlearner_squeezenet.lr_find()\nlearner_squeezenet.fine_tune(10,base_lr = 1e-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T20:45:45.886457Z","iopub.execute_input":"2022-08-15T20:45:45.886823Z","iopub.status.idle":"2022-08-15T21:49:42.230631Z","shell.execute_reply.started":"2022-08-15T20:45:45.886781Z","shell.execute_reply":"2022-08-15T21:49:42.229256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner_squeezenet.recorder.plot_loss()\ninterp = ClassificationInterpretation.from_learner(learner_squeezenet)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T21:49:42.232421Z","iopub.execute_input":"2022-08-15T21:49:42.232852Z","iopub.status.idle":"2022-08-15T21:50:54.780402Z","shell.execute_reply.started":"2022-08-15T21:49:42.232787Z","shell.execute_reply":"2022-08-15T21:50:54.779516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(\n    project='Ml Report 2',\n    name='alexnet'\n)\nlearner_alexnet = cnn_learner(ds,alexnet,metrics=[error_rate,accuracy],cbs=WandbCallback())\nlearner_alexnet.lr_find()\nlearner_alexnet.fine_tune(10,base_lr = 1e-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T23:14:21.964153Z","iopub.execute_input":"2022-08-15T23:14:21.964535Z","iopub.status.idle":"2022-08-16T00:18:54.785707Z","shell.execute_reply.started":"2022-08-15T23:14:21.964501Z","shell.execute_reply":"2022-08-16T00:18:54.784675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner_alexnet.recorder.plot_loss()\ninterp = ClassificationInterpretation.from_learner(learner_alexnet)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-08-16T00:32:13.689500Z","iopub.execute_input":"2022-08-16T00:32:13.689857Z","iopub.status.idle":"2022-08-16T00:33:26.505227Z","shell.execute_reply.started":"2022-08-16T00:32:13.689810Z","shell.execute_reply":"2022-08-16T00:33:26.504236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T01:21:50.483044Z","iopub.execute_input":"2022-08-16T01:21:50.483419Z","iopub.status.idle":"2022-08-16T01:21:50.506635Z","shell.execute_reply.started":"2022-08-16T01:21:50.483386Z","shell.execute_reply":"2022-08-16T01:21:50.505257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.finish()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T01:22:34.135561Z","iopub.execute_input":"2022-08-16T01:22:34.135892Z","iopub.status.idle":"2022-08-16T01:22:38.238874Z","shell.execute_reply.started":"2022-08-16T01:22:34.135861Z","shell.execute_reply":"2022-08-16T01:22:38.237889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}