{"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 tensorflow as tf\nimport os\nimport random\nimport numpy as np\n \nfrom tqdm import tqdm \n\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T06:51:47.765960Z","iopub.execute_input":"2021-09-05T06:51:47.766345Z","iopub.status.idle":"2021-09-05T06:51:52.573211Z","shell.execute_reply.started":"2021-09-05T06:51:47.766252Z","shell.execute_reply":"2021-09-05T06:51:52.572307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"../input/data-science-bowl-2018/stage1_train.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"stage1_train\")","metadata":{"execution":{"iopub.status.busy":"2021-09-05T06:52:04.734807Z","iopub.execute_input":"2021-09-05T06:52:04.735123Z","iopub.status.idle":"2021-09-05T06:52:10.415534Z","shell.execute_reply.started":"2021-09-05T06:52:04.735092Z","shell.execute_reply":"2021-09-05T06:52:10.414681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"../input/data-science-bowl-2018/stage1_test.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"stage1_test\")","metadata":{"execution":{"iopub.status.busy":"2021-09-05T06:52:10.416903Z","iopub.execute_input":"2021-09-05T06:52:10.417238Z","iopub.status.idle":"2021-09-05T06:52:10.704105Z","shell.execute_reply.started":"2021-09-05T06:52:10.417201Z","shell.execute_reply":"2021-09-05T06:52:10.703277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\nnp.random.seed = seed\n\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T06:51:54.724743Z","iopub.execute_input":"2021-09-05T06:51:54.725071Z","iopub.status.idle":"2021-09-05T06:51:54.730076Z","shell.execute_reply.started":"2021-09-05T06:51:54.725044Z","shell.execute_reply":"2021-09-05T06:51:54.729102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = './stage1_train/'\nTEST_PATH = './stage1_test/'\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T06:51:55.974865Z","iopub.execute_input":"2021-09-05T06:51:55.975204Z","iopub.status.idle":"2021-09-05T06:51:55.979150Z","shell.execute_reply.started":"2021-09-05T06:51:55.975174Z","shell.execute_reply":"2021-09-05T06:51:55.978018Z"},"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]\n\nX_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nY_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T06:52:17.035709Z","iopub.execute_input":"2021-09-05T06:52:17.036029Z","iopub.status.idle":"2021-09-05T06:59:54.398859Z","shell.execute_reply.started":"2021-09-05T06:52:17.036001Z","shell.execute_reply":"2021-09-05T06:59:54.397888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Resizing training images 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_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    X_train[n] = img  #Fill empty X_train with values from img\n    mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)\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_HEIGHT, IMG_WIDTH), mode='constant',  \n                                      preserve_range=True), axis=-1)\n        mask = np.maximum(mask, mask_)  \n            \n    Y_train[n] = mask   \n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nsizes_test = []\nprint('Resizing test images') \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_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    X_test[n] = img\nprint('Done!')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_x = random.randint(0, len(train_ids))\nimshow(X_train[image_x])\nplt.show()\nprint(Y_train[image_x].shape)\nprint(np.squeeze(Y_train[image_x]).shape)\nimshow(np.squeeze(Y_train[image_x]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:00:02.823046Z","iopub.execute_input":"2021-09-05T07:00:02.823367Z","iopub.status.idle":"2021-09-05T07:00:03.188938Z","shell.execute_reply.started":"2021-09-05T07:00:02.823338Z","shell.execute_reply":"2021-09-05T07:00:03.188044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Build the model\ninputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))\ns = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)\n\n#Contraction path\nc1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)\nc1 = tf.keras.layers.Dropout(0.2)(c1)\nc1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)\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.2)(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.3)(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.3)(c4)\nc4 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c4)\np4 = tf.keras.layers.MaxPooling2D(pool_size=(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.4)(c5)\nc5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c5)\n\n#Expansive path \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.3)(c6)\nc6 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c6)\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.3)(c7)\nc7 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c7)\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.2)(c8)\nc8 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c8)\n \nu9 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)\nu9 = tf.keras.layers.concatenate([u9, c1], axis=3)\nc9 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u9)\nc9 = tf.keras.layers.Dropout(0.2)(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","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:09:13.222423Z","iopub.execute_input":"2021-09-05T07:09:13.222780Z","iopub.status.idle":"2021-09-05T07:09:13.485036Z","shell.execute_reply.started":"2021-09-05T07:09:13.222748Z","shell.execute_reply":"2021-09-05T07:09:13.483708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"################################\n#Modelcheckpoint\ncheckpointer = tf.keras.callbacks.ModelCheckpoint('model_for_nuclei.h5', verbose=1, save_best_only=True)\n\ncallbacks = [\n        tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),\n        tf.keras.callbacks.TensorBoard(log_dir='logs'),\n        tf.keras.callbacks.ModelCheckpoint('model_for_nuclei.h5', verbose=1, save_best_only=True)]\n\nresults = model.fit(X_train, Y_train, validation_split=0.2, batch_size=16, epochs=25, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:16:31.783006Z","iopub.execute_input":"2021-09-05T07:16:31.783327Z","iopub.status.idle":"2021-09-05T07:16:41.575749Z","shell.execute_reply.started":"2021-09-05T07:16:31.783297Z","shell.execute_reply":"2021-09-05T07:16:41.574960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model_3\")","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:16:45.822817Z","iopub.execute_input":"2021-09-05T07:16:45.823150Z","iopub.status.idle":"2021-09-05T07:16:50.678605Z","shell.execute_reply.started":"2021-09-05T07:16:45.823120Z","shell.execute_reply":"2021-09-05T07:16:50.677677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model('./model_2')","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:16:50.682056Z","iopub.execute_input":"2021-09-05T07:16:50.682327Z","iopub.status.idle":"2021-09-05T07:16:52.290394Z","shell.execute_reply.started":"2021-09-05T07:16:50.682300Z","shell.execute_reply":"2021-09-05T07:16:52.289599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = random.randint(0, len(X_train))\n\n\npreds_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)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:16:52.299983Z","iopub.execute_input":"2021-09-05T07:16:52.300253Z","iopub.status.idle":"2021-09-05T07:16:52.899523Z","shell.execute_reply.started":"2021-09-05T07:16:52.300227Z","shell.execute_reply":"2021-09-05T07:16:52.898750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_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)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:16:52.901100Z","iopub.execute_input":"2021-09-05T07:16:52.901439Z","iopub.status.idle":"2021-09-05T07:16:52.913965Z","shell.execute_reply.started":"2021-09-05T07:16:52.901405Z","shell.execute_reply":"2021-09-05T07:16:52.912928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random training samples\nix = random.randint(0, len(preds_train_t))\nimshow(X_train[ix])\nplt.show()\nimshow(np.squeeze(Y_train[ix]))\nplt.show()\nimshow(np.squeeze(preds_train_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:16:52.915237Z","iopub.execute_input":"2021-09-05T07:16:52.915597Z","iopub.status.idle":"2021-09-05T07:16:53.303643Z","shell.execute_reply.started":"2021-09-05T07:16:52.915547Z","shell.execute_reply":"2021-09-05T07:16:53.301977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random validation samples\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\nimshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\nplt.show()\nimshow(np.squeeze(preds_val_t[ix]))\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:17:02.057770Z","iopub.execute_input":"2021-09-05T07:17:02.058128Z","iopub.status.idle":"2021-09-05T07:17:02.771770Z","shell.execute_reply.started":"2021-09-05T07:17:02.058097Z","shell.execute_reply":"2021-09-05T07:17:02.770974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a sanity check on some random validation samples\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_test.shape[0]*0.9):][ix])\nplt.show()\n\nimshow(np.squeeze(preds_test_t[ix]))\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T07:17:55.157755Z","iopub.execute_input":"2021-09-05T07:17:55.158068Z","iopub.status.idle":"2021-09-05T07:17:55.607678Z","shell.execute_reply.started":"2021-09-05T07:17:55.158039Z","shell.execute_reply":"2021-09-05T07:17:55.606811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}