Giter Club home page Giter Club logo

image-segmentation-keras's Introduction

Image Segmentation Keras : Implementation of Segnet, FCN, UNet, PSPNet and other models in Keras.

PyPI version Downloads Build Status MIT license Twitter

Implementation of various Deep Image Segmentation models in keras.

News : Some functionality of this repository has been integrated with https://liner.ai . Check it out!!

Link to the full blog post with tutorial : https://divamgupta.com/image-segmentation/2019/06/06/deep-learning-semantic-segmentation-keras.html

Working Google Colab Examples:

Training using GUI interface

You can also train segmentation models on your computer with https://liner.ai

Train Inference / Export
https://liner.ai https://liner.ai
https://liner.ai https://liner.ai

Models

Following models are supported:

model_name Base Model Segmentation Model
fcn_8 Vanilla CNN FCN8
fcn_32 Vanilla CNN FCN8
fcn_8_vgg VGG 16 FCN8
fcn_32_vgg VGG 16 FCN32
fcn_8_resnet50 Resnet-50 FCN32
fcn_32_resnet50 Resnet-50 FCN32
fcn_8_mobilenet MobileNet FCN32
fcn_32_mobilenet MobileNet FCN32
pspnet Vanilla CNN PSPNet
pspnet_50 Vanilla CNN PSPNet
pspnet_101 Vanilla CNN PSPNet
vgg_pspnet VGG 16 PSPNet
resnet50_pspnet Resnet-50 PSPNet
unet_mini Vanilla Mini CNN U-Net
unet Vanilla CNN U-Net
vgg_unet VGG 16 U-Net
resnet50_unet Resnet-50 U-Net
mobilenet_unet MobileNet U-Net
segnet Vanilla CNN Segnet
vgg_segnet VGG 16 Segnet
resnet50_segnet Resnet-50 Segnet
mobilenet_segnet MobileNet Segnet

Example results for the pre-trained models provided :

Input Image Output Segmentation Image

How to cite

If you are using this library, please cite using:

@article{gupta2023image,
  title={Image segmentation keras: Implementation of segnet, fcn, unet, pspnet and other models in keras},
  author={Gupta, Divam},
  journal={arXiv preprint arXiv:2307.13215},
  year={2023}
}

Getting Started

Prerequisites

  • Keras ( recommended version : 2.4.3 )
  • OpenCV for Python
  • Tensorflow ( recommended version : 2.4.1 )
apt-get install -y libsm6 libxext6 libxrender-dev
pip install opencv-python

Installing

Install the module

Recommended way:

pip install --upgrade git+https://github.com/divamgupta/image-segmentation-keras

or

pip install keras-segmentation

or

git clone https://github.com/divamgupta/image-segmentation-keras
cd image-segmentation-keras
python setup.py install

Pre-trained models:

from keras_segmentation.pretrained import pspnet_50_ADE_20K , pspnet_101_cityscapes, pspnet_101_voc12

model = pspnet_50_ADE_20K() # load the pretrained model trained on ADE20k dataset

model = pspnet_101_cityscapes() # load the pretrained model trained on Cityscapes dataset

model = pspnet_101_voc12() # load the pretrained model trained on Pascal VOC 2012 dataset

# load any of the 3 pretrained models

out = model.predict_segmentation(
    inp="input_image.jpg",
    out_fname="out.png"
)

Preparing the data for training

You need to make two folders

  • Images Folder - For all the training images
  • Annotations Folder - For the corresponding ground truth segmentation images

The filenames of the annotation images should be same as the filenames of the RGB images.

The size of the annotation image for the corresponding RGB image should be same.

For each pixel in the RGB image, the class label of that pixel in the annotation image would be the value of the blue pixel.

Example code to generate annotation images :

import cv2
import numpy as np

ann_img = np.zeros((30,30,3)).astype('uint8')
ann_img[ 3 , 4 ] = 1 # this would set the label of pixel 3,4 as 1

cv2.imwrite( "ann_1.png" ,ann_img )

