diff --git a/.gitignore b/.gitignore index 92ae505..8b8c4ec 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ venv/ **/data/train_features* **/data/train_labels* **/__pycache__* +**/benchmark/__pycache__* +**/models* \ No newline at end of file diff --git a/unet.ipynb b/unet.ipynb new file mode 100644 index 0000000..1efc301 --- /dev/null +++ b/unet.ipynb @@ -0,0 +1,203 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "import shutil\n", + "import tensorflow as tf\n", + "from tensorflow.keras import backend as K\n", + "from tensorflow.keras.layers import Activation, Lambda, GlobalAveragePooling2D, concatenate\n", + "from tensorflow.keras.layers import UpSampling2D, Conv2D, Dropout, MaxPooling2D, Conv2DTranspose\n", + "from tensorflow.keras.layers import Dense, Flatten, Input\n", + "from tensorflow.keras.models import Model, Sequential, load_model\n", + "from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import pandas as pd\n", + "import cv2\n", + "import pickle\n", + "import random\n", + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "from src.metrics import IOU\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "IMG_HEIGHT = 512\n", + "IMG_WIDTH = 512\n", + "img_dir = '/images'" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "EPOCHS = 30\n", + "batch_size = 16" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Unet model" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "class Unet():\n", + " def __init__(self, num_classes=1):\n", + " self.num_classes=num_classes\n", + "\n", + " def build_model(self):\n", + " in1 = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3 ))\n", + "\n", + " conv1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(in1)\n", + " conv1 = Dropout(0.2)(conv1)\n", + " conv1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv1)\n", + " pool1 = MaxPooling2D((2, 2))(conv1)\n", + "\n", + " conv2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool1)\n", + " conv2 = Dropout(0.2)(conv2)\n", + " conv2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv2)\n", + " pool2 = MaxPooling2D((2, 2))(conv2)\n", + "\n", + " conv3 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool2)\n", + " conv3 = Dropout(0.2)(conv3)\n", + " conv3 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv3)\n", + " pool3 = MaxPooling2D((2, 2))(conv3)\n", + "\n", + " conv4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool3)\n", + " conv4 = Dropout(0.2)(conv4)\n", + " conv4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv4)\n", + "\n", + " up1 = concatenate([UpSampling2D((2, 2))(conv4), conv3], axis=-1)\n", + " conv5 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(up1)\n", + " conv5 = Dropout(0.2)(conv5)\n", + " conv5 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv5)\n", + " \n", + " up2 = concatenate([UpSampling2D((2, 2))(conv5), conv2], axis=-1)\n", + " conv6 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(up2)\n", + " conv6 = Dropout(0.2)(conv6)\n", + " conv6 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv6)\n", + "\n", + " up2 = concatenate([UpSampling2D((2, 2))(conv6), conv1], axis=-1)\n", + " conv7 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(up2)\n", + " conv7 = Dropout(0.2)(conv7)\n", + " conv7 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv7)\n", + " segmentation = Conv2D(self.num_classes, (1, 1), activation='sigmoid', name='seg')(conv7)\n", + " #segmentation = Conv2D(3, (1, 1), activation='sigmoid', name='seg')(conv7)\n", + " model = Model(inputs=[in1], outputs=[segmentation])\n", + "\n", + " return model\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "from src.loss import jaccard_loss" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'jaccard_loss' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/Users/patrycjalazna/Desktop/wko-projekt/unet.ipynb Cell 7'\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m model \u001b[39m=\u001b[39m Unet(num_classes\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m)\u001b[39m.\u001b[39mbuild_model()\n\u001b[1;32m 3\u001b[0m compile_params \u001b[39m=\u001b[39m{\n\u001b[0;32m----> 4\u001b[0m \u001b[39m'\u001b[39m\u001b[39mloss\u001b[39m\u001b[39m'\u001b[39m:jaccard_loss(smooth\u001b[39m=\u001b[39m\u001b[39m90\u001b[39m), \n\u001b[1;32m 5\u001b[0m \u001b[39m'\u001b[39m\u001b[39moptimizer\u001b[39m\u001b[39m'\u001b[39m:\u001b[39m'\u001b[39m\u001b[39mrmsprop\u001b[39m\u001b[39m'\u001b[39m,\n\u001b[1;32m 6\u001b[0m \u001b[39m'\u001b[39m\u001b[39mmetrics\u001b[39m\u001b[39m'\u001b[39m:[IOU]\n\u001b[1;32m 7\u001b[0m }\n\u001b[1;32m 10\u001b[0m model\u001b[39m.\u001b[39mcompile(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mcompile_params)\n\u001b[1;32m 12\u001b[0m tf\u001b[39m.\u001b[39mkeras\u001b[39m.\u001b[39mutils\u001b[39m.\u001b[39mplot_model(model, show_shapes\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n", + "\u001b[0;31mNameError\u001b[0m: name 'jaccard_loss' is not defined" + ] + } + ], + "source": [ + "model = Unet(num_classes=1).build_model()\n", + "\n", + "compile_params ={\n", + " 'loss':jaccard_loss(smooth=90), \n", + " 'optimizer':'rmsprop',\n", + " 'metrics':[IOU]\n", + " }\n", + " \n", + " \n", + "model.compile(**compile_params)\n", + "\n", + "tf.keras.utils.plot_model(model, show_shapes=True)\n", + "\n", + "model_name = \"models/unet.h5\"\n", + "\n", + "modelcheckpoint = ModelCheckpoint(model_name,\n", + " monitor='val_loss',\n", + " mode='auto',\n", + " verbose=1,\n", + " save_best_only=True)\n", + "\n", + "\n", + "history = model.fit_generator(train_gen,\n", + " validation_data=val_gen,\n", + " epochs=EPOCHS,\n", + " steps_per_epoch=100,\n", + " validation_steps = 100,\n", + " shuffle=True,\n", + " validation_freq=1\n", + ")" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "1be0d4179f0eed29061aece4de36a3b887fc5990f515aa1f08bfa33b9ed547bc" + }, + "kernelspec": { + "display_name": "Python 3.9.7 64-bit ('widzenie-komputerowe-env': conda)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}