{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\nfrom tqdm.notebook import tqdm\nfrom multiprocessing import cpu_count\n\nimport imageio\nimport tifffile\nimport cv2\nimport math\nimport sys\nimport glob\nimport os\nimport joblib","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-22T07:42:48.419935Z","iopub.execute_input":"2022-09-22T07:42:48.420278Z","iopub.status.idle":"2022-09-22T07:42:48.425503Z","shell.execute_reply.started":"2022-09-22T07:42:48.420241Z","shell.execute_reply":"2022-09-22T07:42:48.424672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load training DataFrame\ntrain = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\n\ndisplay(train.head())\n\ndisplay(train.info())","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:45:17.375312Z","iopub.execute_input":"2022-09-22T07:45:17.375819Z","iopub.status.idle":"2022-09-22T07:45:17.742895Z","shell.execute_reply.started":"2022-09-22T07:45:17.375788Z","shell.execute_reply":"2022-09-22T07:45:17.742136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAX_WIDTH = train['img_width'].max()\nMAX_HEIGHT = train['img_height'].max()\nN_CHANNELS = 3\nIMG_SIZE = 300 # 640\nPATCH_SIZE = 300 # 640\nN_PATCHES_PER_IMAGE = (IMG_SIZE // PATCH_SIZE) ** 2\nN_SAMPLES = len(train)\nN_PATCHES = N_SAMPLES * N_PATCHES_PER_IMAGE\n\nprint(f'N_SAMPLES: {N_SAMPLES}, N_PATCHES: {N_PATCHES}, MAX_WIDTH: {MAX_WIDTH}, MAX_HEIGHT: {MAX_HEIGHT}')\nprint(f'IMG_SIZE: {IMG_SIZE}, PATCH_SIZE: {PATCH_SIZE}, N_PATCHES_PER_IMAGE: {N_PATCHES_PER_IMAGE}')","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:46:44.670825Z","iopub.execute_input":"2022-09-22T07:46:44.671169Z","iopub.status.idle":"2022-09-22T07:46:44.678748Z","shell.execute_reply.started":"2022-09-22T07:46:44.671142Z","shell.execute_reply":"2022-09-22T07:46:44.677744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image Sizes, most images are 3000x3000 pixels\ntrain[['img_height', 'img_width']].value_counts().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:46:50.334229Z","iopub.execute_input":"2022-09-22T07:46:50.334794Z","iopub.status.idle":"2022-09-22T07:46:50.344722Z","shell.execute_reply.started":"2022-09-22T07:46:50.334759Z","shell.execute_reply":"2022-09-22T07:46:50.343745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Source Distribution\ndisplay(train['data_source'].value_counts().to_frame())","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:46:55.869817Z","iopub.execute_input":"2022-09-22T07:46:55.870142Z","iopub.status.idle":"2022-09-22T07:46:55.879962Z","shell.execute_reply.started":"2022-09-22T07:46:55.870115Z","shell.execute_reply":"2022-09-22T07:46:55.878920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pixel Size Distribution\ndisplay(train['pixel_size'].value_counts().to_frame())","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:00.093730Z","iopub.execute_input":"2022-09-22T07:47:00.094084Z","iopub.status.idle":"2022-09-22T07:47:00.105010Z","shell.execute_reply.started":"2022-09-22T07:47:00.094054Z","shell.execute_reply":"2022-09-22T07:47:00.104144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tissue Thickness\ndisplay(train['tissue_thickness'].value_counts().to_frame())","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:05.278365Z","iopub.execute_input":"2022-09-22T07:47:05.278725Z","iopub.status.idle":"2022-09-22T07:47:05.286930Z","shell.execute_reply.started":"2022-09-22T07:47:05.278680Z","shell.execute_reply":"2022-09-22T07:47:05.286113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\ntrain['age'].plot(kind='hist')\nplt.title('Age Distribution', size=24)\nplt.xlim(0, 100)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:09.660583Z","iopub.execute_input":"2022-09-22T07:47:09.660951Z","iopub.status.idle":"2022-09-22T07:47:09.844215Z","shell.execute_reply.started":"2022-09-22T07:47:09.660923Z","shell.execute_reply":"2022-09-22T07:47:09.843401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = plt.figure(figsize=(8, 8), facecolor='white')\ntrain['organ'].value_counts().plot(kind='pie', autopct='%1.1f%%', title='Organ Distribution')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:15.006846Z","iopub.execute_input":"2022-09-22T07:47:15.007180Z","iopub.status.idle":"2022-09-22T07:47:15.127809Z","shell.execute_reply.started":"2022-09-22T07:47:15.007153Z","shell.execute_reply":"2022-09-22T07:47:15.127043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Resize all images to 3000x3000\ndef resize_tensor(tensor):\n    return cv2.resize(tensor, [IMG_SIZE, IMG_SIZE], interpolation=cv2.INTER_CUBIC).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:22.142919Z","iopub.execute_input":"2022-09-22T07:47:22.143233Z","iopub.status.idle":"2022-09-22T07:47:22.147562Z","shell.execute_reply.started":"2022-09-22T07:47:22.143206Z","shell.execute_reply":"2022-09-22T07:47:22.146735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef get_mask(image_id):\n    row = train.loc[train['id'] == image_id].squeeze()\n    h, w = row[['img_height', 'img_width']]\n    mask = np.zeros(shape=[h * w], dtype=np.uint8)\n    s = row['rle'].split()\n    starts, lengths = [ np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2]) ]\n    starts -= 1\n    ends = starts + lengths\n    for lo, hi in zip(starts, ends):\n        mask[lo : hi] = 1\n        \n    mask = mask.reshape([h, w]).T\n        \n    mask = resize_tensor(mask)\n    \n    mask = np.expand_dims(mask, axis=2)\n        \n    return mask","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:28.831065Z","iopub.execute_input":"2022-09-22T07:47:28.831406Z","iopub.status.idle":"2022-09-22T07:47:28.839030Z","shell.execute_reply.started":"2022-09-22T07:47:28.831376Z","shell.execute_reply":"2022-09-22T07:47:28.838025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reads an image and makes a negative to make tissue colored and background black\ndef get_image(image_id, negative=False):\n    image = tifffile.imread(f'/kaggle/input/hubmap-organ-segmentation/train_images/{image_id}.tiff')\n    if len(image.shape) == 5:\n        image = image.squeeze().transpose(1, 2, 0)\n        \n    # Reverse pixels to make tissue colored and background black\n    if negative:\n        image = image - image.min()\n        image = image / (image.max() - image.min())\n        image = image * 255\n        image = 255 - image.astype(np.uint8)\n        \n    image = resize_tensor(image)\n        \n    return image\n\nimage = get_image(train.loc[0, 'id'], True)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:32.559561Z","iopub.execute_input":"2022-09-22T07:47:32.559921Z","iopub.status.idle":"2022-09-22T07:47:33.358505Z","shell.execute_reply.started":"2022-09-22T07:47:32.559891Z","shell.execute_reply":"2022-09-22T07:47:33.357295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shows a batch of images\ndef show_image_and_masks(rows=4, cols=4):\n    # Figure\n    fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(cols*8, rows*6))\n    # Unique Image Ids\n    image_ids = train['id'].unique()\n    \n    for r in range(rows):\n        image_id = image_ids[r]\n        df_row = train.loc[train['id'] == image_id].head(1).squeeze()\n        # Rad Image\n        image = get_image(image_id)\n        image_negative = get_image(image_id, negative=True)\n        \n        # Image Original\n        axes[r, 0].imshow(image)\n        axes[r, 0].set_title(f'Image {image_id} Raw', size=16)\n        axes[r, 0].axis(False)\n        \n        # Image Negative Original\n        axes[r, 1].imshow(image_negative)\n        axes[r, 1].set_title(f'Image {image_id} Negative min: {image_negative.min()} max: {image_negative.max()}', size=16)\n        axes[r, 1].axis(False)\n        \n        # Mask\n        mask = get_mask(image_id)\n        axes[r, 2].imshow(mask)\n        axes[r, 2].set_title('Mask', size=16)\n        axes[r, 2].axis(False)\n        \n        # Image with Mask\n        axes[r, 3].imshow(image)\n        axes[r, 3].imshow((mask * np.array([255, 0, 0])), alpha=0.50)\n        axes[r, 3].set_title('Image and Mask', size=16)\n        axes[r, 3].axis(False)\n            \n    # Adjust Vertical Space Between Subplots\n    fig.subplots_adjust(wspace=0.10)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:37.359348Z","iopub.execute_input":"2022-09-22T07:47:37.360236Z","iopub.status.idle":"2022-09-22T07:47:37.369700Z","shell.execute_reply.started":"2022-09-22T07:47:37.360204Z","shell.execute_reply":"2022-09-22T07:47:37.368846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image_and_masks(rows=3)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:42.628425Z","iopub.execute_input":"2022-09-22T07:47:42.629394Z","iopub.status.idle":"2022-09-22T07:47:45.881204Z","shell.execute_reply.started":"2022-09-22T07:47:42.629362Z","shell.execute_reply":"2022-09-22T07:47:45.880062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using build in Tensorflow functions, patches an images\ndef extract_patches(image):\n    _, _, c = image.shape\n    image = tf.expand_dims(image, 0)\n    image_patches = tf.image.extract_patches(image, [1,PATCH_SIZE,PATCH_SIZE,1], [1, PATCH_SIZE, PATCH_SIZE, 1], [1, 1, 1, 1], padding='SAME')\n    image_patches = tf.reshape(image_patches, [N_PATCHES_PER_IMAGE, PATCH_SIZE, PATCH_SIZE, c])\n    image_patches = image_patches.numpy()\n\n    return image_patches","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:47.345047Z","iopub.execute_input":"2022-09-22T07:47:47.347145Z","iopub.status.idle":"2022-09-22T07:47:47.356378Z","shell.execute_reply.started":"2022-09-22T07:47:47.347084Z","shell.execute_reply":"2022-09-22T07:47:47.355052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to get both the image and mask for a given image_id\ndef get_image_mask(image_id):    \n    image = get_image(image_id, True)\n    image_patches = extract_patches(image)\n    \n    mask = get_mask(image_id)\n    mask_patches = extract_patches(mask)\n    \n    organ = str.encode(train.loc[train['id'] == image_id, 'organ'].squeeze())\n    \n    return image_patches, mask_patches, organ","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:52.931095Z","iopub.execute_input":"2022-09-22T07:47:52.932019Z","iopub.status.idle":"2022-09-22T07:47:52.937725Z","shell.execute_reply.started":"2022-09-22T07:47:52.931978Z","shell.execute_reply":"2022-09-22T07:47:52.936891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Put every image in a seperate TFRecord file\nN_CHUNKS = len(train)\nCHUNKS = np.array_split(train['id'].values, N_CHUNKS)\n\nprint(f'N_CHUNKS: {N_CHUNKS}, CHUNK_SIZE: {len(CHUNKS[0])}')","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:47:57.437827Z","iopub.execute_input":"2022-09-22T07:47:57.438157Z","iopub.status.idle":"2022-09-22T07:47:57.445482Z","shell.execute_reply.started":"2022-09-22T07:47:57.438130Z","shell.execute_reply":"2022-09-22T07:47:57.444522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_tf_records(chunks):\n    N_SAMPLES_PER_TFRECORD = []\n    ORGAN_PER_TFRECORD = []\n    for chunk_idx, image_id in enumerate(tqdm(chunks)):\n        # Get Image and Mask Patches\n        image_patches, mask_patches, organ = get_image_mask(image_id.squeeze())\n        tfrecord_name = f'batch_{chunk_idx}.tfrecords'\n        \n        # Create the actual TFRecords\n        with tf.io.TFRecordWriter(tfrecord_name) as file_writer:\n            sample_count = 0\n            for image, mask in zip(image_patches, mask_patches):\n                sample_count += 1\n\n                image_serialized = tf.io.serialize_tensor(image).numpy()\n                mask_serialized = tf.io.serialize_tensor(mask).numpy()\n\n                record_bytes = tf.train.Example(features=tf.train.Features(feature={\n                    # Image\n                    'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_serialized])),\n                    # Mask\n                    'mask': tf.train.Feature(bytes_list=tf.train.BytesList(value=[mask_serialized])),\n                    \n                    # Organ\n                    'organ': tf.train.Feature(bytes_list=tf.train.BytesList(value=[organ])),\n                })).SerializeToString()\n                file_writer.write(record_bytes)\n                    \n            # Add Sample Count\n            N_SAMPLES_PER_TFRECORD.append(sample_count)\n            # Add organ\n            ORGAN_PER_TFRECORD.append(organ)\n            \n    # Save Number of Samples per TFRecord to determine step count during training\n    np.save('N_SAMPLES_PER_TFRECORD.npy', np.array(N_SAMPLES_PER_TFRECORD, dtype=np.int16))\n    # Save organ per TFRecord for stratifying kfolds\n    np.save('ORGAN_PER_TFRECORD.npy', np.array(ORGAN_PER_TFRECORD, dtype=str))","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:48:03.198868Z","iopub.execute_input":"2022-09-22T07:48:03.199190Z","iopub.status.idle":"2022-09-22T07:48:03.209453Z","shell.execute_reply.started":"2022-09-22T07:48:03.199162Z","shell.execute_reply":"2022-09-22T07:48:03.208042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create TFRecords\nto_tf_records(CHUNKS)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:48:07.262980Z","iopub.execute_input":"2022-09-22T07:48:07.263321Z","iopub.status.idle":"2022-09-22T07:50:20.908526Z","shell.execute_reply.started":"2022-09-22T07:48:07.263293Z","shell.execute_reply":"2022-09-22T07:50:20.907560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to decode the TFRecords\ndef decode_tfrecord(record_bytes):\n    features = tf.io.parse_single_example(record_bytes, {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'mask': tf.io.FixedLenFeature([], tf.string),\n        'organ': tf.io.FixedLenFeature([], tf.string),\n    })\n    \n    image = tf.io.parse_tensor(features['image'], out_type=tf.uint8)\n    image = tf.reshape(image, [PATCH_SIZE, PATCH_SIZE, N_CHANNELS])\n    \n    mask = tf.io.parse_tensor(features['mask'], out_type=tf.uint8)\n    mask = tf.reshape(mask, [PATCH_SIZE, PATCH_SIZE, 1])\n\n    organ = features['organ']\n    \n    # Explicit reshape needed for TPU, tell cimpiler dimensions of image\n    image = tf.reshape(image, [PATCH_SIZE, PATCH_SIZE, N_CHANNELS])\n    # Explicit reshape needed for TPU, tell cimpiler dimensions of image\n    mask = tf.reshape(mask, [PATCH_SIZE, PATCH_SIZE, 1])\n    \n    return image, mask, organ","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:20.909966Z","iopub.execute_input":"2022-09-22T07:50:20.910559Z","iopub.status.idle":"2022-09-22T07:50:20.916674Z","shell.execute_reply.started":"2022-09-22T07:50:20.910534Z","shell.execute_reply":"2022-09-22T07:50:20.916118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sample Dataset\ndef get_train_dataset(bs):\n    FNAMES_TRAIN_TFRECORDS = tf.io.gfile.glob('./*.tfrecords')\n    train_dataset = tf.data.TFRecordDataset(FNAMES_TRAIN_TFRECORDS, num_parallel_reads=1)\n    train_dataset = train_dataset.map(decode_tfrecord, num_parallel_calls=cpu_count())\n    train_dataset = train_dataset.batch(bs)\n    \n    return train_dataset","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:20.917606Z","iopub.execute_input":"2022-09-22T07:50:20.917851Z","iopub.status.idle":"2022-09-22T07:50:20.931165Z","shell.execute_reply.started":"2022-09-22T07:50:20.917829Z","shell.execute_reply":"2022-09-22T07:50:20.930447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shows a batch of images\ndef show_batch(dataset, rows=10, cols=2):\n    images, masks, organs = next(iter(dataset))\n    fig, axes = plt.subplots(nrows=rows, ncols=cols, figsize=(cols*6, rows*6))\n    for r in range(rows):\n        axes[r, 0].imshow(images[r])\n        organ = organs[r].numpy().decode(\"UTF-8\")\n        axes[r, 0].set_title(f'Organ: {organ}', size=16)\n        axes[r, 1].imshow(masks[r])\n        axes[r, 1].set_title('Mask', size=16)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:20.932887Z","iopub.execute_input":"2022-09-22T07:50:20.933109Z","iopub.status.idle":"2022-09-22T07:50:20.941045Z","shell.execute_reply.started":"2022-09-22T07:50:20.933088Z","shell.execute_reply":"2022-09-22T07:50:20.940371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = get_train_dataset(10)\nshow_batch(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:20.942227Z","iopub.execute_input":"2022-09-22T07:50:20.942847Z","iopub.status.idle":"2022-09-22T07:50:23.859595Z","shell.execute_reply.started":"2022-09-22T07:50:20.942814Z","shell.execute_reply":"2022-09-22T07:50:23.858602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reconstruct Images/Masks from patches to visualize dataset\ndef show_image_mask_from_patches(dataset, n_samples=10):\n    s = int(N_PATCHES_PER_IMAGE ** 0.50)\n    counter = 0\n    dataset_iter = iter(dataset)\n    \n    for _ in tqdm(range(n_samples)):\n        # Main plot\n        fig = plt.figure(figsize=(30, 10))\n        subfigs = fig.subfigures(1, 3)\n        plt.suptitle('Patched Image/Mask/Combined', fontsize=48, y=1.1)\n        images, masks, organs = next(dataset_iter)\n        o = organs[0].numpy().decode()\n        \n        # Make subplots\n        ax_images = subfigs[0].subplots(s, s)\n        subfigs[0].suptitle(f'Image ({o})', size=32)\n        ax_masks = subfigs[1].subplots(s, s)\n        subfigs[1].suptitle('Mask', size=32)\n        ax_combined = subfigs[2].subplots(s, s)\n        subfigs[2].suptitle('Combined', size=32)\n        \n        count = 0\n        for r in range(s):\n            for c in range(s):\n                idx = r * s + c\n                \n                if s > 1:\n                    # Image\n                    ax_images[r,c].imshow(images[idx].numpy())\n                    ax_images[r,c].set_title(f'σ{images[idx].numpy().std():.2f}', c='red')\n                    ax_images[r,c].axis(False)\n                    # Mask\n                    ax_masks[r,c].imshow(masks[idx].numpy() * [255,255,0])\n                    ax_masks[r,c].axis(False)\n                    # Combined\n                    ax_combined[r,c].imshow(images[idx].numpy())\n                    ax_combined[r,c].imshow((masks[idx] * np.array([255, 0, 0])), alpha=0.50)\n                    ax_combined[r,c].axis(False)\n                else:\n                    # Image\n                    ax_images.imshow(images[idx].numpy())\n                    ax_images.set_title(f'σ{images[idx].numpy().std():.2f}', c='red')\n                    ax_images.axis(False)\n                    # Mask\n                    ax_masks.imshow(masks[idx].numpy() * [255,255,0])\n                    ax_masks.axis(False)\n                    # Combined\n                    ax_combined.imshow(images[idx].numpy())\n                    ax_combined.imshow((masks[idx] * np.array([255, 0, 0])), alpha=0.50)\n                    ax_combined.axis(False)\n                \n                count += 1\n                \n        # Show plot\n        fig.subplots_adjust(wspace=0.05)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:23.860995Z","iopub.execute_input":"2022-09-22T07:50:23.861455Z","iopub.status.idle":"2022-09-22T07:50:24.174292Z","shell.execute_reply.started":"2022-09-22T07:50:23.861426Z","shell.execute_reply":"2022-09-22T07:50:24.173191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reconstruct samples from patches\ntrain_dataset = get_train_dataset(N_PATCHES_PER_IMAGE)\nshow_image_mask_from_patches(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:24.175678Z","iopub.execute_input":"2022-09-22T07:50:24.175946Z","iopub.status.idle":"2022-09-22T07:50:30.668733Z","shell.execute_reply.started":"2022-09-22T07:50:24.175923Z","shell.execute_reply":"2022-09-22T07:50:30.667870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Mean/STD Samples\nMEAN = np.zeros(3, dtype=np.float32)\nSTD = np.zeros(3, dtype=np.float32)\n\nfor image, _, _ in tqdm(get_train_dataset(1), total=N_PATCHES):\n    # Update Mean and STD\n    image_np = image.numpy().squeeze()\n    MEAN += (image_np / 255).mean(axis=(0,1)) / N_PATCHES\n    STD += (image_np /  255).std(axis=(0,1)) / N_PATCHES\n\ndisplay(pd.Series(MEAN).to_frame('Mean'))\ndisplay(pd.Series(STD).to_frame('Standard Deviation'))","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:30.669875Z","iopub.execute_input":"2022-09-22T07:50:30.670124Z","iopub.status.idle":"2022-09-22T07:50:33.202500Z","shell.execute_reply.started":"2022-09-22T07:50:30.670101Z","shell.execute_reply":"2022-09-22T07:50:33.201674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save Mean and STD to Normalize Imagesm During Training\nnp.save('MEAN.npy', MEAN)\nnp.save('STD.npy', STD)","metadata":{"execution":{"iopub.status.busy":"2022-09-22T07:50:33.203605Z","iopub.execute_input":"2022-09-22T07:50:33.203937Z","iopub.status.idle":"2022-09-22T07:50:33.209302Z","shell.execute_reply.started":"2022-09-22T07:50:33.203913Z","shell.execute_reply":"2022-09-22T07:50:33.208511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}