Only use bmp or png format for the annotation images.

Download the sample prepared dataset

Download and extract the following:

https://drive.google.com/file/d/0B0d9ZiqAgFkiOHR1NTJhWVJMNEU/view?usp=sharing

You will get a folder named dataset1/

Using the python module

You can import keras_segmentation in your python script and use the API

from keras_segmentation.models.unet import vgg_unet

model = vgg_unet(n_classes=51 ,  input_height=416, input_width=608  )

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5
)

out = model.predict_segmentation(
    inp="dataset1/images_prepped_test/0016E5_07965.png",
    out_fname="/tmp/out.png"
)

import matplotlib.pyplot as plt
plt.imshow(out)

# evaluating the model 
print(model.evaluate_segmentation( inp_images_dir="dataset1/images_prepped_test/"  , annotations_dir="dataset1/annotations_prepped_test/" ) )

Usage via command line

You can also use the tool just using command line

Visualizing the prepared data

You can also visualize your prepared annotations for verification of the prepared data.

python -m keras_segmentation verify_dataset \
 --images_path="dataset1/images_prepped_train/" \
 --segs_path="dataset1/annotations_prepped_train/"  \
 --n_classes=50
python -m keras_segmentation visualize_dataset \
 --images_path="dataset1/images_prepped_train/" \
 --segs_path="dataset1/annotations_prepped_train/"  \
 --n_classes=50

Training the Model

To train the model run the following command:

python -m keras_segmentation train \
 --checkpoints_path="path_to_checkpoints" \
 --train_images="dataset1/images_prepped_train/" \
 --train_annotations="dataset1/annotations_prepped_train/" \
 --val_images="dataset1/images_prepped_test/" \
 --val_annotations="dataset1/annotations_prepped_test/" \
 --n_classes=50 \
 --input_height=320 \
 --input_width=640 \
 --model_name="vgg_unet"

Choose model_name from the table above

Getting the predictions

To get the predictions of a trained model

python -m keras_segmentation predict \
 --checkpoints_path="path_to_checkpoints" \
 --input_path="dataset1/images_prepped_test/" \
 --output_path="path_to_predictions"

Video inference

To get predictions of a video

python -m keras_segmentation predict_video \
 --checkpoints_path="path_to_checkpoints" \
 --input="path_to_video" \
 --output_file="path_for_save_inferenced_video" \
 --display

If you want to make predictions on your webcam, don't use --input, or pass your device number: --input 0
--display opens a window with the predicted video. Remove this argument when using a headless system.

Model Evaluation

To get the IoU scores

python -m keras_segmentation evaluate_model \
 --checkpoints_path="path_to_checkpoints" \
 --images_path="dataset1/images_prepped_test/" \
 --segs_path="dataset1/annotations_prepped_test/"

Fine-tuning from existing segmentation model

The following example shows how to fine-tune a model with 10 classes .

from keras_segmentation.models.model_utils import transfer_weights
from keras_segmentation.pretrained import pspnet_50_ADE_20K
from keras_segmentation.models.pspnet import pspnet_50

pretrained_model = pspnet_50_ADE_20K()

new_model = pspnet_50( n_classes=51 )

transfer_weights( pretrained_model , new_model  ) # transfer weights from pre-trained model to your model

new_model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5
)

Knowledge distillation for compressing the model

The following example shows transfer the knowledge from a larger ( and more accurate ) model to a smaller model. In most cases the smaller model trained via knowledge distilation is more accurate compared to the same model trained using vanilla supervised learning.

from keras_segmentation.predict import model_from_checkpoint_path
from keras_segmentation.models.unet import unet_mini
from keras_segmentation.model_compression import perform_distilation

model_large = model_from_checkpoint_path( "/checkpoints/path/of/trained/model" )
model_small = unet_mini( n_classes=51, input_height=300, input_width=400  )

perform_distilation ( data_path="/path/to/large_image_set/" , checkpoints_path="path/to/save/checkpoints" , 
    teacher_model=model_large ,  student_model=model_small  , distilation_loss='kl' , feats_distilation_loss='pa' )

