Skip to content

Instantly share code, notes, and snippets.

@angadbajwa23
Last active September 28, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angadbajwa23/5bfee380692a566476c3e070bb002a67 to your computer and use it in GitHub Desktop.
Save angadbajwa23/5bfee380692a566476c3e070bb002a67 to your computer and use it in GitHub Desktop.
tts
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "eAokxZAdGPM5"
},
"outputs": [],
"source": [
"import os\n",
"import argparse\n",
"import sys\n",
"import numpy as np\n",
"import hdf5storage\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"import tensorflow as tf\n",
"\n",
"import skimage.io as io\n",
"import skimage.transform as trans\n",
"from keras.models import *\n",
"from keras.layers import *\n",
"from keras.optimizers import *\n",
"from keras.callbacks import ModelCheckpoint, LearningRateScheduler\n",
"from keras import backend as keras\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Q3gZqj05GRhO"
},
"outputs": [],
"source": [
"labels = np.load('labels.npy')\n",
"images = np.load('images.npy')\n",
"masks = np.load('masks.npy')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "EZ6Y6fgQGYWR"
},
"outputs": [],
"source": [
"images = images[..., np.newaxis]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "5TRyOB6kGbUA"
},
"outputs": [],
"source": [
"masks = masks[..., np.newaxis]"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "oq5FmP4AweNP"
},
"source": [
"Normalizing the Images\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "YzH7Ubi5GchL"
},
"outputs": [],
"source": [
"for i in range(images.shape[0]):\n",
" images[i,:, :] = (images[i,:, :] - np.min(images[i,:, :]))/ (np.max(images[i,:, :])-np.min(images[i,:, :]) )"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "DBBxBpCqwVgg"
},
"source": [
"Evaluation Metrics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Bnak4QYfGUUr"
},
"outputs": [],
"source": [
"from keras.losses import binary_crossentropy\n",
"smooth = 1\n",
"def dice_coef(y_true, y_pred):\n",
" y_true_f = K.flatten(y_true)\n",
" y_pred_f = K.flatten(y_pred)\n",
" intersection = K.sum(y_true_f * y_pred_f)\n",
" return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n",
"\n",
"\n",
"def dice_coef_loss(y_true, y_pred):\n",
" return 1.-dice_coef(y_true, y_pred)\n",
"\n",
"def bce_dice_loss(y_true, y_pred):\n",
" return binary_crossentropy(y_true, y_pred) + dice_coef_loss(y_true, y_pred)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "yqqq5osgwhvY"
},
"source": [
"Splitting the dataset into Training, Validation and Test Set"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "sWmHu-oAG7OY"
},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"images_train, images_test, masks_train, masks_test = train_test_split(images,masks,test_size=0.2,train_size=0.8,random_state=1)\n",
"images_test, images_cv, masks_test, masks_cv = train_test_split(images_test,masks_test,test_size = 0.5,train_size =0.5,random_state=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 168
},
"colab_type": "code",
"executionInfo": {
"elapsed": 50121,
"status": "ok",
"timestamp": 1592337903996,
"user": {
"displayName": "angad bajwa",
"photoUrl": "",
"userId": "04291991230709138404"
},
"user_tz": -330
},
"id": "b97E5Ck5G8lh",
"outputId": "565f7ebb-2e75-4a20-ded8-3708aaad5300"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number of training examples = 2451\n",
"number of development examples = 307\n",
"number of test examples = 306\n",
"Images_train shape: (2451, 512, 512, 1)\n",
"Masks_train shape: (2451, 512, 512, 1)\n",
"Images_val (dev) shape: (307, 512, 512, 1)\n",
"Masks_val (dev) shape: (307, 512, 512, 1)\n",
"Images_test shape: (306, 512, 512, 1)\n",
"Masks_test shape: (306, 512, 512, 1)\n"
]
}
],
"source": [
"print (\"number of training examples = \" + str(images_train.shape[0]))\n",
"print (\"number of development examples = \" + str(images_cv.shape[0]))\n",
"print (\"number of test examples = \" + str(images_test.shape[0]))\n",
"print (\"Images_train shape: \" + str(images_train.shape))\n",
"print (\"Masks_train shape: \" + str(masks_train.shape))\n",
"print (\"Images_val (dev) shape: \" + str(images_cv.shape))\n",
"print (\"Masks_val (dev) shape: \" + str(masks_cv.shape))\n",
"print (\"Images_test shape: \" + str(images_test.shape))\n",
"print (\"Masks_test shape: \" + str(masks_test.shape))"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "U-net_Model.ipynb",
"provenance": []
},
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment