{"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":"**if you want to see Train & Sumbission Pipeline's result, click the link below here**\n\n1. Train_Pipeline result => https://www.kaggle.com/code/qcqced/strip-ai-efficient-net-regnet-train-pipeline\n2. Submission_Pipeline result => https://www.kaggle.com/code/qcqced/strip-ai-submission-pipeline ","metadata":{}},{"cell_type":"markdown","source":"Step 1. EDA\n\nStep 2. Convert Train Data to 1k(1024,1024) PNG (Use Rasterio Module)\n\n**Step 3. Convert 1k(1024,1024) PNG Data (Step 2's Data) to No Unnecessary Background Image  \n(Use Seam Carving to remove \"Unnecessary Background\") => Use Rasterio NOT PIL, PIL has potential making kaggle notebook kernel die issue**\n\n**(This Notebook is Step 3!! If you want to see Step 1 & Step 2, click the URL)** => https://www.kaggle.com/code/qcqced/strip-ai-eda-data-preprocessing-1k-png\n\n**(Step 3's Idea from @yu4u Thanks yu4u!!, And want to see more detail, click the link below here)** \n\n=> https://www.kaggle.com/code/ren4yu/mayo-clinic-removing-background-via-seam-carving\n\n=> https://github.com/yu4u/seam-carving","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport numpy as np \nimport pandas as pd \nimport os, sys, random, gc\nimport torch\nimport torch.nn as nn # neural network module\nimport torch.nn.functional as F # neural network module에서 자주 사용되는 함수\nimport torchvision\nimport matplotlib.pyplot as plt\nimport matplotlib as matp\nimport matplotlib.gridspec as gridspec \nimport cv2, math, shutil # OpenCV => cv2\nimport albumentations as Albu\nfrom torchvision import models\nfrom torchvision import transforms\nfrom albumentations.pytorch import ToTensorV2  \nfrom sklearn.model_selection import train_test_split\nfrom zipfile import ZipFile\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.metrics import roc_auc_score\nfrom tqdm.notebook import tqdm \nfrom transformers import get_cosine_schedule_with_warmup # 스케줄러\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-21T14:41:48.960026Z","iopub.execute_input":"2022-08-21T14:41:48.960521Z","iopub.status.idle":"2022-08-21T14:42:01.106002Z","shell.execute_reply.started":"2022-08-21T14:41:48.96039Z","shell.execute_reply":"2022-08-21T14:42:01.104528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3.1 Seam Carving Module Import \n!pip install git+https://github.com/li-plus/seam-carving.git@master","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:05.443809Z","iopub.execute_input":"2022-08-21T14:42:05.444496Z","iopub.status.idle":"2022-08-21T14:42:22.87776Z","shell.execute_reply.started":"2022-08-21T14:42:05.444456Z","shell.execute_reply":"2022-08-21T14:42:22.876545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_path = './convert_train_seam_carving'\nos.mkdir(dir_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:22.880052Z","iopub.execute_input":"2022-08-21T14:42:22.880497Z","iopub.status.idle":"2022-08-21T14:42:22.888385Z","shell.execute_reply.started":"2022-08-21T14:42:22.880456Z","shell.execute_reply":"2022-08-21T14:42:22.887223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 1. Data Upload & Check\n# Goal of Competition => 허혈성 뇌졸증의 원인이 되는 두 가지 혈전증을 병리적 이미지를 통해 분류\n# Evaluation of Competition => Binary Classification\n# CE => cardioembolic, 심인성 색전증\n# LAA => Large artery atherosclerosis, 큰동맥죽상경화증\ndata_path = '../input/mayo-clinic-strip-ai/'\n\nlabels = pd.read_csv(data_path + 'train.csv') # Train Data Set\ntest = pd.read_csv(data_path + 'test.csv')\nsubmission = pd.read_csv(data_path + 'sample_submission.csv')\n\nlabels, test, submission ","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:22.890051Z","iopub.execute_input":"2022-08-21T14:42:22.8904Z","iopub.status.idle":"2022-08-21T14:42:22.960901Z","shell.execute_reply.started":"2022-08-21T14:42:22.890369Z","shell.execute_reply":"2022-08-21T14:42:22.959133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 2. Target Data Check \nnum_img = 4\n\nCE = labels.loc[labels['label'] == \"CE\"]\nLAA = labels.loc[labels['label'] == \"LAA\"]\n\nlast_CE_img_id = CE['image_id'][-num_img:]\nlast_LAA_img_id = LAA['image_id'][-num_img:]\n\nCE_img_id = CE['image_id']\nLAA_img_id = LAA['image_id']\nlast_CE_img_id","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:22.963592Z","iopub.execute_input":"2022-08-21T14:42:22.964014Z","iopub.status.idle":"2022-08-21T14:42:22.983406Z","shell.execute_reply.started":"2022-08-21T14:42:22.963975Z","shell.execute_reply":"2022-08-21T14:42:22.982214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image Visualization Function => CE와 LAA는 어떤 차이가 있을까??\nimport rasterio\nfrom rasterio.enums import Resampling\nfrom rasterio.transform import Affine\n\nimage_scalar = 0.1\n\ndef img_show(img_ids, rows=2, cols=2): # diseases_name => string type\n    matp.rc('font', size=10)\n    plt.figure(figsize=(40,40))\n    grid = gridspec.GridSpec(rows, cols)\n    \n    for idx, img_id in enumerate(img_ids):\n        img_path = f'{data_path}/train/{img_id}.tif'\n        img = rasterio.open(img_path)\n        image = img.read(out_shape=(img.count, int(img.height * image_scalar), int(img.width * image_scalar)),\n                         resampling=Resampling.bilinear).transpose(1,2,0) # imshow() => (너비, 높이, 채널) 순으로 매개변수를 요구하기 때문에 Transpose 필요함\n        print(image.shape)\n        print(type(image))\n        ax = plt.subplot(grid[idx])\n        ax.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:22.985456Z","iopub.execute_input":"2022-08-21T14:42:22.986115Z","iopub.status.idle":"2022-08-21T14:42:23.070511Z","shell.execute_reply.started":"2022-08-21T14:42:22.986074Z","shell.execute_reply":"2022-08-21T14:42:23.069253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_show(last_CE_img_id)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_show(last_LAA_img_id)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seam_carving\nseam_carving.carve.MAX_MEAN_ENERGY = 10.0\n\nimg_ids = labels['image_id']\nimage_scalar = 1024\n\n# Step 3.2 Resize Train data Using Module with Rasterio Not PIL\n#image_scalar = 2048\ndata_path = '../input/mayo-clinic-strip-ai-competition-1k-png-data'\ndef seam_carve(img_ids):\n    for img_id in tqdm(img_ids):\n        try:\n            #img_path = f'{data_path}/train/{img_id}.tif'\n            img_path = f'{data_path}/{img_id}.png'\n\n            image = rasterio.open(img_path)\n            image = image.read(resampling=Resampling.bilinear).transpose(1,2,0)\n            image_h, image_w, _ = image.shape\n            image = seam_carving.resize(image, (image_w-512, image_h-512),\n                                        energy_mode='backward',\n                                        order=('width-first'),\n                                        keep_mask=None)\n            image = image.transpose(2,0,1)\n            with rasterio.open(f'./convert_train_seam_carving/{img_id}.png', 'w', driver='png', height = image.shape[1], width = image.shape[2], dtype = image.dtype, count=3) as images: # count => Image Channel 개수 (RGB의 경우 3개)\n                images.write(image)\n            del image\n            gc.collect()\n            \n        except OSError as e:\n            print(e)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:23.072159Z","iopub.execute_input":"2022-08-21T14:42:23.072523Z","iopub.status.idle":"2022-08-21T14:42:23.086999Z","shell.execute_reply.started":"2022-08-21T14:42:23.072492Z","shell.execute_reply":"2022-08-21T14:42:23.085816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3.3 Using Seam_Carving => Convert Train Image to 1K Png\nseam_carve(img_ids)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T14:42:23.08892Z","iopub.execute_input":"2022-08-21T14:42:23.089394Z","iopub.status.idle":"2022-08-21T14:49:10.984583Z","shell.execute_reply.started":"2022-08-21T14:42:23.089357Z","shell.execute_reply":"2022-08-21T14:49:10.982261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3.4 Converted PNG Image Check\ncarving_convert_img = './convert_train_seam_carving/006388_0.png'\ncarving_convert_img = cv2.imread(carving_convert_img)\ncarving_convert_img = cv2.cvtColor(carving_convert_img, cv2.COLOR_BGR2RGB)\n\nmatp.rc('font', size=10)\nplt.figure(figsize=(20,30))\ngrid = gridspec.GridSpec(1, 1)\n\nax = plt.subplot(grid[0])\nax.imshow(carving_convert_img)\n\ncarving_convert_img.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3.3 Convert PNG Image Data to Zip File\n!zip -r convert_train_seam_carving.zip ./*","metadata":{},"execution_count":null,"outputs":[]}]}