Adding custom augmentation function to training

The following example shows how to define a custom augmentation function for training.

from keras_segmentation.models.unet import vgg_unet
from imgaug import augmenters as iaa

def custom_augmentation():
    return  iaa.Sequential(
        [
            # apply the following augmenters to most images
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.5), # horizontally flip 50% of all images
        ])

model = vgg_unet(n_classes=51 ,  input_height=416, input_width=608)

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5, 
    do_augment=True, # enable augmentation 
    custom_augmentation=custom_augmentation # sets the augmention function to use
)

Custom number of input channels

The following example shows how to set the number of input channels.

from keras_segmentation.models.unet import vgg_unet

model = vgg_unet(n_classes=51 ,  input_height=416, input_width=608, 
                 channels=1 # Sets the number of input channels
                 )

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5, 
    read_image_type=0  # Sets how opencv will read the images
                       # cv2.IMREAD_COLOR = 1 (rgb),
                       # cv2.IMREAD_GRAYSCALE = 0,
                       # cv2.IMREAD_UNCHANGED = -1 (4 channels like RGBA)
)

Custom preprocessing

The following example shows how to set a custom image preprocessing function.

from keras_segmentation.models.unet import vgg_unet

def image_preprocessing(image):
    return image + 1

model = vgg_unet(n_classes=51 ,  input_height=416, input_width=608)

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5,
    preprocessing=image_preprocessing # Sets the preprocessing function
)

Custom callbacks

The following example shows how to set custom callbacks for the model training.

from keras_segmentation.models.unet import vgg_unet
from keras.callbacks import ModelCheckpoint, EarlyStopping

model = vgg_unet(n_classes=51 ,  input_height=416, input_width=608 )

# When using custom callbacks, the default checkpoint saver is removed
callbacks = [
    ModelCheckpoint(
                filepath="checkpoints/" + model.name + ".{epoch:05d}",
                save_weights_only=True,
                verbose=True
            ),
    EarlyStopping()
]

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5,
    callbacks=callbacks
)

Multi input image input

The following example shows how to add additional image inputs for models.

from keras_segmentation.models.unet import vgg_unet

model = vgg_unet(n_classes=51 ,  input_height=416, input_width=608)

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5,
    other_inputs_paths=[
        "/path/to/other/directory"
    ],
    
    
#     Ability to add preprocessing
    preprocessing=[lambda x: x+1, lambda x: x+2, lambda x: x+3], # Different prepocessing for each input
#     OR
    preprocessing=lambda x: x+1, # Same preprocessing for each input
)

Projects using keras-segmentation

Here are a few projects which are using our library :

If you use our code in a publicly available project, please add the link here ( by posting an issue or creating a PR )

image-segmentation-keras's People

Contributors

bakwc avatar bhawnamehbubani avatar divamgupta avatar dreamyang-liu avatar fracpete avatar hmate9 avatar jaledmc avatar keval2232 avatar marius-juston avatar matthew-mcateer avatar piebro avatar rjalfa avatar rusito-23 avatar sushantag9 avatar thomasaarholt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

image-segmentation-keras's Issues

I was traning segnet model using ADE20K_2016_07_26 dataset with resizing the image to 256*256*3 i was getting error

Traceback (most recent call last):
File "train.py", line 42, in
verbose=2)
File "/usr/local/lib/python3.6/dist-packages/keras/models.py", line 1002, in fit
validation_steps=validation_steps)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1630, in fit
batch_size=batch_size)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1480, in _standardize_user_data
exception_prefix='target')
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected activation_26 to have 3 dimensions, but got array with shape (5, 256, 256, 3)

Input dimension mis-match

I've got the following error on predict.py:

Traceback (most recent call last):
File "C:\Users\Matheus\Anaconda3\envs\tensorflow\lib\site-packages\theano\compile\function_module.py", line 903, in call
self.fn() if output_subset is None else
ValueError: Input dimension mis-match. (input[0].shape[3] = 50, input[1].shape[3] = 40)

