{"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":"# Motivation\nTrying pure, simple and straigntforward fastai v2 process.\nFastai2 (v.2.6.3) full path from training to testing and submission, with the augmentation during training.","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\nimport pandas as pd\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:33:33.911418Z","iopub.execute_input":"2022-05-26T07:33:33.912588Z","iopub.status.idle":"2022-05-26T07:33:37.896752Z","shell.execute_reply.started":"2022-05-26T07:33:33.912445Z","shell.execute_reply":"2022-05-26T07:33:37.895715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Training-Validation data loader","metadata":{}},{"cell_type":"code","source":"train_path='../input/histopathologic-cancer-detection/train/'\ntrain_df = pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')\n# for interactive DEBUG: reduce amount of train images : train_df = train_df[:1024]\n\nbatch_tfms = [ *aug_transforms(max_rotate=0, max_zoom=1., max_warp=0), Normalize.from_stats(*imagenet_stats) ]\n\ndls = ImageDataLoaders.from_df(train_df, path=train_path, suff='.tif', \n      batch_tfms=batch_tfms, bs=128, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:33:37.898746Z","iopub.execute_input":"2022-05-26T07:33:37.899069Z","iopub.status.idle":"2022-05-26T07:33:38.572459Z","shell.execute_reply.started":"2022-05-26T07:33:37.899025Z","shell.execute_reply":"2022-05-26T07:33:38.571790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try this to see dataloader working\n\n# dls.train.show_batch(max_n=12)\n# dls.valid.show_batch(max_n=12)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-26T07:34:30.272417Z","iopub.execute_input":"2022-05-26T07:34:30.272864Z","iopub.status.idle":"2022-05-26T07:34:31.322345Z","shell.execute_reply.started":"2022-05-26T07:34:30.272826Z","shell.execute_reply":"2022-05-26T07:34:31.321667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load pre-trained model and fine tune it","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, densenet121, path='.', \n    loss_func=FocalLoss(),  \n    metrics=[BalancedAccuracy(), RocAucBinary()] ) # this dataset is unbalanced (40/60)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:34:54.134553Z","iopub.execute_input":"2022-05-26T07:34:54.135173Z","iopub.status.idle":"2022-05-26T07:34:58.182485Z","shell.execute_reply.started":"2022-05-26T07:34:54.135134Z","shell.execute_reply":"2022-05-26T07:34:58.181791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(4)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:35:05.586732Z","iopub.execute_input":"2022-05-26T07:35:05.587005Z","iopub.status.idle":"2022-05-26T07:40:10.655183Z","shell.execute_reply.started":"2022-05-26T07:35:05.586968Z","shell.execute_reply":"2022-05-26T07:40:10.653991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T15:42:10.687293Z","iopub.execute_input":"2022-05-25T15:42:10.687644Z","iopub.status.idle":"2022-05-25T15:42:11.406419Z","shell.execute_reply.started":"2022-05-25T15:42:10.687595Z","shell.execute_reply":"2022-05-25T15:42:11.405648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Testing data loader","metadata":{}},{"cell_type":"code","source":"test_path='../input/histopathologic-cancer-detection/test/'\ntest_df = pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')\n# for interactive DEBUG: test_df = test_df[:12]\n\n# new loader for the sake of path, which if different from the training path\ntdls = ImageDataLoaders.from_df(test_df, path=test_path, suff='.tif',\n       batch_tfms=batch_tfms, shuffle=False)\ntst_dl = tdls.test_dl(test_df) ","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:41:05.173321Z","iopub.execute_input":"2022-05-26T07:41:05.173747Z","iopub.status.idle":"2022-05-26T07:41:05.393599Z","shell.execute_reply.started":"2022-05-26T07:41:05.173699Z","shell.execute_reply":"2022-05-26T07:41:05.392422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try it working !!! here I need to make sure aug is working, seems like not\n# tst_dl.show_batch(max_n=12)","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-26T07:41:21.545076Z","iopub.execute_input":"2022-05-26T07:41:21.545912Z","iopub.status.idle":"2022-05-26T07:41:21.549779Z","shell.execute_reply.started":"2022-05-26T07:41:21.545869Z","shell.execute_reply":"2022-05-26T07:41:21.548904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test and output Submission file","metadata":{}},{"cell_type":"code","source":"preds, y = learn.get_preds(dl=tst_dl)\n\npreds_f1 = torch.softmax(preds, dim=1)[:, 1]\nlb_df = pd.DataFrame({ 'label' : preds_f1 })\ntest_df.label = lb_df.label\ntest_df.to_csv(f'submission_HST.csv', header=True, index=False)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:45:54.707258Z","iopub.execute_input":"2022-05-26T07:45:54.707744Z","iopub.status.idle":"2022-05-26T07:45:55.765658Z","shell.execute_reply.started":"2022-05-26T07:45:54.707711Z","shell.execute_reply":"2022-05-26T07:45:55.764419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Have a nice day!\nWas it easy? Upvote and comment, please :)","metadata":{}}]}