{"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":"import pandas as pd\nimport PIL\nfrom PIL import Image\nimport glob\nimport numpy as np\nimport random\nimport matplotlib.pyplot as plt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-07T08:24:09.846486Z","iopub.execute_input":"2023-11-07T08:24:09.846919Z","iopub.status.idle":"2023-11-07T08:24:09.853732Z","shell.execute_reply.started":"2023-11-07T08:24:09.846885Z","shell.execute_reply":"2023-11-07T08:24:09.852599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dir='/kaggle/input/UBC-OCEAN/train_thumbnails'\nimage_list= sorted(glob.glob('/kaggle/input/UBC-OCEAN/train_thumbnails/*.png'))\ndef get_file_path(image_id):\n    return f'{img_dir}/{image_id}_thumbnail.png'","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:07:38.207672Z","iopub.execute_input":"2023-11-07T08:07:38.208108Z","iopub.status.idle":"2023-11-07T08:07:38.222332Z","shell.execute_reply.started":"2023-11-07T08:07:38.208077Z","shell.execute_reply":"2023-11-07T08:07:38.221084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ndf['file_path']=df['image_id'].apply(get_file_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:07:40.057112Z","iopub.execute_input":"2023-11-07T08:07:40.058093Z","iopub.status.idle":"2023-11-07T08:07:40.070732Z","shell.execute_reply.started":"2023-11-07T08:07:40.058057Z","shell.execute_reply":"2023-11-07T08:07:40.0697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['file_path'].isin(image_list).unique()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:03:51.264738Z","iopub.execute_input":"2023-11-07T08:03:51.265447Z","iopub.status.idle":"2023-11-07T08:03:51.274424Z","shell.execute_reply.started":"2023-11-07T08:03:51.265402Z","shell.execute_reply":"2023-11-07T08:03:51.273191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -r /kaggle/working/train_images/*\n!mkdir -p /kaggle/working/train_images\nfor label in df.label.unique():\n    !mkdir -p /kaggle/working/train_images/{label}\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-07T06:44:30.176716Z","iopub.execute_input":"2023-11-07T06:44:30.177089Z","iopub.status.idle":"2023-11-07T06:44:36.835426Z","shell.execute_reply.started":"2023-11-07T06:44:30.177059Z","shell.execute_reply":"2023-11-07T06:44:36.833824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HGSC=df.loc[df['label']=='HGSC']\nLGSC=df.loc[df['label']=='LGSC']\nEC=df.loc[df['label']=='EC']\nCC=df.loc[df['label']=='CC']\nMC=df.loc[df['label']=='MC']\ndisplay(HGSC,CC,EC,MC,LGSC)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:08:01.77254Z","iopub.execute_input":"2023-11-07T08:08:01.77298Z","iopub.status.idle":"2023-11-07T08:08:01.851618Z","shell.execute_reply.started":"2023-11-07T08:08:01.772949Z","shell.execute_reply":"2023-11-07T08:08:01.850536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# borrowed from: https://www.kaggle.com/code/iafoss/panda-16x128x128-tiles\ndef tile(img,sz=224,N=4):\n    result = []\n    shape = img.shape\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                constant_values=255)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    if len(img) < N:\n        img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n    img = img[idxs]\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:13:05.980098Z","iopub.execute_input":"2023-11-07T08:13:05.981009Z","iopub.status.idle":"2023-11-07T08:13:05.991067Z","shell.execute_reply.started":"2023-11-07T08:13:05.98097Z","shell.execute_reply":"2023-11-07T08:13:05.990021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://github.com/darraghdog/rsna/blob/a97018a7b7ec920425189c7e37c1128dd9cb0158/scripts/resnext101v12/trainorig.py#L139\ndef autocrop(image, threshold=0):\n    \"\"\"Crops any edges below or equal to threshold\n    Crops blank image to 1x1.\n    Returns cropped image.\n    https://stackoverflow.com/questions/13538748/crop-black-edges-with-opencv\n    \"\"\"\n    if len(image.shape) == 3:\n        flatImage = np.max(image, 2)\n    else:\n        flatImage = image\n    rows = np.where(np.max(flatImage, 0) > threshold)[0]\n    cols = np.where(np.max(flatImage, 1) > threshold)[0]\n    image = image[cols[0]: cols[-1] + 1, rows[0]: rows[-1] + 1]\n    #logger.info(image.shape)\n    sqside = max(image.shape)\n    imageout = np.zeros((sqside, sqside, 3), dtype = 'uint8')\n    imageout[:image.shape[0], :image.shape[1],:] = image.copy()\n    return imageout","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:20:17.736226Z","iopub.execute_input":"2023-11-07T08:20:17.737114Z","iopub.status.idle":"2023-11-07T08:20:17.746579Z","shell.execute_reply.started":"2023-11-07T08:20:17.737076Z","shell.execute_reply":"2023-11-07T08:20:17.745451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=HGSC['file_path'].iloc[2]\nfix_size=224\n# label=\nprint(path)\n# if path in image_list:\nimage=np.array(Image.open(path))\nimage= autocrop(image, threshold=0)\ndata=tile(image,1024,4)[2]\nh,w= data.shape[0],data.shape[1]\ny_coord,x_coord=random.randint(0, h),random.randint(0,w)\nimage_=[]\nif y_coord + fix_size <= h and x_coord+fix_size <= w:\n    crop_image=data[y_coord:y_coord+fix_size,x_coord:x_coord+fix_size]\n    if len(np.unique(crop_image))>2:\n        image_.append(crop_image)\nprint(image_)\nplt.imshow(data)\n# for image in data:\n#     print(image.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T08:34:05.070693Z","iopub.execute_input":"2023-11-07T08:34:05.071083Z","iopub.status.idle":"2023-11-07T08:34:06.439509Z","shell.execute_reply.started":"2023-11-07T08:34:05.071052Z","shell.execute_reply":"2023-11-07T08:34:06.438549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}