Any thoughts? =/

readme instructions for installation are not safe

I was looking at the instructions for installation, and it is not recommended to do sudo pip install. If you want to install the packages system wide, you should use your package manager, otherwise just use pip install --user or virtualenv.

There was a timeout, so I thought it hadn't been posted. Sorry about the needless issue.

vgg.load_weights(VGG_Weights_path) Error: input shapes: [102400,4096], [25088,4096]

I am getting an error at the line:
vgg.load_weights(VGG_Weights_path)

the error is:
ValueError: Dimension 0 in both shapes must be equal, but are 102400 and 25088 for 'Assign_26' (op: 'Assign') with input shapes: [102400,4096], [25088,4096].

Please note that:
I am running Keras with the Tensorflow backend because, with the Theano backend, I am getting an error which causes Theano to run on the CPU rather thsan the GPU (and only on 1 Core of the CPU).
ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: ('nvcc return status', 2, ....

why my loss is nan [I use the data set in the link that you provide」

THEANO_FLAGS=device=gpu,floatX=float32 python train.py --save_weights_path=weights/ex1 --train_images="data/dataset2/images_prepped_train/" --train_annotations="data/dataset2/annotations_prepped_train/" --val_images="data/dataset2/images_prepped_test/" --val_annotations="data/dataset2/annotations_prepped_test/" --n_classes=2 --input_height=256 --input_width=320 --model_name="vgg_segnet"

512/512 [==============================] - 49s - loss: nan - acc: 0.9032 - val_loss: nan - val_acc: 0.8787
Epoch 1/1
512/512 [==============================] - 49s - loss: nan - acc: 0.9005 - val_loss: nan - val_acc: 0.8800
Epoch 1/1
512/512 [==============================] - 49s - loss: nan - acc: 0.9017 - val_loss: nan - val_acc: 0.8788
Epoch 1/1
512/512 [==============================] - 49s - loss: nan - acc: 0.9033 - val_loss: nan - val_acc: 0.8804
Epoch 1/1
512/512 [==============================] - 50s - loss: nan - acc: 0.9006 - val_loss: nan - val_acc: 0.8791
Epoch 1/1
512/512 [==============================] - 49s - loss: nan - acc: 0.9056 - val_loss: nan - val_acc: 0.8805
Epoch 1/1

When I run train.py, ImportError:No module named numpy

Thank you for the code you provided and it helps me a lot,but when I run train.py, the ImportError:No module named numpy happened,But I already downloaded numpy,I don’t know what’s going on. I hope you can give me some advice.thanks!
1795787392
211000934

951529398

Errors that occur in my own data

File "train.py", line 79, in
m.fit_generator( G , 871 , validation_data=G2 , validation_steps=1 , epochs=1 )
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/engine/training.py", line 2262, in fit_generator
workers=0)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/engine/training.py", line 2377, in evaluate_generator
generator_output = next(output_generator)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/utils/data_utils.py", line 779, in get
six.reraise(value.class, value, value.traceback)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/utils/data_utils.py", line 644, in _data_generator_task
generator_output = next(self._generator)
File "/home/atsushi/keras/work/image-segmentation-keras/LoadBatches.py", line 69, in imageSegmentationGenerator
assert len( images ) == len(segmentations)
AssertionError

Is there a problem with Labels?

ValueError: rng_mrg cpu-implementation does not support more than (2**31 -1) samples

KERAS_BACKEND=theano THEANO_FLAGS=floatX=float32 python predict.py --save_weights_path=weights/ex1 --epoch_number=0 --test_images="data/dataset1/my_test/" --output_path="data/dataset1/my_result/" --n_classes=10 --input_height=853 --input_width=1280 --model_name="vgg_segnet"
Using Theano backend.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
Traceback (most recent call last):
File "predict.py", line 31, in
m = modelFN( n_classes , input_height=input_height, input_width=input_width )
File "/home/sanek/Java/Projects/Python/ImageSegmentation/Models/VGGSegnet.py", line 51, in VGGSegnet
x = Dense(4096, activation='relu', name='fc1')(x)
File "/usr/lib64/python2.7/site-packages/keras/engine/base_layer.py", line 431, in call
self.build(unpack_singleton(input_shapes))
File "/usr/lib64/python2.7/site-packages/keras/layers/core.py", line 866, in build
constraint=self.kernel_constraint)
File "/usr/lib64/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, kwargs)
File "/usr/lib64/python2.7/site-packages/keras/engine/base_layer.py", line 252, in add_weight
constraint=constraint)
File "/usr/lib64/python2.7/site-packages/keras/backend/theano_backend.py", line 155, in variable
value = value.eval()
File "/usr/lib64/python2.7/site-packages/theano/gof/graph.py", line 525, in eval
rval = self._fn_cacheinputs
File "/usr/lib64/python2.7/site-packages/theano/compile/function_module.py", line 917, in call
storage_map=getattr(self.fn, 'storage_map', None))
File "/usr/lib64/python2.7/site-packages/theano/gof/link.py", line 325, in raise_with_op
reraise(exc_type, exc_value, exc_trace)
File "/usr/lib64/python2.7/site-packages/theano/compile/function_module.py", line 903, in call
self.fn() if output_subset is None else
ValueError: rng_mrg cpu-implementation does not support more than (2
31 -1) samples
Apply node that caused the error: mrg_uniform{TensorType(float32, matrix),inplace}(<TensorType(int32, matrix)>, TensorConstant{[532480 4096]})
Toposort index: 0
Inputs types: [TensorType(int32, matrix), TensorType(int64, vector)]
Inputs shapes: [(15360, 6), (2,)]
Inputs strides: [(24, 4), (8,)]
Inputs values: ['not shown', array([532480, 4096])]
Outputs clients: [['output'], [Elemwise{Composite{(i0 + (i1 * i2))}}[(0, 2)](TensorConstant{(1, 1) of ..0033439517}, TensorConstant{(1, 1) of ..0066879033}, mrg_uniform{TensorType(float32, matrix),inplace}.1)]]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

