-
-
Save angadbajwa23/c98cc1cc4d04215f750ba89ced962bec to your computer and use it in GitHub Desktop.
unet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"id": "QgR0FXo2IJ3Y" | |
}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"import skimage.io as io\n", | |
"import skimage.color as color\n", | |
"import random as r\n", | |
"import math\n", | |
"from keras.models import Model\n", | |
"from keras.layers import Dense, Dropout, Activation, Flatten\n", | |
"from keras.layers import concatenate, Conv2D, MaxPooling2D, Conv2DTranspose\n", | |
"from keras.layers import Input, merge, UpSampling2D,BatchNormalization\n", | |
"from keras.callbacks import ModelCheckpoint, LearningRateScheduler\n", | |
"from tensorflow.keras.optimizers import Adam\n", | |
"from keras.preprocessing.image import ImageDataGenerator\n", | |
"from keras import backend as keras\n", | |
"import os\n", | |
"import argparse\n", | |
"import sys\n", | |
"import cv2\n", | |
"import skimage.transform as trans" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MtM4b4wLJqNL" | |
}, | |
"source": [ | |
"Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"id": "HqOj4vbbJnvZ" | |
}, | |
"outputs": [], | |
"source": [ | |
"def unet(input_size = (512,512,1)):\n", | |
" inputs = Input(input_size)\n", | |
" conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)\n", | |
" conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)\n", | |
" pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n", | |
" conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)\n", | |
" conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)\n", | |
" pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n", | |
" conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)\n", | |
" conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)\n", | |
" pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n", | |
" conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)\n", | |
" conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)\n", | |
" drop4 = Dropout(0.5)(conv4)\n", | |
" pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)\n", | |
"\n", | |
" conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)\n", | |
" conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)\n", | |
" drop5 = Dropout(0.5)(conv5)\n", | |
"\n", | |
" up6 = Conv2DTranspose(512,2,strides=(2,2),padding='same')(drop5)\n", | |
" merge6 = concatenate([drop4,up6], axis = 3)\n", | |
" conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)\n", | |
" conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)\n", | |
"\n", | |
" up7 = Conv2DTranspose(256,2,strides=(2,2),padding='same')(conv6)\n", | |
" merge7 = concatenate([conv3,up7], axis = 3)\n", | |
" conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7)\n", | |
" conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)\n", | |
"\n", | |
" up8 = Conv2DTranspose(128,2,strides=(2,2),padding='same')(conv7)\n", | |
" merge8 = concatenate([conv2,up8], axis = 3)\n", | |
" conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8)\n", | |
" conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)\n", | |
"\n", | |
" up9 = Conv2DTranspose(64,2,strides=(2,2),padding='same')(conv8)\n", | |
" merge9 = concatenate([conv1,up9], axis = 3)\n", | |
" conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9)\n", | |
" conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)\n", | |
"\n", | |
" conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)\n", | |
" conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)\n", | |
"\n", | |
" model = Model(inputs, conv10)\n", | |
"\n", | |
" model.compile(optimizer = Adam(lr = 1e-4), loss='binary_crossentropy', metrics=[dice_coef])\n", | |
"\n", | |
" model.summary()\n", | |
" return model" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "MPecemg6WvYD" | |
}, | |
"source": [ | |
"To save checkpoints" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"id": "ChpWH7MXSG5r" | |
}, | |
"outputs": [], | |
"source": [ | |
"filepath=\"cnn-parameters-improvement-{epoch:02d}-{dice_coef:.2f}\"\n", | |
"# save the model with the best validation (development) accuracy till now\n", | |
"checkpoint = ModelCheckpoint(\"models/{}.model\".format(filepath, monitor=[dice_coef], verbose=1, save_best_only=True, mode='max'))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "SA5AnvRJSPLY", | |
"outputId": "f44c4825-6a0f-4e8b-dc05-111e3f453f4a", | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Model: \"model_2\"\n", | |
"__________________________________________________________________________________________________\n", | |
" Layer (type) Output Shape Param # Connected to \n", | |
"==================================================================================================\n", | |
" input_3 (InputLayer) [(None, 512, 512, 1 0 [] \n", | |
" )] \n", | |
" \n", | |
" conv2d_40 (Conv2D) (None, 512, 512, 64 640 ['input_3[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_41 (Conv2D) (None, 512, 512, 64 36928 ['conv2d_40[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" max_pooling2d_8 (MaxPooling2D) (None, 256, 256, 64 0 ['conv2d_41[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_42 (Conv2D) (None, 256, 256, 12 73856 ['max_pooling2d_8[0][0]'] \n", | |
" 8) \n", | |
" \n", | |
" conv2d_43 (Conv2D) (None, 256, 256, 12 147584 ['conv2d_42[0][0]'] \n", | |
" 8) \n", | |
" \n", | |
" max_pooling2d_9 (MaxPooling2D) (None, 128, 128, 12 0 ['conv2d_43[0][0]'] \n", | |
" 8) \n", | |
" \n", | |
" conv2d_44 (Conv2D) (None, 128, 128, 25 295168 ['max_pooling2d_9[0][0]'] \n", | |
" 6) \n", | |
" \n", | |
" conv2d_45 (Conv2D) (None, 128, 128, 25 590080 ['conv2d_44[0][0]'] \n", | |
" 6) \n", | |
" \n", | |
" max_pooling2d_10 (MaxPooling2D (None, 64, 64, 256) 0 ['conv2d_45[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_46 (Conv2D) (None, 64, 64, 512) 1180160 ['max_pooling2d_10[0][0]'] \n", | |
" \n", | |
" conv2d_47 (Conv2D) (None, 64, 64, 512) 2359808 ['conv2d_46[0][0]'] \n", | |
" \n", | |
" dropout_4 (Dropout) (None, 64, 64, 512) 0 ['conv2d_47[0][0]'] \n", | |
" \n", | |
" max_pooling2d_11 (MaxPooling2D (None, 32, 32, 512) 0 ['dropout_4[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_48 (Conv2D) (None, 32, 32, 1024 4719616 ['max_pooling2d_11[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_49 (Conv2D) (None, 32, 32, 1024 9438208 ['conv2d_48[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" dropout_5 (Dropout) (None, 32, 32, 1024 0 ['conv2d_49[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_transpose_8 (Conv2DTran (None, 64, 64, 512) 2097664 ['dropout_5[0][0]'] \n", | |
" spose) \n", | |
" \n", | |
" concatenate_8 (Concatenate) (None, 64, 64, 1024 0 ['dropout_4[0][0]', \n", | |
" ) 'conv2d_transpose_8[0][0]'] \n", | |
" \n", | |
" conv2d_50 (Conv2D) (None, 64, 64, 512) 4719104 ['concatenate_8[0][0]'] \n", | |
" \n", | |
" conv2d_51 (Conv2D) (None, 64, 64, 512) 2359808 ['conv2d_50[0][0]'] \n", | |
" \n", | |
" conv2d_transpose_9 (Conv2DTran (None, 128, 128, 25 524544 ['conv2d_51[0][0]'] \n", | |
" spose) 6) \n", | |
" \n", | |
" concatenate_9 (Concatenate) (None, 128, 128, 51 0 ['conv2d_45[0][0]', \n", | |
" 2) 'conv2d_transpose_9[0][0]'] \n", | |
" \n", | |
" conv2d_52 (Conv2D) (None, 128, 128, 25 1179904 ['concatenate_9[0][0]'] \n", | |
" 6) \n", | |
" \n", | |
" conv2d_53 (Conv2D) (None, 128, 128, 25 590080 ['conv2d_52[0][0]'] \n", | |
" 6) \n", | |
" \n", | |
" conv2d_transpose_10 (Conv2DTra (None, 256, 256, 12 131200 ['conv2d_53[0][0]'] \n", | |
" nspose) 8) \n", | |
" \n", | |
" concatenate_10 (Concatenate) (None, 256, 256, 25 0 ['conv2d_43[0][0]', \n", | |
" 6) 'conv2d_transpose_10[0][0]'] \n", | |
" \n", | |
" conv2d_54 (Conv2D) (None, 256, 256, 12 295040 ['concatenate_10[0][0]'] \n", | |
" 8) \n", | |
" \n", | |
" conv2d_55 (Conv2D) (None, 256, 256, 12 147584 ['conv2d_54[0][0]'] \n", | |
" 8) \n", | |
" \n", | |
" conv2d_transpose_11 (Conv2DTra (None, 512, 512, 64 32832 ['conv2d_55[0][0]'] \n", | |
" nspose) ) \n", | |
" \n", | |
" concatenate_11 (Concatenate) (None, 512, 512, 12 0 ['conv2d_41[0][0]', \n", | |
" 8) 'conv2d_transpose_11[0][0]'] \n", | |
" \n", | |
" conv2d_56 (Conv2D) (None, 512, 512, 64 73792 ['concatenate_11[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_57 (Conv2D) (None, 512, 512, 64 36928 ['conv2d_56[0][0]'] \n", | |
" ) \n", | |
" \n", | |
" conv2d_58 (Conv2D) (None, 512, 512, 2) 1154 ['conv2d_57[0][0]'] \n", | |
" \n", | |
" conv2d_59 (Conv2D) (None, 512, 512, 1) 3 ['conv2d_58[0][0]'] \n", | |
" \n", | |
"==================================================================================================\n", | |
"Total params: 31,031,685\n", | |
"Trainable params: 31,031,685\n", | |
"Non-trainable params: 0\n", | |
"__________________________________________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"model=unet()" | |
] | |
} | |
], | |
"metadata": { | |
"accelerator": "GPU", | |
"colab": { | |
"collapsed_sections": [], | |
"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