{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":30201,"databundleVersionId":2750748,"sourceType":"competition"},{"sourceId":2933774,"sourceType":"datasetVersion","datasetId":1798650},{"sourceId":2934127,"sourceType":"datasetVersion","datasetId":1798881},{"sourceId":2934241,"sourceType":"datasetVersion","datasetId":1798953}],"dockerImageVersionId":30153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","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"}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nfrom skimage import io,img_as_ubyte\nfrom pathlib import Path\nimport tensorflow as tf\nfrom keras.models import Sequential, Model,load_model\nfrom keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nimport os\nfrom tqdm import tqdm\nfrom scipy.ndimage import rotate\nimport albumentations as A\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.models import Model, load_model\nfrom keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda\n\nfrom matplotlib import pyplot as plt\nfrom skimage.transform import resize\nimport random","metadata":{"execution":{"iopub.status.busy":"2021-12-16T11:52:14.501084Z","iopub.execute_input":"2021-12-16T11:52:14.501342Z","iopub.status.idle":"2021-12-16T11:52:20.547714Z","shell.execute_reply.started":"2021-12-16T11:52:14.501314Z","shell.execute_reply":"2021-12-16T11:52:20.547025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")\ndf_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T11:52:21.359595Z","iopub.execute_input":"2021-12-16T11:52:21.360117Z","iopub.status.idle":"2021-12-16T11:52:21.885383Z","shell.execute_reply.started":"2021-12-16T11:52:21.360079Z","shell.execute_reply":"2021-12-16T11:52:21.88464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_load_path = '../input/sartorius-cell-instance-segmentation/train/'\nimages_save_path = './train/'\nmasks_path = './mask/'","metadata":{"execution":{"iopub.status.busy":"2021-12-16T11:52:22.923917Z","iopub.execute_input":"2021-12-16T11:52:22.924164Z","iopub.status.idle":"2021-12-16T11:52:22.927682Z","shell.execute_reply.started":"2021-12-16T11:52:22.924139Z","shell.execute_reply":"2021-12-16T11:52:22.927017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import glob\n\n#files = glob.glob('./mask/*')\n#for f in files:\n#    os.remove(f)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:15.741504Z","iopub.execute_input":"2021-12-16T09:43:15.741971Z","iopub.status.idle":"2021-12-16T09:43:15.748925Z","shell.execute_reply.started":"2021-12-16T09:43:15.741937Z","shell.execute_reply":"2021-12-16T09:43:15.748237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Path('./train/').mkdir(parents=True, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:15.750309Z","iopub.execute_input":"2021-12-16T09:43:15.750631Z","iopub.status.idle":"2021-12-16T09:43:15.75725Z","shell.execute_reply.started":"2021-12-16T09:43:15.750597Z","shell.execute_reply":"2021-12-16T09:43:15.756507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_mask(img_id):\n\n    img = io.imread(images_load_path + img_id + '.png')\n    img_masks = df_train.loc[df_train['id'] == img_id, 'annotation'].tolist()\n    \n    \n    all_masks = np.zeros((img.shape[0],img.shape[1]))\n    for mask in img_masks:\n\n        s = mask.split()\n        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n        starts -= 1\n        ends = starts + lengths\n        img_mask = np.zeros(img.shape[0]*img.shape[1], dtype=np.uint8)\n        for lo, hi in zip(starts, ends):\n            img_mask[lo:hi] = 1\n        \n        all_masks += img_mask.reshape(img.shape)\n\n    Path(masks_path).mkdir(parents=True, exist_ok=True)\n    \n    io.imsave(f'./train/{img_id}.png',img)\n    io.imsave(f'./mask/{img_id}_mask.png',all_masks)\n    #print(f'{img_id} mask created')","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:15.75869Z","iopub.execute_input":"2021-12-16T09:43:15.75895Z","iopub.status.idle":"2021-12-16T09:43:15.768606Z","shell.execute_reply.started":"2021-12-16T09:43:15.758917Z","shell.execute_reply":"2021-12-16T09:43:15.767815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image_and_mask(ImageId):\n    img = io.imread(images_save_path + ImageId + '.png')\n    img_mask = io.imread('./mask/' + ImageId + '_mask.png')\n    fig, axarr = plt.subplots(1, 3, figsize=(15, 40))\n    axarr[0].axis('off')\n    axarr[1].axis('off')\n    axarr[2].axis('off')\n    axarr[0].imshow(img)\n    axarr[1].imshow(img_mask)\n    axarr[2].imshow(img)\n    axarr[2].imshow(img_mask, alpha=0.4)\n    plt.tight_layout(h_pad=0.1, w_pad=0.1)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:15.770019Z","iopub.execute_input":"2021-12-16T09:43:15.770367Z","iopub.status.idle":"2021-12-16T09:43:15.780852Z","shell.execute_reply.started":"2021-12-16T09:43:15.770315Z","shell.execute_reply":"2021-12-16T09:43:15.780144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = list(set(df_train['id'].tolist()))","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:16.988096Z","iopub.execute_input":"2021-12-16T09:43:16.988873Z","iopub.status.idle":"2021-12-16T09:43:16.997142Z","shell.execute_reply.started":"2021-12-16T09:43:16.988821Z","shell.execute_reply":"2021-12-16T09:43:16.996379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for n,id in tqdm(enumerate(id_list), total=len(id_list)):\n    create_mask(id)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T23:57:15.055745Z","iopub.execute_input":"2021-12-15T23:57:15.057314Z","iopub.status.idle":"2021-12-15T23:59:14.922318Z","shell.execute_reply.started":"2021-12-15T23:57:15.057264Z","shell.execute_reply":"2021-12-15T23:59:14.921613Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(images_save_path))","metadata":{"execution":{"iopub.status.busy":"2021-12-16T00:01:38.577835Z","iopub.execute_input":"2021-12-16T00:01:38.578666Z","iopub.status.idle":"2021-12-16T00:01:38.585901Z","shell.execute_reply.started":"2021-12-16T00:01:38.578615Z","shell.execute_reply":"2021-12-16T00:01:38.585104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_img = id_list[np.random.randint(0,len(id_list))]\nshow_image_and_mask(random_img)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T00:02:38.57935Z","iopub.execute_input":"2021-12-16T00:02:38.579922Z","iopub.status.idle":"2021-12-16T00:02:39.083407Z","shell.execute_reply.started":"2021-12-16T00:02:38.57988Z","shell.execute_reply":"2021-12-16T00:02:39.082786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_to_generate=1000","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:26.642847Z","iopub.execute_input":"2021-12-16T09:43:26.643114Z","iopub.status.idle":"2021-12-16T09:43:26.646448Z","shell.execute_reply.started":"2021-12-16T09:43:26.643087Z","shell.execute_reply":"2021-12-16T09:43:26.645789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug = A.Compose([\n    A.VerticalFlip(p=0.5),              \n    A.RandomRotate90(p=0.5),\n    A.HorizontalFlip(p=1),\n    A.Transpose(p=1),\n    #A.ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),\n    A.GridDistortion(p=1)\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:27.061192Z","iopub.execute_input":"2021-12-16T09:43:27.0617Z","iopub.status.idle":"2021-12-16T09:43:27.065927Z","shell.execute_reply.started":"2021-12-16T09:43:27.061663Z","shell.execute_reply":"2021-12-16T09:43:27.065238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_augm_images_and_masks(images_to_generate):\n    for i in tqdm(range(0,images_to_generate)):\n        random_img = id_list[np.random.randint(0,len(id_list))]\n        img = io.imread(images_save_path + random_img + '.png')\n        \n        img_mask = io.imread(masks_path + random_img + '_mask.png')\n        augmented = aug(image=img, mask=img_mask)\n        transformed_image = augmented['image']\n        transformed_mask = augmented['mask']\n        \n        while os.path.isfile(f'{images_save_path}/aug_{random_img}.png'):\n            random_img +='_1'\n        \n        io.imsave(f'{images_save_path}/aug_{random_img}.png', transformed_image)\n        io.imsave(f'{masks_path}/aug_{random_img}_mask.png', transformed_mask)\n        ","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:43:28.861472Z","iopub.execute_input":"2021-12-16T09:43:28.862294Z","iopub.status.idle":"2021-12-16T09:43:28.871008Z","shell.execute_reply.started":"2021-12-16T09:43:28.862258Z","shell.execute_reply":"2021-12-16T09:43:28.870212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_augm_images_and_masks(images_to_generate)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T00:06:00.741544Z","iopub.execute_input":"2021-12-16T00:06:00.741906Z","iopub.status.idle":"2021-12-16T00:09:39.444921Z","shell.execute_reply.started":"2021-12-16T00:06:00.741873Z","shell.execute_reply":"2021-12-16T00:09:39.444201Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_img = np.random.randint(0,len(os.listdir(images_save_path)))\nid = os.listdir(images_save_path)[random_img]\nshow_image_and_mask(os.path.splitext(id)[0])\nos.path.splitext(id)[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-16T00:19:40.116809Z","iopub.execute_input":"2021-12-16T00:19:40.117346Z","iopub.status.idle":"2021-12-16T00:19:40.570428Z","shell.execute_reply.started":"2021-12-16T00:19:40.117305Z","shell.execute_reply":"2021-12-16T00:19:40.569819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.to_int32=lambda x: tf.cast(x, tf.int32)\n\n# Define IoU metric\ndef mean_iou(y_true, y_pred, smooth=1):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n    iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n    return tf.convert_to_tensor(iou)\ndef dice_coefficient(y_true, y_pred):\n    numerator = 2 * tf.reduce_sum(y_true * y_pred)\n    denominator = tf.reduce_sum(y_true + y_pred)\n    return numerator / (denominator + tf.keras.backend.epsilon())","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:40:57.340859Z","iopub.execute_input":"2021-12-16T12:40:57.341268Z","iopub.status.idle":"2021-12-16T12:40:57.351982Z","shell.execute_reply.started":"2021-12-16T12:40:57.341235Z","shell.execute_reply":"2021-12-16T12:40:57.350692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def simple_unet_model(img_height, img_width, img_channels):\n\n    inputs = Input((img_height, img_width, img_channels))\n    s = Lambda(lambda x: x / 255)(inputs) \n\n    c1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)\n    c1 = Dropout(0.1)(c1)\n    c1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)\n    p1 = MaxPooling2D((2, 2))(c1)\n    \n    c2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)\n    c2 = Dropout(0.1)(c2)\n    c2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)\n    p2 = MaxPooling2D((2, 2))(c2)\n     \n    c3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)\n    c3 = Dropout(0.2)(c3)\n    c3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)\n    p3 = MaxPooling2D((2, 2))(c3)\n     \n    c4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)\n    c4 = Dropout(0.2)(c4)\n    c4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c4)\n    p4 = MaxPooling2D(pool_size=(2, 2))(c4)\n     \n    c5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p4)\n    c5 = Dropout(0.3)(c5)\n    c5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c5)\n    \n    #----------------------------\n    u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5)\n    u6 = concatenate([u6, c4])\n    c6 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u6)\n    c6 = Dropout(0.2)(c6)\n    c6 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c6)\n     \n    u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)\n    u7 = concatenate([u7, c3])\n    c7 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u7)\n    c7 = Dropout(0.2)(c7)\n    c7 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c7)\n     \n    u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)\n    u8 = concatenate([u8, c2])\n    c8 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u8)\n    c8 = Dropout(0.1)(c8)\n    c8 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c8)\n     \n    u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)\n    u9 = concatenate([u9, c1], axis=3)\n    c9 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u9)\n    c9 = Dropout(0.1)(c9)\n    c9 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c9)\n     \n    outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)\n     \n    model = Model(inputs=[inputs], outputs=[outputs])\n    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[dice_coefficient])\n    model.summary()\n    \n    return model\n ","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:29.664063Z","iopub.execute_input":"2021-12-16T12:41:29.664366Z","iopub.status.idle":"2021-12-16T12:41:29.687666Z","shell.execute_reply.started":"2021-12-16T12:41:29.664337Z","shell.execute_reply":"2021-12-16T12:41:29.686839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(img_height, img_width, img_channels):\n    return simple_unet_model(img_height, img_width, img_channels)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:30.542673Z","iopub.execute_input":"2021-12-16T12:41:30.543375Z","iopub.status.idle":"2021-12-16T12:41:30.547634Z","shell.execute_reply.started":"2021-12-16T12:41:30.54334Z","shell.execute_reply":"2021-12-16T12:41:30.546693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 256","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:33.622218Z","iopub.execute_input":"2021-12-16T12:41:33.622782Z","iopub.status.idle":"2021-12-16T12:41:33.626195Z","shell.execute_reply.started":"2021-12-16T12:41:33.622734Z","shell.execute_reply":"2021-12-16T12:41:33.625259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model(size,size,1)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:34.620386Z","iopub.execute_input":"2021-12-16T12:41:34.621033Z","iopub.status.idle":"2021-12-16T12:41:34.849411Z","shell.execute_reply.started":"2021-12-16T12:41:34.620994Z","shell.execute_reply":"2021-12-16T12:41:34.848706Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(\n    model,\n    to_file=\"model.png\",\n    show_shapes=1,\n    show_dtype=1,\n    show_layer_names=True,\n    rankdir=\"TB\",\n    expand_nested=False,\n    dpi=96)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:58:43.033572Z","iopub.execute_input":"2021-12-16T09:58:43.033809Z","iopub.status.idle":"2021-12-16T09:58:44.761997Z","shell.execute_reply.started":"2021-12-16T09:58:43.033777Z","shell.execute_reply":"2021-12-16T09:58:44.76113Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '../input/another-data-to-train/another data/train/'\nmask_path = '../input/another-data-to-train/another data/mask/'","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:33:16.839249Z","iopub.execute_input":"2021-12-16T12:33:16.839505Z","iopub.status.idle":"2021-12-16T12:33:16.842908Z","shell.execute_reply.started":"2021-12-16T12:33:16.839477Z","shell.execute_reply":"2021-12-16T12:33:16.842217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((len(os.listdir(image_path)), size, size, 1), dtype=np.uint8)\nY_train = np.zeros((len(os.listdir(image_path)), size, size, 1), dtype=np.bool)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:44.781338Z","iopub.execute_input":"2021-12-16T12:41:44.781589Z","iopub.status.idle":"2021-12-16T12:41:44.789249Z","shell.execute_reply.started":"2021-12-16T12:41:44.781561Z","shell.execute_reply":"2021-12-16T12:41:44.788458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:45.656779Z","iopub.execute_input":"2021-12-16T12:41:45.657431Z","iopub.status.idle":"2021-12-16T12:41:45.662838Z","shell.execute_reply.started":"2021-12-16T12:41:45.657394Z","shell.execute_reply":"2021-12-16T12:41:45.662074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for n, id_ in tqdm(enumerate(os.listdir(image_path)), total=len(os.listdir(image_path))):\n    id = os.path.splitext(id_)[0]\n    \n    img = io.imread(image_path + id + '.png')\n    img = resize(img, (size, size), mode='constant', preserve_range=True)\n    img = np.expand_dims(img, axis = 2)\n    X_train[n] = img\n\n    \n    mask = io.imread(mask_path + id + '_mask.png')\n    mask = resize(mask, (size, size), mode='constant', preserve_range=True)\n    mask = np.expand_dims(mask,axis=2)\n\n    Y_train[n] = mask","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:41:46.613516Z","iopub.execute_input":"2021-12-16T12:41:46.614221Z","iopub.status.idle":"2021-12-16T12:42:18.229842Z","shell.execute_reply.started":"2021-12-16T12:41:46.614185Z","shell.execute_reply":"2021-12-16T12:42:18.229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:42:21.175407Z","iopub.execute_input":"2021-12-16T12:42:21.17565Z","iopub.status.idle":"2021-12-16T12:42:21.180465Z","shell.execute_reply.started":"2021-12-16T12:42:21.175623Z","shell.execute_reply":"2021-12-16T12:42:21.179801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystopper = EarlyStopping(patience=15, verbose=1)\ncheckpointer = ModelCheckpoint('best_model.h5', verbose=1, save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:42:23.424832Z","iopub.execute_input":"2021-12-16T12:42:23.425355Z","iopub.status.idle":"2021-12-16T12:42:23.429754Z","shell.execute_reply.started":"2021-12-16T12:42:23.425321Z","shell.execute_reply":"2021-12-16T12:42:23.428731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.remove(\"/kaggle/working/best_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:51:19.044858Z","iopub.execute_input":"2021-12-16T12:51:19.045117Z","iopub.status.idle":"2021-12-16T12:51:19.053328Z","shell.execute_reply.started":"2021-12-16T12:51:19.045089Z","shell.execute_reply":"2021-12-16T12:51:19.05258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_train, im_test, msk_train, msk_test = train_test_split(X_train, Y_train, test_size = 0.1, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:51:23.030601Z","iopub.execute_input":"2021-12-16T12:51:23.030889Z","iopub.status.idle":"2021-12-16T12:51:23.054982Z","shell.execute_reply.started":"2021-12-16T12:51:23.030856Z","shell.execute_reply":"2021-12-16T12:51:23.054155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(im_train, msk_train, validation_split=0.15, batch_size=5, epochs=200, \n                    callbacks=[earlystopper, checkpointer])","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:51:24.465956Z","iopub.execute_input":"2021-12-16T12:51:24.466216Z","iopub.status.idle":"2021-12-16T12:52:35.720689Z","shell.execute_reply.started":"2021-12-16T12:51:24.466187Z","shell.execute_reply":"2021-12-16T12:52:35.719589Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('../input/best-model/best_model.h5')\npreds_train = model.predict(im_train[:int(im_train.shape[0]*0.9)], verbose=1)\n#preds_val = model.predict(im_train[int(im_train.shape[0]*0.9):], verbose=1)\npreds_test = model.predict(im_test, verbose=1)\n\n# Threshold predictions\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\n#preds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)\n\n# Create list of upsampled test masks\npreds_test_upsampled = []\nfor i in range(len(preds_test)):\n    preds_test_upsampled.append(resize(np.squeeze(preds_test[i]), \n                                       (size, size), \n                                       mode='constant', preserve_range=True))","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:52:39.176446Z","iopub.execute_input":"2021-12-16T12:52:39.176876Z","iopub.status.idle":"2021-12-16T12:52:41.783534Z","shell.execute_reply.started":"2021-12-16T12:52:39.176841Z","shell.execute_reply":"2021-12-16T12:52:41.782558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(preds_test_t)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:52:41.789558Z","iopub.execute_input":"2021-12-16T12:52:41.789897Z","iopub.status.idle":"2021-12-16T12:52:41.802475Z","shell.execute_reply.started":"2021-12-16T12:52:41.789853Z","shell.execute_reply":"2021-12-16T12:52:41.80144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img = io.imread(images_save_path + ImageId + '.png')\n#img_mask = io.imread('./mask/' + ImageId + '_mask.png')\nix = random.randint(0, len(preds_train_t))\nfig, axarr = plt.subplots(1, 3, figsize=(15, 40))\naxarr[0].axis('off')\naxarr[1].axis('off')\naxarr[2].axis('off')\naxarr[0].imshow(im_train[ix])\naxarr[1].imshow(im_train[ix])\naxarr[1].imshow(msk_train[ix], alpha=0.4)\naxarr[2].imshow(preds_train_t[ix])\naxarr[2].imshow(im_train[ix], alpha=0.4)\nplt.tight_layout(h_pad=0.1, w_pad=0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:52:43.91174Z","iopub.execute_input":"2021-12-16T12:52:43.912447Z","iopub.status.idle":"2021-12-16T12:52:44.447221Z","shell.execute_reply.started":"2021-12-16T12:52:43.912411Z","shell.execute_reply":"2021-12-16T12:52:44.446603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_test_path = '../input/sartorius-cell-instance-segmentation/test/'","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:52:52.698967Z","iopub.execute_input":"2021-12-16T12:52:52.699748Z","iopub.status.idle":"2021-12-16T12:52:52.703626Z","shell.execute_reply.started":"2021-12-16T12:52:52.699698Z","shell.execute_reply":"2021-12-16T12:52:52.702611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = np.zeros((len(os.listdir(image_test_path)), size, size, 1), dtype=np.uint8)\ntest_image_name = []\nfor n, id_ in tqdm(enumerate(os.listdir(image_test_path)), total=len(os.listdir(image_test_path))):\n    id = os.path.splitext(id_)[0]\n    \n    img = io.imread(image_test_path + id + '.png')\n    img = resize(img, (size, size), mode='constant', preserve_range=True)\n    img = np.expand_dims(img, axis = 2)\n    test_image[n] = img\n    test_image_name.append(id)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:56:59.938676Z","iopub.execute_input":"2021-12-16T12:56:59.939355Z","iopub.status.idle":"2021-12-16T12:57:00.027637Z","shell.execute_reply.started":"2021-12-16T12:56:59.939321Z","shell.execute_reply":"2021-12-16T12:57:00.026812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_name[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:57:05.376319Z","iopub.execute_input":"2021-12-16T12:57:05.376577Z","iopub.status.idle":"2021-12-16T12:57:05.381627Z","shell.execute_reply.started":"2021-12-16T12:57:05.376547Z","shell.execute_reply":"2021-12-16T12:57:05.380604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('../input/best-model/best_model.h5')\n#preds_train = model.predict(im_train[:int(im_train.shape[0]*0.9)], verbose=1)\n#preds_val = model.predict(im_train[int(im_train.shape[0]*0.9):], verbose=1)\npreds_test = model.predict(test_image, verbose=1)\n\n# Threshold predictions\n#preds_train_t = (preds_train > 0.5).astype(np.uint8)\n#preds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:52:58.733256Z","iopub.execute_input":"2021-12-16T12:52:58.733946Z","iopub.status.idle":"2021-12-16T12:53:00.220822Z","shell.execute_reply.started":"2021-12-16T12:52:58.733909Z","shell.execute_reply":"2021-12-16T12:53:00.220114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ix = random.randint(0, len(test_image))\nfor ix in range(0,3):\n    fig, axarr = plt.subplots(1, 2, figsize=(15, 40))\n    axarr[0].axis('off')\n    axarr[1].axis('off')\n\n    axarr[0].imshow(test_image[ix])\n    axarr[1].imshow(preds_train_t[ix])\n    axarr[1].imshow(test_image[ix], alpha=0.4)\n    plt.tight_layout(h_pad=0.1, w_pad=0.1)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:53:00.774203Z","iopub.execute_input":"2021-12-16T12:53:00.774662Z","iopub.status.idle":"2021-12-16T12:53:02.218242Z","shell.execute_reply.started":"2021-12-16T12:53:00.774624Z","shell.execute_reply":"2021-12-16T12:53:02.217627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask_decode(mask):\n    pixels = mask.flatten()\n    vals = np.unique(pixels)\n    positions = np.where(pixels != 0)[0]\n    mask_str = ''\n    i = 0\n    pos = positions[i]\n    occ = 1\n    while i < len(positions)-1:\n        if positions[i+1] == positions[i] + 1:\n            occ += 1\n            i += 1\n        else:\n            i += 1\n            if mask_str != '':\n                mask_str += ' '\n            mask_str += f'{pos} {occ}'\n\n            pos = positions[i]\n            occ = 1\n    \n    return mask_str","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:07.251224Z","iopub.execute_input":"2021-12-16T12:54:07.251481Z","iopub.status.idle":"2021-12-16T12:54:07.258139Z","shell.execute_reply.started":"2021-12-16T12:54:07.251453Z","shell.execute_reply":"2021-12-16T12:54:07.257423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_train_t[ix]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df = pd.DataFrame(columns = ['id','predicted'])\nfor ix in range(0,3):\n    d = {'id':test_image_name[ix], 'predicted':mask_decode(preds_train_t[ix])}\n    output_df = output_df.append(d,ignore_index=True)\n\n        \n#output_df.to_csv('submission.csv', index = False)\noutput_df = output_df.set_index('id')\noutput_df","metadata":{"execution":{"iopub.status.busy":"2021-12-16T13:00:54.028276Z","iopub.execute_input":"2021-12-16T13:00:54.028543Z","iopub.status.idle":"2021-12-16T13:00:54.084404Z","shell.execute_reply.started":"2021-12-16T13:00:54.028515Z","shell.execute_reply":"2021-12-16T13:00:54.083663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-16T13:01:05.026721Z","iopub.execute_input":"2021-12-16T13:01:05.027206Z","iopub.status.idle":"2021-12-16T13:01:05.035478Z","shell.execute_reply.started":"2021-12-16T13:01:05.027169Z","shell.execute_reply":"2021-12-16T13:01:05.034564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}