{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"}],"dockerImageVersionId":30302,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# [Create U-Net from scratch -Image segmentation with U-Net in Keras and TensorFlow](https://www.machinelearningnuggets.com/image-segmentation-with-u-net-define-u-net-model-from-scratch-in-keras-and-tensorflow)","metadata":{}},{"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\nfrom sklearn.model_selection import train_test_split\n","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:28:00.562903Z","iopub.execute_input":"2024-07-09T03:28:00.563433Z","iopub.status.idle":"2024-07-09T03:28:06.907439Z","shell.execute_reply.started":"2024-07-09T03:28:00.563330Z","shell.execute_reply":"2024-07-09T03:28:06.906222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\nwith ZipFile(\"../input/data-science-bowl-2018/stage1_train.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"./stage1_train\")\n    \nwith ZipFile(\"../input/data-science-bowl-2018/stage1_test.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"./stage1_test\")","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:28:26.295561Z","iopub.execute_input":"2024-07-09T03:28:26.296352Z","iopub.status.idle":"2024-07-09T03:28:32.025402Z","shell.execute_reply.started":"2024-07-09T03:28:26.296315Z","shell.execute_reply":"2024-07-09T03:28:32.024380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\nnp.random.seed = seed\n\nIMG_WIDTH = 256\nIMG_HEIGHT = 256\nIMG_CHANNELS = 3\n\nTRAIN_PATH = 'stage1_train/'\nTEST_PATH = 'stage1_test/'\n\ntrain_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]\n\nX = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\ny = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:30:03.214367Z","iopub.execute_input":"2024-07-09T03:30:03.215385Z","iopub.status.idle":"2024-07-09T03:30:03.224365Z","shell.execute_reply.started":"2024-07-09T03:30:03.215338Z","shell.execute_reply":"2024-07-09T03:30:03.223198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"__author__ = \"Sreenivas Bhattiprolu\"\n__license__ = \"Feel free to copy, I appreciate if you acknowledge Python for Microscopists\"\n\n# Credits https://github.com/bnsreenu/python_for_microscopists \n\n\"\"\"\n@author: Sreenivas Bhattiprolu\n\"\"\"\n\nprint('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[n] = img  #Fill empty X_train with values from img\n    mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=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[n] = mask ","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:30:06.770449Z","iopub.execute_input":"2024-07-09T03:30:06.770835Z","iopub.status.idle":"2024-07-09T03:35:07.424034Z","shell.execute_reply.started":"2024-07-09T03:30:06.770805Z","shell.execute_reply":"2024-07-09T03:35:07.422844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:38:13.563828Z","iopub.execute_input":"2024-07-09T03:38:13.564250Z","iopub.status.idle":"2024-07-09T03:38:13.647001Z","shell.execute_reply.started":"2024-07-09T03:38:13.564214Z","shell.execute_reply":"2024-07-09T03:38:13.645836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:38:14.712512Z","iopub.execute_input":"2024-07-09T03:38:14.713018Z","iopub.status.idle":"2024-07-09T03:38:14.749932Z","shell.execute_reply.started":"2024-07-09T03:38:14.712973Z","shell.execute_reply":"2024-07-09T03:38:14.748705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport cv2\nimport random\n\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\nimport torch\nimport torch.optim as optim\n# torch.cuda.set_device(0)\nfrom torch.utils.data import Dataset\nfrom PIL import Image\nimport torch.nn as nn\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport glob\nimport h5py\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:39:57.116693Z","iopub.execute_input":"2024-07-09T03:39:57.117100Z","iopub.status.idle":"2024-07-09T03:39:59.196589Z","shell.execute_reply.started":"2024-07-09T03:39:57.117065Z","shell.execute_reply":"2024-07-09T03:39:59.195518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np.array(y_train, dtype = np.uint)\ny_val = np.array(y_val, dtype = np.uint)\ny_test = np.array(y_test, dtype = np.uint)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:50:58.390205Z","iopub.execute_input":"2024-07-09T03:50:58.390668Z","iopub.status.idle":"2024-07-09T03:50:58.574869Z","shell.execute_reply.started":"2024-07-09T03:50:58.390631Z","shell.execute_reply":"2024-07-09T03:50:58.573423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np.where(y_train > 0, 1., 0.)\ny_val = np.where(y_val > 0, 1., 0.)\ny_test = np.where(y_test > 0, 1., 0.)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:50:59.523712Z","iopub.execute_input":"2024-07-09T03:50:59.524743Z","iopub.status.idle":"2024-07-09T03:50:59.727317Z","shell.execute_reply.started":"2024-07-09T03:50:59.524702Z","shell.execute_reply":"2024-07-09T03:50:59.726104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.dtype ","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:51:01.672379Z","iopub.execute_input":"2024-07-09T03:51:01.673138Z","iopub.status.idle":"2024-07-09T03:51:01.679596Z","shell.execute_reply.started":"2024-07-09T03:51:01.673097Z","shell.execute_reply":"2024-07-09T03:51:01.678382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.array(y_train[0], dtype = np.uint))","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:48:51.439459Z","iopub.execute_input":"2024-07-09T03:48:51.439866Z","iopub.status.idle":"2024-07-09T03:48:51.623721Z","shell.execute_reply.started":"2024-07-09T03:48:51.439832Z","shell.execute_reply":"2024-07-09T03:48:51.622462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tạo empty lists để lưu trữ ảnh và mask\nall_images = []\nall_masks = []\n\n# Lặp qua từng batch trong trainloader\nfor id, (images, masks) in enumerate(zip(X_train, y_train)):\n    masks = np.where(masks > 0., 1., 0.)\n    masks = masks.transpose(2,0,1)\n    images = images.transpose(2,0,1)\n    images = images/255.\n    all_images.append(np.array(images))\n    all_masks.append(np.array(masks))\n\n# Chuyển lists thành numpy arrays\nall_images = np.array(all_images)\nall_masks = np.array(all_masks)\n\n# Lưu ảnh và mask ra file .npy\nnp.save(os.path.join('/kaggle/working', 'images_train.npy'), all_images)\nnp.save(os.path.join('/kaggle/working', 'masks_train.npy'), all_masks)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:52:49.801968Z","iopub.execute_input":"2024-07-09T03:52:49.802807Z","iopub.status.idle":"2024-07-09T03:52:51.825407Z","shell.execute_reply.started":"2024-07-09T03:52:49.802767Z","shell.execute_reply":"2024-07-09T03:52:51.824131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tạo empty lists để lưu trữ ảnh và mask\nall_images = []\nall_masks = []\n\n# Lặp qua từng batch trong trainloader\nfor id, (images, masks) in enumerate(zip(X_test, y_test)):\n    masks = np.where(masks > 0., 1., 0.)\n    masks = masks.transpose(2,0,1)\n    images = images.transpose(2,0,1)\n    images = images/255.\n    all_images.append(np.array(images))\n    all_masks.append(np.array(masks))\n\n# Chuyển lists thành numpy arrays\nall_images = np.array(all_images)\nall_masks = np.array(all_masks)\n\n# Lưu ảnh và mask ra file .npy\nnp.save(os.path.join('/kaggle/working', 'images_test.npy'), all_images)\nnp.save(os.path.join('/kaggle/working', 'masks_test.npy'), all_masks)","metadata":{"execution":{"iopub.status.busy":"2024-07-09T03:58:28.671507Z","iopub.execute_input":"2024-07-09T03:58:28.672443Z","iopub.status.idle":"2024-07-09T03:58:29.091215Z","shell.execute_reply.started":"2024-07-09T03:58:28.672396Z","shell.execute_reply":"2024-07-09T03:58:29.089874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tạo empty lists để lưu trữ ảnh và mask\nall_images = []\nall_masks = []\n\n# Lặp qua từng batch trong trainloader\nfor id, (images, masks) in enumerate(zip(X_val, y_val)):\n    masks = np.where(masks > 0., 1., 0.)\n    masks = masks.transpose(2,0,1)\n    images = images.transpose(2,0,1)\n    images = images/255.\n    all_images.append(np.array(images))\n    all_masks.append(np.array(masks))\n\n# Chuyển lists thành numpy arrays\nall_images = np.array(all_images)\nall_masks = np.array(all_masks)\n\n# Lưu ảnh và mask ra file .npy\nnp.save(os.path.join('/kaggle/working', 'images_val.npy'), all_images)\nnp.save(os.path.join('/kaggle/working', 'masks_val.npy'), all_masks)","metadata":{},"execution_count":null,"outputs":[]}]}