{"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":"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-17T02:39:51.312064Z","iopub.execute_input":"2021-09-17T02:39:51.312413Z","iopub.status.idle":"2021-09-17T02:39:51.408848Z","shell.execute_reply.started":"2021-09-17T02:39:51.312304Z","shell.execute_reply":"2021-09-17T02:39:51.40807Z"},"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\nfrom sklearn.model_selection import train_test_split\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-17T02:39:51.410596Z","iopub.execute_input":"2021-09-17T02:39:51.410855Z","iopub.status.idle":"2021-09-17T02:39:52.132449Z","shell.execute_reply.started":"2021-09-17T02:39:51.410822Z","shell.execute_reply":"2021-09-17T02:39:52.131622Z"},"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-17T02:39:52.133919Z","iopub.execute_input":"2021-09-17T02:39:52.134189Z","iopub.status.idle":"2021-09-17T02:39:52.162449Z","shell.execute_reply.started":"2021-09-17T02:39:52.134151Z","shell.execute_reply":"2021-09-17T02:39:52.161765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = 0\nend = train_nbatch\nnp.random.seed(1)\n\nxdata = utils.read_nbatch(train_dir + '/origin', start, end)['x']\nydata = utils.read_nbatch(train_dir + '/masks', start, end)['x'].astype('float32')\n\nindices = []\n\nflower_to_bg_ratio_range = (0.15, 0.85)\n\nfor i in range(len(ydata)):\n    if flower_to_bg_ratio_range[0] < np.sum(ydata[i]) / (244**2) < flower_to_bg_ratio_range[1]:\n        indices.append(i)\n        \nxdata = xdata[indices]\nydata = ydata[indices]\n\nxtrain, xtest, ytrain, ytest = train_test_split(xdata, ydata, test_size=0.1, random_state=42)\n\nprint(xtrain.shape, ytrain.shape)\nprint(xtest.shape, ytest.shape)\ndel xdata\ndel ydata","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:39:52.164473Z","iopub.execute_input":"2021-09-17T02:39:52.164721Z","iopub.status.idle":"2021-09-17T02:40:40.928368Z","shell.execute_reply.started":"2021-09-17T02:39:52.164689Z","shell.execute_reply":"2021-09-17T02:40:40.927563Z"},"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 = 4, figsize: tuple = (10, 6)):\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-17T02:40:40.929518Z","iopub.execute_input":"2021-09-17T02:40:40.930226Z","iopub.status.idle":"2021-09-17T02:40:40.937206Z","shell.execute_reply.started":"2021-09-17T02:40:40.930188Z","shell.execute_reply":"2021-09-17T02:40:40.936404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sample(images, sample_size=4, 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-17T02:40:40.93986Z","iopub.execute_input":"2021-09-17T02:40:40.94006Z","iopub.status.idle":"2021-09-17T02:40:40.950992Z","shell.execute_reply.started":"2021-09-17T02:40:40.940038Z","shell.execute_reply":"2021-09-17T02:40:40.950241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_sample = sample(xtrain)\ny_sample = sample(ytrain)\n\nprint(x_sample.shape, y_sample.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:40:40.952443Z","iopub.execute_input":"2021-09-17T02:40:40.952708Z","iopub.status.idle":"2021-09-17T02:40:40.961656Z","shell.execute_reply.started":"2021-09-17T02:40:40.952676Z","shell.execute_reply":"2021-09-17T02:40:40.960862Z"},"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-17T02:40:40.963206Z","iopub.execute_input":"2021-09-17T02:40:40.963476Z","iopub.status.idle":"2021-09-17T02:40:41.500456Z","shell.execute_reply.started":"2021-09-17T02:40:40.963445Z","shell.execute_reply":"2021-09-17T02:40:41.499661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.layers import Input, Dense, Activation, BatchNormalization, Flatten, Reshape\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import optimizers, losses, metrics","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:40:41.501936Z","iopub.execute_input":"2021-09-17T02:40:41.502201Z","iopub.status.idle":"2021-09-17T02:40:45.612276Z","shell.execute_reply.started":"2021-09-17T02:40:41.502167Z","shell.execute_reply":"2021-09-17T02:40:45.611599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install segmentation-models\nimport segmentation_models as sm\n\nBACKBONE = 'efficientnetb4'\npreprocess_input = sm.get_preprocessing(BACKBONE)\n\nxtrain = preprocess_input(xtrain)\nxtest = preprocess_input(xtest)\n\nmodel = sm.Unet(BACKBONE, encoder_weights=None)\nmodel.compile(\n    'Adam',\n    loss=sm.losses.bce_jaccard_loss,\n    metrics=[sm.metrics.iou_score],\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:40:45.614633Z","iopub.execute_input":"2021-09-17T02:40:45.614833Z","iopub.status.idle":"2021-09-17T02:41:03.257409Z","shell.execute_reply.started":"2021-09-17T02:40:45.614808Z","shell.execute_reply":"2021-09-17T02:41:03.25667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:41:03.258577Z","iopub.execute_input":"2021-09-17T02:41:03.258817Z","iopub.status.idle":"2021-09-17T02:41:03.473865Z","shell.execute_reply.started":"2021-09-17T02:41:03.258785Z","shell.execute_reply":"2021-09-17T02:41:03.473222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping_callback = tf.keras.callbacks.EarlyStopping(patience=5)\nmodel_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath='checkpoint.h5',\n    save_weights_only=True,\n    monitor='val_loss',\n    mode='max',\n    save_best_only=True)\nmodel.fit(xtrain, ytrain, epochs=100, batch_size=8, validation_data=(xtest, ytest), callbacks=[early_stopping_callback, model_checkpoint_callback])\nmodel.load_weights('checkpoint.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:41:03.475171Z","iopub.execute_input":"2021-09-17T02:41:03.475441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{}},{"cell_type":"code","source":"test_nbatches = utils.num_of_batches(test_dir + '/origin')\n\nprint('No. Batches: %i'%test_nbatches)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = utils.read_nbatch(test_dir + 'origin/', 108, 108 + test_nbatches)\ntest_data['x'] = preprocess_input(test_data['x'])\n\ntest_size = len(test_data['id'])\n\nprint(test_size)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = test_data['id']\n\ntest_masks = model.predict(test_data['x']) > 0.5","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_sample = sample(test_data['x'], sample_size=5, random_state=523)\ny_sample = sample(test_masks, sample_size=5, random_state=523)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('grayscale')\n\nfig, ax = plot(x_sample, ncols=5)\nfig, ax = plot(y_sample, ncols=5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.export('submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}