{"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":"# to process and visualize type data\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# utilities\nimport os\nimport shutil\nimport random\nimport string\n\n# fixed figure size\nplt.rcParams[\"figure.figsize\"] = (21, 12)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-19T20:41:38.709216Z","iopub.execute_input":"2022-11-19T20:41:38.709580Z","iopub.status.idle":"2022-11-19T20:41:38.715615Z","shell.execute_reply.started":"2022-11-19T20:41:38.709547Z","shell.execute_reply":"2022-11-19T20:41:38.714401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.applications.resnet import ResNet101\n\n# image preprocessing\nfrom tensorflow.keras.preprocessing.image import img_to_array, ImageDataGenerator, load_img\n\n# to build model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Dense, MaxPooling2D, Dropout, Flatten, BatchNormalization\nfrom tensorflow.keras.activations import softmax\n\n# cost function / optimizer\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:38.883779Z","iopub.execute_input":"2022-11-19T20:41:38.884103Z","iopub.status.idle":"2022-11-19T20:41:38.891306Z","shell.execute_reply.started":"2022-11-19T20:41:38.884074Z","shell.execute_reply":"2022-11-19T20:41:38.890203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT, WIDTH = (224, 224)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:39.055748Z","iopub.execute_input":"2022-11-19T20:41:39.056590Z","iopub.status.idle":"2022-11-19T20:41:39.061385Z","shell.execute_reply.started":"2022-11-19T20:41:39.056551Z","shell.execute_reply":"2022-11-19T20:41:39.060347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:39.244540Z","iopub.execute_input":"2022-11-19T20:41:39.245525Z","iopub.status.idle":"2022-11-19T20:41:39.521766Z","shell.execute_reply.started":"2022-11-19T20:41:39.245482Z","shell.execute_reply":"2022-11-19T20:41:39.520525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dff = df.drop([\"annotation\", \"width\", \"height\", \"plate_time\", \"sample_date\", \"sample_id\", \"elapsed_timedelta\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:39.523977Z","iopub.execute_input":"2022-11-19T20:41:39.524367Z","iopub.status.idle":"2022-11-19T20:41:39.531738Z","shell.execute_reply.started":"2022-11-19T20:41:39.524326Z","shell.execute_reply":"2022-11-19T20:41:39.530624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dff.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:39.603723Z","iopub.execute_input":"2022-11-19T20:41:39.604713Z","iopub.status.idle":"2022-11-19T20:41:39.614839Z","shell.execute_reply.started":"2022-11-19T20:41:39.604672Z","shell.execute_reply":"2022-11-19T20:41:39.613605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEMI_PATH = \"../input/sartorius-cell-instance-segmentation/train_semi_supervised\"\npatos = []\n\nfor f in os.listdir(SEMI_PATH):\n    pato = os.path.join(SEMI_PATH, f)\n    patos.append(pato)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:39.760579Z","iopub.execute_input":"2022-11-19T20:41:39.760883Z","iopub.status.idle":"2022-11-19T20:41:39.771882Z","shell.execute_reply.started":"2022-11-19T20:41:39.760855Z","shell.execute_reply":"2022-11-19T20:41:39.770979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"../input/sartorius-cell-instance-segmentation/train/\"\npaths = []\n\nfor i in range(len(dff)):\n    path = os.path.join(TRAIN_PATH, dff.iloc[i][\"id\"]) + \".png\"\n    paths.append(path)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:39.948616Z","iopub.execute_input":"2022-11-19T20:41:39.948966Z","iopub.status.idle":"2022-11-19T20:41:44.335827Z","shell.execute_reply.started":"2022-11-19T20:41:39.948914Z","shell.execute_reply":"2022-11-19T20:41:44.334840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dff[\"paths\"] = paths\ndff.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:44.337593Z","iopub.execute_input":"2022-11-19T20:41:44.338441Z","iopub.status.idle":"2022-11-19T20:41:44.354227Z","shell.execute_reply.started":"2022-11-19T20:41:44.338404Z","shell.execute_reply":"2022-11-19T20:41:44.353004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"semi_types = []\n\nfor i in range(len(patos)):\n    semi_type = patos[i].split(\"[\")[0].split(\"/\")[-1]\n    if semi_type == \"astros\":\n        semi_type = \"astro\"\n    semi_types.append(semi_type)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:44.355562Z","iopub.execute_input":"2022-11-19T20:41:44.356088Z","iopub.status.idle":"2022-11-19T20:41:44.368051Z","shell.execute_reply.started":"2022-11-19T20:41:44.356050Z","shell.execute_reply":"2022-11-19T20:41:44.367229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {\n    \"semi_type\": semi_types,\n    \"paths\": patos,\n}","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:44.370596Z","iopub.execute_input":"2022-11-19T20:41:44.371291Z","iopub.status.idle":"2022-11-19T20:41:44.380088Z","shell.execute_reply.started":"2022-11-19T20:41:44.371257Z","shell.execute_reply":"2022-11-19T20:41:44.379278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"semi_df = pd.DataFrame(data)\nsemi_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:44.381470Z","iopub.execute_input":"2022-11-19T20:41:44.381996Z","iopub.status.idle":"2022-11-19T20:41:44.396558Z","shell.execute_reply.started":"2022-11-19T20:41:44.381938Z","shell.execute_reply":"2022-11-19T20:41:44.395483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = \"./classification\"\n\nTRAIN_CL_PATH = os.path.join(BASE_DIR, \"training_classification\")\nVAL_CL_PATH = os.path.join(BASE_DIR, \"validation_classification\")\n\nif os.path.exists(BASE_DIR):\n    shutil.rmtree(BASE_DIR)\n\nos.makedirs(BASE_DIR)\nos.makedirs(TRAIN_CL_PATH)\nos.makedirs(VAL_CL_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:44.398291Z","iopub.execute_input":"2022-11-19T20:41:44.398632Z","iopub.status.idle":"2022-11-19T20:41:44.410828Z","shell.execute_reply.started":"2022-11-19T20:41:44.398598Z","shell.execute_reply":"2022-11-19T20:41:44.409677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_types = semi_df[\"semi_type\"].unique()\ncell_types","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:44.413283Z","iopub.execute_input":"2022-11-19T20:41:44.413941Z","iopub.status.idle":"2022-11-19T20:41:44.422787Z","shell.execute_reply.started":"2022-11-19T20:41:44.413907Z","shell.execute_reply":"2022-11-19T20:41:44.421898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cell_type in cell_types:\n    os.makedirs(os.path.join(TRAIN_CL_PATH, cell_type))\n    os.makedirs(os.path.join(VAL_CL_PATH, cell_type))","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:46.496040Z","iopub.execute_input":"2022-11-19T20:41:46.496408Z","iopub.status.idle":"2022-11-19T20:41:46.502225Z","shell.execute_reply.started":"2022-11-19T20:41:46.496377Z","shell.execute_reply":"2022-11-19T20:41:46.501021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_val_generators(train_dir, val_dir):\n    \"\"\" Returns train and validation image data generators \"\"\"\n    train_gen = ImageDataGenerator(rescale=1/255.,\n                                  )\n    val_gen = ImageDataGenerator(rescale=1/255.,\n                                )\n    train_generator = train_gen.flow_from_directory(directory=train_dir,\n                                                    batch_size=16,\n                                                    class_mode=\"categorical\",\n                                                    target_size=(HEIGHT, WIDTH),\n                                                    shuffle=True,\n                                                   color_mode=\"grayscale\")\n    val_generator = val_gen.flow_from_directory(directory=val_dir,\n                                               batch_size=16,\n                                               class_mode=\"categorical\",\n                                               target_size=(HEIGHT, WIDTH),\n                                               shuffle=True,\n                                               color_mode=\"grayscale\")\n    return train_generator, val_generator","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:43:07.021809Z","iopub.execute_input":"2022-11-19T20:43:07.022746Z","iopub.status.idle":"2022-11-19T20:43:07.030383Z","shell.execute_reply.started":"2022-11-19T20:43:07.022710Z","shell.execute_reply":"2022-11-19T20:43:07.029335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_data(source, train_path, val_path, split_size=0.8):\n    \"\"\" Returns None. Shuffles and splits data as given split_size.\n        Then, copies data to the created directories, so that the power of data generators usage will be enabled.\n    \"\"\"\n    all_files = []\n    for path in semi_df[\"paths\"]:\n        if os.path.getsize(path):\n            all_files.append(path)\n        else:\n            print(f\"{path} has zero size, so skipping.\")\n    n_files = len(all_files)\n    split_point = int(split_size * n_files)\n    shuffled_files = random.sample(all_files, n_files)\n\n    train_image = shuffled_files[:split_point]\n    val_image = shuffled_files[split_point:]\n\n    for train_cell in train_image:\n        cell_type = train_cell.split(\"[\")[0].split(\"/\")[-1]\n        to_where = os.path.join(train_path, cell_type)\n        shutil.copy(train_cell, to_where)\n\n    for val_cell in val_image:\n        cell_type = val_cell.split(\"[\")[0].split(\"/\")[-1]\n        to_where = os.path.join(val_path, cell_type)\n        shutil.copy(val_cell, to_where)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:46.821391Z","iopub.execute_input":"2022-11-19T20:41:46.821754Z","iopub.status.idle":"2022-11-19T20:41:46.833150Z","shell.execute_reply.started":"2022-11-19T20:41:46.821714Z","shell.execute_reply":"2022-11-19T20:41:46.832044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split_data(SEMI_PATH, TRAIN_CL_PATH, VAL_CL_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T20:41:49.221692Z","iopub.execute_input":"2022-11-19T20:41:49.222084Z","iopub.status.idle":"2022-11-19T20:41:52.024905Z","shell.execute_reply.started":"2022-11-19T20:41:49.222050Z","shell.execute_reply":"2022-11-19T20:41:52.023911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen, val_gen = train_val_generators(TRAIN_CL_PATH, VAL_CL_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:20:40.625443Z","iopub.execute_input":"2022-11-19T21:20:40.625815Z","iopub.status.idle":"2022-11-19T21:20:40.837115Z","shell.execute_reply.started":"2022-11-19T21:20:40.625782Z","shell.execute_reply":"2022-11-19T21:20:40.836012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()\n\nmodel_classification = Sequential([\n    Conv2D(20, (3,3), activation=\"relu\", input_shape=(WIDTH, HEIGHT, 1)),\n    BatchNormalization(),\n    MaxPooling2D(2,2),\n    Dropout(0.2),\n\n    Flatten(),\n    Dense(16, activation=\"relu\"),\n    Dense(3, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:42:18.551561Z","iopub.execute_input":"2022-11-19T21:42:18.552395Z","iopub.status.idle":"2022-11-19T21:42:18.608802Z","shell.execute_reply.started":"2022-11-19T21:42:18.552355Z","shell.execute_reply":"2022-11-19T21:42:18.607888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=7)","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:42:19.463801Z","iopub.execute_input":"2022-11-19T21:42:19.464381Z","iopub.status.idle":"2022-11-19T21:42:19.469140Z","shell.execute_reply.started":"2022-11-19T21:42:19.464344Z","shell.execute_reply":"2022-11-19T21:42:19.468126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optim = tf.keras.optimizers.Adam(learning_rate=0.00099)\noptim = tf.keras.optimizers.Adam(learning_rate=0.00099)\n\nmodel_classification.compile(optimizer=optim, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:42:27.999436Z","iopub.execute_input":"2022-11-19T21:42:27.999782Z","iopub.status.idle":"2022-11-19T21:42:28.017586Z","shell.execute_reply.started":"2022-11-19T21:42:27.999753Z","shell.execute_reply":"2022-11-19T21:42:28.016274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_filepath = './'\nmodel_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=True,\n    monitor='val_accuracy',\n    mode='max',\n    save_best_only=True)\n\nhistory = model_classification.fit(train_gen,\n                                   validation_data=val_gen,\n                                   epochs=200,\n                                   callbacks=[callback, model_checkpoint_callback])","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:42:28.380984Z","iopub.execute_input":"2022-11-19T21:42:28.381659Z","iopub.status.idle":"2022-11-19T21:44:30.364117Z","shell.execute_reply.started":"2022-11-19T21:42:28.381623Z","shell.execute_reply":"2022-11-19T21:44:30.363017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the results\n\neps = range(len(history.history[\"loss\"]))\nplt.figure(figsize=(10, 6))\nplt.plot(eps, history.history[\"loss\"])\nplt.plot(eps, history.history[\"val_loss\"])\nplt.legend([\"loss\", \"val_loss\"])\nplt.savefig(\"cell_classification_2021_loss.png\")\n\neps = range(len(history.history[\"accuracy\"]))\nplt.figure(figsize=(10, 6))\nplt.plot(eps, history.history[\"accuracy\"])\nplt.plot(eps, history.history[\"val_accuracy\"])\nplt.legend([\"accuracy\", \"val_accuracy\"])\nplt.savefig(\"cell_classification_2021_acc.png\")","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:44:41.905140Z","iopub.execute_input":"2022-11-19T21:44:41.905510Z","iopub.status.idle":"2022-11-19T21:44:42.466048Z","shell.execute_reply.started":"2022-11-19T21:44:41.905479Z","shell.execute_reply":"2022-11-19T21:44:42.464924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_classification.save(\"semi_cell_classification_lastt.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:44:55.665854Z","iopub.execute_input":"2022-11-19T21:44:55.666567Z","iopub.status.idle":"2022-11-19T21:44:55.752796Z","shell.execute_reply.started":"2022-11-19T21:44:55.666529Z","shell.execute_reply":"2022-11-19T21:44:55.751842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# actually astro\np = '../input/sartorius-cell-instance-segmentation/train_semi_supervised/astro[hippo]_H3-2_Vessel-361_2020-09-16_13h00m00s_Ph_2.png'\npp = np.resize(plt.imread(p), (HEIGHT, WIDTH))\n\n\npred = model_classification.predict(pp[np.newaxis, :, :, np.newaxis])\nprint(f\"{p.split('/')[-1]} is predicted as {cell_types[np.argmax(pred)]}\")","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:54:56.440401Z","iopub.execute_input":"2022-11-19T21:54:56.440763Z","iopub.status.idle":"2022-11-19T21:54:56.499541Z","shell.execute_reply.started":"2022-11-19T21:54:56.440731Z","shell.execute_reply":"2022-11-19T21:54:56.498619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# actually shsy5y\np = '../input/sartorius-cell-instance-segmentation/train_semi_supervised/shsy5y[diff]_D7-4_Vessel-714_2019-06-15_11h30m00s_Ph_1.png'\npp = np.resize(plt.imread(p), (HEIGHT, WIDTH))\n\n\npred = model_classification.predict(pp[np.newaxis, :, :, np.newaxis])\nprint(f\"{p.split('/')[-1]} is predicted as {cell_types[np.argmax(pred)]}\")","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:53:51.060603Z","iopub.execute_input":"2022-11-19T21:53:51.061189Z","iopub.status.idle":"2022-11-19T21:53:51.187083Z","shell.execute_reply.started":"2022-11-19T21:53:51.061150Z","shell.execute_reply":"2022-11-19T21:53:51.185882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# actually shsy5y\np = '../input/sartorius-cell-instance-segmentation/train_semi_supervised/shsy5y[diff]_E1-3_Vessel-714_2019-06-16_11h30m00s_Ph_1.png'\npp = np.resize(plt.imread(p), (HEIGHT, WIDTH))\n\n\npred = model_classification.predict(pp[np.newaxis, :, :, np.newaxis])\nprint(f\"{p.split('/')[-1]} is predicted as {cell_types[np.argmax(pred)]}\")","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:56:16.147635Z","iopub.execute_input":"2022-11-19T21:56:16.148054Z","iopub.status.idle":"2022-11-19T21:56:16.207566Z","shell.execute_reply.started":"2022-11-19T21:56:16.148000Z","shell.execute_reply":"2022-11-19T21:56:16.206487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# actually shsy5y\np = '../input/sartorius-cell-instance-segmentation/train_semi_supervised/shsy5y[diff]_D5-4_Vessel-714_2019-06-16_11h30m00s_Ph_3.png'\npp = np.resize(plt.imread(p), (HEIGHT, WIDTH))\n\n\npred = model_classification.predict(pp[np.newaxis, :, :, np.newaxis])\nprint(f\"{p.split('/')[-1]} is predicted as {cell_types[np.argmax(pred)]}\")","metadata":{"execution":{"iopub.status.busy":"2022-11-19T21:58:04.580444Z","iopub.execute_input":"2022-11-19T21:58:04.580812Z","iopub.status.idle":"2022-11-19T21:58:04.639572Z","shell.execute_reply.started":"2022-11-19T21:58:04.580780Z","shell.execute_reply":"2022-11-19T21:58:04.638627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* shsy5y is the hardest one to classify. the first and last images look like astro and cort but the mid images look like shsy5y more and easy to classify.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}