{"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":"!pip install --user ../input/fastaimaster/torch-1.9.0-cp37-cp37m-manylinux1_x86_64.whl\n!pip install -U fastai\n!mkdir ./model\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-18T22:55:10.375075Z","iopub.execute_input":"2022-07-18T22:55:10.375659Z","iopub.status.idle":"2022-07-18T22:55:33.195867Z","shell.execute_reply.started":"2022-07-18T22:55:10.375539Z","shell.execute_reply":"2022-07-18T22:55:33.194447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom pathlib import Path\nimport fastai\nfrom fastai.vision.all import *\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom sklearn.model_selection import ShuffleSplit","metadata":{"execution":{"iopub.status.busy":"2022-07-18T22:55:33.198360Z","iopub.execute_input":"2022-07-18T22:55:33.199030Z","iopub.status.idle":"2022-07-18T22:55:36.370808Z","shell.execute_reply.started":"2022-07-18T22:55:33.198984Z","shell.execute_reply":"2022-07-18T22:55:36.369759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(Path(\"../input/xray-body-images-in-png-unifesp-competion/\", \"train_df.csv\"))\ndf_train['Target'] = df_train['Target'].apply(lambda x: x.strip())\nX = df_train\ny = df_train['Target']\nsss = ShuffleSplit(n_splits=1, test_size=.4, random_state=42)\ntrain_idx, val_idx = next(sss.split(X, y))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T22:55:36.372383Z","iopub.execute_input":"2022-07-18T22:55:36.373211Z","iopub.status.idle":"2022-07-18T22:55:36.448219Z","shell.execute_reply.started":"2022-07-18T22:55:36.373172Z","shell.execute_reply":"2022-07-18T22:55:36.447327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['is_valid'] = False\ndf_train.loc[val_idx, 'is_valid'] = True\n\ndls = ImageDataLoaders.from_df(df_train, \n                               fn_col = 'image_path',\n                               label_col = 'Target',\n                               label_delim = ' ',\n                               valid_col='is_valid',\n                               path = \"../input/xray-body-images-in-png-unifesp-competion/\",\n                               item_tfms=Resize(224), \n                               batch_tfms=aug_transforms(size=224))\n\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T22:55:36.453905Z","iopub.execute_input":"2022-07-18T22:55:36.456213Z","iopub.status.idle":"2022-07-18T22:55:46.596688Z","shell.execute_reply.started":"2022-07-18T22:55:36.456174Z","shell.execute_reply":"2022-07-18T22:55:46.595731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_macro = F1ScoreMulti(thresh=0.5, average='macro')\nf1_samples = F1ScoreMulti(thresh=0.5, average='samples')\nf1_micro = F1ScoreMulti(thresh=0.5, average='micro')\nf1_weighted = F1ScoreMulti(thresh=0.5, average='weighted')\nlearn = vision_learner(dls, resnet50, \n                    metrics=[partial(accuracy_multi, thresh=0.5), f1_macro, f1_samples, f1_micro, f1_weighted], \n                    model_dir='/kaggle/working/model')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T22:55:46.597732Z","iopub.execute_input":"2022-07-18T22:55:46.598098Z","iopub.status.idle":"2022-07-18T22:55:58.509927Z","shell.execute_reply.started":"2022-07-18T22:55:46.598063Z","shell.execute_reply":"2022-07-18T22:55:58.508875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T22:55:58.514632Z","iopub.execute_input":"2022-07-18T22:55:58.517232Z","iopub.status.idle":"2022-07-18T22:57:21.600691Z","shell.execute_reply.started":"2022-07-18T22:55:58.517191Z","shell.execute_reply":"2022-07-18T22:57:21.599605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(100, lr[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T22:57:21.605561Z","iopub.execute_input":"2022-07-18T22:57:21.608277Z","iopub.status.idle":"2022-07-18T23:11:57.362527Z","shell.execute_reply.started":"2022-07-18T22:57:21.608235Z","shell.execute_reply":"2022-07-18T23:11:57.361342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:11:57.364531Z","iopub.execute_input":"2022-07-18T23:11:57.365152Z","iopub.status.idle":"2022-07-18T23:11:58.800048Z","shell.execute_reply.started":"2022-07-18T23:11:57.365106Z","shell.execute_reply":"2022-07-18T23:11:58.795618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = Interpretation.from_learner(learn)\ninterp.plot_top_losses(9)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:11:58.801765Z","iopub.execute_input":"2022-07-18T23:11:58.802394Z","iopub.status.idle":"2022-07-18T23:12:04.769128Z","shell.execute_reply.started":"2022-07-18T23:11:58.802352Z","shell.execute_reply":"2022-07-18T23:12:04.768011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(Path(\"../input/xray-body-images-in-png-unifesp-competion/\", \"test_df.csv\"))\ntest_dl_df = dls.test_dl(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:12:04.773060Z","iopub.execute_input":"2022-07-18T23:12:04.773695Z","iopub.status.idle":"2022-07-18T23:12:04.806317Z","shell.execute_reply.started":"2022-07-18T23:12:04.773652Z","shell.execute_reply":"2022-07-18T23:12:04.805433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test_df = learn.get_preds(dl=test_dl_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:12:04.810028Z","iopub.execute_input":"2022-07-18T23:12:04.810298Z","iopub.status.idle":"2022-07-18T23:12:10.676954Z","shell.execute_reply.started":"2022-07-18T23:12:04.810271Z","shell.execute_reply":"2022-07-18T23:12:10.675941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_preds_from_preds(pred_tensor):\n    has_tsh = any(pred_tensor >= 0.5)\n    if has_tsh: \n        return learn.dls.vocab[pred_tensor >= 0.5]\n    else:\n        return learn.dls.vocab[pred_tensor.argmax()]\n\ndef get_labels_from_preds(items):\n    r = []\n    for item in items:\n        if(item.isdigit()):\n            r.append(int(item))\n    r = [str(i) for i in sorted(r)]\n    return \" \".join(r) if any(r) else \"-1\"\n\nlabelled_preds = [get_preds_from_preds(pred) for pred in preds_test_df[0]]\nstr_preds = [get_labels_from_preds(items) for items in labelled_preds]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:12:10.679125Z","iopub.execute_input":"2022-07-18T23:12:10.679593Z","iopub.status.idle":"2022-07-18T23:12:11.276439Z","shell.execute_reply.started":"2022-07-18T23:12:10.679549Z","shell.execute_reply":"2022-07-18T23:12:11.275510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['Target'] = str_preds\ndf_submission = df_test[['SOPInstanceUID', 'Target']]\nprint(df_submission.shape)\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:12:11.277833Z","iopub.execute_input":"2022-07-18T23:12:11.278176Z","iopub.status.idle":"2022-07-18T23:12:11.293330Z","shell.execute_reply.started":"2022-07-18T23:12:11.278138Z","shell.execute_reply":"2022-07-18T23:12:11.292376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T23:12:11.294775Z","iopub.execute_input":"2022-07-18T23:12:11.295366Z","iopub.status.idle":"2022-07-18T23:12:11.307445Z","shell.execute_reply.started":"2022-07-18T23:12:11.295326Z","shell.execute_reply":"2022-07-18T23:12:11.306656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}