When making predictions, my command is :
python predict.py --save_weights_path=weights/ex1 --epoch_number=5 --test_images="data/dataset1/images_prepped_test/" --output_path="data/predictions/"--n_classes=10 --input_height=320 --input_width=640 --model_name="vgg_segnet"
However I got the following error:
(keras_segmentation) C:\MyKeras\image-segmentation-keras-py3>python predict.py --save_weights_path=weights/ex1 --epoch_number=5 --test_images="data/dataset1/images_prepped_test/" --output_path="data/predictions/"--n_classes=10 --input_height=320 --input_width=640 --model_name="vgg_segnet" Using Theano backend. Traceback (most recent call last): File "predict.py", line 36, in <module> m = modelFN( n_classes , input_height=input_height, input_width=input_width ) File "C:\MyKeras\image-segmentation-keras-py3\Models\VGGSegnet.py", line 77, in VGGSegnet o = Conv2D( n_classes , (3, 3) , padding='same', data_format='channels_first' )( o ) File "C:\Users\two\Anaconda3\envs\keras_segmentation\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__ self.build(unpack_singleton(input_shapes)) File "C:\Users\two\Anaconda3\envs\keras_segmentation\lib\site-packages\keras\layers\convolutional.py", line 138, in build constraint=self.kernel_constraint) File "C:\Users\two\Anaconda3\envs\keras_segmentation\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "C:\Users\two\Anaconda3\envs\keras_segmentation\lib\site-packages\keras\engine\base_layer.py", line 249, in add_weight weight = K.variable(initializer(shape), File "C:\Users\two\Anaconda3\envs\keras_segmentation\lib\site-packages\keras\initializers.py", line 202, in __call__ fan_in, fan_out = _compute_fans(shape) File "C:\Users\two\Anaconda3\envs\keras_segmentation\lib\site-packages\keras\initializers.py", line 473, in _compute_fans fan_out = shape[-1] * receptive_field_size TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

Kindly to ask how to fix this? : )

