{"nbformat": 4, "metadata": {"language_info": {"nbconvert_exporter": "python", "name": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python", "file_extension": ".py", "pygments_lexer": "ipython3", "version": "3.6.4"}, "kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}}, "nbformat_minor": 1, "cells": [{"source": ["this notebook is based on *Kjetil \u00c5mdal-S\u00e6vi*k  **Keras U-Net starter**   great notebook \n", "https://www.kaggle.com/keegil/keras-u-net-starter-lb-0-277\n", "\n", "and i just want to add augmentation to it. so the notebook starts after u imported X_train and Y_train.\n", "it is based on keras documantation how to augment masks.  https://keras.io/preprocessing/image/\n", "\n", "\n", "\n"], "metadata": {"_uuid": "1d7f9212364126eebf56dc9ca4490ac10ba6b8c7", "_cell_guid": "ddc861c8-353f-4128-912f-73c95e4a1aa3"}, "cell_type": "markdown"}, {"outputs": [], "execution_count": null, "metadata": {"_uuid": "cab327e38a25a03ddd881bbc79c836b1c5fe3b7c", "collapsed": true, "_cell_guid": "9a939230-2c46-4616-96c1-164468375e7f"}, "cell_type": "code", "source": ["from keras.preprocessing import image\n", "\n", "# u can play with the parameters of the augmentation, there are mant more\n", "image_datagen = image.ImageDataGenerator(height_shift_range=0.2 , horizontal_flip=True , rotation_range=15 ,width_shift_range=0.2 ,shear_range=0.2)\n", "mask_datagen = image.ImageDataGenerator(height_shift_range=0.2 , horizontal_flip=True , rotation_range=15 ,width_shift_range=0.2 ,shear_range=0.2)\n"]}, {"outputs": [], "execution_count": null, "metadata": {"_kg_hide-input": false, "_kg_hide-output": true, "_uuid": "cb62d161d078459a481d589d84f7df0f2fe61b90", "_cell_guid": "a2381da3-0d41-4317-b980-8e3226aa9cf4", "collapsed": true}, "cell_type": "code", "source": ["#the seed is what determines the random augmentation so u can change it but keep it is the same in the\n", "#mask and image generators\n", "seed = 1\n", "image_datagen.fit(X_train, augment=True, seed=seed)\n", "mask_datagen.fit(Y_train, augment=True, seed=seed)\n", "\n", "x=image_datagen.flow (X_train,batch_size=15,shuffle=True,seed=seed)\n", "y=mask_datagen.flow (Y_train,batch_size=15,shuffle=True,seed=seed)\n", "\n", "\n"]}, {"outputs": [], "execution_count": null, "metadata": {"_uuid": "9fa4dcce7241cddb5d01c1355b61846234e292e2", "_kg_hide-output": true, "_cell_guid": "0284a40f-5a6e-4d2d-b945-48a868c2de0c", "collapsed": true}, "cell_type": "code", "source": ["#u can see here if the generators are sychronized and check the images and masks they produce\n", "\n", "from matplotlib import pyplot as plt\n", "%matplotlib inline\n", "\n", "imshow(x.next()[0].astype (np.uint8))\n", "plt.show()\n", "imshow(np.squeeze (y.next()[0].astype (np.uint8)))\n", "plt.show()"]}, {"outputs": [], "execution_count": null, "metadata": {"_uuid": "05e341103f780b743f8a2ddf95d29fd01b30e415", "_kg_hide-output": true, "_cell_guid": "5a660303-3635-45f7-9caa-fa30320c02ad", "collapsed": true}, "cell_type": "code", "source": ["#creating one generator that generates masks and images\n", "train_generator = zip(x, y)\n", "\n", "model.fit_generator(\n", "    train_generator,\n", "    steps_per_epoch=60,\n", "    epochs=200 )"]}]}