{"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":"# Concept borrowed from https://www.kaggle.com/code/analokamus/a-fast-tile-generation. \n# Creates a dataset of image tiles and labels. Dumps image tiles and corresponding labels in pickle files.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-26T07:02:43.116343Z","iopub.execute_input":"2022-07-26T07:02:43.116785Z","iopub.status.idle":"2022-07-26T07:02:49.748138Z","shell.execute_reply.started":"2022-07-26T07:02:43.116751Z","shell.execute_reply":"2022-07-26T07:02:49.746764Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom pathlib import Path\nimport skimage.io as io\nimport cv2\nimport matplotlib.pyplot as plt\nimport os\nimport pickle\nimport gc","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:07.535488Z","iopub.execute_input":"2022-08-29T16:03:07.53635Z","iopub.status.idle":"2022-08-29T16:03:08.432574Z","shell.execute_reply.started":"2022-08-29T16:03:07.536252Z","shell.execute_reply":"2022-08-29T16:03:08.431162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_tiles(img, tile_size, num_tiles):\n    '''\n    img: np.ndarray with dtype np.uint8 and shape (width, height, channel)\n    '''\n    w, h, ch = img.shape\n    pad0, pad1 = (tile_size - w%tile_size) % tile_size, (tile_size - h%tile_size) % tile_size\n    padding = [[pad0//2, pad0-pad0//2], [pad1//2, pad1-pad1//2], [0, 0]]\n    img = np.pad(img, padding, mode='constant', constant_values=255)\n    img = img.reshape(img.shape[0]//tile_size, tile_size, img.shape[1]//tile_size, tile_size, ch)\n    img = img.transpose(0, 2, 1, 3, 4).reshape(-1, tile_size, tile_size, ch)\n    if len(img) < num_tiles: # pad images so that the output shape be the same\n        padding = [[0, num_tiles-len(img)], [0, 0], [0, 0], [0, 0]]\n        img = np.pad(img, padding, mode='constant', constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0], -1).sum(-1))[:num_tiles] # pick up Top N dark tiles\n    img = img[idxs]\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:08.434937Z","iopub.execute_input":"2022-08-29T16:03:08.435415Z","iopub.status.idle":"2022-08-29T16:03:08.446324Z","shell.execute_reply.started":"2022-08-29T16:03:08.43537Z","shell.execute_reply":"2022-08-29T16:03:08.445446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = list(Path('../input/mayo-clinic-strip-ai/train').glob('*.tif'))\nlen(image_paths)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:08.447606Z","iopub.execute_input":"2022-08-29T16:03:08.451271Z","iopub.status.idle":"2022-08-29T16:03:08.648804Z","shell.execute_reply.started":"2022-08-29T16:03:08.451218Z","shell.execute_reply":"2022-08-29T16:03:08.647627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(img_path, resize_factor=16):\n    img = io.imread(img_path)\n    img = cv2.resize(img, dsize=None, fx=1/resize_factor, fy=1/resize_factor)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:08.652Z","iopub.execute_input":"2022-08-29T16:03:08.652766Z","iopub.status.idle":"2022-08-29T16:03:08.65875Z","shell.execute_reply.started":"2022-08-29T16:03:08.652723Z","shell.execute_reply":"2022-08-29T16:03:08.657618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = load_image(image_paths[1])\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:08.660558Z","iopub.execute_input":"2022-08-29T16:03:08.661351Z","iopub.status.idle":"2022-08-29T16:03:45.6451Z","shell.execute_reply.started":"2022-08-29T16:03:08.661309Z","shell.execute_reply":"2022-08-29T16:03:45.644132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_size = 256\nnum_tiles = 16\ntiles = make_tiles(image,tile_size=tile_size, num_tiles=num_tiles)\ntiles.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:45.646444Z","iopub.execute_input":"2022-08-29T16:03:45.646759Z","iopub.status.idle":"2022-08-29T16:03:45.713539Z","shell.execute_reply.started":"2022-08-29T16:03:45.64673Z","shell.execute_reply":"2022-08-29T16:03:45.712464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for tile in tiles:\n    plt.imshow(tile)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:45.71463Z","iopub.execute_input":"2022-08-29T16:03:45.714953Z","iopub.status.idle":"2022-08-29T16:03:48.267664Z","shell.execute_reply.started":"2022-08-29T16:03:45.714925Z","shell.execute_reply":"2022-08-29T16:03:48.266481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:03:48.269171Z","iopub.execute_input":"2022-08-29T16:03:48.269525Z","iopub.status.idle":"2022-08-29T16:03:48.444Z","shell.execute_reply.started":"2022-08-29T16:03:48.269492Z","shell.execute_reply":"2022-08-29T16:03:48.442947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize image tiles\ndef normalize_image(img,new_max,new_min):\n    min_per_img_per_channel = np.min(img,axis=(0,1),keepdims=True)\n    max_per_img_per_channel = np.max(img,axis=(0,1),keepdims=True)\n    new_max_minus_new_min = new_max - new_min\n    max_minus_min = max_per_img_per_channel - min_per_img_per_channel\n    divide_new_max_minus_new_min_by_max_minus_min = new_max_minus_new_min/max_minus_min\n    img_1 = (img - min_per_img_per_channel)*divide_new_max_minus_new_min_by_max_minus_min+new_min\n    img_1[np.isnan(img_1)] = img[np.isnan(img_1)]\n    img = img_1.astype('uint8')\n#     img=(img/255).astype('float16')\n    del img_1\n    del min_per_img_per_channel\n    del max_per_img_per_channel\n    del new_max_minus_new_min\n    del divide_new_max_minus_new_min_by_max_minus_min\n    gc.collect()\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:08:48.220975Z","iopub.execute_input":"2022-08-29T16:08:48.222226Z","iopub.status.idle":"2022-08-29T16:08:48.23138Z","shell.execute_reply.started":"2022-08-29T16:08:48.222184Z","shell.execute_reply":"2022-08-29T16:08:48.23Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_tile_list = []\nimg_label_list=[]\nfor image_path in image_paths:\n    img = load_image(image_path)\n    img = normalize_image(img,new_max=255,new_min=0)\n    img_tiles = make_tiles(img, tile_size=tile_size, num_tiles=num_tiles)\n    image_name = os.path.basename(image_path)\n    image_id = image_name[0:image_name.index('.')]\n    image_label = df[df['image_id']==image_id]['label'].values[0]\n    img_tile_list.append(img_tiles)\n    img_label_list.append(image_label)\n\nimg_tile_arr = np.array(img_tile_list)\nimg_label_arr = np.array(img_label_list)\nfilehandler = open(\"image_tiles\",\"wb\")\npickle.dump(img_tile_arr,filehandler)\nfilehandler.close()\nfilehandler = open(\"image_labels\",\"wb\")\npickle.dump(img_label_arr,filehandler)\nfilehandler.close()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-29T16:08:51.863272Z","iopub.execute_input":"2022-08-29T16:08:51.863662Z","iopub.status.idle":"2022-08-29T16:09:13.005592Z","shell.execute_reply.started":"2022-08-29T16:08:51.86363Z","shell.execute_reply":"2022-08-29T16:09:13.003936Z"},"trusted":true},"execution_count":null,"outputs":[]}]}