predict output_path

Please notice that before running predict.py you should first create the output_path directory

`load_weights` requires h5py.

I Download the sample prepared dataset and Place the dataset1/ folder in data/
When I run

python visualizeDataset.py \
 --images="data/dataset1/images_prepped_train/" \
 --annotations="data/dataset1/annotations_prepped_train/" \
 --n_classes=10 

It works.
But when I run

THEANO_FLAGS=device=cpu,floatX=float32  python  train.py  --save_weights_path=weights/ex1  --train_images="data/dataset1/images_prepped_train/"  --train_annotations="data/dataset1/annotations_prepped_train/"  --val_images="data/dataset1/images_prepped_test/"  --val_annotations="data/dataset1/annotations_prepped_test/"  --n_classes=10  --input_height=320  --input_width=640  --model_name="vgg_segnet" --load_weights="data/vgg16_weights_th_dim_ordering_th_kernels.h5"

It comes to a bug

Traceback (most recent call last):
  File "train.py", line 51, in <module>
    m = modelFN( n_classes , input_height=input_height, input_width=input_width   )
  File "/home/Proj/T_keras/image-segmentation-keras/Models/VGGSegnet.py", line 56, in VGGSegnet
    vgg.load_weights(VGG_Weights_path)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2646, in load_weights
    raise ImportError('`load_weights` requires h5py.')
ImportError: `load_weights` requires h5py.

What should I do?
Thanks a lot.

Resize method for segmentation

In the function getSegmentationArr in LoadBatches.py, I think that you need to use

cv2.resize(img, ( width , height ), cv2.INTER_NEAREST)

instead of the default one that you are currently using, which is equivalent to:

cv2.resize(img, ( width , height ), cv2.INTER_LINEAR)

The latter one is used to image resizing, but if you want to resize a segmentation with fixed number of labels, you probably don't want to use a bilinear interpolation. This may introduce labels that do not exist in the original image.

Convert to tensorflow backend

How this part should look using tensorflow backend?

https://github.com/divamgupta/image-segmentation-keras/blob/master/Models/FCN32.py#L82

o = (Reshape(( -1  , outputHeight*outputWidth   )))(o)
o = (Permute((2, 1)))(o)
o = (Activation('softmax'))(o)

Like this? :

    x = Conv2DTranspose(NUMBER_OF_CLASSES, kernel_size=(32, 32), strides=(16, 16), activation='softmax', padding='same')(x)
    head = Reshape((-1,NUMBER_OF_CLASSES))(x)
    model = Model(inputs=model.inputs, outputs=head)

Also why is bias is not used in Conv2DTranspose layer?
https://github.com/divamgupta/image-segmentation-keras/blob/master/Models/FCN32.py#L74

wrong with predict.py

when I python predict.py
`Traceback (most recent call last):
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1567, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 40 and 14. Shapes are [40] and [14]. for 'Assign_22' (op: 'Assign') with input shapes: [40], [14].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/media/wrc/新加卷/lxy/image-segmentation-keras/predict.py", line 34, in
m.load_weights( args.save_weights_path + "epoch.h5" )
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/keras/engine/network.py", line 1166, in load_weights
f, self.layers, reshape=reshape)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/keras/engine/saving.py", line 1058, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 2465, in batch_set_value
assign_op = x.assign(assign_placeholder)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 615, in assign
return state_ops.assign(self._variable, value, use_locking=use_locking)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/ops/state_ops.py", line 283, in assign
validate_shape=validate_shape)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/ops/gen_state_ops.py", line 60, in assign
use_locking=use_locking, name=name)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
op_def=op_def)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1734, in init
control_input_ops)
File "/home/wrc/anaconda3/envs/unet/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1570, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 0 in both shapes must be equal, but are 40 and 14. Shapes are [40] and [14]. for 'Assign_22' (op: 'Assign') with input shapes: [40], [14].`
anybody can help me?

