{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-30T11:28:07.123519Z","iopub.execute_input":"2022-06-30T11:28:07.123928Z","iopub.status.idle":"2022-06-30T11:28:07.151747Z","shell.execute_reply.started":"2022-06-30T11:28:07.123846Z","shell.execute_reply":"2022-06-30T11:28:07.150893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:10.781895Z","iopub.execute_input":"2022-06-30T11:28:10.78274Z","iopub.status.idle":"2022-06-30T11:28:15.022758Z","shell.execute_reply.started":"2022-06-30T11:28:10.782702Z","shell.execute_reply":"2022-06-30T11:28:15.021753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH  = 128\nIMG_HIGHT = 128\nIMG_CHANNELS = 3\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:18.281938Z","iopub.execute_input":"2022-06-30T11:28:18.283248Z","iopub.status.idle":"2022-06-30T11:28:18.288437Z","shell.execute_reply.started":"2022-06-30T11:28:18.283193Z","shell.execute_reply":"2022-06-30T11:28:18.287366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nseed = 42\nnp.random.seed = seed\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:35.812617Z","iopub.execute_input":"2022-06-30T11:28:35.812957Z","iopub.status.idle":"2022-06-30T11:28:35.818278Z","shell.execute_reply.started":"2022-06-30T11:28:35.812928Z","shell.execute_reply":"2022-06-30T11:28:35.817119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport random\nfrom tqdm import tqdm\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:38.962843Z","iopub.execute_input":"2022-06-30T11:28:38.963196Z","iopub.status.idle":"2022-06-30T11:28:39.594899Z","shell.execute_reply.started":"2022-06-30T11:28:38.963164Z","shell.execute_reply":"2022-06-30T11:28:39.593964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train_data test_data\n!unzip -qq ../input/data-science-bowl-2018/stage1_train.zip -d train_data\n!unzip -qq ../input/data-science-bowl-2018/stage1_test.zip -d test_data","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:43.761533Z","iopub.execute_input":"2022-06-30T11:28:43.76241Z","iopub.status.idle":"2022-06-30T11:28:48.583192Z","shell.execute_reply.started":"2022-06-30T11:28:43.76237Z","shell.execute_reply":"2022-06-30T11:28:48.581878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"./train_data/\"\nTEST_PATH = \"./test_data/\"","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:49.342378Z","iopub.execute_input":"2022-06-30T11:28:49.342747Z","iopub.status.idle":"2022-06-30T11:28:49.34793Z","shell.execute_reply.started":"2022-06-30T11:28:49.342714Z","shell.execute_reply":"2022-06-30T11:28:49.346806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:52.352391Z","iopub.execute_input":"2022-06-30T11:28:52.352749Z","iopub.status.idle":"2022-06-30T11:28:52.360249Z","shell.execute_reply.started":"2022-06-30T11:28:52.352719Z","shell.execute_reply":"2022-06-30T11:28:52.358962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((len(train_ids) ,IMG_HIGHT,IMG_WIDTH,  IMG_CHANNELS ), dtype = np.uint8)\n\n# Y_train = np.zeros((len(train_ids),IMG_HIGHT,IMG_WIDTH, 1 ), dtype = np.bool8)\nY_train = np.zeros((len(train_ids),IMG_HIGHT,IMG_WIDTH, 1 )).astype(np.uint8)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:55.94279Z","iopub.execute_input":"2022-06-30T11:28:55.943163Z","iopub.status.idle":"2022-06-30T11:28:55.979902Z","shell.execute_reply.started":"2022-06-30T11:28:55.943133Z","shell.execute_reply":"2022-06-30T11:28:55.978928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"resizing image and masks\")\nfor n , id_ in  tqdm(enumerate(train_ids), total = len(train_ids)):\n    path = TRAIN_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:, :, :IMG_CHANNELS ]\n    img = resize(img, (IMG_HIGHT,IMG_WIDTH ), mode = 'constant' , preserve_range = True)\n    X_train[n]= img\n#     mask = np.zeros((IMG_HIGHT,IMG_WIDTH , 1) , dtype = np.bool8)\n    mask = np.zeros((IMG_HIGHT,IMG_WIDTH , 1)).astype(np.uint8)\n\n    for mask_file in next(os.walk(path + '/masks/'))[2]:\n        mask_  = imread(path + '/masks/' + mask_file)\n        mask_ = np.expand_dims(resize(mask_ ,(IMG_HIGHT,IMG_WIDTH ), mode = \"constant\",\n                                      preserve_range = True  ), axis = -1)\n        mask= np.maximum(mask, mask_)\n    Y_train[n] = mask\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:28:58.622064Z","iopub.execute_input":"2022-06-30T11:28:58.622428Z","iopub.status.idle":"2022-06-30T11:37:01.406891Z","shell.execute_reply.started":"2022-06-30T11:28:58.622398Z","shell.execute_reply":"2022-06-30T11:37:01.405563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.zeros((len(test_ids),IMG_HIGHT,IMG_WIDTH, IMG_CHANNELS ), dtype = np.uint8)\n\nprint(\"resizing test images \")\nsizes_test = []\nfor n , id_ in  tqdm(enumerate(test_ids), total = len(test_ids)):\n    path = TEST_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:, :, :IMG_CHANNELS ]\n    sizes_test.append([img.shape[0], img.shape[1]])\n    img = resize(img, (IMG_HIGHT,IMG_WIDTH), mode = 'constant' , preserve_range = True)\n    X_test[n] = img\n    \nprint(\"done\")","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:37:01.409659Z","iopub.execute_input":"2022-06-30T11:37:01.410664Z","iopub.status.idle":"2022-06-30T11:37:03.567318Z","shell.execute_reply.started":"2022-06-30T11:37:01.410625Z","shell.execute_reply":"2022-06-30T11:37:03.566304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !mkdir train_data test_data\n# !unzip -qq ../input/data-science-bowl-2018/stage1_train.zip -d train_data\n# !unzip -qq ../input/data-science-bowl-2018/stage1_test.zip -d test_data","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:37:03.56911Z","iopub.execute_input":"2022-06-30T11:37:03.569817Z","iopub.status.idle":"2022-06-30T11:37:03.575244Z","shell.execute_reply.started":"2022-06-30T11:37:03.569778Z","shell.execute_reply":"2022-06-30T11:37:03.574163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"resizeing test images\")\n# for n , id_ in  tqdm(enumerate(test_ids), total = len(test_ids)):\n#     path = TEST_PATH + id_\n#     img = imread(path + '/images/' + id_ + '.png')[:, :, :IMG_CHANNELS ]\n#     img = resize(img, (IMG_WIDTH, IMG_HIGHT), mode = 'constant' , preserve_range = True)\n#     X_train[n]= img","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:37:03.57977Z","iopub.execute_input":"2022-06-30T11:37:03.580559Z","iopub.status.idle":"2022-06-30T11:37:03.586741Z","shell.execute_reply.started":"2022-06-30T11:37:03.580523Z","shell.execute_reply":"2022-06-30T11:37:03.585301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(np.squeeze(Y_train[image_x]))","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:37:03.588706Z","iopub.execute_input":"2022-06-30T11:37:03.58962Z","iopub.status.idle":"2022-06-30T11:37:03.676079Z","shell.execute_reply.started":"2022-06-30T11:37:03.589563Z","shell.execute_reply":"2022-06-30T11:37:03.672732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimage_x = random.randint(0, len(train_ids))\nimshow(X_train[image_x])\nplt.show()\nimshow(np.squeeze(Y_train[image_x]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:49:30.703216Z","iopub.execute_input":"2022-06-30T11:49:30.703564Z","iopub.status.idle":"2022-06-30T11:49:31.117606Z","shell.execute_reply.started":"2022-06-30T11:49:30.703537Z","shell.execute_reply":"2022-06-30T11:49:31.116614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH  = 128\nIMG_HIGHT = 128\nIMG_CHANNELS = 3\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T13:52:09.49937Z","iopub.execute_input":"2022-06-29T13:52:09.499808Z","iopub.status.idle":"2022-06-29T13:52:09.505348Z","shell.execute_reply.started":"2022-06-29T13:52:09.499772Z","shell.execute_reply":"2022-06-29T13:52:09.504341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#taking inputs using tensorflow \ninputs = tf.keras.layers.Input((IMG_HIGHT,IMG_WIDTH,IMG_CHANNELS)) \n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:49:42.912963Z","iopub.execute_input":"2022-06-30T11:49:42.913493Z","iopub.status.idle":"2022-06-30T11:49:43.90176Z","shell.execute_reply.started":"2022-06-30T11:49:42.91346Z","shell.execute_reply":"2022-06-30T11:49:43.900702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we need to convert our inputs into float because weights are in float so we have make it compatiblea \ns = tf.keras.layers.Lambda(lambda x : x/256)(inputs)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:49:50.855044Z","iopub.execute_input":"2022-06-30T11:49:50.855407Z","iopub.status.idle":"2022-06-30T11:49:50.871448Z","shell.execute_reply.started":"2022-06-30T11:49:50.855377Z","shell.execute_reply":"2022-06-30T11:49:50.870589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# using tensorflow creating convolution laysers \n# with 16 samplesize 3,3 kernal size. relu actionvation function, \n# kernal initializer  \"he_normal\" paddoing same mean output will be as input\nc1 = tf.keras.layers.Conv2D(16, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(s)\n# applying dropout for 10% to avoid overfiting\nc1 = tf.keras.layers.Dropout(0.1)(c1)\nc1 = tf.keras.layers.Conv2D(16, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c1)\n\n#appling maxpooling layar of 2 by 2 matrix\np1 = tf.keras.layers.MaxPooling2D((2,2))(c1)\n\nc2 = tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(p1)\nc2 = tf.keras.layers.Dropout(0.1)(c2)\nc2 = tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c2)\np2 = tf.keras.layers.MaxPooling2D((2,2))(c2)\n\nc3 = tf.keras.layers.Conv2D(64, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(p2)\nc3 = tf.keras.layers.Dropout(0.1)(c3)\nc3 = tf.keras.layers.Conv2D(64, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c3)\np3 = tf.keras.layers.MaxPooling2D((2,2))(c3)\n\nc4 = tf.keras.layers.Conv2D(128, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(p3)\nc4 = tf.keras.layers.Dropout(0.1)(c4)\nc4 = tf.keras.layers.Conv2D(128, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c4)\np4 = tf.keras.layers.MaxPooling2D((2,2))(c4)\n\nc5 = tf.keras.layers.Conv2D(256, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(p4)\nc5 = tf.keras.layers.Dropout(0.1)(c5)\nc5 = tf.keras.layers.Conv2D(256, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c5)\n\n\nu6 = tf.keras.layers.Conv2DTranspose(128, (2,2), strides = (2,2), padding = 'same')(c5)\nu6 = tf.keras.layers.concatenate([u6,c4])\nc6 = tf.keras.layers.Conv2D(128, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(u6)\nc6 = tf.keras.layers.Dropout(0.1)(c6)\nc6 = tf.keras.layers.Conv2D(128, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c6)\n\n\nu7 = tf.keras.layers.Conv2DTranspose(64, (2,2), strides = (2,2), padding= 'same')(c6)\nu7 = tf.keras.layers.concatenate([u7,c3])\nc7 = tf.keras.layers.Conv2D(64, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(u7)\nc7 = tf.keras.layers.Dropout(0.1)(c7)\nc7 = tf.keras.layers.Conv2D(64, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c7)\n\n\n# u7 = tf.keras.layers.Conv2DTranspose(64, (2,2), strides = (2,2), padding= 'same')(c6)\n# u7 = tf.keras.layers.concatenate([u7,c3])\n# c7 = tf.keras.layers.Conv2D(64, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(u7)\n# c7 = tf.keras.layers.Dropout(0.1)(c7)\n# c7 = tf.keras.layers.Conv2D(64, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c7)\n\n\nu8 = tf.keras.layers.Conv2DTranspose(32, (2,2), strides = (2,2), padding= 'same')(c7)\nu8 = tf.keras.layers.concatenate([u8,c2])\nc8 = tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(u8)\nc8 = tf.keras.layers.Dropout(0.1)(c8)\nc8 = tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c8)\n\n\nu9 = tf.keras.layers.Conv2DTranspose(16, (2,2), strides = (2,2), padding= 'same')(c8)\nu9 = tf.keras.layers.concatenate([u9,c1])\nc9 = tf.keras.layers.Conv2D(16, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(u9)\nc9 = tf.keras.layers.Dropout(0.1)(c9)\nc9 = tf.keras.layers.Conv2D(16, (3,3), activation = 'relu', kernel_initializer = \"he_normal\", padding = 'same' )(c9)\n\noutputs= tf.keras.layers.Conv2D(1, (1,1), activation = 'sigmoid')(c9)\n\nmodel = tf.keras.Model(inputs = [inputs], outputs = [outputs])\nmodel.compile(optimizer  = 'adam' , loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.summary()\n\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:49:53.87696Z","iopub.execute_input":"2022-06-30T11:49:53.877823Z","iopub.status.idle":"2022-06-30T11:49:56.963341Z","shell.execute_reply.started":"2022-06-30T11:49:53.877785Z","shell.execute_reply":"2022-06-30T11:49:56.962074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Here I am saving check point of the model in case somthing happaned while model is running\n\ncheckpointer = tf.keras.callbacks.ModelCheckpoint('model_for_nuclei.h5', verbose = 1, save_best_only = True)\ncallbacks = [\n    tf.keras.callbacks.EarlyStopping(patience = 3 , monitor = 'val_loss'),\n    tf.keras.callbacks.TensorBoard(log_dir = 'logs')\n]\n\nresult = model.fit(X_train,Y_train, validation_split = 0.1, batch_size = 16, epochs = 25, callbacks = callbacks)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T11:53:19.198164Z","iopub.execute_input":"2022-06-30T11:53:19.199071Z","iopub.status.idle":"2022-06-30T11:53:32.629718Z","shell.execute_reply.started":"2022-06-30T11:53:19.198998Z","shell.execute_reply":"2022-06-30T11:53:32.628743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_train = model.predict(X_train[: int(X_train.shape[0]*0.9)], verbose = 1)\npreds_val = model.predict(X_train[int(X_train.shape[0]*0.9) :], verbose = 1)\npreds_test = model.predict(X_test, verbose = 1)\n\n\n\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)\n\n\n# ix = random.randint(0, len(preds_train_t))\nix = random.randint(0, len(preds_test_t))\n\nimshow(X_train[ix])\nplt.show()\nimshow(np.squeeze(Y_train[ix]))\nplt.show()\n# ix = random.randint(0, len(preds_train_t))\nimshow(np.squeeze(preds_test_t[ix]))\nplt.show()\n\n\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\n\nimshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\nplt.show()\n\nimshow(np.squeeze(preds_val_t[ix]))\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T12:12:49.348798Z","iopub.execute_input":"2022-06-30T12:12:49.349324Z","iopub.status.idle":"2022-06-30T12:12:51.192513Z","shell.execute_reply.started":"2022-06-30T12:12:49.349288Z","shell.execute_reply":"2022-06-30T12:12:51.191431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}