{"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":"markdown","source":"## Cell Instance Segmentation - Deep Learning Project - Emirhan BULUT","metadata":{"id":"QxX8pz5quVQV"}},{"cell_type":"markdown","source":"Hello!\n\nI am Emirhan! I am Machine Learning and Deep Learning Engineer. I am very pleased to present to you the artificial intelligence software that I have carefully prepared for the 'Sartorius - Cell Instance Segmentation' competition on Kaggle. This software; Thanks to the high accuracy and low loss system it contains, it detects single neuronal cells in microscopy images according to the rules set by the artificial neuronal networks I have created. In addition, I present the schematic of the model I developed in a .png format with high resolution.\n\nIn addition, although the software took a long time to complete due to the insufficient hardware I have, I waited for this time to end for the people in the world and completed the artificial intelligence software.\n\nThe artificial intelligence software I developed was first in a 9-pack.\n\nFinally, I developed deep learning (Artificial neural networks) software segmentation that can detect different objects of interest with 97.19% accuracy in biological images showing neuronal cell types.","metadata":{"id":"DvkdSwj5bGGy","execution":{"iopub.status.busy":"2021-10-22T00:01:29.39993Z","iopub.execute_input":"2021-10-22T00:01:29.400252Z","iopub.status.idle":"2021-10-22T00:01:29.427272Z","shell.execute_reply.started":"2021-10-22T00:01:29.400163Z","shell.execute_reply":"2021-10-22T00:01:29.425728Z"}}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\n\ntrain_datagen = ImageDataGenerator(\n    rescale = 1. / 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\n\ntrain_generator = train_datagen.flow_from_directory(\n    '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_train_val_images',\n    target_size=(32, 32),\n    batch_size=64,\n    class_mode='categorical')\n\nprint(train_generator.image_shape)\n\ntest_datagen = ImageDataGenerator(\n    rescale = 1. / 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\n\ntest_generator = test_datagen.flow_from_directory(\n    '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_test_images',\n    target_size=(32, 32),\n    batch_size=64,\n    class_mode='categorical')\n\nprint(test_generator.image_shape)","metadata":{"id":"zPPQwcINMDPt","outputId":"2750e9f5-090d-48a6-d186-f9cfcfd1bcf5","execution":{"iopub.status.busy":"2021-10-22T00:38:36.689103Z","iopub.execute_input":"2021-10-22T00:38:36.68978Z","iopub.status.idle":"2021-10-22T00:38:42.415143Z","shell.execute_reply.started":"2021-10-22T00:38:36.689643Z","shell.execute_reply":"2021-10-22T00:38:42.414412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot images :)\n\nimport numpy as np\n\nfrom matplotlib import pyplot\n\nx=np.concatenate([train_generator.next()[0] for i in range(train_generator.__len__())])\n\n# plot, first of few images\nfor i in range(9):\n\t# define of subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# plot, raw pixel of data\n\tpyplot.imshow(x[i], cmap=pyplot.get_cmap('gray'))\n# show of the figure\npyplot.show()","metadata":{"id":"yH3J8KMFdc8a","outputId":"ca0d22b6-29ac-4b41-d6be-d1383346935a","execution":{"iopub.status.busy":"2021-10-22T00:38:42.416846Z","iopub.execute_input":"2021-10-22T00:38:42.417581Z","iopub.status.idle":"2021-10-22T00:39:36.378286Z","shell.execute_reply.started":"2021-10-22T00:38:42.417537Z","shell.execute_reply":"2021-10-22T00:39:36.377598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras import layers\nfrom keras import utils\nfrom keras import Sequential\n\nfunction = Sequential()\n\ndef make_model(input_shape, num_classes):\n    inputs = keras.Input(shape=input_shape)\n\n    x = function(inputs)\n\n\n    x = layers.Rescaling(1.0 / 255)(x)\n    x = layers.Conv2D(32, 3, strides=2, padding=\"same\")(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.Activation(\"relu\")(x)\n\n    x = layers.Conv2D(64, 3, padding=\"same\")(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.Activation(\"relu\")(x)\n\n    previous_block_activation = x \n\n    for size in [128, 256, 552, 945]:\n        x = layers.Activation(\"relu\")(x)\n        x = layers.SeparableConv2D(size, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.Activation(\"relu\")(x)\n        x = layers.SeparableConv2D(size, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.MaxPooling2D(3, strides=2, padding=\"same\")(x)\n\n        \n        residual = layers.Conv2D(size, 1, strides=2, padding=\"same\")(\n            previous_block_activation\n        )\n        x = layers.add([x, residual]) \n        previous_block_activation = x \n\n    x = layers.SeparableConv2D(1540, 3, padding=\"same\")(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.Activation(\"relu\")(x)\n\n    x = layers.GlobalAveragePooling2D()(x)\n    if num_classes == 2:\n        activation = \"sigmoid\"\n        units = 1\n    else:\n        activation = \"softmax\"\n        units = num_classes\n\n    x = layers.Dropout(0.5)(x)\n    outputs = layers.Dense(units, activation=activation)(x)\n    return keras.Model(inputs, outputs)\n\nfrom keras.utils.vis_utils import plot_model\n\nmodel = make_model(input_shape=(32,32,3), num_classes=9)\nplot_model(model, show_shapes=True)","metadata":{"id":"Ov7XbnBNOGx1","outputId":"2f4a1fd7-1a12-4e99-a7cd-ff0056667ee1","execution":{"iopub.status.busy":"2021-10-22T00:39:36.379583Z","iopub.execute_input":"2021-10-22T00:39:36.380007Z","iopub.status.idle":"2021-10-22T00:39:40.336245Z","shell.execute_reply.started":"2021-10-22T00:39:36.379967Z","shell.execute_reply":"2021-10-22T00:39:40.333768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n       optimizer='adam',\n       loss=\"categorical_crossentropy\",\n       metrics=['accuracy'])\n\nprint(train_generator)\n\nmodel.fit(train_generator,batch_size=16,epochs=200,shuffle=True)\n\nmodel.save('final_model.h5')\n\n\nmodel.summary()","metadata":{"id":"GzeqJP3NPuxP","outputId":"a4a9b6a9-1a65-4719-c275-002112896bd0","execution":{"iopub.status.busy":"2021-10-22T00:39:40.337915Z","iopub.execute_input":"2021-10-22T00:39:40.338252Z","iopub.status.idle":"2021-10-22T02:41:29.400689Z","shell.execute_reply.started":"2021-10-22T00:39:40.338216Z","shell.execute_reply":"2021-10-22T02:41:29.396074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = test_generator.filenames\nnb_samples = len(filenames)\n\npredict = model.predict(test_generator,nb_samples)\n\nprint(predict)","metadata":{"id":"yq9JvjlIjkFW","outputId":"dae82be5-6ef6-4cc2-b890-4d029ad7c1c4","execution":{"iopub.status.busy":"2021-10-22T02:41:29.403202Z","iopub.execute_input":"2021-10-22T02:41:29.403446Z","iopub.status.idle":"2021-10-22T02:41:56.131092Z","shell.execute_reply.started":"2021-10-22T02:41:29.403411Z","shell.execute_reply":"2021-10-22T02:41:56.130339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom keras.models import load_model\n\nimport numpy as np\n\nimg = cv2.imread('../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_test_images/BV2/BV2_Phase_A4_1_00d00h00m_2.tif')\nimg = cv2.resize(img,(32,32))\nimg = np.reshape(img,[1,32,32,3])\n\nmodell = load_model('./final_model.h5')\n\n\nclasses = modell.predict(img)\n\n\nfor class_name in classes[0]:\n  if 1.0 == classes[0][0]:\n    print(\"Image in A172 Class\")\n  elif 1.0 == classes[0][1]:\n    print(\"Image in BT474 Class\")\n  elif 1.0 == classes[0][2]:\n    print(\"Image in BV2 Class\")\n    break\n  elif 1.0 == classes[0][3]:\n    print(\"Image in Huh7 Class\")\n  elif 1.0 == classes[0][4]:\n    print(\"Image in MCF7 Class\")\n  elif 1.0 == classes[0][5]:\n    print(\"Image in RatC6 Class\")\n  elif 1.0 == classes[0][6]:\n    print(\"Image in SHSY5Y Class\")\n  elif 1.0 == classes[0][7]:\n    print(\"Image in SkBr3 Class\")\n  else:\n    print(\"Image in SKOV3 Class\")","metadata":{"id":"SvxOKwPRGJf1","outputId":"1f41bd11-ee87-4cc7-d69f-50703ab44b79","execution":{"iopub.status.busy":"2021-10-22T02:41:56.132375Z","iopub.execute_input":"2021-10-22T02:41:56.13262Z","iopub.status.idle":"2021-10-22T02:41:57.414355Z","shell.execute_reply.started":"2021-10-22T02:41:56.132581Z","shell.execute_reply":"2021-10-22T02:41:57.413038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pixellib","metadata":{"id":"1Mbk151whUWT","outputId":"11969ca0-1401-4047-bb43-301d80f20801","execution":{"iopub.status.busy":"2021-10-22T02:41:57.415613Z","iopub.execute_input":"2021-10-22T02:41:57.415886Z","iopub.status.idle":"2021-10-22T02:42:18.471426Z","shell.execute_reply.started":"2021-10-22T02:41:57.415851Z","shell.execute_reply":"2021-10-22T02:42:18.470596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pixellib\nfrom pixellib.semantic import semantic_segmentation \nsegment_image = semantic_segmentation()","metadata":{"id":"dMS8xoiMhR9p","execution":{"iopub.status.busy":"2021-10-22T02:42:18.472937Z","iopub.execute_input":"2021-10-22T02:42:18.473195Z","iopub.status.idle":"2021-10-22T02:42:23.152764Z","shell.execute_reply.started":"2021-10-22T02:42:18.47316Z","shell.execute_reply":"2021-10-22T02:42:23.151988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgg = cv2.imread('../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_test_images/MCF7/MCF7_Phase_H4_1_00d00h00m_3.tif')\nimgg = cv2.resize(imgg,(704,520))\nprint(imgg.shape)\n\nfrom PIL import Image\nimport numpy as np\n\nimage = Image.fromarray(imgg)\nimage.save('testtt.png')\nimage.show()\n","metadata":{"id":"pfOGtvGGhmAK","outputId":"d85da671-a26f-4688-d2d3-2a445c3d7be2","execution":{"iopub.status.busy":"2021-10-22T02:42:23.153968Z","iopub.execute_input":"2021-10-22T02:42:23.155896Z","iopub.status.idle":"2021-10-22T02:42:23.35932Z","shell.execute_reply.started":"2021-10-22T02:42:23.155862Z","shell.execute_reply":"2021-10-22T02:42:23.358287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodels = load_model('./final_model.h5')\n\nmodelss = semantic_segmentation(models)\n\nmodelss.segmentAsPascalvoc(\"./testtt.png\", output_image_name = \"image_new.png\")","metadata":{"id":"pekUnDnur5eD","outputId":"43784817-4b15-4682-835d-7ba973e64d30","execution":{"iopub.status.busy":"2021-10-22T02:42:23.362388Z","iopub.execute_input":"2021-10-22T02:42:23.362615Z","iopub.status.idle":"2021-10-22T02:42:31.815117Z","shell.execute_reply.started":"2021-10-22T02:42:23.362583Z","shell.execute_reply":"2021-10-22T02:42:31.814383Z"},"trusted":true},"execution_count":null,"outputs":[]}]}