Segmentation fault (core dumped)

When training the model getting following error:
azhe@GTX1080Ti:~/projects/segmentation$ THEANO_FLAGS=device=cuda,floatX=float32 python train.py --save_weights_path=weights/export_unet --train_images="data/dataset/train/" --train_annotations="data/dataset/train_label/" --val_images="data/dataset/val/" --val_annotations="data/dataset/val_label/" --n_classes=256 --input_height=512 --input_width=512 --model_name='vgg_unet'
/home/azhe/anaconda3/lib/python3.6/site-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Using Theano backend.
Using cuDNN version 7003 on context None
Mapped name None to device cuda: GeForce GTX 1080 Ti (0000:01:00.0)
Model output shape (None, 65536, 256)
Epoch 1/1
Segmentation fault (core dumped)

How to solve it,can you help me?

ModuleNotFoundError: No module named 'VGGUnet'

When training the model getting following error.

Traceback (most recent call last):
  File "train.py", line 2, in <module>
    import Models , LoadBatches
  File "/home/nd/image-segmentation-keras-master/Models/__init__.py", line 1, in <module>
    import VGGUnet
ModuleNotFoundError: No module named 'VGGUnet'

How to solve it.

Simple Segnet Model Implementation

The paper for segnet says that encoder and decoder pairs are connected so as to pass memorized pooling indices. These pooling indices are used by the decoder during the upsampling. However, in the implementation, Upsampling2D will not do what has been proposed in the paper.

Getting "Killed" ouput when trying to run training example

After following the README instructions, and running
THEANO_FLAGS=device=gpu,floatX=float32 python train.py --save_weights_path=weights/ex1 --train_images="data/dataset1/images_prepped_train/" --train_annotations="data/dataset1/annotations_prepped_train/" --val_images="data/dataset1/images_prepped_test/" --val_annotations="data/dataset1/annotations_prepped_test/" --n_classes=10 --model_name="vgg_segnet"

I'm getting the output

/usr/local/lib/python2.7/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
2018-01-10 15:35:45.311492: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
Killed

Trying to run the command with sudo give the same, minus the 'killed':

/usr/local/lib/python2.7/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.

Assertion error at LoadBatches.py for training

I'm having the following error: /LoadBatches.py", line 71, in imageSegmentationGenerator
assert( im.split('/')[-1].split(".")[0] == seg.split('/')[-1].split(".")[0] )
AssertionError

