[ccb1dd]: / notebooks / Check_Augmentations.ipynb

Download this file

345 lines (344 with data), 9.6 kB

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import sys\n",
    "sys.path.append('..')\n",
    "\n",
    "import nibabel as nib\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "from fetal_net.augment import augment_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def slice_it(arr, inds):\n",
    "    return arr[inds[0][0]:inds[0][1], inds[1][0]: inds[1][1], inds[2][0]:inds[2][1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vol = nib.load('../../Datasets/fetus_window_1_99/255/volume.nii')\n",
    "mask = nib.load('../../Datasets/fetus_window_1_99/255/truth.nii')\n",
    "vol.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "patch_corner = [70, 70, 30]\n",
    "patch_shape = [128,128,5]\n",
    "data_range = [(start, start + size) for start, size in zip(patch_corner, patch_shape)]\n",
    "data_range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "truth_index = 2\n",
    "truth_size = 1\n",
    "truth_range = data_range[:2] + [(patch_corner[2] + truth_index,\n",
    "                                patch_corner[2] + truth_index + truth_size)]\n",
    "truth_range"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gaussian Filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           gaussian_filter={\n",
    "            'max_sigma': 1.5,\n",
    "            'prob': 1,\n",
    "        }, poisson_noise=1)\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Shot Noise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           poisson_noise=0.5)\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# contrast deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "print(data.min(), data.max())\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           contrast_deviation={'min_factor': 0.2, 'max_factor': 0.8})\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# intensity_multiplication_range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           intensity_multiplication_range=[0.8, 1.2])\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# piecewise_affine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           piecewise_affine={'scale': 0.5})\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# elastic_transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           elastic_transform={'alpha': 5, 'sigma': 1})\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# scale_deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                           scale_deviation=[0.1, 0.1, 0.0])\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# rotate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                                rotate_deviation=[0, 0, 1800])\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')\n",
    "\n",
    "#print(slice_it(truth, truth_range).shape)\n",
    "#print(truth2.shape)\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(truth, truth_range)[..., 0], truth2[..., 0]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Flip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                                flip=[0, 0, 1])\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')\n",
    "\n",
    "#print(slice_it(truth, truth_range).shape)\n",
    "#print(truth2.shape)\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(truth, truth_range)[..., 0], truth2[..., 0]], cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Translate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = vol.get_fdata()\n",
    "truth = mask.get_fdata()\n",
    "data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n",
    "                                translate_deviation=[0, 0, 10])\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')\n",
    "\n",
    "#print(slice_it(truth, truth_range).shape)\n",
    "#print(truth2.shape)\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(truth, truth_range)[..., 0], truth2[..., 0]], cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z_trans = 9\n",
    "\n",
    "data_range2 = data_range.copy()\n",
    "data_range2[-1] = np.add(data_range2[-1], z_trans)\n",
    "\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(data, data_range2)[..., 2], data2[..., 2]], cmap='gray')\n",
    "\n",
    "truth_range2 = truth_range\n",
    "truth_range2[-1] = np.add(truth_range2[-1], z_trans)\n",
    "plt.figure(figsize = (16,14))\n",
    "plt.imshow(np.c_[slice_it(truth, truth_range2)[..., 0], truth2[..., 0]], cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}