{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.layers import Activation, Dropout, BatchNormalization, Flatten, Dense, AvgPool2D,MaxPool2D\nfrom tensorflow.keras.applications.vgg16 import VGG16, preprocess_input\nfrom tensorflow.keras.optimizers import Adam, SGD, RMSprop\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nfrom tensorflow.keras.preprocessing import image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-19T16:18:11.140203Z","iopub.execute_input":"2021-10-19T16:18:11.140555Z","iopub.status.idle":"2021-10-19T16:18:11.149244Z","shell.execute_reply.started":"2021-10-19T16:18:11.140523Z","shell.execute_reply":"2021-10-19T16:18:11.148090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traincsv=pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")\ntest=\"../input/sartorius-cell-instance-segmentation/test\"\ntrain=\"../input/sartorius-cell-instance-segmentation/train\"\ntrain_supervised=\"../input/sartorius-cell-instance-segmentation/train_semi_supervised\"\nprint(\"Shape: \", traincsv.shape)\ntraincsv.tail(30)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:11.210084Z","iopub.execute_input":"2021-10-19T16:18:11.210507Z","iopub.status.idle":"2021-10-19T16:18:11.738763Z","shell.execute_reply.started":"2021-10-19T16:18:11.210477Z","shell.execute_reply":"2021-10-19T16:18:11.737909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traincsv.info()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:11.740887Z","iopub.execute_input":"2021-10-19T16:18:11.741409Z","iopub.status.idle":"2021-10-19T16:18:11.824669Z","shell.execute_reply.started":"2021-10-19T16:18:11.741362Z","shell.execute_reply":"2021-10-19T16:18:11.823449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traincsv.describe()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:11.826378Z","iopub.execute_input":"2021-10-19T16:18:11.826687Z","iopub.status.idle":"2021-10-19T16:18:11.856662Z","shell.execute_reply.started":"2021-10-19T16:18:11.826642Z","shell.execute_reply":"2021-10-19T16:18:11.855777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"length of test: \", len(test))\nprint(\"length of train: \", len(train))\nprint(\"\\nNumber of unique values:\")\ntraincsv.nunique()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:11.859083Z","iopub.execute_input":"2021-10-19T16:18:11.860053Z","iopub.status.idle":"2021-10-19T16:18:11.989873Z","shell.execute_reply.started":"2021-10-19T16:18:11.860005Z","shell.execute_reply":"2021-10-19T16:18:11.988798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nax = sns.countplot(x=\"cell_type\", data=traincsv, palette=\"Set3\")","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:11.991207Z","iopub.execute_input":"2021-10-19T16:18:11.991502Z","iopub.status.idle":"2021-10-19T16:18:12.298112Z","shell.execute_reply.started":"2021-10-19T16:18:11.991459Z","shell.execute_reply":"2021-10-19T16:18:12.297203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize = (15,8))\nax = sns.countplot(x=\"plate_time\", data=traincsv, palette=\"Set2\",)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:12.299239Z","iopub.execute_input":"2021-10-19T16:18:12.299454Z","iopub.status.idle":"2021-10-19T16:18:12.647702Z","shell.execute_reply.started":"2021-10-19T16:18:12.299427Z","shell.execute_reply":"2021-10-19T16:18:12.646886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,8))\nax = sns.countplot(x=\"sample_date\", data=traincsv, palette=\"dark\")","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:12.649123Z","iopub.execute_input":"2021-10-19T16:18:12.649342Z","iopub.status.idle":"2021-10-19T16:18:13.100989Z","shell.execute_reply.started":"2021-10-19T16:18:12.649315Z","shell.execute_reply":"2021-10-19T16:18:13.100014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,8))\nax = sns.countplot(x=\"elapsed_timedelta\", data=traincsv, palette=\"colorblind\",)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:13.102236Z","iopub.execute_input":"2021-10-19T16:18:13.102772Z","iopub.status.idle":"2021-10-19T16:18:13.455905Z","shell.execute_reply.started":"2021-10-19T16:18:13.102737Z","shell.execute_reply":"2021-10-19T16:18:13.455288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image    \nim1_path=\"../input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png\"\ntest_image=image.load_img(im1_path,target_size=(256,256))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:13.456848Z","iopub.execute_input":"2021-10-19T16:18:13.457569Z","iopub.status.idle":"2021-10-19T16:18:13.475671Z","shell.execute_reply.started":"2021-10-19T16:18:13.457518Z","shell.execute_reply":"2021-10-19T16:18:13.474935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:13.477882Z","iopub.execute_input":"2021-10-19T16:18:13.478435Z","iopub.status.idle":"2021-10-19T16:18:13.482029Z","shell.execute_reply.started":"2021-10-19T16:18:13.478402Z","shell.execute_reply":"2021-10-19T16:18:13.481224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nassert os.path.isdir(train) == True\nsample_train_images = list(os.walk(train))[0][2][:8]\nsample_train_images = list(map(lambda x: os.path.join(train, x), sample_train_images))\nplt.figure(figsize = (17,17))\nfor iterator, filename in enumerate(sample_train_images):\n    image = Image.open(filename)\n    plt.subplot(4,2,iterator+1)\n    plt.imshow(image,cmap='bone_r')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:13.483538Z","iopub.execute_input":"2021-10-19T16:18:13.483751Z","iopub.status.idle":"2021-10-19T16:18:15.883620Z","shell.execute_reply.started":"2021-10-19T16:18:13.483725Z","shell.execute_reply":"2021-10-19T16:18:15.881524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert os.path.isdir(test) == True\nsample_test_images = list(os.walk(test))[0][2][:8]\nsample_test_images = list(map(lambda x: os.path.join(test, x), sample_test_images))\nplt.figure(figsize = (17,17))\nfor iterator, filename in enumerate(sample_test_images):\n    image = Image.open(filename)\n    plt.subplot(4,2,iterator+1)\n    plt.imshow(image,cmap='bone_r')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:15.885080Z","iopub.execute_input":"2021-10-19T16:18:15.885847Z","iopub.status.idle":"2021-10-19T16:18:16.858697Z","shell.execute_reply.started":"2021-10-19T16:18:15.885794Z","shell.execute_reply":"2021-10-19T16:18:16.858016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert os.path.isdir(train_supervised) == True\nsample_trainsemi_images = list(os.walk(train_supervised))[0][2][:8]\nsample_trainsemi_images = list(map(lambda x: os.path.join(train_supervised, x), sample_trainsemi_images))\nplt.figure(figsize = (17,17))\nfor iterator, filename in enumerate(sample_trainsemi_images):\n    image = Image.open(filename)\n    plt.subplot(4,2,iterator+1)\n    plt.imshow(image,cmap='bone_r')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:18:16.859726Z","iopub.execute_input":"2021-10-19T16:18:16.860615Z","iopub.status.idle":"2021-10-19T16:18:20.408068Z","shell.execute_reply.started":"2021-10-19T16:18:16.860578Z","shell.execute_reply":"2021-10-19T16:18:20.407057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install segmentation-models","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:51:58.530458Z","iopub.execute_input":"2021-10-19T16:51:58.530851Z","iopub.status.idle":"2021-10-19T16:52:09.019519Z","shell.execute_reply.started":"2021-10-19T16:51:58.530800Z","shell.execute_reply":"2021-10-19T16:52:09.018270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install git+https://github.com/qubvel/segmentation_models","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:52:26.985681Z","iopub.execute_input":"2021-10-19T16:52:26.985991Z","iopub.status.idle":"2021-10-19T16:52:37.403660Z","shell.execute_reply.started":"2021-10-19T16:52:26.985958Z","shell.execute_reply":"2021-10-19T16:52:37.402445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\nfrom keras.losses import binary_crossentropy\nfrom segmentation_models.losses import bce_jaccard_loss\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\nimport tensorflow as tf\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\n\ndef iou_coef(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 iou\ndef dice_loss(y_true, y_pred):\n    smooth = 1.\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = y_true_f * y_pred_f\n    score = (2. * K.sum(intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1. - score\n\ndef bce_dice_loss(y_true, y_pred):\n    return binary_crossentropy(tf.cast(y_true, tf.float32), y_pred) + 0.5 * dice_loss(tf.cast(y_true, tf.float32), y_pred)\nimport segmentation_models as sm\nsm.set_framework('tf.keras')\nsm.framework()\n\nfrom segmentation_models import Unet\nfrom segmentation_models.utils import set_trainable\n\n\nmodel = Unet('efficientnetb7',input_shape=(512, 512, 3), classes=3, activation='sigmoid',encoder_weights='imagenet')\nmodel.compile(optimizer='adam', loss=bce_dice_loss,metrics=[dice_coef,iou_coef,'accuracy']) #bce_dice_loss binary_crossentropy\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T16:54:31.464270Z","iopub.execute_input":"2021-10-19T16:54:31.465179Z","iopub.status.idle":"2021-10-19T16:54:42.876311Z","shell.execute_reply.started":"2021-10-19T16:54:31.465125Z","shell.execute_reply":"2021-10-19T16:54:42.875296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Please Upvote, Work in progress","metadata":{}}]}