{"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":"!unzip -oq /kaggle/input/carvana-image-masking-challenge/train.zip\n!unzip -oq /kaggle/input/carvana-image-masking-challenge/train_masks.zip\n!unzip -oq /kaggle/input/carvana-image-masking-challenge/train_masks.csv.zip","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:47:19.337452Z","iopub.execute_input":"2022-11-04T15:47:19.337855Z","iopub.status.idle":"2022-11-04T15:47:34.293665Z","shell.execute_reply.started":"2022-11-04T15:47:19.337771Z","shell.execute_reply":"2022-11-04T15:47:34.292532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:34.297107Z","iopub.execute_input":"2022-11-04T15:47:34.297968Z","iopub.status.idle":"2022-11-04T15:47:34.457168Z","shell.execute_reply.started":"2022-11-04T15:47:34.297924Z","shell.execute_reply":"2022-11-04T15:47:34.456095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape=(1280, 1918, 1)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    '''\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n\n    s = mask_rle.split()\n    \n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    \n    ends = starts + lengths    \n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n        \n    img = img.reshape(shape)\n    return img\n","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:34.460440Z","iopub.execute_input":"2022-11-04T15:47:34.461069Z","iopub.status.idle":"2022-11-04T15:47:34.468865Z","shell.execute_reply.started":"2022-11-04T15:47:34.461022Z","shell.execute_reply":"2022-11-04T15:47:34.467986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('./train_masks.csv')\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:38.510463Z","iopub.execute_input":"2022-11-04T15:47:38.510837Z","iopub.status.idle":"2022-11-04T15:47:38.899702Z","shell.execute_reply.started":"2022-11-04T15:47:38.510804Z","shell.execute_reply":"2022-11-04T15:47:38.897612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_size = 0.2\nval_split = int(val_size * df.shape[0])\n\ntrain_df = df[:-val_split]\nval_df = df[-val_split:]\n\ntrain_df.shape, val_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:38.901413Z","iopub.execute_input":"2022-11-04T15:47:38.901730Z","iopub.status.idle":"2022-11-04T15:47:38.911313Z","shell.execute_reply.started":"2022-11-04T15:47:38.901701Z","shell.execute_reply":"2022-11-04T15:47:38.910362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:39.106607Z","iopub.execute_input":"2022-11-04T15:47:39.106976Z","iopub.status.idle":"2022-11-04T15:47:39.131202Z","shell.execute_reply.started":"2022-11-04T15:47:39.106943Z","shell.execute_reply":"2022-11-04T15:47:39.130338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_name, mask_rle = train_df.iloc[0]\n\nimg = cv2.imread(f'./train/{img_name}')\nmask = rle_decode(mask_rle)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:39.439582Z","iopub.execute_input":"2022-11-04T15:47:39.439930Z","iopub.status.idle":"2022-11-04T15:47:39.513881Z","shell.execute_reply.started":"2022-11-04T15:47:39.439900Z","shell.execute_reply":"2022-11-04T15:47:39.512946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(25, 25))\nax[0].imshow(img)\nax[1].imshow(mask)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:39.753726Z","iopub.execute_input":"2022-11-04T15:47:39.754060Z","iopub.status.idle":"2022-11-04T15:47:40.691944Z","shell.execute_reply.started":"2022-11-04T15:47:39.754029Z","shell.execute_reply":"2022-11-04T15:47:40.690635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sample(1)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:40.693845Z","iopub.execute_input":"2022-11-04T15:47:40.694164Z","iopub.status.idle":"2022-11-04T15:47:40.709863Z","shell.execute_reply.started":"2022-11-04T15:47:40.694132Z","shell.execute_reply":"2022-11-04T15:47:40.709055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def keras_generator(df, batch_size, preprocess_input=None):\n    while True:\n        X_batch = []\n        y_batch = []\n        \n        for i in range(batch_size):\n            img_name, mask_rle = df.sample(1).values[0]\n            img = cv2.imread(f'./train/{img_name}')\n            mask = rle_decode(mask_rle)\n            \n            img = cv2.resize(img, (256, 256))\n            mask = cv2.resize(mask, (256, 256))\n            \n            X_batch += [img]\n            y_batch += [mask]\n\n        if preprocess_input:\n            X_batch = preprocess_input(np.array(X_batch))\n        else:\n            X_batch = np.array(X_batch) / 255.0\n            \n        y_batch = np.array(y_batch, dtype='float')\n\n        yield X_batch, y_batch","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:40.711290Z","iopub.execute_input":"2022-11-04T15:47:40.712051Z","iopub.status.idle":"2022-11-04T15:47:40.720680Z","shell.execute_reply.started":"2022-11-04T15:47:40.712009Z","shell.execute_reply":"2022-11-04T15:47:40.719739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for X, y in keras_generator(val_df, batch_size=32):\n    print(X.shape, y.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:40.723488Z","iopub.execute_input":"2022-11-04T15:47:40.724247Z","iopub.status.idle":"2022-11-04T15:47:41.669192Z","shell.execute_reply.started":"2022-11-04T15:47:40.724206Z","shell.execute_reply":"2022-11-04T15:47:41.668111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## FCN\n<img src='https://production-media.paperswithcode.com/methods/new_alex-model.jpg' width=550>","metadata":{}},{"cell_type":"code","source":"import keras\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D, Dropout, UpSampling2D, Conv2D, MaxPooling2D, Activation\n\n\nbase_model = VGG16(weights='imagenet', input_shape=(256, 256, 3), include_top=False)\nbase_out = base_model.output\nbase_out","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:41.674971Z","iopub.execute_input":"2022-11-04T15:47:41.675382Z","iopub.status.idle":"2022-11-04T15:47:51.371581Z","shell.execute_reply.started":"2022-11-04T15:47:41.675328Z","shell.execute_reply":"2022-11-04T15:47:51.370602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"up = UpSampling2D(32)(base_out)\nup","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:51.373566Z","iopub.execute_input":"2022-11-04T15:47:51.373869Z","iopub.status.idle":"2022-11-04T15:47:51.392735Z","shell.execute_reply.started":"2022-11-04T15:47:51.373840Z","shell.execute_reply":"2022-11-04T15:47:51.391905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn = Model(inputs=base_model.input, outputs=up)\nfcn.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:47:51.395307Z","iopub.execute_input":"2022-11-04T15:47:51.395925Z","iopub.status.idle":"2022-11-04T15:47:51.413457Z","shell.execute_reply.started":"2022-11-04T15:47:51.395882Z","shell.execute_reply":"2022-11-04T15:47:51.412527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = fcn.predict(X[:1])\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:51.415028Z","iopub.execute_input":"2022-11-04T15:47:51.415441Z","iopub.status.idle":"2022-11-04T15:47:55.304188Z","shell.execute_reply.started":"2022-11-04T15:47:51.415403Z","shell.execute_reply":"2022-11-04T15:47:55.303173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred[0, :, :, 0])","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:55.307435Z","iopub.execute_input":"2022-11-04T15:47:55.308154Z","iopub.status.idle":"2022-11-04T15:47:55.469476Z","shell.execute_reply.started":"2022-11-04T15:47:55.308075Z","shell.execute_reply":"2022-11-04T15:47:55.468662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"pred - карта с признаками (feature map), которая как-то характеризует картинку. \nЕсли посмотреть на любой канал у предсказания, то получим карту вероятностей.\n\nВсё получилось пикселизировано, можно добавить линейную интерполяцию.","metadata":{}},{"cell_type":"code","source":"up = UpSampling2D(32, interpolation='bilinear')(base_out)\nup","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:55.472383Z","iopub.execute_input":"2022-11-04T15:47:55.472694Z","iopub.status.idle":"2022-11-04T15:47:55.488375Z","shell.execute_reply.started":"2022-11-04T15:47:55.472659Z","shell.execute_reply":"2022-11-04T15:47:55.487454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn = Model(inputs=base_model.input, outputs=up)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:55.493254Z","iopub.execute_input":"2022-11-04T15:47:55.493579Z","iopub.status.idle":"2022-11-04T15:47:55.503543Z","shell.execute_reply.started":"2022-11-04T15:47:55.493549Z","shell.execute_reply":"2022-11-04T15:47:55.502819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = fcn.predict(X[:1])\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:55.508012Z","iopub.execute_input":"2022-11-04T15:47:55.508327Z","iopub.status.idle":"2022-11-04T15:47:55.842594Z","shell.execute_reply.started":"2022-11-04T15:47:55.508299Z","shell.execute_reply":"2022-11-04T15:47:55.841699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred[0, :, :, 0])","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:55.844122Z","iopub.execute_input":"2022-11-04T15:47:55.844535Z","iopub.status.idle":"2022-11-04T15:47:55.999508Z","shell.execute_reply.started":"2022-11-04T15:47:55.844500Z","shell.execute_reply":"2022-11-04T15:47:55.998424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Теперь feature map нужно сжать до того количества каналов, сколько у нас классов для масок, в нашем случае с машинами есть только один класс.","metadata":{}},{"cell_type":"code","source":"conv = Conv2D(1, (1, 1), activation='sigmoid')(up)  # (1, 1) потому что не надо изменять изображение, хочется только сжать каналы\nconv","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:56.001213Z","iopub.execute_input":"2022-11-04T15:47:56.001605Z","iopub.status.idle":"2022-11-04T15:47:56.019875Z","shell.execute_reply.started":"2022-11-04T15:47:56.001567Z","shell.execute_reply":"2022-11-04T15:47:56.018615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn = Model(inputs=base_model.input, outputs=conv)\nfcn.summary()","metadata":{"scrolled":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-11-04T15:47:56.021443Z","iopub.execute_input":"2022-11-04T15:47:56.021779Z","iopub.status.idle":"2022-11-04T15:47:56.040241Z","shell.execute_reply.started":"2022-11-04T15:47:56.021745Z","shell.execute_reply":"2022-11-04T15:47:56.039495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = fcn.predict(X)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:56.044423Z","iopub.execute_input":"2022-11-04T15:47:56.044716Z","iopub.status.idle":"2022-11-04T15:47:57.113531Z","shell.execute_reply.started":"2022-11-04T15:47:56.044688Z","shell.execute_reply":"2022-11-04T15:47:57.112486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred[0]);","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:57.115071Z","iopub.execute_input":"2022-11-04T15:47:57.115450Z","iopub.status.idle":"2022-11-04T15:47:57.278600Z","shell.execute_reply.started":"2022-11-04T15:47:57.115412Z","shell.execute_reply":"2022-11-04T15:47:57.277587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_best = keras.callbacks.ModelCheckpoint('fcn_best.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=True,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\ncheckpoint_last = keras.callbacks.ModelCheckpoint('fcn_last.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=False,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\n\ncallbacks = [checkpoint_best, checkpoint_last]\n\n\n\nadam = keras.optimizers.Adam(lr=0.0001)\n\nfcn.compile(adam, 'binary_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:57.280382Z","iopub.execute_input":"2022-11-04T15:47:57.280898Z","iopub.status.idle":"2022-11-04T15:47:57.301407Z","shell.execute_reply.started":"2022-11-04T15:47:57.280856Z","shell.execute_reply":"2022-11-04T15:47:57.300436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\n\nfcn.fit(keras_generator(train_df, batch_size), \n        steps_per_epoch=100,\n        epochs=2, verbose=1,\n        callbacks=callbacks,\n        validation_data=keras_generator(val_df, batch_size),\n        validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:47:57.302798Z","iopub.execute_input":"2022-11-04T15:47:57.303437Z","iopub.status.idle":"2022-11-04T15:50:36.873807Z","shell.execute_reply.started":"2022-11-04T15:47:57.303328Z","shell.execute_reply":"2022-11-04T15:50:36.872502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fcn.evaluate(keras_generator(val_df, batch_size), steps=25)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:36.880200Z","iopub.execute_input":"2022-11-04T15:50:36.883561Z","iopub.status.idle":"2022-11-04T15:50:48.932741Z","shell.execute_reply.started":"2022-11-04T15:50:36.883505Z","shell.execute_reply":"2022-11-04T15:50:48.931645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = fcn.predict(X)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:48.934311Z","iopub.execute_input":"2022-11-04T15:50:48.934732Z","iopub.status.idle":"2022-11-04T15:50:49.255377Z","shell.execute_reply.started":"2022-11-04T15:50:48.934691Z","shell.execute_reply":"2022-11-04T15:50:49.254308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"На выходе у нас будет не бинарная маска, а маска вероятностей.","metadata":{}},{"cell_type":"code","source":"idx = 1\nfig, axes = plt.subplots(1, 2, figsize=(15, 15))\naxes[0].imshow(X[idx])\naxes[1].imshow(pred[idx, ..., 0])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:49.256956Z","iopub.execute_input":"2022-11-04T15:50:49.257384Z","iopub.status.idle":"2022-11-04T15:50:49.650808Z","shell.execute_reply.started":"2022-11-04T15:50:49.257324Z","shell.execute_reply":"2022-11-04T15:50:49.649904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Чтобы перевести в бинарную маску, нужно сравнивать предсказанную вероятность с отсечкой.","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(15, 15))\naxes[0].imshow(X[idx])\naxes[1].imshow(pred[idx, ..., 0] > 0.5)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:49.652220Z","iopub.execute_input":"2022-11-04T15:50:49.652710Z","iopub.status.idle":"2022-11-04T15:50:50.038889Z","shell.execute_reply.started":"2022-11-04T15:50:49.652675Z","shell.execute_reply":"2022-11-04T15:50:50.037989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Большой недостаток рассмотренной архитектуры - слишком резко происходит увеличение изображения. Можно это смягчить за счет архитектуры SegNet.\n\n## SegNet\n\n<img src='https://production-media.paperswithcode.com/methods/segnet_Vorazx7.png'>\n","metadata":{}},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import Input, Dense\nfrom keras.layers import Dense, GlobalAveragePooling2D, Dropout, UpSampling2D, Conv2D, MaxPooling2D\n\n\ninp = Input(shape=(256, 256, 3))\n\nconv_1_1 = Conv2D(32, (3, 3), padding='same', activation='relu')(inp)\nconv_1_2 = Conv2D(32, (3, 3), padding='same', activation='relu')(conv_1_1)\n\npool_1 = MaxPooling2D(2)(conv_1_2)\n\n\nconv_2_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(pool_1)\nconv_2_2 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_2_1)\n\npool_2 = MaxPooling2D(2)(conv_2_2)\n\n\nconv_3_1 = Conv2D(128, (3, 3), padding='same', activation='relu')(pool_2)\nconv_3_2 = Conv2D(128, (3, 3), padding='same', activation='relu')(conv_3_1)\n\npool_3 = MaxPooling2D(2)(conv_3_2)\n\n\nconv_4_1 = Conv2D(256, (3, 3), padding='same', activation='relu')(pool_3)\nconv_4_2 = Conv2D(256, (3, 3), padding='same', activation='relu')(conv_4_1)\n\npool_4 = MaxPooling2D(2)(conv_4_2)\npool_4","metadata":{"_kg_hide-output":false,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:50:50.040296Z","iopub.execute_input":"2022-11-04T15:50:50.040826Z","iopub.status.idle":"2022-11-04T15:50:50.131459Z","shell.execute_reply.started":"2022-11-04T15:50:50.040770Z","shell.execute_reply":"2022-11-04T15:50:50.130468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"up_1 = UpSampling2D(2, interpolation='bilinear')(pool_4)\n\nconv_up_1_1 = Conv2D(256, (3, 3), padding='same', activation='relu')(up_1)\nconv_up_1_2 = Conv2D(256, (3, 3), padding='same', activation='relu')(conv_up_1_1)\n\n\nup_2 = UpSampling2D(2, interpolation='bilinear')(conv_up_1_2)\n\nconv_up_2_1 = Conv2D(128, (3, 3), padding='same', activation='relu')(up_2)\nconv_up_2_2 = Conv2D(128, (3, 3), padding='same', activation='relu')(conv_up_2_1)\n\n\nup_3 = UpSampling2D(2, interpolation='bilinear')(conv_up_2_2)\n\nconv_up_3_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(up_3)\nconv_up_3_2 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_up_3_1)\n\n\nup_4 = UpSampling2D(2, interpolation='bilinear')(conv_up_3_2)\n\nconv_up_4_1 = Conv2D(32, (3, 3), padding='same', activation='relu')(up_4)\nconv_up_4_2 = Conv2D(1, (3, 3), padding='same', activation='sigmoid')(conv_up_4_1)\nconv_up_4_2","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:50.132894Z","iopub.execute_input":"2022-11-04T15:50:50.133279Z","iopub.status.idle":"2022-11-04T15:50:50.231286Z","shell.execute_reply.started":"2022-11-04T15:50:50.133240Z","shell.execute_reply":"2022-11-04T15:50:50.230209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segnet = Model(inputs=inp, outputs=conv_up_4_2)\nsegnet.summary()","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:50:50.232850Z","iopub.execute_input":"2022-11-04T15:50:50.233259Z","iopub.status.idle":"2022-11-04T15:50:50.250196Z","shell.execute_reply.started":"2022-11-04T15:50:50.233218Z","shell.execute_reply":"2022-11-04T15:50:50.249125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_best = keras.callbacks.ModelCheckpoint('segnet_best.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=True,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\ncheckpoint_last = keras.callbacks.ModelCheckpoint('segnet_last.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=False,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\n\ncallbacks = [checkpoint_best, checkpoint_last]\n\nadam = keras.optimizers.Adam(lr=0.0001)\n\nsegnet.compile(adam, 'binary_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:50.252191Z","iopub.execute_input":"2022-11-04T15:50:50.252747Z","iopub.status.idle":"2022-11-04T15:50:50.269374Z","shell.execute_reply.started":"2022-11-04T15:50:50.252695Z","shell.execute_reply":"2022-11-04T15:50:50.268398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\n\nsegnet.fit(keras_generator(train_df, batch_size),\n           steps_per_epoch=100,\n           epochs=3, verbose=1,\n           callbacks=callbacks,\n           validation_data=keras_generator(val_df, batch_size),\n           validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:50:50.271215Z","iopub.execute_input":"2022-11-04T15:50:50.271665Z","iopub.status.idle":"2022-11-04T15:54:30.366027Z","shell.execute_reply.started":"2022-11-04T15:50:50.271612Z","shell.execute_reply":"2022-11-04T15:54:30.365028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segnet.evaluate(keras_generator(val_df, batch_size), steps=25)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:30.367451Z","iopub.execute_input":"2022-11-04T15:54:30.367823Z","iopub.status.idle":"2022-11-04T15:54:42.620905Z","shell.execute_reply.started":"2022-11-04T15:54:30.367792Z","shell.execute_reply":"2022-11-04T15:54:42.619771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = segnet.predict(X)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:42.622481Z","iopub.execute_input":"2022-11-04T15:54:42.623092Z","iopub.status.idle":"2022-11-04T15:54:43.469246Z","shell.execute_reply.started":"2022-11-04T15:54:42.623050Z","shell.execute_reply":"2022-11-04T15:54:43.468451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 0\nfig, axes = plt.subplots(1, 2, figsize=(15, 15))\naxes[0].imshow(X[idx])\naxes[1].imshow(pred[idx, ..., 0] > 0.5)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:43.472996Z","iopub.execute_input":"2022-11-04T15:54:43.473300Z","iopub.status.idle":"2022-11-04T15:54:43.852758Z","shell.execute_reply.started":"2022-11-04T15:54:43.473270Z","shell.execute_reply":"2022-11-04T15:54:43.851601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## U-net\n<img src='https://miro.medium.com/max/1200/1*f7YOaE4TWubwaFF7Z1fzNw.png'>","metadata":{}},{"cell_type":"code","source":"from keras.models import Model\nfrom keras.layers import Input, Dense, Concatenate\nfrom keras.layers import Dense, GlobalAveragePooling2D, Dropout, UpSampling2D, Conv2D, MaxPooling2D\n\ninp = Input(shape=(256, 256, 3))\n\n\n# Downsampling\nconv_1_1 = Conv2D(32, (3, 3), padding='same', activation='relu')(inp)\nconv_1_2 = Conv2D(32, (3, 3), padding='same')(conv_1_1)\n\npool_1 = MaxPooling2D(2)(conv_1_2)\n\n\nconv_2_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(pool_1)\nconv_2_2 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_2_1)\n\npool_2 = MaxPooling2D(2)(conv_2_2)\n\n\nconv_3_1 = Conv2D(128, (3, 3), padding='same', activation='relu')(pool_2)\nconv_3_2 = Conv2D(128, (3, 3), padding='same', activation='relu')(conv_3_1)\n\npool_3 = MaxPooling2D(2)(conv_3_2)\n\n\n# Bottleneck\nconv_4_1 = Conv2D(256, (3, 3), padding='same', activation='relu')(pool_3)\nconv_4_2 = Conv2D(256, (3, 3), padding='same', activation='relu')(conv_4_1)\n\npool_4 = MaxPooling2D(2)(conv_4_2)\npool_4","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:43.854484Z","iopub.execute_input":"2022-11-04T15:54:43.854908Z","iopub.status.idle":"2022-11-04T15:54:43.948220Z","shell.execute_reply.started":"2022-11-04T15:54:43.854861Z","shell.execute_reply":"2022-11-04T15:54:43.947115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Upsampling\nup_1 = UpSampling2D(2, interpolation='bilinear')(pool_4)\nconc_1 = Concatenate()([conv_4_2, up_1])\n\nconv_up_1_1 = Conv2D(256, (3, 3), padding='same', activation='relu')(conc_1)\nconv_up_1_2 = Conv2D(256, (3, 3), padding='same', activation='relu')(conv_up_1_1)\n\n\nup_2 = UpSampling2D(2, interpolation='bilinear')(conv_up_1_2)\nconc_2 = Concatenate()([conv_3_2, up_2])\n\nconv_up_2_1 = Conv2D(128, (3, 3), padding='same', activation='relu')(conc_2)\nconv_up_2_2 = Conv2D(128, (3, 3), padding='same', activation='relu')(conv_up_2_1)\n\n\nup_3 = UpSampling2D(2, interpolation='bilinear')(conv_up_2_2)\nconc_3 = Concatenate()([conv_2_2, up_3])\n\nconv_up_3_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(conc_3)\nconv_up_3_2 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_up_3_1)\n\n\nup_4 = UpSampling2D(2, interpolation='bilinear')(conv_up_3_2)\nconc_4 = Concatenate()([conv_1_2, up_4])\n\nconv_up_4_1 = Conv2D(32, (3, 3), padding='same', activation='relu')(conc_4)\nconv_up_4_2 = Conv2D(1, (3, 3), padding='same', activation='sigmoid')(conv_up_4_1)\nconv_up_4_2","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:43.949914Z","iopub.execute_input":"2022-11-04T15:54:43.950323Z","iopub.status.idle":"2022-11-04T15:54:44.062922Z","shell.execute_reply.started":"2022-11-04T15:54:43.950282Z","shell.execute_reply":"2022-11-04T15:54:44.061952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet = Model(inputs=inp, outputs=conv_up_4_2)\nunet.summary()","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:54:44.064811Z","iopub.execute_input":"2022-11-04T15:54:44.065237Z","iopub.status.idle":"2022-11-04T15:54:44.087137Z","shell.execute_reply.started":"2022-11-04T15:54:44.065198Z","shell.execute_reply":"2022-11-04T15:54:44.086321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_best = keras.callbacks.ModelCheckpoint('unet_best.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=True,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\ncheckpoint_last = keras.callbacks.ModelCheckpoint('unet_last.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=False,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\n\ncallbacks = [checkpoint_best, checkpoint_last]\n\nadam = keras.optimizers.Adam(lr=0.0001)\n\nunet.compile(adam, 'binary_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:44.091775Z","iopub.execute_input":"2022-11-04T15:54:44.092058Z","iopub.status.idle":"2022-11-04T15:54:44.110878Z","shell.execute_reply.started":"2022-11-04T15:54:44.092030Z","shell.execute_reply":"2022-11-04T15:54:44.109146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\n\nunet.fit(keras_generator(train_df, batch_size),\n         steps_per_epoch=100,\n         epochs=3, verbose=1,\n         callbacks=callbacks,\n         validation_data=keras_generator(val_df, batch_size),\n         validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:54:44.112263Z","iopub.execute_input":"2022-11-04T15:54:44.112714Z","iopub.status.idle":"2022-11-04T15:58:28.103310Z","shell.execute_reply.started":"2022-11-04T15:54:44.112685Z","shell.execute_reply":"2022-11-04T15:58:28.102401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet.evaluate(keras_generator(val_df, batch_size), steps=25)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:28.104694Z","iopub.execute_input":"2022-11-04T15:58:28.105034Z","iopub.status.idle":"2022-11-04T15:58:40.146348Z","shell.execute_reply.started":"2022-11-04T15:58:28.105004Z","shell.execute_reply":"2022-11-04T15:58:40.145302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = unet.predict(X)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:40.147786Z","iopub.execute_input":"2022-11-04T15:58:40.148418Z","iopub.status.idle":"2022-11-04T15:58:40.833731Z","shell.execute_reply.started":"2022-11-04T15:58:40.148376Z","shell.execute_reply":"2022-11-04T15:58:40.832683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 0\nfig, axes = plt.subplots(1, 2, figsize=(15, 15))\naxes[0].imshow(X[idx])\naxes[1].imshow(pred[idx, ..., 0] > 0.5)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:40.835304Z","iopub.execute_input":"2022-11-04T15:58:40.835718Z","iopub.status.idle":"2022-11-04T15:58:41.216432Z","shell.execute_reply.started":"2022-11-04T15:58:40.835676Z","shell.execute_reply":"2022-11-04T15:58:41.215385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## U-net + ResNet50\n\n<img src='https://miro.medium.com/max/1200/1*eKrh8FqJL3jodebYlielNg.png'>","metadata":{}},{"cell_type":"code","source":"import keras\nfrom keras.applications.resnet50 import ResNet50, preprocess_input\nfrom keras.preprocessing import image\nfrom keras.models import Model\nfrom keras.layers import Dense, GlobalAveragePooling2D, Dropout, UpSampling2D, Conv2D, MaxPooling2D, Concatenate, Activation\nfrom keras import backend as K\n\n\nbase_model = ResNet50(weights='imagenet', input_shape=(256, 256, 3), include_top=False)\n \nbase_out = base_model.output\nbase_out","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:41.217872Z","iopub.execute_input":"2022-11-04T15:58:41.218481Z","iopub.status.idle":"2022-11-04T15:58:42.988291Z","shell.execute_reply.started":"2022-11-04T15:58:41.218444Z","shell.execute_reply":"2022-11-04T15:58:42.987465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(base_model.layers)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:42.989835Z","iopub.execute_input":"2022-11-04T15:58:42.990207Z","iopub.status.idle":"2022-11-04T15:58:42.995271Z","shell.execute_reply.started":"2022-11-04T15:58:42.990170Z","shell.execute_reply":"2022-11-04T15:58:42.994239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:58:42.997683Z","iopub.execute_input":"2022-11-04T15:58:42.998502Z","iopub.status.idle":"2022-11-04T15:58:43.054579Z","shell.execute_reply.started":"2022-11-04T15:58:42.998458Z","shell.execute_reply":"2022-11-04T15:58:43.053304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv1 = base_model.get_layer('conv1_relu').output  # (128, 128, 64)\nconv2 = base_model.get_layer('conv2_block1_out').output  # (64, 64, 256)\nconv3 = base_model.get_layer('conv3_block1_1_relu').output  # (32, 32, 128)\nconv4 = base_model.get_layer('conv4_block2_2_relu').output  # (16, 16, 256)\nconv5 = base_model.get_layer('conv5_block1_2_relu').output  # (8, 8, 512)\n\n\ninp = base_model.get_layer('input_4').output","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:43.056170Z","iopub.execute_input":"2022-11-04T15:58:43.056520Z","iopub.status.idle":"2022-11-04T15:58:43.068216Z","shell.execute_reply.started":"2022-11-04T15:58:43.056484Z","shell.execute_reply":"2022-11-04T15:58:43.067418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv5","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:43.072851Z","iopub.execute_input":"2022-11-04T15:58:43.073186Z","iopub.status.idle":"2022-11-04T15:58:43.082507Z","shell.execute_reply.started":"2022-11-04T15:58:43.073158Z","shell.execute_reply":"2022-11-04T15:58:43.080213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"up1 = UpSampling2D(2, interpolation='bilinear')(conv5)\nconc_1 = Concatenate()([up1, conv4])\nprint(conc_1)\nconv_conc_1 = Conv2D(256, (3, 3), padding='same', activation='relu')(conc_1)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:43.084313Z","iopub.execute_input":"2022-11-04T15:58:43.084914Z","iopub.status.idle":"2022-11-04T15:58:43.124968Z","shell.execute_reply.started":"2022-11-04T15:58:43.084839Z","shell.execute_reply":"2022-11-04T15:58:43.124125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"up2 = UpSampling2D(2, interpolation='bilinear')(conv_conc_1)\nconc_2 = Concatenate()([up2, conv3])\nconv_conc_2 = Conv2D(128, (3, 3), padding='same', activation='relu')(conc_2)\n\nup3 = UpSampling2D(2, interpolation='bilinear')(conv_conc_2)\nconc_3 = Concatenate()([up3, conv2])\nconv_conc_3 = Conv2D(64, (3, 3), padding='same', activation='relu')(conc_3)\n\nup4 = UpSampling2D(2, interpolation='bilinear')(conv_conc_3)\nconc_4 = Concatenate()([up4, conv1])\nconv_conc_4 = Conv2D(32, (3, 3), padding='same', activation='relu')(conc_4)\n\nup5 = UpSampling2D(2, interpolation='bilinear')(conv_conc_4)\nconv_conc_5 = Conv2D(1, (3, 3), padding='same', activation='sigmoid')(up5)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:43.129628Z","iopub.execute_input":"2022-11-04T15:58:43.129927Z","iopub.status.idle":"2022-11-04T15:58:43.231695Z","shell.execute_reply.started":"2022-11-04T15:58:43.129898Z","shell.execute_reply":"2022-11-04T15:58:43.230838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_50 = Model(inputs=base_model.input, outputs=conv_conc_5)\nunet_50.summary()","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T15:58:43.240246Z","iopub.execute_input":"2022-11-04T15:58:43.240560Z","iopub.status.idle":"2022-11-04T15:58:43.301404Z","shell.execute_reply.started":"2022-11-04T15:58:43.240533Z","shell.execute_reply":"2022-11-04T15:58:43.300553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_best = keras.callbacks.ModelCheckpoint('unet_50_best.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=True,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\ncheckpoint_last = keras.callbacks.ModelCheckpoint('unet_50_last.h5',\n                                                  monitor='val_loss',\n                                                  verbose=0,\n                                                  save_best_only=False,\n                                                  save_weights_only=False,\n                                                  mode='auto',\n                                                  period=1)\n\n\ncallbacks = [checkpoint_best, checkpoint_last]\n\nadam = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)\n\nunet_50.compile(adam, 'binary_crossentropy')","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:43.306335Z","iopub.execute_input":"2022-11-04T15:58:43.306677Z","iopub.status.idle":"2022-11-04T15:58:43.332274Z","shell.execute_reply.started":"2022-11-04T15:58:43.306644Z","shell.execute_reply":"2022-11-04T15:58:43.331248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\n\nunet_50.fit(keras_generator(train_df, batch_size, preprocess_input),\n            steps_per_epoch=100,\n            epochs=3, verbose=1,\n            callbacks=callbacks,\n            validation_data=keras_generator(val_df, batch_size, preprocess_input),\n            validation_steps=10)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T15:58:43.333917Z","iopub.execute_input":"2022-11-04T15:58:43.334305Z","iopub.status.idle":"2022-11-04T16:01:40.928732Z","shell.execute_reply.started":"2022-11-04T15:58:43.334264Z","shell.execute_reply":"2022-11-04T16:01:40.927391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_50.evaluate(keras_generator(val_df, batch_size, preprocess_input), steps=25)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:01:40.937417Z","iopub.execute_input":"2022-11-04T16:01:40.937737Z","iopub.status.idle":"2022-11-04T16:01:53.159529Z","shell.execute_reply.started":"2022-11-04T16:01:40.937690Z","shell.execute_reply":"2022-11-04T16:01:53.158384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for X, y in keras_generator(val_df, 16, preprocess_input):\n    plt.imshow(X[0])\n    print(X[0].min(), X[0].max())\n    break","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:01:53.161157Z","iopub.execute_input":"2022-11-04T16:01:53.161769Z","iopub.status.idle":"2022-11-04T16:01:53.748901Z","shell.execute_reply.started":"2022-11-04T16:01:53.161728Z","shell.execute_reply":"2022-11-04T16:01:53.747854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = unet_50.predict(X)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:01:53.750937Z","iopub.execute_input":"2022-11-04T16:01:53.751664Z","iopub.status.idle":"2022-11-04T16:01:54.704265Z","shell.execute_reply.started":"2022-11-04T16:01:53.751590Z","shell.execute_reply":"2022-11-04T16:01:54.703126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 0\nfig, axes = plt.subplots(1, 2, figsize=(15, 15))\naxes[0].imshow(X[idx])\naxes[1].imshow(pred[idx, ..., 0] > 0.5)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:01:54.705946Z","iopub.execute_input":"2022-11-04T16:01:54.706335Z","iopub.status.idle":"2022-11-04T16:01:55.053623Z","shell.execute_reply.started":"2022-11-04T16:01:54.706294Z","shell.execute_reply":"2022-11-04T16:01:55.052629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Segmantational models\n\nhttps://github.com/qubvel/segmentation_models","metadata":{}},{"cell_type":"code","source":"!pip install segmentation-models","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-04T16:01:55.055151Z","iopub.execute_input":"2022-11-04T16:01:55.055736Z","iopub.status.idle":"2022-11-04T16:02:04.437471Z","shell.execute_reply.started":"2022-11-04T16:01:55.055690Z","shell.execute_reply":"2022-11-04T16:02:04.436261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['SM_FRAMEWORK'] = 'tf.keras'\nimport segmentation_models as sm\n\nBACKBONE = 'resnet34'\npreprocess_input = sm.get_preprocessing(BACKBONE)\n\n# define model\nmodel = sm.Unet(BACKBONE, classes=1, encoder_weights='imagenet')\n\nmodel.compile(\n    keras.optimizers.Adam(lr=0.001),\n    loss='binary_crossentropy',\n)\n\n# fit model\nmodel.fit(keras_generator(train_df, batch_size, preprocess_input),\n          steps_per_epoch=100,\n          epochs=3, verbose=1,\n          callbacks=callbacks,\n          validation_data=keras_generator(val_df, batch_size, preprocess_input),\n          validation_steps=10)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:02:04.439596Z","iopub.execute_input":"2022-11-04T16:02:04.440054Z","iopub.status.idle":"2022-11-04T16:04:58.821640Z","shell.execute_reply.started":"2022-11-04T16:02:04.440010Z","shell.execute_reply":"2022-11-04T16:04:58.820607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(keras_generator(val_df, batch_size, preprocess_input), steps=25)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:04:58.824326Z","iopub.execute_input":"2022-11-04T16:04:58.824943Z","iopub.status.idle":"2022-11-04T16:05:11.186912Z","shell.execute_reply.started":"2022-11-04T16:04:58.824897Z","shell.execute_reply":"2022-11-04T16:05:11.185972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for X, y in keras_generator(val_df, 16, preprocess_input):\n    plt.imshow(X[0])\n    print(X[0].min(), X[0].max())\n    break","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:05:11.190253Z","iopub.execute_input":"2022-11-04T16:05:11.190592Z","iopub.status.idle":"2022-11-04T16:05:11.770269Z","shell.execute_reply.started":"2022-11-04T16:05:11.190564Z","shell.execute_reply":"2022-11-04T16:05:11.769262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:05:11.771767Z","iopub.execute_input":"2022-11-04T16:05:11.772372Z","iopub.status.idle":"2022-11-04T16:05:12.583057Z","shell.execute_reply.started":"2022-11-04T16:05:11.772307Z","shell.execute_reply":"2022-11-04T16:05:12.581940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 0\nfig, axes = plt.subplots(1, 2, figsize=(15, 15))\naxes[0].imshow(X[idx])\naxes[1].imshow(pred[idx, ..., 0] > 0.5)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T16:05:12.584709Z","iopub.execute_input":"2022-11-04T16:05:12.585091Z","iopub.status.idle":"2022-11-04T16:05:12.945303Z","shell.execute_reply.started":"2022-11-04T16:05:12.585047Z","shell.execute_reply":"2022-11-04T16:05:12.944341Z"},"trusted":true},"execution_count":null,"outputs":[]}]}