{"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":"# 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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-11T20:08:04.004287Z","iopub.execute_input":"2022-03-11T20:08:04.004612Z","iopub.status.idle":"2022-03-11T20:08:28.430492Z","shell.execute_reply.started":"2022-03-11T20:08:04.004524Z","shell.execute_reply":"2022-03-11T20:08:28.429653Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom IPython.display import Image, display\nfrom tensorflow.keras.preprocessing.image import load_img\nimport PIL\nfrom PIL import ImageOps\n\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:09:20.794003Z","iopub.execute_input":"2022-03-11T20:09:20.794434Z","iopub.status.idle":"2022-03-11T20:09:26.510516Z","shell.execute_reply.started":"2022-03-11T20:09:20.794398Z","shell.execute_reply":"2022-03-11T20:09:26.509573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\ninput_dir = \"../input/ultra-mnist/train\"\ntarget_dir = \"../input/ultra-mnist/test\"\nimg_size = (160, 160)\nnum_classes = 3\nbatch_size = 16\n\ninput_img_paths = sorted(\n    [\n        os.path.join(input_dir, fname)\n        for fname in os.listdir(input_dir)\n        if fname.endswith(\".jpeg\")\n    ]\n)\ntarget_img_paths = sorted(\n    [\n        os.path.join(target_dir, fname)\n        for fname in os.listdir(target_dir)\n        if fname.endswith(\".jpeg\") and not fname.startswith(\".\")\n    ]\n)\n\nprint(\"Number of samples:\", len(input_img_paths))","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:09:46.688624Z","iopub.execute_input":"2022-03-11T20:09:46.688919Z","iopub.status.idle":"2022-03-11T20:09:46.874621Z","shell.execute_reply.started":"2022-03-11T20:09:46.688868Z","shell.execute_reply":"2022-03-11T20:09:46.873741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n#Display sample of Image Dataset\ni = 7\nfigure, ax = plt.subplots(nrows=1,ncols=2,figsize=(8,8))\nax.ravel()[0].imshow(mpimg.imread(input_img_paths[i]))\nax.ravel()[0].set_title(\"Original image\")\nax.ravel()[0].set_axis_off()\nax.ravel()[1].imshow(mpimg.imread(target_img_paths[i]))\nax.ravel()[1].set_title(\"Mask\")\nax.ravel()[1].set_axis_off()\n#ax.ravel()[2].imshow(PIL.ImageOps.autocontrast(load_img(target_img_paths[i])))\n#ax.ravel()[2].set_title(\"Contrast of mask\")\n#ax.ravel()[2].set_axis_off()\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:10:06.60765Z","iopub.execute_input":"2022-03-11T20:10:06.608257Z","iopub.status.idle":"2022-03-11T20:10:11.433755Z","shell.execute_reply.started":"2022-03-11T20:10:06.608207Z","shell.execute_reply":"2022-03-11T20:10:11.432975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\nclass MnistDataset(keras.utils.Sequence):\n    \"\"\"Helper to iterate over the data (as Numpy arrays).\"\"\"\n\n    def __init__(self, batch_size, img_size, input_img_paths, target_img_paths):\n        self.batch_size = batch_size\n        self.img_size = img_size\n        self.input_img_paths = input_img_paths\n        self.target_img_paths = target_img_paths\n\n    def __len__(self):\n        return len(self.target_img_paths) // self.batch_size\n\n    def __getitem__(self, idx):\n        \"\"\"Returns tuple (input, target) correspond to batch #idx.\"\"\"\n        i = idx * self.batch_size\n        batch_input_img_paths = self.input_img_paths[i : i + self.batch_size]\n        batch_target_img_paths = self.target_img_paths[i : i + self.batch_size]\n        x = np.zeros((self.batch_size,) + self.img_size + (3,), dtype=\"float32\")\n        for j, path in enumerate(batch_input_img_paths):\n            img = load_img(path, target_size=self.img_size)\n            x[j] = img\n        y = np.zeros((self.batch_size,) + self.img_size + (1,), dtype=\"uint8\")\n        for j, path in enumerate(batch_target_img_paths):\n            img = load_img(path, target_size=self.img_size, color_mode=\"grayscale\")\n            y[j] = np.expand_dims(img, 2)\n            # Ground truth labels are 1, 2, 3. Subtract one to make them 0, 1, 2:\n            y[j] -= 1\n        return x, y","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:10:19.808443Z","iopub.execute_input":"2022-03-11T20:10:19.808893Z","iopub.status.idle":"2022-03-11T20:10:19.821034Z","shell.execute_reply.started":"2022-03-11T20:10:19.808862Z","shell.execute_reply":"2022-03-11T20:10:19.820151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n#Build the U-Net Model Architecture\n\ndef get_model(img_size, num_classes):\n    inputs = keras.Input(shape=img_size + (3,))\n\n    ### [First half of the network: downsampling inputs] ###\n\n    # Entry block\n    x = layers.Conv2D(32, 3, strides=2, padding=\"same\")(inputs)\n    x = layers.BatchNormalization()(x)\n    x = layers.Activation(\"relu\")(x)\n\n    previous_block_activation = x  # Set aside residual\n\n    # Blocks 1, 2, 3 are identical apart from the feature depth.\n    for filters in [64, 128, 256]:\n        x = layers.Activation(\"relu\")(x)\n        x = layers.SeparableConv2D(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.Activation(\"relu\")(x)\n        x = layers.SeparableConv2D(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.MaxPooling2D(3, strides=2, padding=\"same\")(x)\n\n        # Project residual\n        residual = layers.Conv2D(filters, 1, strides=2, padding=\"same\")(\n            previous_block_activation\n        )\n        x = layers.add([x, residual])  # Add back residual\n        previous_block_activation = x  # Set aside next residual\n\n    ### [Second half of the network: upsampling inputs] ###\n\n    for filters in [256, 128, 64, 32]:\n        x = layers.Activation(\"relu\")(x)\n        x = layers.Conv2DTranspose(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.Activation(\"relu\")(x)\n        x = layers.Conv2DTranspose(filters, 3, padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n        x = layers.UpSampling2D(2)(x)\n\n        # Project residual\n        residual = layers.UpSampling2D(2)(previous_block_activation)\n        residual = layers.Conv2D(filters, 1, padding=\"same\")(residual)\n        x = layers.add([x, residual])  # Add back residual\n        previous_block_activation = x  # Set aside next residual\n\n    # Add a per-pixel classification layer\n    outputs = layers.Conv2D(num_classes, 3, activation=\"softmax\", padding=\"same\")(x)\n\n    # Define the model\n    model = keras.Model(inputs, outputs)\n    return model\n\n\n# Free up RAM in case the model definition cells were run multiple times\nkeras.backend.clear_session()\n\n# Build model\nmodel = get_model(img_size, num_classes)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:10:35.888399Z","iopub.execute_input":"2022-03-11T20:10:35.88869Z","iopub.status.idle":"2022-03-11T20:10:36.481797Z","shell.execute_reply.started":"2022-03-11T20:10:35.888658Z","shell.execute_reply":"2022-03-11T20:10:36.481048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n#Must calculate 15% of Number of samples: 28000 (=4200) Then 85% (=23800)\n\n#Split Dataset into a training and a validation set\n\nval_samples = 4200 # 85% Training -- 15% Validation (28000 samples 15% is 4200)\nrandom.Random(7000).shuffle(input_img_paths)#Original here is 25%\nrandom.Random(7000).shuffle(target_img_paths)\ntrain_input_img_paths = input_img_paths[:-val_samples]\ntrain_target_img_paths = target_img_paths[:-val_samples]\nval_input_img_paths = input_img_paths[-val_samples:]\nval_target_img_paths = target_img_paths[-val_samples:]\n\n# Instantiate data Sequences for each split\ntrain_gen = MnistDataset(\n    batch_size, img_size, train_input_img_paths, train_target_img_paths\n)\nval_gen = MnistDataset(batch_size, img_size, val_input_img_paths, val_target_img_paths)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:10:51.658737Z","iopub.execute_input":"2022-03-11T20:10:51.659032Z","iopub.status.idle":"2022-03-11T20:10:51.729297Z","shell.execute_reply.started":"2022-03-11T20:10:51.658998Z","shell.execute_reply":"2022-03-11T20:10:51.728579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n#Training\n\n# We use the \"sparse\" version of categorical_crossentropy\n# because our target data is integers.\n#I changed to binary\nmodel.compile(optimizer=\"rmsprop\", loss=\"binary_crossentropy\", metrics=['accuracy'])\n\ncallbacks = [\n    keras.callbacks.ModelCheckpoint(\"mnist_segmentation.h5\", save_best_only=True)\n]\n\nepochs = 1 #I don't have time for 30 or even memeory for that\nmodelunet=model.fit(train_gen, epochs=epochs, validation_data=val_gen, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T20:11:25.51956Z","iopub.execute_input":"2022-03-11T20:11:25.520167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#History for Accuracy","metadata":{}},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n# summarize history for accuracy\nplt.plot(modelunet.history['accuracy'])\nplt.plot(modelunet.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.grid(True)\nplt.show()\n# summarize history for loss\nplt.plot(modelunet.history['loss'])\nplt.plot(modelunet.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.grid(True)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Inference","metadata":{}},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n#Inference\n\n# Generate predictions for all images in the validation set\nval_gen = MnistDataset(batch_size, img_size, val_input_img_paths, val_target_img_paths)\nval_preds = model.predict(val_gen)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n#Display results for validation image\n\ndef display_mask(i):\n    \"\"\"Quick utility to display a model's prediction.\"\"\"\n    mask = np.argmax(val_preds[i], axis=-1)\n    mask = np.expand_dims(mask, axis=-1)\n    img = PIL.ImageOps.autocontrast(keras.preprocessing.image.array_to_img(mask))\n    return img","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Ammar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras\n\n# Display image #20\ni = 'kwvwdnxbot'\nfigure, ax = plt.subplots(nrows=1,ncols=3,figsize=(8,5))\nax.ravel()[0].imshow(mpimg.imread(val_input_img_paths[i]))\nax.ravel()[0].set_title(\"Original image\")\nax.ravel()[0].set_axis_off()\nax.ravel()[1].imshow(mpimg.imread(val_target_img_paths[i]))\nax.ravel()[1].set_title(\"Mask\")\nax.ravel()[1].set_axis_off()\n#ax.ravel()[2].imshow(display_mask(i))  ##commented to avoid error since I don't have mask/valid\n#ax.ravel()[2].set_title(\"Predicted mask \")\nax.ravel()[2].set_axis_off()\nplt.tight_layout()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgement:\n\nAmmar Alhaj Ali  https://www.kaggle.com/ammarnassanalhajali/image-segmentation-with-a-u-net-and-keras","metadata":{}}]}