{"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":"!conda install /kaggle/input/pyvips-package/*.tar.bz2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport pandas as pd\nimport numpy as np\nimport math\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport pyvips\nimport warnings\nimport random\nimport tensorflow as tf\nprint(\"TensorFlow version:\", tf.__version__)\n\nfrom tqdm import tqdm\nfrom PIL import Image\nfrom random import randrange\n\nfrom pathlib import Path\nfrom glob import glob\n\nfrom skimage.exposure import is_low_contrast\nfrom scipy.ndimage import zoom, rotate\nfrom skimage.io import imread, imsave\n\nfrom collections import defaultdict\nfrom openslide import OpenSlide\n\nrandom.seed = 19","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:07:54.710712Z","iopub.execute_input":"2022-10-11T04:07:54.711361Z","iopub.status.idle":"2022-10-11T04:07:57.525568Z","shell.execute_reply.started":"2022-10-11T04:07:54.711232Z","shell.execute_reply":"2022-10-11T04:07:57.524159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path='../input/mayo-clinic-strip-ai/'\ntrain_path = data_path + 'train/'\ntrain_label_df = pd.read_csv(data_path + 'train.csv')\ntrain_images_data = glob(train_path + \"*\")\nprint(f\"Number of images in a training set: {len(train_images_data)}\")","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:07:57.528412Z","iopub.execute_input":"2022-10-11T04:07:57.529411Z","iopub.status.idle":"2022-10-11T04:07:57.550876Z","shell.execute_reply.started":"2022-10-11T04:07:57.529349Z","shell.execute_reply":"2022-10-11T04:07:57.549762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:07:58.974991Z","iopub.execute_input":"2022-10-11T04:07:58.975437Z","iopub.status.idle":"2022-10-11T04:07:58.996675Z","shell.execute_reply.started":"2022-10-11T04:07:58.975396Z","shell.execute_reply":"2022-10-11T04:07:58.994832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:07:59.224887Z","iopub.execute_input":"2022-10-11T04:07:59.225328Z","iopub.status.idle":"2022-10-11T04:07:59.235785Z","shell.execute_reply.started":"2022-10-11T04:07:59.225295Z","shell.execute_reply":"2022-10-11T04:07:59.234446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_info(images_data, label_df):\n    img_prop = defaultdict(list)    \n    for i, path in enumerate(images_data):\n\n        img_path = images_data[i]\n        slide = OpenSlide(img_path)    \n\n        big_dim = 'none'\n        max_min_dim_ratio = 1.0\n\n        img_width = slide.dimensions[0]\n        img_height = slide.dimensions[1]\n\n        if(img_width > img_height):\n            big_dim = 'width'\n            max_min_dim_ratio = round(img_width/img_height, 2)\n        elif(img_width < img_height):\n            big_dim = 'height'\n            max_min_dim_ratio = round(img_height/img_width, 2)\n\n        img_prop['image_id'].append(img_path[-12:-4])\n        img_prop['width'].append(img_width)\n        img_prop['height'].append(img_height)\n        img_prop['big_dim'].append(big_dim)\n        #img_prop['size'].append(round(os.path.getsize(img_path) / 1e6, 2))\n        img_prop['max_min_dim_ratio'].append(max_min_dim_ratio)\n        \n        #if(max_min_dim_ratio < 2.0):\n        #    split_size = round(max_min_dim_ratio)\n        #else:\n        #    split_size = math.floor(max_min_dim_ratio)\n        split_size = round(max_min_dim_ratio)\n        img_prop['split_size'].append(split_size)\n        img_prop['path'].append(img_path)\n        \n        img_info = pd.DataFrame(img_prop)\n        img_info.sort_values(by='image_id', inplace=True)\n        img_info.reset_index(inplace=True, drop=True)\n        img_info = img_info.merge(label_df, on='image_id')\n\n    return img_info","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:07:59.470349Z","iopub.execute_input":"2022-10-11T04:07:59.471366Z","iopub.status.idle":"2022-10-11T04:07:59.482747Z","shell.execute_reply.started":"2022-10-11T04:07:59.47132Z","shell.execute_reply":"2022-10-11T04:07:59.481499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_info = get_img_info(train_images_data, train_label_df)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:07:59.655085Z","iopub.execute_input":"2022-10-11T04:07:59.657477Z","iopub.status.idle":"2022-10-11T04:08:15.484595Z","shell.execute_reply.started":"2022-10-11T04:07:59.657379Z","shell.execute_reply":"2022-10-11T04:08:15.483042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_img_info['split_size'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:08:15.487252Z","iopub.execute_input":"2022-10-11T04:08:15.487771Z","iopub.status.idle":"2022-10-11T04:08:15.495126Z","shell.execute_reply.started":"2022-10-11T04:08:15.487726Z","shell.execute_reply":"2022-10-11T04:08:15.493737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_count = train_img_info['split_size'].sum()\nprint('total number of train images after split: ', train_images_count)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:08:15.497169Z","iopub.execute_input":"2022-10-11T04:08:15.497667Z","iopub.status.idle":"2022-10-11T04:08:15.509671Z","shell.execute_reply.started":"2022-10-11T04:08:15.497604Z","shell.execute_reply":"2022-10-11T04:08:15.508338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224\nIMG_CHANNELS = 3","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:08:15.512343Z","iopub.execute_input":"2022-10-11T04:08:15.512724Z","iopub.status.idle":"2022-10-11T04:08:15.519365Z","shell.execute_reply.started":"2022-10-11T04:08:15.512693Z","shell.execute_reply":"2022-10-11T04:08:15.518395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# function to check if the image contains useful information after splitting.\ndef get_score(img):\n    imgray = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)\n    ret, thresh = cv2.threshold(imgray, 127, 255, 0)\n    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n    return len(contours)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T04:08:15.520396Z","iopub.execute_input":"2022-10-11T04:08:15.521003Z","iopub.status.idle":"2022-10-11T04:08:15.533322Z","shell.execute_reply.started":"2022-10-11T04:08:15.520969Z","shell.execute_reply":"2022-10-11T04:08:15.532212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_tile(path, name, vips_tile, label):\n    img = vips_tile.numpy()\n    im = Image.fromarray(img)\n    im.save(path + name + '_0.tif')\n    im.rotate(90).save(path + name + '_1.tif')\n    \n    if(label == 'LAA'):\n        Image.fromarray(rotate(img, 135, reshape=False, mode='reflect')).save(path + name + '_2.tif')\n        im.rotate(180).save(path + name + '_3.tif')\n        Image.fromarray(rotate(img, 225, reshape=False, mode='reflect')).save(path + name + '_4.tif')\n    print(name, 'done saving a tile...')","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:52:15.04105Z","iopub.execute_input":"2022-10-11T05:52:15.041534Z","iopub.status.idle":"2022-10-11T05:52:15.049685Z","shell.execute_reply.started":"2022-10-11T05:52:15.041491Z","shell.execute_reply":"2022-10-11T05:52:15.048748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_save_tiles(row, last_img_index, path_tiles):    \n    image_id = row['image_id']\n    \n    width = row['width']\n    height = row['height']\n    big_dim = row['big_dim']\n    split_size = row['split_size']\n    input_train_path = row['path']\n    label = row['label']\n    center_id = row['center_id']\n    \n    n_across = 1\n    n_down = 1\n\n    vips_img = pyvips.Image.new_from_file(input_train_path, access='sequential')\n    \n    if(split_size == 1):\n        crop_width = width\n        crop_height = height\n    elif(big_dim == 'width'):\n        crop_width = width//split_size\n        crop_height = height\n        n_across = split_size\n    else:\n        crop_height = height//split_size\n        crop_width = width\n        n_down = split_size\n\n    for x in range(n_across):\n        for y in range(n_down):\n            vips_tile = None\n            if(split_size > 1):\n                vips_tile = vips_img.crop(x*crop_width, y*crop_height, crop_width, crop_height)\n            else:\n                vips_tile = vips_img\n            print(last_img_index, image_id, 'processing image with splits(', split_size, ')' , crop_width, ' X ', crop_height)            \n            vips_tile = vips_tile.thumbnail_image(IMG_SIZE, height=IMG_SIZE, size='force')\n            if(is_low_contrast(vips_tile)):\n#                 print('low contrast image')\n                continue\n            tile_name = image_id + '_' + str(x+y)\n            save_tile(path_tiles, tile_name, vips_tile, label)\n            \n            last_img_index += 1\n    \n    vips_img = None\n    return last_img_index","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:52:15.275333Z","iopub.execute_input":"2022-10-11T05:52:15.276453Z","iopub.status.idle":"2022-10-11T05:52:15.289087Z","shell.execute_reply.started":"2022-10-11T05:52:15.276398Z","shell.execute_reply":"2022-10-11T05:52:15.287682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_tiles():\n    path_tiles = 'output/train/tiles2/'\n    # Check whether the specified path exists or not\n    exists_tiles = os.path.exists(path_tiles)\n    \n    if(not exists_tiles):\n        print('creating folder', path_tiles)\n        os.makedirs(path_tiles)\n\n    last_img_index = 0\n    for ind, row in train_img_info.iterrows():\n#         if(ind != 2):\n#             continue\n        print('started processing image:', str(ind + 1))\n        last_img_index = split_save_tiles(row, last_img_index, path_tiles)\n    print('processed train images:', last_img_index)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:52:15.550096Z","iopub.execute_input":"2022-10-11T05:52:15.550566Z","iopub.status.idle":"2022-10-11T05:52:15.557913Z","shell.execute_reply.started":"2022-10-11T05:52:15.550526Z","shell.execute_reply":"2022-10-11T05:52:15.556949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_tiles()","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:52:15.930298Z","iopub.execute_input":"2022-10-11T05:52:15.931174Z","iopub.status.idle":"2022-10-11T05:52:31.389595Z","shell.execute_reply.started":"2022-10-11T05:52:15.931111Z","shell.execute_reply":"2022-10-11T05:52:31.38803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tiles = os.listdir('output/train/tiles2/')\n#os.listdir('output/train/tiles2/')","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:54:05.081521Z","iopub.execute_input":"2022-10-11T05:54:05.082004Z","iopub.status.idle":"2022-10-11T05:54:05.088573Z","shell.execute_reply.started":"2022-10-11T05:54:05.08196Z","shell.execute_reply":"2022-10-11T05:54:05.086586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def show_crop(path):\n#     image = Image.open(path)\n#     print(image.size)\n#     image = np.asarray(image)\n#     plt.figure()\n#     plt.imshow(image, cmap='gray')\n#     plt.colorbar()\n#     plt.grid(False)\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:54:05.311154Z","iopub.execute_input":"2022-10-11T05:54:05.311668Z","iopub.status.idle":"2022-10-11T05:54:05.317416Z","shell.execute_reply.started":"2022-10-11T05:54:05.311598Z","shell.execute_reply":"2022-10-11T05:54:05.31591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show_crop('output/train/tiles2/' + tiles[14])","metadata":{"execution":{"iopub.status.busy":"2022-10-11T05:54:05.630142Z","iopub.execute_input":"2022-10-11T05:54:05.630592Z","iopub.status.idle":"2022-10-11T05:54:05.636609Z","shell.execute_reply.started":"2022-10-11T05:54:05.630557Z","shell.execute_reply":"2022-10-11T05:54:05.635032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}