{"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":"# Based on @sonujha090 script --- https://www.kaggle.com/code/sonujha090/fastai-inference-ubc-ovarian-cancer-subtype\nfrom fastai.vision.all import *\nfrom fastai.torch_core import set_seed\nImage.MAX_IMAGE_PIXELS = 20641052620\n\nimport os\nimport torch\nimport numpy\nimport pandas as pd\nfrom sklearn.utils.class_weight import compute_class_weight\n\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-03T22:41:52.218242Z","iopub.execute_input":"2023-11-03T22:41:52.218531Z","iopub.status.idle":"2023-11-03T22:41:58.157209Z","shell.execute_reply.started":"2023-11-03T22:41:52.218506Z","shell.execute_reply":"2023-11-03T22:41:58.156316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fix random seed\nSEED = 123\nset_seed(SEED, reproducible=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.158722Z","iopub.execute_input":"2023-11-03T22:41:58.159005Z","iopub.status.idle":"2023-11-03T22:41:58.168098Z","shell.execute_reply.started":"2023-11-03T22:41:58.15898Z","shell.execute_reply":"2023-11-03T22:41:58.167314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = '/kaggle/input/UBC-OCEAN'\ntrain_path = '/kaggle/input/UBC-OCEAN/train.csv'\n\ndf = pd.read_csv(train_path)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.16913Z","iopub.execute_input":"2023-11-03T22:41:58.169413Z","iopub.status.idle":"2023-11-03T22:41:58.198233Z","shell.execute_reply.started":"2023-11-03T22:41:58.16939Z","shell.execute_reply":"2023-11-03T22:41:58.197325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3>Training (on the entire dataset)\n<h4> You can skip this part and go directly to Inference part\n<h5> Uncomment the cells below for training","metadata":{}},{"cell_type":"code","source":"'''\nclasses = np.unique(df['label'])\n\ndef get_file_path(image_id):\n    thubs_path = f\"{base_path}/train_thumbnails/{image_id}_thumbnail.png\"\n    alternative_path = f\"{base_path}/train_images/{image_id}.png\"\n    \n    if os.path.exists(thubs_path):\n        return thubs_path\n    else:\n        return alternative_path\n        \ndf['image_id'] = df['image_id'].apply(get_file_path)\n\ndf['is_valid'] = False\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.200228Z","iopub.execute_input":"2023-11-03T22:41:58.200513Z","iopub.status.idle":"2023-11-03T22:41:58.20674Z","shell.execute_reply.started":"2023-11-03T22:41:58.20049Z","shell.execute_reply":"2023-11-03T22:41:58.205818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ntfms =aug_transforms(\n    mult = 1,\n    do_flip = True,\n    flip_vert = True,\n    max_rotate = 10,\n    min_zoom = 1,\n    max_zoom = 1.1,\n    max_lighting = 0.2,\n    max_warp = 0.2,\n    p_affine = 0.75,\n    p_lighting = 0.75,\n    xtra_tfms = None,\n    size = None,\n    mode = \"bilinear\",\n    pad_mode = \"reflection\",\n    align_corners = True,\n    batch = False,\n    min_scale = 1,\n)\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.207815Z","iopub.execute_input":"2023-11-03T22:41:58.208073Z","iopub.status.idle":"2023-11-03T22:41:58.216019Z","shell.execute_reply.started":"2023-11-03T22:41:58.208044Z","shell.execute_reply":"2023-11-03T22:41:58.215185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''mean_variance = [[0.42423851, 0.39279015, 0.43824578], \n                 [0.69582516, 0.58197726, 0.74412118]]\n\n#Total Mean (R, G, B): [0.42423851 0.39279015 0.43824578]\n#Total Variance (R, G, B): [0.69582516 0.58197726 0.74412118]\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.21703Z","iopub.execute_input":"2023-11-03T22:41:58.217315Z","iopub.status.idle":"2023-11-03T22:41:58.224899Z","shell.execute_reply.started":"2023-11-03T22:41:58.217266Z","shell.execute_reply":"2023-11-03T22:41:58.2241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nBS = 80\nRESIZE_TO_DIM = 224\n\ndblock = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    get_x=ColReader('image_id'),\n    get_y = ColReader('label'),\n    splitter = ColSplitter(col='is_valid'),\n    item_tfms = Resize(RESIZE_TO_DIM),\n    batch_tfms=[\n        *aug_transforms(), \n        Normalize.from_stats(*mean_variance),\n    ],\n)\n\ndls = dblock.dataloaders(df, bs=BS)\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.22609Z","iopub.execute_input":"2023-11-03T22:41:58.226368Z","iopub.status.idle":"2023-11-03T22:41:58.234517Z","shell.execute_reply.started":"2023-11-03T22:41:58.226345Z","shell.execute_reply":"2023-11-03T22:41:58.233567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nclasses = np.unique(df['label'])\ny = df['label']\n\nweights = compute_class_weight(class_weight='balanced',classes=classes,y=y)\nweights = torch.tensor(weights).float()\n\nwce = torch.nn.CrossEntropyLoss(weight=weights)\n\ndict(zip(classes, weights.numpy()))\n\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.23551Z","iopub.execute_input":"2023-11-03T22:41:58.235761Z","iopub.status.idle":"2023-11-03T22:41:58.243132Z","shell.execute_reply.started":"2023-11-03T22:41:58.235739Z","shell.execute_reply":"2023-11-03T22:41:58.24234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nlearn = vision_learner(\n    dls, efficientnet_b0,\n    loss_func=wce,\n    opt_func=ranger,\n)\n\nlearn.to_fp16()\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.244203Z","iopub.execute_input":"2023-11-03T22:41:58.244762Z","iopub.status.idle":"2023-11-03T22:41:58.255189Z","shell.execute_reply.started":"2023-11-03T22:41:58.244731Z","shell.execute_reply":"2023-11-03T22:41:58.254247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nN = 20\n\nlearn.unfreeze()\nlearn.freeze_to(-N) # Use only the last N layers for training\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.258655Z","iopub.execute_input":"2023-11-03T22:41:58.259211Z","iopub.status.idle":"2023-11-03T22:41:58.264462Z","shell.execute_reply.started":"2023-11-03T22:41:58.259187Z","shell.execute_reply":"2023-11-03T22:41:58.263667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nLR = 1e-3\n\nlearn.fit_flat_cos(n_epoch=9, lr=slice(LR)) #9 Epoche\n'''","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.265361Z","iopub.execute_input":"2023-11-03T22:41:58.265584Z","iopub.status.idle":"2023-11-03T22:41:58.274505Z","shell.execute_reply.started":"2023-11-03T22:41:58.265564Z","shell.execute_reply":"2023-11-03T22:41:58.273569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn.export('export_norm.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.275585Z","iopub.execute_input":"2023-11-03T22:41:58.275871Z","iopub.status.idle":"2023-11-03T22:41:58.281421Z","shell.execute_reply.started":"2023-11-03T22:41:58.275842Z","shell.execute_reply":"2023-11-03T22:41:58.280491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2>Inference","metadata":{}},{"cell_type":"code","source":"classes = np.unique(df['label'])\n\ndef get_file_test_path(image_id):\n    '''Pick the original image in case thumbnail is not available'''\n    \n    thubs_path = f\"{base_path}/test_thumbnails/{image_id}_thumbnail.png\"\n    alternative_path = f\"{base_path}/test_images/{image_id}.png\"\n    \n    if os.path.exists(thubs_path):\n        return thubs_path\n    else:\n        return alternative_path\n\nencode = {lbl: idx for idx, lbl in enumerate(classes)}\ndecode = {lbl: idx for idx, lbl in encode.items()}","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.282631Z","iopub.execute_input":"2023-11-03T22:41:58.282955Z","iopub.status.idle":"2023-11-03T22:41:58.290947Z","shell.execute_reply.started":"2023-11-03T22:41:58.282926Z","shell.execute_reply":"2023-11-03T22:41:58.290222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = load_learner('/kaggle/input/ubc-learner/export_norm.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.291923Z","iopub.execute_input":"2023-11-03T22:41:58.292165Z","iopub.status.idle":"2023-11-03T22:41:58.611034Z","shell.execute_reply.started":"2023-11-03T22:41:58.292144Z","shell.execute_reply":"2023-11-03T22:41:58.610222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv('/kaggle/input/UBC-OCEAN/sample_submission.csv')\nsample_copy = sample_df.copy()\n\nsample_copy['image_id'] = sample_df['image_id'].apply(get_file_test_path)\ntest_dl = learn.dls.test_dl(sample_copy)\n\npreds, _ = learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:58.612141Z","iopub.execute_input":"2023-11-03T22:41:58.61249Z","iopub.status.idle":"2023-11-03T22:41:59.290044Z","shell.execute_reply.started":"2023-11-03T22:41:58.612458Z","shell.execute_reply":"2023-11-03T22:41:59.289042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['label'] = preds.argmax(dim=-1).numpy()\nsample_df['label'] = sample_df['label'].map(decode)\n\nsample_df.to_csv('submission.csv',index=False)\n\nsample_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:59.2917Z","iopub.execute_input":"2023-11-03T22:41:59.29254Z","iopub.status.idle":"2023-11-03T22:41:59.313744Z","shell.execute_reply.started":"2023-11-03T22:41:59.292501Z","shell.execute_reply":"2023-11-03T22:41:59.312883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TODO: Try tta","metadata":{"execution":{"iopub.status.busy":"2023-11-03T22:41:59.315097Z","iopub.execute_input":"2023-11-03T22:41:59.31548Z","iopub.status.idle":"2023-11-03T22:41:59.319736Z","shell.execute_reply.started":"2023-11-03T22:41:59.315447Z","shell.execute_reply":"2023-11-03T22:41:59.318861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}