{"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":"## **Background Removal using TRACER - State of the Art RGB Salient Object Detector**","metadata":{}},{"cell_type":"markdown","source":"Created a forked repo of TRACER, and made changes there itself, to output images with segmentation masks applied to the original images rather than just simple binary masks.\n\nDataset Generated: [Background Removed Happywhale Dataset](https://www.kaggle.com/adnanpen/background-removed-happywhale-dataset)\n\nAccordingly made changes to code from testing files to remove evaluation part.\n\nDataset used for background removal: [Cropped 512x512](https://www.kaggle.com/phalanx/whale2-cropped-dataset)\n\nOriginal TRACER repo: [TRACER](https://github.com/Karel911/TRACER)\n\nMy forked TRACER repo: [TRACER](https://github.com/adnan119/TRACER)\n\nFiles modified include: \n[trainer.py](https://github.com/adnan119/TRACER/blob/main/trainer.py) \n[dataloader.py](https://github.com/adnan119/TRACER/blob/main/dataloader.py)\n[main.py](https://github.com/adnan119/TRACER/blob/main/main.py)","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/adnan119/TRACER.git","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:18:55.577182Z","iopub.execute_input":"2022-03-11T05:18:55.577763Z","iopub.status.idle":"2022-03-11T05:18:58.001362Z","shell.execute_reply.started":"2022-03-11T05:18:55.577684Z","shell.execute_reply":"2022-03-11T05:18:58.000605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/Karel911/TRACER/releases/download/v1.0/TRACER-Efficient-7.pth","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:18:58.003251Z","iopub.execute_input":"2022-03-11T05:18:58.003466Z","iopub.status.idle":"2022-03-11T05:19:08.68436Z","shell.execute_reply.started":"2022-03-11T05:18:58.00344Z","shell.execute_reply":"2022-03-11T05:19:08.683597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv ./TRACER-Efficient-7.pth ./best_model.pth","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:19:08.686121Z","iopub.execute_input":"2022-03-11T05:19:08.686414Z","iopub.status.idle":"2022-03-11T05:19:09.373229Z","shell.execute_reply.started":"2022-03-11T05:19:08.68636Z","shell.execute_reply":"2022-03-11T05:19:09.372314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ./TRACER\n!mkdir ./results/\n!mkdir ./results/DUTS/\n!mkdir ./results/DUTS/TE7_0/","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:19:09.376918Z","iopub.execute_input":"2022-03-11T05:19:09.377514Z","iopub.status.idle":"2022-03-11T05:19:11.325452Z","shell.execute_reply.started":"2022-03-11T05:19:09.377478Z","shell.execute_reply":"2022-03-11T05:19:11.32456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv ../best_model.pth ./results/DUTS/TE7_0/","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:19:11.328852Z","iopub.execute_input":"2022-03-11T05:19:11.329085Z","iopub.status.idle":"2022-03-11T05:19:12.021734Z","shell.execute_reply.started":"2022-03-11T05:19:11.329055Z","shell.execute_reply":"2022-03-11T05:19:12.020749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Imports </h1>","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport math\nimport copy\nimport time\nimport random\nimport warnings\nimport shutil\nfrom pathlib import Path\n\n# For data manipulation\nimport numpy as np\nimport pandas as pd\nfrom PIL import *\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:41:56.083337Z","iopub.execute_input":"2022-03-12T10:41:56.083969Z","iopub.status.idle":"2022-03-12T10:41:57.783389Z","shell.execute_reply.started":"2022-03-12T10:41:56.083878Z","shell.execute_reply":"2022-03-12T10:41:57.782679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Segmenting the training set**","metadata":{}},{"cell_type":"code","source":"#segment test images \n!python main.py test --exp_num 0 --arch 7 --img_size 512 --batch_size 16 --dataset \"DUTS\" --save_map True --data_path ../../input/whale2-cropped-dataset/cropped_train_images/cropped_train_images/","metadata":{"execution":{"iopub.status.busy":"2022-03-11T07:24:44.737216Z","iopub.execute_input":"2022-03-11T07:24:44.737499Z","iopub.status.idle":"2022-03-11T10:31:42.112012Z","shell.execute_reply.started":"2022-03-11T07:24:44.737467Z","shell.execute_reply":"2022-03-11T10:31:42.111133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create zipfile \nshutil.make_archive('./seg_img', 'zip', './seg_img')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T10:31:42.114284Z","iopub.execute_input":"2022-03-11T10:31:42.114586Z","iopub.status.idle":"2022-03-11T10:38:08.731499Z","shell.execute_reply.started":"2022-03-11T10:31:42.114543Z","shell.execute_reply":"2022-03-11T10:38:08.73021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-03-11T10:43:22.42136Z","iopub.execute_input":"2022-03-11T10:43:22.421643Z","iopub.status.idle":"2022-03-11T10:43:23.246517Z","shell.execute_reply.started":"2022-03-11T10:43:22.421613Z","shell.execute_reply":"2022-03-11T10:43:23.245685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg = os.listdir(\"./seg_img/\") # os.listdir(\"../input/background-removed-happywhale-dataset/seg_img\")\nprint(len(seg))","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:43:36.096564Z","iopub.execute_input":"2022-03-12T10:43:36.097029Z","iopub.status.idle":"2022-03-12T10:43:36.858061Z","shell.execute_reply.started":"2022-03-12T10:43:36.096992Z","shell.execute_reply":"2022-03-12T10:43:36.857233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_num = 17009 #sample test image index\nimg_name = Path(seg[img_num]).stem","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:43:40.641605Z","iopub.execute_input":"2022-03-12T10:43:40.642167Z","iopub.status.idle":"2022-03-12T10:43:40.648898Z","shell.execute_reply.started":"2022-03-12T10:43:40.642127Z","shell.execute_reply":"2022-03-12T10:43:40.648090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '../input/whale2-cropped-dataset/cropped_train_images/cropped_train_images/'\npil_tes = Image.open(test_dir + img_name + \".jpg\")\ndisplay(pil_tes)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:44:10.916563Z","iopub.execute_input":"2022-03-12T10:44:10.917324Z","iopub.status.idle":"2022-03-12T10:44:11.081898Z","shell.execute_reply.started":"2022-03-12T10:44:10.917277Z","shell.execute_reply":"2022-03-12T10:44:11.079506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pil_im = Image.open(\"./seg_img/\" + img_name + \".png\") # Image.open(\"../input/background-removed-happywhale-dataset/seg_img/\" + img_name + \".png\")\ndisplay(pil_im)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T10:44:36.730981Z","iopub.execute_input":"2022-03-12T10:44:36.731686Z","iopub.status.idle":"2022-03-12T10:44:36.803930Z","shell.execute_reply.started":"2022-03-12T10:44:36.731643Z","shell.execute_reply":"2022-03-12T10:44:36.803225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r './seg_img' #remove the folder containing the segmented images for saving the zipfile","metadata":{"execution":{"iopub.status.busy":"2022-03-11T11:10:18.588611Z","iopub.execute_input":"2022-03-11T11:10:18.589301Z","iopub.status.idle":"2022-03-11T11:10:21.228246Z","shell.execute_reply.started":"2022-03-11T11:10:18.589261Z","shell.execute_reply":"2022-03-11T11:10:21.227335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}