diff --git a/model.py b/model.py index 7df5b6889..f15b8c753 100644 --- a/model.py +++ b/model.py @@ -52,11 +52,11 @@ def unet(pretrained_weights = None,input_size = (256,256,1)): conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9) - model = Model(input = inputs, output = conv10) + model = Model(inputs = inputs, outputs = conv10) model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy']) - #model.summary() + model.summary() if(pretrained_weights): model.load_weights(pretrained_weights) diff --git a/output.txt b/output.txt new file mode 100644 index 000000000..609e97b36 --- /dev/null +++ b/output.txt @@ -0,0 +1,49 @@ +[[[21 21 21] + [21 21 21] + [21 21 21] + ... + [11 11 11] + [11 11 11] + [11 11 11]] + + [[21 21 21] + [21 21 21] + [21 21 21] + ... + [11 11 11] + [11 11 11] + [11 11 11]] + + [[21 21 21] + [21 21 21] + [21 21 21] + ... + [11 11 11] + [11 11 11] + [11 11 11]] + + ... + + [[ 7 7 7] + [ 7 7 7] + [ 7 7 7] + ... + [26 26 26] + [26 26 26] + [26 26 26]] + + [[ 7 7 7] + [ 7 7 7] + [ 7 7 7] + ... + [26 26 26] + [26 26 26] + [26 26 26]] + + [[ 7 7 7] + [ 7 7 7] + [ 7 7 7] + ... + [26 26 26] + [26 26 26] + [26 26 26]]] \ No newline at end of file diff --git a/testGenerators.ipynb b/testGenerators.ipynb new file mode 100644 index 000000000..ef6681eaf --- /dev/null +++ b/testGenerators.ipynb @@ -0,0 +1,83 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2023-06-02 14:00:54.171861: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2023-06-02 14:00:54.227441: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2023-06-02 14:00:54.227936: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", + "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2023-06-02 14:00:55.231488: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" + ] + } + ], + "source": [ + "from data import *" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "data_gen_args = dict(rotation_range=0.2,\n", + " width_shift_range=0.05,\n", + " height_shift_range=0.05,\n", + " shear_range=0.05,\n", + " zoom_range=0.05,\n", + " horizontal_flip=True,\n", + " fill_mode='nearest')\n", + "myGenerator = trainGenerator(20,'data/membrane/train','image','label',data_gen_args,save_to_dir = \"data/membrane/train/aug\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'generator' object is not subscriptable", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m myGenerator[\u001b[39m0\u001b[39;49m]\n", + "\u001b[0;31mTypeError\u001b[0m: 'generator' object is not subscriptable" + ] + } + ], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.10.6" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/testMaskShape.ipynb b/testMaskShape.ipynb new file mode 100644 index 000000000..0c7ae1c8b --- /dev/null +++ b/testMaskShape.ipynb @@ -0,0 +1,247 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import cv2\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "mask = cv2.imread(\"./img/2label.png\")\n", + "# Open the file in write mode\n", + "with open(\"output.txt\", \"w\") as file:\n", + " array_string = np.array2string(mask)\n", + " file.write(array_string) # Write the data to the file" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(375, 1242, 3)" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mask.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(375, 1242)" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mask = mask[:,:,:,0] if(len(mask.shape) == 4) else mask[:,:,0]\n", + "mask.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(375, 1242, 8)" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "zero_new_mask = np.zeros(mask.shape + (8,))\n", + "zero_new_mask.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " ...\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]]\n", + "\n", + " [[0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " ...\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]]\n", + "\n", + " [[0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " ...\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]]\n", + "\n", + " ...\n", + "\n", + " [[0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " ...\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]]\n", + "\n", + " [[0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " ...\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]]\n", + "\n", + " [[0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " ...\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]\n", + " [0. 0. 0. ... 0. 0. 0.]]]\n" + ] + } + ], + "source": [ + "print(zero_new_mask)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 2]\n", + " [1 2 0]]\n" + ] + } + ], + "source": [ + "mask = np.array([[0, 1, 2], [1, 2, 0]])\n", + "mask.shape\n", + "print(mask)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0.]]\n", + "\n", + " [[0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0. 0.]]]\n" + ] + } + ], + "source": [ + "zero_new_mask = np.zeros(mask.shape + (8,))\n", + "zero_new_mask.shape\n", + "print(zero_new_mask)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0., 0., 0., 0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0., 0., 0., 0.]])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.reshape(zero_new_mask,(zero_new_mask.shape[0]*zero_new_mask.shape[1],zero_new_mask.shape[2]))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.10.6" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/testOneImageWithModel.ipynb b/testOneImageWithModel.ipynb new file mode 100644 index 000000000..8c7579e96 --- /dev/null +++ b/testOneImageWithModel.ipynb @@ -0,0 +1,107 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2023-06-02 14:53:44.720932: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2023-06-02 14:53:44.778378: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2023-06-02 14:53:44.779410: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", + "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2023-06-02 14:53:45.830611: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" + ] + } + ], + "source": [ + "import keras.models as models\n", + "import cv2\n", + "import os\n", + "import numpy as np\n", + "import skimage.io as io\n", + "import skimage.transform as trans" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "model = models.load_model('unet_membrane.hdf5')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1/1 [==============================] - 1s 844ms/step\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "qt.qpa.plugin: Could not find the Qt platform plugin \"wayland\" in \"/home/muss/.local/lib/python3.10/site-packages/cv2/qt/plugins\"\n" + ] + } + ], + "source": [ + "\n", + "# Load the input image\n", + "img_path = './data/membrane/test/0.png'\n", + "input_image = cv2.imread(img_path)\n", + "\n", + "\n", + "img = io.imread(\"./data/membrane/test/0.png\",as_gray = True)\n", + "img = img / 255\n", + "img = trans.resize(img,(256,256))\n", + "img = np.reshape(img,img.shape+(1,)) if (not False) else img\n", + "img = np.reshape(img, img.shape)\n", + "\n", + "# Perform segmentation\n", + "segmented_image = model.predict(np.expand_dims(img, axis=0))\n", + "segmented_image = np.squeeze(segmented_image) # Remove the batch dimension if present\n", + "\n", + "# Post-process the segmented image (if needed)\n", + "# ...\n", + "\n", + "# Visualize the segmented image\n", + "cv2.imshow('Segmented Image', segmented_image)\n", + "cv2.waitKey(0)\n", + "cv2.destroyAllWindows()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.10.6" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/testWithOwnDataset.ipynb b/testWithOwnDataset.ipynb new file mode 100644 index 000000000..a9f368bbc --- /dev/null +++ b/testWithOwnDataset.ipynb @@ -0,0 +1,76 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2023-06-06 13:51:05.795273: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2023-06-06 13:51:05.854367: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.\n", + "2023-06-06 13:51:05.855698: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", + "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2023-06-06 13:51:06.731947: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" + ] + } + ], + "source": [ + "from model import *\n", + "from data import *\n", + "import os\n", + "#os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "data_gen_args = dict(rotation_range=0.2,\n", + " width_shift_range=0.05,\n", + " height_shift_range=0.05,\n", + " shear_range=0.05,\n", + " zoom_range=0.05,\n", + " horizontal_flip=True,\n", + " fill_mode='nearest')\n", + "myGene = trainGenerator(2,'../kitti_pixel_level_semantic/prepared_data/training/','image','mask',data_gen_args,save_to_dir = \"../kitti_pixel_level_semantic/prepared_data/training/augmented/\")\n", + "\n", + "model = unet()\n", + "model_checkpoint = ModelCheckpoint('unet_kitti_epoch{epoch:02d}.hdf5', monitor='loss',verbose=1)\n", + "model.fit_generator(myGene,steps_per_epoch=300,epochs=3,callbacks=[model_checkpoint])\n", + "\n", + "testGene = testGenerator(\"../kitti_pixel_level_semantic/prepared_data/testing/image/\")\n", + "results = model.predict_generator(testGene,30,verbose=1)\n", + "saveResult(\"data/membrane/test\",results)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.10.6" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}