{"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":"# Import Modules","metadata":{}},{"cell_type":"markdown","source":"> https://arxiv.org/pdf/1807.10165.pdf","metadata":{}},{"cell_type":"code","source":"import sys\n\nsys.path.insert(0, '../input/computer-vision-training-fcis-21/')\n\nimport utils\n\nworking_dir = '../input/computer-vision-training-fcis-21/224/content/flowers/224/'\n\ntrain_dir = f'{working_dir}/train/train/'\ntest_dir = f'{working_dir}/test/test/'","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:41.397948Z","iopub.execute_input":"2021-09-23T20:06:41.398521Z","iopub.status.idle":"2021-09-23T20:06:41.500834Z","shell.execute_reply.started":"2021-09-23T20:06:41.398433Z","shell.execute_reply":"2021-09-23T20:06:41.499815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","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\n\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\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":{"execution":{"iopub.status.busy":"2021-09-23T20:06:41.503129Z","iopub.execute_input":"2021-09-23T20:06:41.503462Z","iopub.status.idle":"2021-09-23T20:06:41.510173Z","shell.execute_reply.started":"2021-09-23T20:06:41.503422Z","shell.execute_reply":"2021-09-23T20:06:41.508791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read N-Batches","metadata":{}},{"cell_type":"code","source":"train_nbatch = utils.num_of_batches(train_dir + '/origin')\n\nprint('No. Batches: %i'%train_nbatch)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:41.5123Z","iopub.execute_input":"2021-09-23T20:06:41.512676Z","iopub.status.idle":"2021-09-23T20:06:41.540974Z","shell.execute_reply.started":"2021-09-23T20:06:41.512634Z","shell.execute_reply":"2021-09-23T20:06:41.539968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = 0\nend = 5\n\nxdata = utils.read_nbatch(train_dir + '/origin', start, end)\nydata = utils.read_nbatch(train_dir + '/masks', start, end)\n\nprint(xdata['x'].shape, ydata['x'].shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:41.544463Z","iopub.execute_input":"2021-09-23T20:06:41.54514Z","iopub.status.idle":"2021-09-23T20:06:43.335793Z","shell.execute_reply.started":"2021-09-23T20:06:41.545088Z","shell.execute_reply":"2021-09-23T20:06:43.334747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xdata['x'].shape","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:43.338441Z","iopub.execute_input":"2021-09-23T20:06:43.338677Z","iopub.status.idle":"2021-09-23T20:06:43.354999Z","shell.execute_reply.started":"2021-09-23T20:06:43.338647Z","shell.execute_reply":"2021-09-23T20:06:43.353935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# normalization test","metadata":{}},{"cell_type":"code","source":"def normalizeIt(images: np.ndarray):\n    return images.astype('float32') ","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:43.356755Z","iopub.execute_input":"2021-09-23T20:06:43.358172Z","iopub.status.idle":"2021-09-23T20:06:43.365536Z","shell.execute_reply.started":"2021-09-23T20:06:43.358127Z","shell.execute_reply":"2021-09-23T20:06:43.364104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore","metadata":{}},{"cell_type":"code","source":"def plot(images: np.ndarray, nrows: int = 1, ncols: int = 5, figsize: tuple = (10, 10)):\n\n    fig, axs = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize)\n    \n    axs = axs.flatten()\n\n    for i in range(len(axs)):\n\n        axs[i].imshow(images[i].squeeze())\n\n        axs[i].grid(None)\n        axs[i].axis('off')\n    \n    \n    fig.tight_layout()\n    \n    return fig, axs","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:43.368267Z","iopub.execute_input":"2021-09-23T20:06:43.368705Z","iopub.status.idle":"2021-09-23T20:06:43.380426Z","shell.execute_reply.started":"2021-09-23T20:06:43.368663Z","shell.execute_reply":"2021-09-23T20:06:43.379094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sample(images, sample_size=10, random_state=42):\n    \n    random = np.random.RandomState(random_state)\n\n    indices = np.arange(0, 320)\n\n    random.shuffle(indices)\n    \n    indices = indices[:sample_size]    \n    \n    return images[indices]","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:43.38322Z","iopub.execute_input":"2021-09-23T20:06:43.385149Z","iopub.status.idle":"2021-09-23T20:06:43.392811Z","shell.execute_reply.started":"2021-09-23T20:06:43.385103Z","shell.execute_reply":"2021-09-23T20:06:43.391412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_sample = sample(xdata['x'])\ny_sample = sample(ydata['x'])\n\nprint(x_sample.shape, y_sample.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:43.39517Z","iopub.execute_input":"2021-09-23T20:06:43.395664Z","iopub.status.idle":"2021-09-23T20:06:43.410184Z","shell.execute_reply.started":"2021-09-23T20:06:43.395519Z","shell.execute_reply":"2021-09-23T20:06:43.4087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('grayscale')\n\nfig, ax = plot(x_sample)\nfig, ax = plot(y_sample)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:43.416306Z","iopub.execute_input":"2021-09-23T20:06:43.416552Z","iopub.status.idle":"2021-09-23T20:06:44.158172Z","shell.execute_reply.started":"2021-09-23T20:06:43.416524Z","shell.execute_reply":"2021-09-23T20:06:44.156908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\nimport tensorflow as tf\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras import layers, activations, optimizers, losses, metrics\nfrom tensorflow.keras.layers import Input, Dense, Activation, BatchNormalization, Flatten, Reshape, MaxPooling2D, Conv2D, Conv2DTranspose\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\ntf_iou = tf.metrics.MeanIoU(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:44.16841Z","iopub.execute_input":"2021-09-23T20:06:44.169157Z","iopub.status.idle":"2021-09-23T20:06:51.530566Z","shell.execute_reply.started":"2021-09-23T20:06:44.169111Z","shell.execute_reply":"2021-09-23T20:06:51.529503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def EncodeingBlock(inputs,kernalPara=64, kernalSize=(3,3)):\n    c1 = Conv2D(kernalPara, kernalSize, padding=\"same\")(inputs)\n    c1 = BatchNormalization()(c1)\n    c1 = activations.relu(c1)\n    c1 = Conv2D(64, (3,3), padding=\"same\")(c1)\n    c1 = BatchNormalization()(c1)\n    c1 = activations.relu(c1)\n    p1 = MaxPooling2D()(c1)\n    p1 = layers.Dropout(0.2)(p1)\n    \n    return c1, p1\ndef DecodeingBlock(inputs, inputs2, kernalPara=64, kernalSize=(3,3)):\n    up = Conv2DTranspose(kernalPara, (3, 3), strides=2, padding=\"same\")(inputs)\n    up = layers.concatenate([up, inputs2])\n    up = Conv2D(kernalPara, kernalSize, padding=\"same\")(up)\n    up = BatchNormalization()(up)\n    up = activations.relu(up)\n    up = Conv2D(kernalPara, kernalSize, padding=\"same\")(up)\n    up = BatchNormalization()(up)\n    up = activations.relu(up)\n    up = layers.Dropout(0.2)(up)\n    return up\ndef Conv_middel(inputs, kernalPara=64, kernalSize=(3,3)):\n    m = Conv2D(kernalPara, kernalSize , padding = \"same\")(inputs)\n    m = BatchNormalization()(m)\n    m = activations.relu(m)\n    \n    # Taking first input and implementing the second conv block\n    m = Conv2D(kernalPara, kernalSize, padding = \"same\")(m)\n    m = BatchNormalization()(m)\n    m = activations.relu(m)\n    \n    return m\ndef Unet1_0():\n    inputs = Input(shape=(224, 224, 3))\n#     inputs1 = Flatten()(inputs)\n#     inputs1 = layers.Dropout(0.4)(inputs1)\n#     inputs1 = Reshape((224, 224, 3))(inputs1)\n    c1, p1 = EncodeingBlock(inputs, kernalPara=64)\n    c2, p2 = EncodeingBlock(p1, kernalPara=64 * 2)\n    c3, p3 = EncodeingBlock(p2, kernalPara=64 * 4)\n    c4, p4 = EncodeingBlock(p3, kernalPara=64 * 8)\n    \n    m = Conv_middel(p4)\n    \n    up4 = DecodeingBlock(m, c4, kernalPara=64 * 8)\n    up3 = DecodeingBlock(up4, c3, kernalPara=64 * 4)\n    up2 = DecodeingBlock(up3, c2, kernalPara=64 * 2)\n    up1 = DecodeingBlock(up2, c1, kernalPara=64 )\n    outputs = Conv2D(1, (1,1), activation=\"sigmoid\")(up1)\n    model = Model(inputs=inputs, outputs=outputs)\n    \n    return model\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:51.532501Z","iopub.execute_input":"2021-09-23T20:06:51.532834Z","iopub.status.idle":"2021-09-23T20:06:51.55407Z","shell.execute_reply.started":"2021-09-23T20:06:51.532785Z","shell.execute_reply":"2021-09-23T20:06:51.553209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def iou_eval(y_true, y_pred, threshold=0.5):\n        \n    y_pred = tf.greater(y_pred, threshold)\n    \n    tf_iou.update_state(y_true, y_pred)\n    \n    return tf_iou.result()","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:51.557085Z","iopub.execute_input":"2021-09-23T20:06:51.558765Z","iopub.status.idle":"2021-09-23T20:06:51.566283Z","shell.execute_reply.started":"2021-09-23T20:06:51.558706Z","shell.execute_reply":"2021-09-23T20:06:51.565305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Unet1_0()\noptimizer = optimizers.Adam(5e-4)\nloss = losses.BinaryCrossentropy('loss')\nmetric = metrics.MeanIoU(2, name='iou')\n\nmodel.compile(optimizer=optimizer, loss= loss, metrics=iou_eval)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:51.568248Z","iopub.execute_input":"2021-09-23T20:06:51.568703Z","iopub.status.idle":"2021-09-23T20:06:52.495096Z","shell.execute_reply.started":"2021-09-23T20:06:51.568659Z","shell.execute_reply":"2021-09-23T20:06:52.4942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-09-23T20:06:52.496621Z","iopub.execute_input":"2021-09-23T20:06:52.496942Z","iopub.status.idle":"2021-09-23T20:06:52.55812Z","shell.execute_reply.started":"2021-09-23T20:06:52.496904Z","shell.execute_reply":"2021-09-23T20:06:52.557173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndef collect(trials=5):\n    while trials and gc.collect():\n        gc.collect() \n        trials = trials - 1","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-23T20:06:52.561356Z","iopub.execute_input":"2021-09-23T20:06:52.56158Z","iopub.status.idle":"2021-09-23T20:06:52.569586Z","shell.execute_reply.started":"2021-09-23T20:06:52.561553Z","shell.execute_reply":"2021-09-23T20:06:52.568494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0, 107, 54):\n    start = i\n    end = i + 53\n\n    xdata = utils.read_nbatch(train_dir + '/origin', start, end)\n    xdata['x'] = normalizeIt(xdata['x'])\n    ydata = utils.read_nbatch(train_dir + '/masks', start, end)\n\n    print(xdata['x'].shape, ydata['x'].shape)\n\n    history = model.fit(xdata['x'], ydata['x'].astype('float32'), epochs=50, batch_size=32)\n    \n    \n    test_data = utils.read_nbatch(test_dir + 'origin/', 108, 108 + 10)\n    # test_data['x'] = normalizeIt(test_data['x'].astype(\"float32\"))\n    test_masks = model.predict(test_data['x']) > 0.1\n    x_sample = sample(test_data['x'])\n    y_sample = sample(test_masks)\n\n    plt.style.use('grayscale')\n\n    fig, ax = plot(x_sample)\n    fig, ax = plot(y_sample)\n    plt.show()\n    \n    del xdata, ydata, test_data, x_sample, y_sample\n    collect()\n\n    print('-->' * 40)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T20:06:52.585813Z","iopub.execute_input":"2021-09-23T20:06:52.586279Z","iopub.status.idle":"2021-09-23T21:56:26.89133Z","shell.execute_reply.started":"2021-09-23T20:06:52.586236Z","shell.execute_reply":"2021-09-23T21:56:26.890075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:26.893762Z","iopub.execute_input":"2021-09-23T21:56:26.894112Z","iopub.status.idle":"2021-09-23T21:56:27.317633Z","shell.execute_reply.started":"2021-09-23T21:56:26.894074Z","shell.execute_reply":"2021-09-23T21:56:27.316702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.318976Z","iopub.execute_input":"2021-09-23T21:56:27.319986Z","iopub.status.idle":"2021-09-23T21:56:27.634664Z","shell.execute_reply.started":"2021-09-23T21:56:27.319954Z","shell.execute_reply":"2021-09-23T21:56:27.633118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictastype","metadata":{}},{"cell_type":"code","source":"# m0 = tf.keras.models.load_model('../input/flower-segmentation-starter-notebook/model.h5', compile=False) # 12 - 2\n# m1 = tf.keras.models.load_model('../input/k/midomoody/flower-segmentation-starter-notebook/model.h5', compile=False) # 8 - 1\n# m2 = tf.keras.models.load_model('../input/notebook58a9c6cc61/model.h5', compile=False) # 7 - 3\n\n# m0.trainable = False\n# m1.trainable = False\n# m2.trainable = False\n\n\n# model = [m0, m1, m2]\nthreshold = [0.5, 0.1, 0.5]\n","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.63818Z","iopub.status.idle":"2021-09-23T21:56:27.639081Z","shell.execute_reply.started":"2021-09-23T21:56:27.638764Z","shell.execute_reply":"2021-09-23T21:56:27.638792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_nbatches = utils.num_of_batches(test_dir + '/origin')\n\nprint('No. Batches: %i'%test_nbatches)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.640394Z","iopub.status.idle":"2021-09-23T21:56:27.641271Z","shell.execute_reply.started":"2021-09-23T21:56:27.640971Z","shell.execute_reply":"2021-09-23T21:56:27.641017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = utils.read_nbatch(test_dir + 'origin/', 108, 108 + test_nbatches)\n# test_data['x'] = normalizeIt(test_data['x'].astype(\"float32\"))\ntest_size = len(test_data['id'])\n\nprint(test_size)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.642735Z","iopub.status.idle":"2021-09-23T21:56:27.643635Z","shell.execute_reply.started":"2021-09-23T21:56:27.643315Z","shell.execute_reply":"2021-09-23T21:56:27.643343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = test_data['id']\n\ntest_masks = model.predict(test_data['x']) > 0.1","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.644979Z","iopub.status.idle":"2021-09-23T21:56:27.645822Z","shell.execute_reply.started":"2021-09-23T21:56:27.645527Z","shell.execute_reply":"2021-09-23T21:56:27.645554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_sample = sample(test_data['x'],10)\ny_sample = sample(test_masks,10)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.647189Z","iopub.status.idle":"2021-09-23T21:56:27.648146Z","shell.execute_reply.started":"2021-09-23T21:56:27.647832Z","shell.execute_reply":"2021-09-23T21:56:27.647859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('grayscale')\n\nfig, ax = plot(x_sample,2)\nfig, ax = plot(y_sample,2)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.649591Z","iopub.status.idle":"2021-09-23T21:56:27.6504Z","shell.execute_reply.started":"2021-09-23T21:56:27.650102Z","shell.execute_reply":"2021-09-23T21:56:27.650129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = utils.Submission(shape=(224, 224))\n\nsubmission.init()\n\nfor i in tqdm(range(test_size)):\n    \n    submission.update(test_ids[i], test_masks[i])","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.651691Z","iopub.status.idle":"2021-09-23T21:56:27.652492Z","shell.execute_reply.started":"2021-09-23T21:56:27.65218Z","shell.execute_reply":"2021-09-23T21:56:27.652206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.export('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:27.653863Z","iopub.status.idle":"2021-09-23T21:56:27.654832Z","shell.execute_reply.started":"2021-09-23T21:56:27.654505Z","shell.execute_reply":"2021-09-23T21:56:27.654549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}