{"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":"# install fastkaggle if not available\ntry: import fastkaggle\nexcept ModuleNotFoundError:\n    !pip install -Uq fastkaggle\n\nfrom fastkaggle import *\nfrom fastai.vision.all import *\n\n!pip install fastai --upgrade\nfrom pathlib import Path\n\nfrom fastai.metrics import error_rate\n\n!pip install -Uqq fastbook\nimport fastbook\nfastbook.setup_book()\n\n\n#import fastai.vision.data.ImageDataLoaders\n\nfrom fastbook import *\n\n# install fastkaggle if not available\ntry: import fastkaggle\nexcept ModuleNotFoundError:\n    !pip install -Uq fastkaggle\n\nfrom fastkaggle import *\n\nimport pandas as pd\n\nfrom fastai.data.all import *\nfrom fastai.vision.all import *\n\nfrom collections import defaultdict\nimport pandas as pd\nimport numpy as np\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-12-18T08:59:31.768578Z","iopub.execute_input":"2022-12-18T08:59:31.769035Z","iopub.status.idle":"2022-12-18T09:00:24.111209Z","shell.execute_reply.started":"2022-12-18T08:59:31.768949Z","shell.execute_reply":"2022-12-18T09:00:24.109885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comp = '/kaggle/input/rsna-breast-cancer-detection-roi/bc_768_roi'\ncomp = '/kaggle/input/rsna-breast-cancer-detection-poi-images/bc_768_roi/bc_768_roi'\n\npath = setup_comp(comp, install='fastai \"timm>=0.6.2.dev0\"')","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:03:57.595329Z","iopub.execute_input":"2022-12-18T09:03:57.595829Z","iopub.status.idle":"2022-12-18T09:04:12.73677Z","shell.execute_reply.started":"2022-12-18T09:03:57.595786Z","shell.execute_reply":"2022-12-18T09:04:12.735458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_path = path/'train'\nfiles = get_image_files(trn_path)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:04:16.283278Z","iopub.execute_input":"2022-12-18T09:04:16.283773Z","iopub.status.idle":"2022-12-18T09:04:26.990038Z","shell.execute_reply.started":"2022-12-18T09:04:16.283731Z","shell.execute_reply":"2022-12-18T09:04:26.988814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(files))","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:36:03.522037Z","iopub.execute_input":"2022-12-18T09:36:03.522589Z","iopub.status.idle":"2022-12-18T09:36:03.529773Z","shell.execute_reply.started":"2022-12-18T09:36:03.522538Z","shell.execute_reply":"2022-12-18T09:36:03.528766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ndf_test = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:04:26.995909Z","iopub.execute_input":"2022-12-18T09:04:26.998574Z","iopub.status.idle":"2022-12-18T09:04:27.088264Z","shell.execute_reply.started":"2022-12-18T09:04:26.998529Z","shell.execute_reply":"2022-12-18T09:04:27.087173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(df_train))","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:36:14.094092Z","iopub.execute_input":"2022-12-18T09:36:14.094593Z","iopub.status.idle":"2022-12-18T09:36:14.102343Z","shell.execute_reply.started":"2022-12-18T09:36:14.094552Z","shell.execute_reply":"2022-12-18T09:36:14.101128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nset_seed(42)\n\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:04:36.135568Z","iopub.execute_input":"2022-12-18T09:04:36.136025Z","iopub.status.idle":"2022-12-18T09:04:36.156211Z","shell.execute_reply.started":"2022-12-18T09:04:36.135987Z","shell.execute_reply":"2022-12-18T09:04:36.155293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:42:21.566455Z","iopub.execute_input":"2022-12-18T09:42:21.56687Z","iopub.status.idle":"2022-12-18T09:42:21.621798Z","shell.execute_reply.started":"2022-12-18T09:42:21.566822Z","shell.execute_reply":"2022-12-18T09:42:21.620196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:42:21.623994Z","iopub.execute_input":"2022-12-18T09:42:21.624458Z","iopub.status.idle":"2022-12-18T09:42:21.644618Z","shell.execute_reply.started":"2022-12-18T09:42:21.624384Z","shell.execute_reply":"2022-12-18T09:42:21.643156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = PILImage.create(files[5])\nprint(img.size)\nimg.to_thumb(128)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:04:46.594301Z","iopub.execute_input":"2022-12-18T09:04:46.594812Z","iopub.status.idle":"2022-12-18T09:04:46.634586Z","shell.execute_reply.started":"2022-12-18T09:04:46.594769Z","shell.execute_reply":"2022-12-18T09:04:46.633566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['img_path'] = df_train.apply(\n    lambda i: os.path.join(\n        f\"{trn_path}\", str(i['patient_id']) + \"_\" + str(i['image_id']) + '.png'\n    ), axis=1\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:04:56.922223Z","iopub.execute_input":"2022-12-18T09:04:56.92272Z","iopub.status.idle":"2022-12-18T09:04:58.292909Z","shell.execute_reply.started":"2022-12-18T09:04:56.922676Z","shell.execute_reply":"2022-12-18T09:04:58.291833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:05:00.139628Z","iopub.execute_input":"2022-12-18T09:05:00.140074Z","iopub.status.idle":"2022-12-18T09:05:00.169907Z","shell.execute_reply.started":"2022-12-18T09:05:00.140035Z","shell.execute_reply":"2022-12-18T09:05:00.168929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def label_func(path):\n    # df_train.cancer[df_train['img_path']==str(path)].value\n    return df_train.cancer[df_train['img_path']==str(path)].item()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:37:11.825295Z","iopub.execute_input":"2022-12-18T09:37:11.825835Z","iopub.status.idle":"2022-12-18T09:37:11.83229Z","shell.execute_reply.started":"2022-12-18T09:37:11.82579Z","shell.execute_reply":"2022-12-18T09:37:11.831175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit label_func(files[0])","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:37:13.438642Z","iopub.execute_input":"2022-12-18T09:37:13.439086Z","iopub.status.idle":"2022-12-18T09:37:18.559324Z","shell.execute_reply.started":"2022-12-18T09:37:13.439047Z","shell.execute_reply":"2022-12-18T09:37:18.5583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_lookup = {str(row['image_id']):row['cancer'] for _,row in df_train.iterrows()}\n\ndef label_func(path):\n    return label_lookup[path.stem.split('_')[-1]]","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:37:25.560879Z","iopub.execute_input":"2022-12-18T09:37:25.561484Z","iopub.status.idle":"2022-12-18T09:37:30.624146Z","shell.execute_reply.started":"2022-12-18T09:37:25.561436Z","shell.execute_reply":"2022-12-18T09:37:30.623103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit label_func(files[0])","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:37:30.626066Z","iopub.execute_input":"2022-12-18T09:37:30.626726Z","iopub.status.idle":"2022-12-18T09:37:33.784003Z","shell.execute_reply.started":"2022-12-18T09:37:30.626684Z","shell.execute_reply":"2022-12-18T09:37:33.782784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastprogress import progress_bar\n\nbroken_labels = []\nfor fn in progress_bar(files):\n    try: label_func(fn)\n    except: broken_labels.append(fn)\nassert len(broken_labels) == 0","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:13:18.522602Z","iopub.execute_input":"2022-12-18T09:13:18.523039Z","iopub.status.idle":"2022-12-18T09:13:18.780442Z","shell.execute_reply.started":"2022-12-18T09:13:18.523002Z","shell.execute_reply":"2022-12-18T09:13:18.779507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks    = (ImageBlock, CategoryBlock),\n                   get_items = get_image_files,\n                   get_y     = label_func,\n                   splitter  = RandomSplitter(),\n                   item_tfms = Resize(64))\n\ndls = dblock.dataloaders(path/\"train\")\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:13:40.593262Z","iopub.execute_input":"2022-12-18T09:13:40.593753Z","iopub.status.idle":"2022-12-18T09:14:02.042962Z","shell.execute_reply.started":"2022-12-18T09:13:40.59371Z","shell.execute_reply":"2022-12-18T09:14:02.04196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating the learner and training for a single epoch¶\n","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate, pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:24:55.01411Z","iopub.execute_input":"2022-12-18T09:24:55.014674Z","iopub.status.idle":"2022-12-18T09:25:00.174593Z","shell.execute_reply.started":"2022-12-18T09:24:55.014621Z","shell.execute_reply":"2022-12-18T09:25:00.173425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit(1)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:25:11.085992Z","iopub.execute_input":"2022-12-18T09:25:11.086465Z","iopub.status.idle":"2022-12-18T09:35:00.139682Z","shell.execute_reply.started":"2022-12-18T09:25:11.086425Z","shell.execute_reply":"2022-12-18T09:35:00.13849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:38:39.538215Z","iopub.execute_input":"2022-12-18T09:38:39.538717Z","iopub.status.idle":"2022-12-18T09:40:26.283174Z","shell.execute_reply.started":"2022-12-18T09:38:39.538674Z","shell.execute_reply":"2022-12-18T09:40:26.281995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(12,12), dpi=40)","metadata":{"execution":{"iopub.status.busy":"2022-12-18T09:40:34.704597Z","iopub.execute_input":"2022-12-18T09:40:34.705238Z","iopub.status.idle":"2022-12-18T09:42:21.563614Z","shell.execute_reply.started":"2022-12-18T09:40:34.705185Z","shell.execute_reply":"2022-12-18T09:42:21.56199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}