{"cells":[{"metadata":{},"cell_type":"markdown","source":"- In this kernel I will be using **U-Net model** for segmentation of nuclei images.\n- U-Net model is commonly used in image segmentation problems. Links to get information about U-Net model - [link1](https://towardsdatascience.com/understanding-semantic-segmentation-with-unet-6be4f42d4b47), [link2](https://en.wikipedia.org/wiki/U-Net)\n- Aim of this kernel is to provide all the steps clearly which are required for segmenting the nuclei images.\n\n**Fork the kernel, make changes to this code and get your hands dirty :)**\n\n---\n\n## Load all libraries"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nprint(\"Loaded all the libraries\")","execution_count":8,"outputs":[{"output_type":"stream","text":"Loaded all the libraries\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"## Data loading and data exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_fpath = \"../input/stage1_train/\"\ntest_fpath = \"../input/stage1_test/\"\n\nprint(\"No. of subfolders in train folder = \",len(os.listdir(train_fpath)))\nprint(\"No. of subfolders in test folder = \",len(os.listdir(test_fpath)))","execution_count":9,"outputs":[{"output_type":"stream","text":"No. of subfolders in train folder =  670\nNo. of subfolders in test folder =  65\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"- Let's understand the directory structure of the train dataset and how is data organised so that we can load the data from the required folder only."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Sub-folders in the first folder '{}':\".format(os.listdir(train_fpath)[0]))\nprint(os.listdir(os.path.join(train_fpath, os.listdir(train_fpath)[0])))","execution_count":10,"outputs":[{"output_type":"stream","text":"Sub-folders in the first folder 'cbca32daaae36a872a11da4eaff65d1068ff3f154eedc9d3fc0c214a4e5d32bd':\n['masks', 'images']\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"- So there are many subfolders in **../input/stage1_train/** folder which in turn contain **['masks', 'images']** subfolders.\n- The images and their corresponding masks have to be retrieved from **['masks', 'images']** subfolders.\n- Let's make a list of all subfolders in **../input/stage1_train/** so that we can retrieve images and corresponding images."},{"metadata":{"trusted":true},"cell_type":"code","source":"#fetching train, test images ids\ntrain_ids = next(os.walk(train_fpath))[1]\ntest_ids = next(os.walk(test_fpath))[1]\n\nprint(\"No. of train ids = \",len(train_ids))\nprint(\"No. of test ids = \",len(test_ids))","execution_count":11,"outputs":[{"output_type":"stream","text":"No. of train ids =  670\nNo. of test ids =  65\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"- Now we have the train, test ids so let's load images and masks which will be useful for training in the succeeding steps."},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_image_data(fpath, ids):\n    img_lst=[]\n    for img_id in ids:\n        #print(os.path.join(fpath, img_id, \"images\", img_id)+\".png\")\n        img = cv2.imread(os.path.join(fpath, img_id, \"images\", img_id)+\".png\")\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n        img_array = Image.fromarray(img, 'RGB')\n\n        resized_img = img_array.resize((150, 150))\n\n        img_lst.append(np.array(resized_img))\n    return img_lst\n\n#loading nuclei image data for training\nX_train = load_image_data(os.path.join(train_fpath), train_ids)\nprint(\"No. of images loaded in X_train = \",len(X_train))\nprint(type(X_train))","execution_count":12,"outputs":[{"output_type":"stream","text":"No. of images loaded in X_train =  670\n<class 'list'>\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"#loading nuclei image data for testing\nX_test = load_image_data(os.path.join(test_fpath), test_ids)\nprint(\"No. of images loaded in X_test = \",len(X_test))\nprint(type(X_test))","execution_count":13,"outputs":[{"output_type":"stream","text":"No. of images loaded in X_test =  65\n<class 'list'>\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_masks_data(fpath, ids):\n    img_lst=[]\n    for img_id in ids:\n        path = fpath + img_id\n        for mask_file in os.listdir(path + '/masks/'):\n            #print(path + '/masks/' + mask_file)\n            img = cv2.imread(path + '/masks/' + mask_file)\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n            img_array = Image.fromarray(img, 'RGB')\n\n            resized_img = img_array.resize((150, 150))\n\n            img_lst.append(np.array(resized_img))\n    return img_lst\n\n#loading nuclei masks data for training\ny_train = load_masks_data(os.path.join(train_fpath), train_ids)\nprint(\"No. of images loaded in y_train = \",len(y_train))\nprint(type(y_train))","execution_count":14,"outputs":[{"output_type":"stream","text":"No. of images loaded in y_train =  29461\n<class 'list'>\n","name":"stdout"}]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}