Any thoughts? =(

Segfault in predict.py

Seemingly at the very end of running predict.py, the program segfaults. It occurs after the for loop that iterates through all the images. It seems that everything works, so it's unclear where the segfault is happening or if it's even actually a problem.

loss: nan - acc: nan - val_loss: nan - val_acc: nan Why

python train.py --save_weights_path=weights/ex1 --train_images="data/dataset1/images_prepped_train/" --train_annotations="data/dataset1/annotations_prepped_train/" --val_images="data/dataset1/images_prepped_test/" --val_annotations="data/dataset1/annotations_prepped_test/" --n_classes=10 --input_height=320 --input_width=640 --model_name="vgg_segnet"

i follow what the tutorial,but the result is this:
Model output shape (None, 51200, 10)
Epoch 1/1
Epoch 1/1
512/512 [==============================] - 127s 247ms/step - loss: nan - acc: nan - val_loss: nan - val_acc: nan
Epoch 1/1
512/512 [==============================] - 127s 248ms/step - loss: nan - acc: nan - val_loss: 51200.0000 - val_acc: 0.0000e+00
Epoch 1/1
512/512 [==============================] - 127s 247ms/step - loss: nan - acc: nan - val_loss: nan - val_acc: nan
Epoch 1/1
512/512 [==============================] - 127s 248ms/step - loss: nan - acc: nan - val_loss: nan - val_acc: nan
Epoch 1/1
512/512 [==============================] - 127s 248ms/step - loss: nan - acc: nan - val_loss: 51200.0000 - val_acc: 0.0000e+00

number of maxpooling and upsampling in VGGSegnet and VGGUnet is not same

The number of times maxpooling used is not equal to number of times upsampling in VGGsegnet and VGGUnet due to which output image dimensions are not equal to input image dimensions.
In this case we need to use resize as you have done in predict.
If # of maxpool == # of upsampling layer, then input image dim == output image dim and no need of resize.

using resize might lead to less accuracy

pre-training

ValueError: You are trying to load a weight file containing 19 layers into a model with 21 layers.

Can you pre-training only vggsegnet?
This error appears in fcn8.

Custom dataset

@divamgupta How do we prepare annotation image?
May I know what "For each pixel in the RGB image, the class label of that pixel in the annotation image would be the value of the blue pixel." means?

readme instructions for installation are not safe

I was looking at the instructions for installation, and it is not recommended to do sudo pip install. If you want to install the packages system wide, you should use your package manager, otherwise just use pip install --user or virtualenv.

What is the cause of this?

Epoch 1/10
Traceback (most recent call last):
File "train.py", line 79, in
m.fit_generator( G , 512 , validation_data=G2 , validation_steps=200 , epochs=10 )
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/engine/training.py", line 2212, in fit_generator
generator_output = next(output_generator)
File "/home/atsushi/.local/lib/python2.7/site-packages/keras/utils/data_utils.py", line 771, in get
raise StopIteration()
StopIteration

What is the cause of this?

AssertionError

I get AssertionError for line 61 in LoadBatches.py that is, for the following line:

assert images_path[-1] == '/'

What could be the problem? In fact, I get AssertionError in all files whereever assert [-1] is present

Different names [ground truth image and training image]

The README says that the ground truth segmented images need to have the same names as the corresponding training image. I want to try this code on ISIC2017 dataset, but there the training image name is ISIC_0000000.jpg while the ground truth segmented images are named as ISIC_0000000_segmentation.png

How to circumvent this problem without renaming all ground truth images?

Question on accuracy and citation

Hello. My name is Jeremy Ban at KAIST.
I'm now writing a paper on image segmentation, and I found this lovely repository.

I have two questions.

  1. How did you measure 'accuracy' value, printed during training?
    Is it defined as (TP + TN) / TOTAL?

  2. How can I cite or mention your works if I use the codes?

Thanks for providing such a great works :)

bad predictions

I got very bad predictions like this(vgg_unet2,epochs=100)
Uploading 0016E5_07959.png…
How can i improve it?

I don't want to use pretrained vgg weight

Hello.

What should I do if I don't want to use pretrained vgg weight?
I want to train all the weight from the beggining, because vgg based model could not catch the features of some CT data.

Getting a strange output from prediction

When I train the model on the dataset provided, I am getting a very strange result when I go about predicting the result (see below).

I used default input values for training using the pretrained vgg net weights to train fcn8 model...

0016e5_08011

KeyError: "Can't open attribute (Can't locate attribute: 'nb_layers')"

import h5py
import os
import numpy as np

def loadWeightsPartial( model , weights_path , n_layers ):
# f=h5py.File(weights_path,mode="r")
# k=f.attrs['layer_names']['block1_conv1']
# weigh
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):
# for k in range(f.attrs['layer_names']):
if k >= n_layers :
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()

When I call this function,it showed this error.
I don't know how to handle it,please!

OOM when allocating tensor with shape[7,7,512,4096].

I attempted to use tensorflow backend. I changed "channel first " to "channel last " and "outputHeight2 = o_shape2[2] outputWidth2 = o_shape2[3]" to "outputHeight2 = o_shape2[1] outputWidth2 = o_shape2[2]",however ,I got one error that is "OOM when allocating tensor with shape[7,7,512,4096]." even though I make train batch only one picture on model fcn8 and fcn32. But when run on model vggunet it worked. Is it because my graphics card memory is not big enough? It is 5G size.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.