{"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-23T21:52:46.031567Z","iopub.execute_input":"2021-09-23T21:52:46.032481Z","iopub.status.idle":"2021-09-23T21:52:46.159912Z","shell.execute_reply.started":"2021-09-23T21:52:46.032383Z","shell.execute_reply":"2021-09-23T21:52:46.158683Z"},"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)\nimport tensorflow as tf\n\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\n\nnp.random.seed(2)\ntf.random.set_seed(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:52:46.162488Z","iopub.execute_input":"2021-09-23T21:52:46.16286Z","iopub.status.idle":"2021-09-23T21:52:52.311485Z","shell.execute_reply.started":"2021-09-23T21:52:46.16282Z","shell.execute_reply":"2021-09-23T21:52:52.310238Z"},"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-23T21:52:52.313496Z","iopub.execute_input":"2021-09-23T21:52:52.31389Z","iopub.status.idle":"2021-09-23T21:52:52.33783Z","shell.execute_reply.started":"2021-09-23T21:52:52.313845Z","shell.execute_reply":"2021-09-23T21:52:52.336442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = 0\nend = train_nbatch\n\nxdata = utils.read_nbatch(train_dir + '/origin', start, end)['x']\nydata = utils.read_nbatch(train_dir + '/masks', start, end)['x'].astype('float32')\n\n\nindices = []#np.arange(2 * len(xdata) // 3)\n#np.random.shuffle(indices)\n\n\nflower_ratio_range = [0.1, 0.9]\nfuckup_factor = 2\nfor i in range(len(xdata)):\n    if flower_ratio_range[0] < np.sum(ydata[i]) / (244**2) < flower_ratio_range[1]:\n        indices.append(i)\n    else:\n        for i in range(fuckup_factor):\n            indices.append(i)\n\n\nnp.random.shuffle(indices)\nindices = indices[:int(0.85*len(indices))]\nprint(len(indices), len(xdata))\nxdata = xdata[indices]\nydata = ydata[indices]\n\n#xtrain, xtest, ytrain, ytest = train_test_split(xdata, ydata, test_size=0.0, random_state=42)\n\nprint(xdata.shape, ydata.shape)\n#del xdata\n#del ydata","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:52:52.341727Z","iopub.execute_input":"2021-09-23T21:52:52.34212Z","iopub.status.idle":"2021-09-23T21:53:39.705355Z","shell.execute_reply.started":"2021-09-23T21:52:52.342078Z","shell.execute_reply":"2021-09-23T21:53:39.704208Z"},"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-23T21:53:39.70719Z","iopub.execute_input":"2021-09-23T21:53:39.707786Z","iopub.status.idle":"2021-09-23T21:53:39.717219Z","shell.execute_reply.started":"2021-09-23T21:53:39.707741Z","shell.execute_reply":"2021-09-23T21:53:39.716002Z"},"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-23T21:53:39.720184Z","iopub.execute_input":"2021-09-23T21:53:39.720634Z","iopub.status.idle":"2021-09-23T21:53:39.730952Z","shell.execute_reply.started":"2021-09-23T21:53:39.720602Z","shell.execute_reply":"2021-09-23T21:53:39.729741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_sample = sample(xdata)\ny_sample = sample(ydata)\n\nprint(x_sample.shape, y_sample.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:53:39.732841Z","iopub.execute_input":"2021-09-23T21:53:39.73337Z","iopub.status.idle":"2021-09-23T21:53:39.746984Z","shell.execute_reply.started":"2021-09-23T21:53:39.733325Z","shell.execute_reply":"2021-09-23T21:53:39.745762Z"},"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-23T21:53:39.748786Z","iopub.execute_input":"2021-09-23T21:53:39.749211Z","iopub.status.idle":"2021-09-23T21:53:40.585898Z","shell.execute_reply.started":"2021-09-23T21:53:39.749157Z","shell.execute_reply":"2021-09-23T21:53:40.584665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''from tf.keras.preprocessing.image import ImageDataGenerator\n\ndef get_generator(X, y, aug_params_dict, seed=42):\n    if (len(y.shape) < 4):\n        y = y.reshape(y.shape + (1,))\n    if (len(X.shape) < 4):\n        X = X.reshape(X.shape + (1,))\n    X_datagen = ImageDataGenerator(**aug_params_dict)\n    y_datagen = ImageDataGenerator(**aug_params_dict)\n    X_generator = X_datagen.flow(X, seed=seed, batch_size=32, subset='training')\n    y_generator = y_datagen.flow(y, seed=seed, batch_size=32, subset='training')\n    X_val_generator = X_datagen.flow(X, seed=seed, batch_size=32, subset='validation')\n    y_val_generator = y_datagen.flow(y, seed=seed, batch_size=32, subset='validation')\n    train_generator = zip(X_generator, y_generator)\n    val_generator = zip(X_val_generator, y_val_generator)\n    return train_generator, val_generator\n \ngen_params = dict(rotation_range=0.2,\n                    width_shift_range=0.1,\n                    height_shift_range=0.1,\n                    shear_range=0.2,\n                    zoom_range=0.25,\n                    horizontal_flip=True,\n                    fill_mode='nearest',\n                    validation_split=0.2)\n \ngenerator, val_generator = get_generator(xdata, ydata, gen_params)'''","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:53:40.587863Z","iopub.execute_input":"2021-09-23T21:53:40.588346Z","iopub.status.idle":"2021-09-23T21:53:40.598681Z","shell.execute_reply.started":"2021-09-23T21:53:40.58824Z","shell.execute_reply":"2021-09-23T21:53:40.597273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"from 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-23T21:53:40.603352Z","iopub.execute_input":"2021-09-23T21:53:40.603719Z","iopub.status.idle":"2021-09-23T21:53:40.613537Z","shell.execute_reply.started":"2021-09-23T21:53:40.603674Z","shell.execute_reply":"2021-09-23T21:53:40.612148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install segmentation-models\nimport segmentation_models as sm\n\nBACKBONE = 'seresnext50'\npreprocess_input = sm.get_preprocessing(BACKBONE)\n\nxdata = preprocess_input(xdata)\nydata = tf.one_hot(ydata, 2)\n\n#xtest = preprocess_input(xtest)\n# detect and init the TPU\n#tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# instantiate a distribution strategy\n#tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\n# instantiating the model in the strategy scope creates the model on the TPU\n#with tpu_strategy.scope():\nmodel = sm.Unet(BACKBONE, input_shape=(224, 224, 3), classes=2, activation='softmax', encoder_weights=None)\nmodel.compile(\n    'adam',\n    loss=sm.losses.bce_dice_loss,\n    metrics=[sm.metrics.iou_score],\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:53:40.615455Z","iopub.execute_input":"2021-09-23T21:53:40.615958Z","iopub.status.idle":"2021-09-23T21:54:10.206882Z","shell.execute_reply.started":"2021-09-23T21:53:40.615813Z","shell.execute_reply":"2021-09-23T21:54:10.205833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:54:10.208655Z","iopub.execute_input":"2021-09-23T21:54:10.209116Z","iopub.status.idle":"2021-09-23T21:54:11.223283Z","shell.execute_reply.started":"2021-09-23T21:54:10.209034Z","shell.execute_reply":"2021-09-23T21:54:11.22038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LRA(tf.keras.callbacks.Callback):\n    def __init__(self, model, patience, threshold, factor):\n        self.model = model\n        self.patience = patience\n        self.threshold = threshold\n        self.factor = factor\n        self.lr = float(tf.keras.backend.get_value(model.optimizer.lr))\n        self.min_vloss = np.inf\n        self.count = 0\n    \n    def on_epoch_end(self, epoch, logs=None):\n        lr = float(tf.keras.backend.get_value(self.model.optimizer.lr))\n        v_loss = logs.get('val_loss')\n        self.min_vloss = min(self.min_vloss, v_loss)\n        if self.min_vloss < self.threshold:\n            if v_loss > self.min_vloss:\n                self.count += 1\n                if self.count == self.patience:\n                    self.lr = lr * self.factor\n                    print(f'adjusting learning rate to {self.lr}')\n                    tf.keras.backend.set_value(self.model.optimizer.lr, self.lr)\n                    self.count = 0\n            else:\n                self.count = 0\n        ","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:54:11.224985Z","iopub.execute_input":"2021-09-23T21:54:11.225309Z","iopub.status.idle":"2021-09-23T21:54:11.239287Z","shell.execute_reply.started":"2021-09-23T21:54:11.225266Z","shell.execute_reply":"2021-09-23T21:54:11.238197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping_callback = tf.keras.callbacks.EarlyStopping(patience=3, monitor='val_loss')\n\nmodel_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath='checkpoint.h5',\n    save_weights_only=True,\n    monitor='val_loss',\n    mode='min',\n    save_best_only=True)\n\n\nlearning_rate_adjustment_callback = LRA(model = model,\n                           patience = 5,\n                           threshold = 0.4,\n                           factor = 0.1)\n\n\nmodel.fit(xdata,\n          ydata,\n          epochs = 25,\n          batch_size = 32,\n          #validation_data = (xtest, ytest),\n          validation_split=0.2,\n          callbacks = [\n                          #early_stopping_callback, \n                          model_checkpoint_callback, \n                          #learning_rate_adjustment_callback\n                      ]\n         )\n\nmodel.load_weights('checkpoint.h5')\ndel xdata\ndel ydata\n#del xtest\n#del ytest","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:54:11.241447Z","iopub.execute_input":"2021-09-23T21:54:11.241761Z","iopub.status.idle":"2021-09-23T21:54:12.600183Z","shell.execute_reply.started":"2021-09-23T21:54:11.241732Z","shell.execute_reply":"2021-09-23T21:54:12.598992Z"},"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":{"execution":{"iopub.status.busy":"2021-09-23T21:54:12.601708Z","iopub.execute_input":"2021-09-23T21:54:12.60431Z","iopub.status.idle":"2021-09-23T21:54:12.622772Z","shell.execute_reply.started":"2021-09-23T21:54:12.604259Z","shell.execute_reply":"2021-09-23T21:54:12.621384Z"},"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":{"execution":{"iopub.status.busy":"2021-09-23T21:54:12.624466Z","iopub.execute_input":"2021-09-23T21:54:12.625459Z","iopub.status.idle":"2021-09-23T21:54:17.741382Z","shell.execute_reply.started":"2021-09-23T21:54:12.625397Z","shell.execute_reply":"2021-09-23T21:54:17.740102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = test_data['id']\n\n#test_masks = model.predict(test_data['x']) > 0.5\ntest_masks = model.predict(test_data['x'])\ntest_masks = np.argmax(test_masks, axis=3)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:54:17.742947Z","iopub.execute_input":"2021-09-23T21:54:17.743355Z","iopub.status.idle":"2021-09-23T21:54:40.370504Z","shell.execute_reply.started":"2021-09-23T21:54:17.743292Z","shell.execute_reply":"2021-09-23T21:54:40.369077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_sample = sample(test_data['x'], sample_size=10, random_state=5)\ny_sample = sample(test_masks, sample_size=10, random_state=5)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:54:40.372609Z","iopub.execute_input":"2021-09-23T21:54:40.372973Z","iopub.status.idle":"2021-09-23T21:54:40.385554Z","shell.execute_reply.started":"2021-09-23T21:54:40.372924Z","shell.execute_reply":"2021-09-23T21:54:40.384086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('grayscale')\n\nfig, ax = plot(x_sample, ncols=10)\nfig, ax = plot(y_sample, ncols=10)","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:54:40.387946Z","iopub.execute_input":"2021-09-23T21:54:40.388853Z","iopub.status.idle":"2021-09-23T21:54:41.86956Z","shell.execute_reply.started":"2021-09-23T21:54:40.3888Z","shell.execute_reply":"2021-09-23T21:54:41.868596Z"},"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":{"execution":{"iopub.status.busy":"2021-09-23T21:54:41.87108Z","iopub.execute_input":"2021-09-23T21:54:41.871964Z","iopub.status.idle":"2021-09-23T21:54:47.727265Z","shell.execute_reply.started":"2021-09-23T21:54:41.87193Z","shell.execute_reply":"2021-09-23T21:54:47.725904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.export('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:55:20.820456Z","iopub.execute_input":"2021-09-23T21:55:20.821037Z","iopub.status.idle":"2021-09-23T21:55:20.971579Z","shell.execute_reply.started":"2021-09-23T21:55:20.820993Z","shell.execute_reply":"2021-09-23T21:55:20.97059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom IPython.display import FileLink\nFileLink(r'submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-23T21:56:48.341864Z","iopub.execute_input":"2021-09-23T21:56:48.342534Z","iopub.status.idle":"2021-09-23T21:56:48.351266Z","shell.execute_reply.started":"2021-09-23T21:56:48.342491Z","shell.execute_reply":"2021-09-23T21:56:48.349258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}