{"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 albumentations as A\nimport numpy as np\nimport pandas as pd\nimport os\nimport random\nimport torch\nimport glob\nimport cv2\nimport matplotlib.pyplot as plt\nimport zipfile\nimport itertools\nfrom tqdm.notebook import tqdm_notebook\nimport multiprocessing","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:29:53.123866Z","iopub.execute_input":"2022-07-25T20:29:53.124798Z","iopub.status.idle":"2022-07-25T20:29:57.274709Z","shell.execute_reply.started":"2022-07-25T20:29:53.124753Z","shell.execute_reply":"2022-07-25T20:29:57.273551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:29:57.277289Z","iopub.execute_input":"2022-07-25T20:29:57.278498Z","iopub.status.idle":"2022-07-25T20:29:57.284065Z","shell.execute_reply.started":"2022-07-25T20:29:57.278432Z","shell.execute_reply":"2022-07-25T20:29:57.283042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed=1234):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:29:57.286010Z","iopub.execute_input":"2022-07-25T20:29:57.286832Z","iopub.status.idle":"2022-07-25T20:29:57.303973Z","shell.execute_reply.started":"2022-07-25T20:29:57.286786Z","shell.execute_reply":"2022-07-25T20:29:57.302534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/hubmap-organ-segmentation\"\ntrain_df = pd.read_csv(os.path.join(BASE_PATH, \"train.csv\"))\ntest_df = pd.read_csv(os.path.join(BASE_PATH, \"test.csv\"))\nsubmission_df = pd.read_csv(os.path.join(BASE_PATH, \"sample_submission.csv\"))\ntrain_labels = glob.glob(os.path.join(BASE_PATH,\"train_annotations/*.json\"), recursive=True)\ntrain_images = glob.glob(os.path.join(BASE_PATH, \"train_images/*.tiff\"), recursive=True)\ntrain_label_map = dict((int(x[:-5].rsplit(\"/\",1)[-1]),x) for x in train_labels)\ntrain_image_map = dict((int(x[:-5].rsplit(\"/\",1)[-1]),x) for x in train_images)\ntest_images = glob.glob(os.path.join(BASE_PATH, \"test_images/*.tiff\"), recursive=True)\ntest_image_map = dict((int(x[:-5].rsplit(\"/\",1)[-1]),x) for x in test_images)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:29:57.306786Z","iopub.execute_input":"2022-07-25T20:29:57.307529Z","iopub.status.idle":"2022-07-25T20:29:57.778241Z","shell.execute_reply.started":"2022-07-25T20:29:57.307467Z","shell.execute_reply":"2022-07-25T20:29:57.776931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def populate_df(df, image_path_map, label_path_map):\n    df[\"image_path\"] = df[\"id\"].map(image_path_map)\n    df[\"label_path\"] = df[\"id\"].map(label_path_map)\n    df[\"age\"] = df[\"age\"]/100.0\n    df[\"sex\"] = df[\"sex\"].map({\"Male\":0, \"Female\":1})\n    df = df[[\"id\", \"organ\", \"data_source\", \"image_path\", \"label_path\", \"img_height\", \"img_width\", \"pixel_size\", \"tissue_thickness\", \"rle\", \"age\", \"sex\"]]\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:29:57.779848Z","iopub.execute_input":"2022-07-25T20:29:57.780491Z","iopub.status.idle":"2022-07-25T20:29:57.787424Z","shell.execute_reply.started":"2022-07-25T20:29:57.780445Z","shell.execute_reply":"2022-07-25T20:29:57.786546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"POPULATING ....\\n\")\ndf = populate_df(train_df, train_image_map, train_label_map)\ndisplay(df.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:29:57.788784Z","iopub.execute_input":"2022-07-25T20:29:57.789848Z","iopub.status.idle":"2022-07-25T20:29:57.844571Z","shell.execute_reply.started":"2022-07-25T20:29:57.789797Z","shell.execute_reply":"2022-07-25T20:29:57.843552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['image_path'][0]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:00.944527Z","iopub.execute_input":"2022-07-25T20:30:00.944900Z","iopub.status.idle":"2022-07-25T20:30:00.952214Z","shell.execute_reply.started":"2022-07-25T20:30:00.944869Z","shell.execute_reply":"2022-07-25T20:30:00.951098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If we have n augementation, we store n different images by appending the id with '-{i}' where 0<= i <= n-1","metadata":{}},{"cell_type":"code","source":"def rle_decode(mask_rle, shape, color=1):\n    s = np.array(mask_rle.split(), dtype=int)\n    start, lengths = s[0::2]-1, s[1::2]\n    ends = start + lengths\n    \n    # The image image is actually flattened since RLE is a 1D \"run\"\n    if len(shape)==3:\n        h, w, d = shape\n        img = np.zeros((h * w, d), dtype=np.float32)\n    else:\n        h, w = shape\n        img = np.zeros((h * w,), dtype=np.float32)\n    \n    for lo,hi in zip(start,ends):\n        img[lo:hi] = color\n    \n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:05.452890Z","iopub.execute_input":"2022-07-25T20:30:05.453287Z","iopub.status.idle":"2022-07-25T20:30:05.461298Z","shell.execute_reply.started":"2022-07-25T20:30:05.453253Z","shell.execute_reply":"2022-07-25T20:30:05.459956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_combinations(arr):\n    combs = []\n    for subset in itertools.combinations(arr, 1):\n        combs.append(subset)\n    return combs","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:06.725097Z","iopub.execute_input":"2022-07-25T20:30:06.728349Z","iopub.status.idle":"2022-07-25T20:30:06.733370Z","shell.execute_reply.started":"2022-07-25T20:30:06.728305Z","shell.execute_reply":"2022-07-25T20:30:06.732393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_aug_funcs():\n    resize_trans = A.Resize(IMG_SIZE, IMG_SIZE, p=1.0)\n    hor_trans = A.HorizontalFlip(p=1.0)\n    vert_trans = A.VerticalFlip(p=1.0)\n    rot_trans = A.RandomRotate90(p=1.0)\n    shift_trans = A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=20, p=1.0, \n                                                 border_mode=cv2.BORDER_REFLECT)\n    \n    optical_trans = A.OpticalDistortion(p=1.0)\n    grid_trans = A.GridDistortion(p=1.0)\n    piecewise_trans = A.IAAPiecewiseAffine(p=1.0)\n    \n    saturate_trans = A.HueSaturationValue(10,15,10)\n    clahe_trans = A.CLAHE(clip_limit=3)\n    brightness_trans = A.RandomBrightnessContrast()\n    \n    base_compose_array = [resize_trans]\n    l1 = [hor_trans, vert_trans, rot_trans, shift_trans]\n    l2 = [optical_trans, grid_trans, piecewise_trans]\n    l3 = [saturate_trans, clahe_trans, brightness_trans]\n    combs = get_all_combinations(l1) + get_all_combinations(l2) + get_all_combinations(l3) +  list(itertools.product(*[l1,l2])) + list(itertools.product(*[l1,l3])) + list(itertools.product(*[l2,l3]))+ list(itertools.product(*[l1,l2,l3]))\n    \n    compose_arrays = [A.Compose(base_compose_array)]+[A.Compose(base_compose_array+list(x)) for x in combs]\n    \n    return compose_arrays","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:08.116924Z","iopub.execute_input":"2022-07-25T20:30:08.117833Z","iopub.status.idle":"2022-07-25T20:30:08.129160Z","shell.execute_reply.started":"2022-07-25T20:30:08.117796Z","shell.execute_reply":"2022-07-25T20:30:08.127830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_mask(row):\n    image = cv2.imread(row.image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    mask = rle_decode(row.rle, (row.img_width, row.img_height), color=1)\n    return image, mask, row.id","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:11.471214Z","iopub.execute_input":"2022-07-25T20:30:11.471639Z","iopub.status.idle":"2022-07-25T20:30:11.477922Z","shell.execute_reply.started":"2022-07-25T20:30:11.471601Z","shell.execute_reply":"2022-07-25T20:30:11.476978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def augment_image_mask(image, mask, transform_func, image_id, index):\n    im, m = transform_func(image=image, mask=mask).values()\n    return im, m, image_id, index\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:14.263338Z","iopub.execute_input":"2022-07-25T20:30:14.265900Z","iopub.status.idle":"2022-07-25T20:30:14.271519Z","shell.execute_reply.started":"2022-07-25T20:30:14.265855Z","shell.execute_reply":"2022-07-25T20:30:14.270366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compose_arrays = get_aug_funcs()\n\nbase_bar = tqdm_notebook(total=df.shape[0], desc='Iterating over all images')\n# t = tqdm_notebook(total=len(compose_arrays), desc='Augmenting current image')\n\npool=multiprocessing.Pool()\n\nimg_out = zipfile.ZipFile(OUT_TRAIN, 'a')\nmask_out = zipfile.ZipFile(OUT_MASKS, 'a')\n\nfor i in range(df.shape[0]):\n    row = df.iloc[i].squeeze()\n    image, mask, id = load_image_mask(row) \n    images = [image]*len(compose_arrays)\n    masks = [mask]*len(compose_arrays)\n    ids = [id]*len(compose_arrays)\n    image_mask_pairs = pool.starmap(augment_image_mask, zip(images,masks, compose_arrays,ids,list(range(len(compose_arrays)))))\n    for im, m, image_id, index in image_mask_pairs:\n        im = cv2.imencode('.png',cv2.cvtColor(im, cv2.COLOR_RGB2BGR))[1]\n        img_out.writestr(f'{image_id}_{index:04d}.png', im)\n        m = cv2.imencode('.png',m)[1]\n        mask_out.writestr(f'{image_id}_{index:04d}.png', m)\n    base_bar.update()\n\nimg_out.close()\nmask_out.close()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:22.715246Z","iopub.execute_input":"2022-07-25T20:30:22.715718Z","iopub.status.idle":"2022-07-25T20:30:33.004149Z","shell.execute_reply.started":"2022-07-25T20:30:22.715674Z","shell.execute_reply":"2022-07-25T20:30:33.002436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Viewing Sample Augmentations","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport io","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:37.028603Z","iopub.execute_input":"2022-07-25T20:30:37.029361Z","iopub.status.idle":"2022-07-25T20:30:37.036815Z","shell.execute_reply.started":"2022-07-25T20:30:37.029294Z","shell.execute_reply":"2022-07-25T20:30:37.035700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id=10044\nimg_out = zipfile.ZipFile(OUT_TRAIN, 'r')\nmask_out = zipfile.ZipFile(OUT_MASKS, 'r')\nimage_files = img_out.namelist()\nmask_files = mask_out.namelist()\n\nindexes = [i for i in range(len(image_files)) if image_files[i].startswith(f\"{image_id}_\")]\nsamples = np.random.choice(indexes, size=50)\n\n\nfig = plt.figure(figsize=(30,50))\nn_rows = 10\nn_cols = 5\n\nfor i,ix in enumerate(samples):\n    image = np.array(Image.open(io.BytesIO(img_out.read(image_files[ix]))))\n    mask = np.array(Image.open(io.BytesIO(mask_out.read(mask_files[ix]))))\n    plt.subplot(n_rows, n_cols, i+1)\n    plt.title(f\"Sample:{image_files[ix][:-4].rsplit('_')[1]}\")\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.2)\n    plt.axis(False)\n\nimg_out.close()\nmask_out.close()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T20:30:40.082866Z","iopub.execute_input":"2022-07-25T20:30:40.083298Z","iopub.status.idle":"2022-07-25T20:30:45.962770Z","shell.execute_reply.started":"2022-07-25T20:30:40.083261Z","shell.execute_reply":"2022-07-25T20:30:45.961850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}