{"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":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:56:43.312555Z","iopub.execute_input":"2023-02-28T12:56:43.312995Z","iopub.status.idle":"2023-02-28T12:56:48.171656Z","shell.execute_reply.started":"2023-02-28T12:56:43.31295Z","shell.execute_reply":"2023-02-28T12:56:48.170337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train.csv\")\ndf = df.groupby('image_id')['class_name'].apply(lambda x: ','.join(set(x))).reset_index()\n# df[\"class_name\"] = df[\"class_name\"].apply(lambda x: x.split(\",\"))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:56:48.174276Z","iopub.execute_input":"2023-02-28T12:56:48.175018Z","iopub.status.idle":"2023-02-28T12:56:48.749775Z","shell.execute_reply.started":"2023-02-28T12:56:48.174977Z","shell.execute_reply":"2023-02-28T12:56:48.74864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/vinbigdata-chest-xray-resized-png-256x256\"","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:56:48.755035Z","iopub.execute_input":"2023-02-28T12:56:48.757499Z","iopub.status.idle":"2023-02-28T12:56:48.76396Z","shell.execute_reply.started":"2023-02-28T12:56:48.757459Z","shell.execute_reply":"2023-02-28T12:56:48.762617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_tfms = aug_transforms(flip_vert=False, max_lighting=0.1, max_zoom=1.05, max_warp=0.)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:56:53.022637Z","iopub.execute_input":"2023-02-28T12:56:53.02309Z","iopub.status.idle":"2023-02-28T12:56:53.036004Z","shell.execute_reply.started":"2023-02-28T12:56:53.023047Z","shell.execute_reply":"2023-02-28T12:56:53.034766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"db = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),\n                   get_x=ColReader(\"image_id\", pref=f'{path}/train/', suff='.png'),\n                   splitter=RandomSplitter(),\n                   get_y=ColReader(\"class_name\", label_delim=','),\n                   batch_tfms = batch_tfms)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:56:53.494869Z","iopub.execute_input":"2023-02-28T12:56:53.495397Z","iopub.status.idle":"2023-02-28T12:56:53.505213Z","shell.execute_reply.started":"2023-02-28T12:56:53.495331Z","shell.execute_reply":"2023-02-28T12:56:53.504167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = db.dataloaders(df)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:56:55.666745Z","iopub.execute_input":"2023-02-28T12:56:55.6672Z","iopub.status.idle":"2023-02-28T12:57:02.478932Z","shell.execute_reply.started":"2023-02-28T12:56:55.667158Z","shell.execute_reply":"2023-02-28T12:57:02.477888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=9, figsize=(12,9))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:57:02.481218Z","iopub.execute_input":"2023-02-28T12:57:02.48164Z","iopub.status.idle":"2023-02-28T12:57:05.643384Z","shell.execute_reply.started":"2023-02-28T12:57:02.481601Z","shell.execute_reply":"2023-02-28T12:57:05.642445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.c","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:11:07.182069Z","iopub.execute_input":"2023-02-28T13:11:07.182564Z","iopub.status.idle":"2023-02-28T13:11:07.191361Z","shell.execute_reply.started":"2023-02-28T13:11:07.182514Z","shell.execute_reply":"2023-02-28T13:11:07.190295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:11:30.005618Z","iopub.execute_input":"2023-02-28T13:11:30.006105Z","iopub.status.idle":"2023-02-28T13:11:30.015732Z","shell.execute_reply.started":"2023-02-28T13:11:30.006062Z","shell.execute_reply":"2023-02-28T13:11:30.014171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import resnet34\nfrom fastai.metrics import accuracy_multi\n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:57:05.64438Z","iopub.execute_input":"2023-02-28T12:57:05.644753Z","iopub.status.idle":"2023-02-28T12:57:05.650398Z","shell.execute_reply.started":"2023-02-28T12:57:05.644714Z","shell.execute_reply":"2023-02-28T12:57:05.649508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, pretrained=True, metrics=[accuracy_multi])","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:57:05.652713Z","iopub.execute_input":"2023-02-28T12:57:05.653392Z","iopub.status.idle":"2023-02-28T12:57:07.053295Z","shell.execute_reply.started":"2023-02-28T12:57:05.653357Z","shell.execute_reply":"2023-02-28T12:57:07.052283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:57:07.058033Z","iopub.execute_input":"2023-02-28T12:57:07.060435Z","iopub.status.idle":"2023-02-28T12:58:39.5918Z","shell.execute_reply.started":"2023-02-28T12:57:07.060394Z","shell.execute_reply":"2023-02-28T12:58:39.590612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.002089\nlearn = learn.to_fp16()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:59:03.152668Z","iopub.execute_input":"2023-02-28T12:59:03.153301Z","iopub.status.idle":"2023-02-28T12:59:03.159626Z","shell.execute_reply.started":"2023-02-28T12:59:03.15325Z","shell.execute_reply":"2023-02-28T12:59:03.15848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, slice(lr))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T12:59:06.009901Z","iopub.execute_input":"2023-02-28T12:59:06.01042Z","iopub.status.idle":"2023-02-28T13:06:33.45469Z","shell.execute_reply.started":"2023-02-28T12:59:06.010367Z","shell.execute_reply":"2023-02-28T13:06:33.453482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results(figsize=(15,15))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:06:47.852115Z","iopub.execute_input":"2023-02-28T13:06:47.85274Z","iopub.status.idle":"2023-02-28T13:06:49.828776Z","shell.execute_reply.started":"2023-02-28T13:06:47.852692Z","shell.execute_reply":"2023-02-28T13:06:49.827315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:09:18.945147Z","iopub.execute_input":"2023-02-28T13:09:18.947936Z","iopub.status.idle":"2023-02-28T13:09:33.916752Z","shell.execute_reply.started":"2023-02-28T13:09:18.947893Z","shell.execute_reply":"2023-02-28T13:09:33.915538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(k=10)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:10:40.22338Z","iopub.execute_input":"2023-02-28T13:10:40.223848Z","iopub.status.idle":"2023-02-28T13:10:41.69473Z","shell.execute_reply.started":"2023-02-28T13:10:40.223808Z","shell.execute_reply":"2023-02-28T13:10:41.693482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.most_confused()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:13:31.897526Z","iopub.execute_input":"2023-02-28T13:13:31.89812Z","iopub.status.idle":"2023-02-28T13:13:46.322185Z","shell.execute_reply.started":"2023-02-28T13:13:31.898073Z","shell.execute_reply":"2023-02-28T13:13:46.320711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.metrics import RocAucMulti\nroc_auc = RocAucMulti(average='macro')","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:17:33.61271Z","iopub.execute_input":"2023-02-28T13:17:33.61317Z","iopub.status.idle":"2023-02-28T13:17:33.619247Z","shell.execute_reply.started":"2023-02-28T13:17:33.613129Z","shell.execute_reply":"2023-02-28T13:17:33.618208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, pretrained=True, metrics=[accuracy_multi, roc_auc])","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:17:33.813044Z","iopub.execute_input":"2023-02-28T13:17:33.81354Z","iopub.status.idle":"2023-02-28T13:17:34.523066Z","shell.execute_reply.started":"2023-02-28T13:17:33.813498Z","shell.execute_reply":"2023-02-28T13:17:34.52195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.freeze()\nlearn.fit_one_cycle(1, slice(lr))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:18:30.174059Z","iopub.execute_input":"2023-02-28T13:18:30.174568Z","iopub.status.idle":"2023-02-28T13:19:40.890793Z","shell.execute_reply.started":"2023-02-28T13:18:30.174522Z","shell.execute_reply":"2023-02-28T13:19:40.889548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(5, slice(lr))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:19:51.290686Z","iopub.execute_input":"2023-02-28T13:19:51.291823Z","iopub.status.idle":"2023-02-28T13:26:17.711692Z","shell.execute_reply.started":"2023-02-28T13:19:51.291775Z","shell.execute_reply":"2023-02-28T13:26:17.710263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/code/jhoward/the-best-vision-models-for-fine-tuning","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:26:17.714088Z","iopub.execute_input":"2023-02-28T13:26:17.714476Z","iopub.status.idle":"2023-02-28T13:26:17.722188Z","shell.execute_reply.started":"2023-02-28T13:26:17.714434Z","shell.execute_reply":"2023-02-28T13:26:17.721093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:30:36.094512Z","iopub.execute_input":"2023-02-28T13:30:36.095103Z","iopub.status.idle":"2023-02-28T13:30:36.101388Z","shell.execute_reply.started":"2023-02-28T13:30:36.095058Z","shell.execute_reply":"2023-02-28T13:30:36.100027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"timm.list_models??","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:33:16.848578Z","iopub.execute_input":"2023-02-28T13:33:16.849046Z","iopub.status.idle":"2023-02-28T13:33:16.879304Z","shell.execute_reply.started":"2023-02-28T13:33:16.849005Z","shell.execute_reply":"2023-02-28T13:33:16.877299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"timm.list_models(\"convnext*\")","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:33:56.852363Z","iopub.execute_input":"2023-02-28T13:33:56.852817Z","iopub.status.idle":"2023-02-28T13:33:56.865607Z","shell.execute_reply.started":"2023-02-28T13:33:56.852778Z","shell.execute_reply":"2023-02-28T13:33:56.864339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, \"convnext_tiny\", pretrained=True, metrics=[accuracy_multi, roc_auc])","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:34:18.244328Z","iopub.execute_input":"2023-02-28T13:34:18.24478Z","iopub.status.idle":"2023-02-28T13:34:21.797586Z","shell.execute_reply.started":"2023-02-28T13:34:18.244739Z","shell.execute_reply":"2023-02-28T13:34:21.79647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.freeze()\nlearn.fit_one_cycle(1, slice(lr))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:34:31.386315Z","iopub.execute_input":"2023-02-28T13:34:31.3869Z","iopub.status.idle":"2023-02-28T13:36:12.118651Z","shell.execute_reply.started":"2023-02-28T13:34:31.386854Z","shell.execute_reply":"2023-02-28T13:36:12.117392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(5, slice(lr))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T13:36:12.123852Z","iopub.execute_input":"2023-02-28T13:36:12.126919Z","iopub.status.idle":"2023-02-28T14:05:07.578506Z","shell.execute_reply.started":"2023-02-28T13:36:12.126864Z","shell.execute_reply":"2023-02-28T14:05:07.577294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}