unet for image segmentation

Overview

Implementation of deep learning framework -- Unet, using Keras

The architecture was inspired by U-Net: Convolutional Networks for Biomedical Image Segmentation.


Overview

Data

The original dataset is from isbi challenge, and I've downloaded it and done the pre-processing.

You can find it in folder data/membrane.

Data augmentation

The data for training contains 30 512*512 images, which are far not enough to feed a deep learning neural network. I use a module called ImageDataGenerator in keras.preprocessing.image to do data augmentation.

See dataPrepare.ipynb and data.py for detail.

Model

img/u-net-architecture.png

This deep neural network is implemented with Keras functional API, which makes it extremely easy to experiment with different interesting architectures.

Output from the network is a 512*512 which represents mask that should be learned. Sigmoid activation function makes sure that mask pixels are in [0, 1] range.

Training

The model is trained for 5 epochs.

After 5 epochs, calculated accuracy is about 0.97.

Loss function for the training is basically just a binary crossentropy.


How to use

Dependencies

This tutorial depends on the following libraries:

  • Tensorflow
  • Keras >= 1.0

Also, this code should be compatible with Python versions 2.7-3.5.

Run main.py

You will see the predicted results of test image in data/membrane/test

Or follow notebook trainUnet

Results

Use the trained model to do segmentation on test images, the result is statisfactory.

img/0test.png

img/0label.png

About Keras

Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.

Use Keras if you need a deep learning library that:

allows for easy and fast prototyping (through total modularity, minimalism, and extensibility). supports both convolutional networks and recurrent networks, as well as combinations of the two. supports arbitrary connectivity schemes (including multi-input and multi-output training). runs seamlessly on CPU and GPU. Read the documentation Keras.io

Keras is compatible with: Python 2.7-3.5.

Comments
  • Incorrect predictions

    Incorrect predictions

    After training the model (for a short time) I see an accuracy of 80%. However, when I test it, the result is as shown for all test images. Can someone help with this? Would be great if someone could share the pre-trained weights.

    image

    opened by sasinghi 8
  • UNet for non-square input images?

    UNet for non-square input images?

    When I train a model with square input size, all goes well. However, when I train a model with an input layer of, for example, 512x768, the training loss increases to up to 97% but the output is almost completely empty.

    Anybody succeeded using UNet with a non-square input size?

    opened by Attila94 5
  • error when checking target

    error when checking target

    i) in your model.py input_size (256,256,1) => (256,256,3) ii) trainGenerator function in your data.py image_color_mode ="grayscale"=>"rgb" iii) testGenerator function in your data.py
    comment out "img = np.reshape(img,img.shape+(1,)) if (not flag_multi_class) else img"

    Originally posted by @teamo429 in https://github.com/zhixuhao/unet/issues/59#issuecomment-439797032

    I followed the above steps but I get the following error: ValueError: Error when checking target: expected conv2d_24 to have shape (256, 256, 1) but got array with shape (256, 256, 3)

    opened by shardul2903 4
  • How to do color rgb segmentation with your trainGenerator ?

    How to do color rgb segmentation with your trainGenerator ?

    Hi, I'm trying to do a color segmentation (I have grayscale image, and their labeled image with colors).

    I changed mask_color_mode to "rgb", and num_class to 3 (because I have 3 differents colors) in data.py, and I changed input_size from 256,256,1 to 256,256,3 in model.py, but I still have errors..

    "expected input_1 to have shape (None,256,256,3) but got array with shape (2,256,256,1). I rune the debugger and, mask.shape gave me : (2,256,256,3) and omg.shape gave me : (2,256,256,1)

    Any idea ? Thanks in advance :)

    opened by arthurPrvst 3
  • Using pretrained weights

    Using pretrained weights

    I have trained my model The weights are now stored in unet_membrane.hdf5

    I want to use these previously-stored weights for testing it on images now.

    How to do it?

    opened by rt30 2
  • An issue about the generator.

    An issue about the generator.

    Hello, in the trainGenerator, you use one image_datagen and one mask_datagen, but they load data from different directory, how could you say ---the label of image 1 is actually the label of image 1?

    opened by alance123 2
  • Model is not compiling when i changed the input images resolution

    Model is not compiling when i changed the input images resolution

    Hi There! By default input size of image is 256x256. Now my images resolution is 1364x659 and when i compile the main file i get this error ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 164, 341, 256), (None, 164, 340, 256)]

    opened by danishnazir 2
  • Could you provide information on test accuracy ? [proposed label] question

    Could you provide information on test accuracy ? [proposed label] question

    You have achieved training accuracy of 97% in 5 epoch but calculation of test accuracy is not provided.Could you provide information on test accuracy ?

    opened by ajithvallabai 2
  • error when running U-Net_2D_ZeroCostDL4Mic_BioImageModelZoo_export

    error when running U-Net_2D_ZeroCostDL4Mic_BioImageModelZoo_export

    I am running the mentioned notebook. All is fine until step 5.2 when I get the following output: Model input size: 512x512 Number of dataset found in the folder: 30

    100% 30/30 [00:02<00:00, 13.98it/s]

    Running QC on: 8.png


    FileNotFoundError Traceback (most recent call last)

    in () 52 print('Running QC on: '+filename) 53 test_input = io.imread(os.path.join(Source_QC_folder, filename), as_gray=True) ---> 54 test_ground_truth_image = io.imread(os.path.join(Target_QC_folder, filename), as_gray=True) 55 56 (threshold_list, iou_scores_per_threshold) = getIoUvsThreshold(os.path.join(prediction_QC_folder, prediction_prefix+filename), os.path.join(Target_QC_folder, filename))

    6 frames

    /usr/local/lib/python3.7/dist-packages/imageio/core/request.py in _parse_uri(self, uri) 381 # Reading: check that the file exists (but is allowed a dir) 382 if not os.path.exists(fn): --> 383 raise FileNotFoundError("No such file: '%s'" % fn) 384 else: 385 # Writing: check that the directory to write to does exist

    FileNotFoundError: No such file: '/content/gdrive/MyDrive/Data/membrane/membrane-model/Quality Control/Prediction/8.png'

    opened by angcriswell 1
  • Error after transformed into ONNX

    Error after transformed into ONNX

    I have used the following code to transform the pytorch model into onnx and found that the output of onnx is different from the original pytorch model.

    unet_model = UNet(n_channels=3, n_classes=1, bilinear=True)
    
    model_weight = './checkpoints/CP_epoch100.pth'
    unet_model.load_state_dict(torch.load(model_weight))
    
    
    # Input to the model
    batch_size = 1
    
    x = torch.randn(batch_size, 3, 160, 128, requires_grad=True)
    
    torch_out = unet_model(x)
    
    # Export the model
    torch.onnx.export(unet_model,  # model being run
                      x,  # model input (or a tuple for multiple inputs)
                      "./onnx/points_unet.onnx",  # where to save the model (can be a file or file-like object)
                      export_params=True,  # store the tra-ined parameter weights inside the model file
                      opset_version=11,  # the ONNX version to export the model to
                      do_constant_folding=True,  # whether to execute constant folding for optimization
                      input_names=['input'],  # the model's input names
                      output_names=['output'],  # the model's output names
                      dynamic_axes={'input': {0: 'batch_size'},  # variable length axes
                                    'output': {0: 'batch_size'}})
    
    opened by lengyuner 1
  • Error: zero-size array to reduction operation maximum which has no identity

    Error: zero-size array to reduction operation maximum which has no identity

    I have a problem when run code in Colab. Can you guys please tell me what's happening ? Thanks!

    WARNING:tensorflow:From :11: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: Please use Model.fit, which supports generators. Found 0 images belonging to 1 classes. Found 0 images belonging to 1 classes.

    ValueError Traceback (most recent call last) in () 9 model = unet() 10 model_checkpoint = ModelCheckpoint('unet_membrane.hdf5', monitor='loss',verbose=1, save_best_only=True) ---> 11 model.fit_generator(myGene,steps_per_epoch=2000,epochs=5,callbacks=[model_checkpoint])

    10 frames <array_function internals> in amax(*args, **kwargs)

    /usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs) 88 return reduction(axis=axis, out=out, **passkwargs) 89 ---> 90 return ufunc.reduce(obj, axis, dtype, out, **passkwargs) 91 92

    ValueError: zero-size array to reduction operation maximum which has no identity

    opened by hduc-le 1
  • 预测结果为全灰图

    预测结果为全灰图

    image

    • 默认数据集,训练5轮
    • 预测发现全部是灰图,没有结果

    看loss比较大,loss: 0.5252 - accuracy: 0.7814,就是没有模型收敛。 修改loss,lr,Adam(lr = 1e-5),steps_per_epoch=2000, epochs=20,batch_size=32。 loss缓慢下降,正常收敛,预测正常 关键是batch_size和lr需要微调,多调整几次看loss有没有下降趋势,没有的话中断训练,继续修改

    2022-09-13 05:10:38.208563: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8201 2000/2000 [==============================] - ETA: 0s - loss: 0.2607 - accuracy: 0.8837
    Epoch 1: loss improved from inf to 0.26072, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 858s 424ms/step - loss: 0.2607 - accuracy: 0.8837 Epoch 2/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1718 - accuracy: 0.9249
    Epoch 2: loss improved from 0.26072 to 0.17182, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 849s 425ms/step - loss: 0.1718 - accuracy: 0.9249 Epoch 3/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1499 - accuracy: 0.9342
    Epoch 3: loss improved from 0.17182 to 0.14994, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 850s 425ms/step - loss: 0.1499 - accuracy: 0.9342 Epoch 4/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1355 - accuracy: 0.9406
    Epoch 4: loss improved from 0.14994 to 0.13549, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 851s 426ms/step - loss: 0.1355 - accuracy: 0.9406 Epoch 5/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1243 - accuracy: 0.9455
    Epoch 5: loss improved from 0.13549 to 0.12432, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 841s 421ms/step - loss: 0.1243 - accuracy: 0.9455 Epoch 6/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1154 - accuracy: 0.9495
    Epoch 6: loss improved from 0.12432 to 0.11543, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 841s 420ms/step - loss: 0.1154 - accuracy: 0.9495 Epoch 7/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1082 - accuracy: 0.9527
    Epoch 7: loss improved from 0.11543 to 0.10821, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 839s 419ms/step - loss: 0.1082 - accuracy: 0.9527 Epoch 8/20 2000/2000 [==============================] - ETA: 0s - loss: 0.1022 - accuracy: 0.9554
    Epoch 8: loss improved from 0.10821 to 0.10224, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 840s 420ms/step - loss: 0.1022 - accuracy: 0.9554 Epoch 9/20 2000/2000 [==============================] - ETA: 0s - loss: 0.0972 - accuracy: 0.9576
    Epoch 9: loss improved from 0.10224 to 0.09725, saving model to unet_membrane.hdf5 2000/2000 [==============================] - 843s 422ms/step - loss: 0.0972 - accuracy: 0.9576

    opened by simplew2011 0
  • Process finished with exit code -1073740791 (0xC0000409)

    Process finished with exit code -1073740791 (0xC0000409)

    2022-07-14 13:42:31.409488: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8400

    Process finished with exit code -1073740791 (0xC0000409)

    How can I fix this mistake?I would appreciate you very much!

    opened by 666tua 0
  • Does your code use the aug dataset?

    Does your code use the aug dataset?

    Thank you for your code, Please allows me ask, does your code use the aug dataset? We ran finishly the prepare.ipynb file. At this time, we had the agument data in the "aug" folder. And in your main.py, the link data to the train folder ( label and image) not aug folder. I though, your main.py only use the original training dataset. Is it right? thanks. image Thanks.

    opened by tphankr 0
  • Enhancement of unet model customized for CPU

    Enhancement of unet model customized for CPU

    committed change :1) Uptaded model.py and decreased the learning rate . I got better result and the accuracy increased to 0.7944 2) Fixed the keras dependency error .

    opened by MitraAbhik007 0
  • Customizing the Code for rectangular input size.

    Customizing the Code for rectangular input size.

    Hi,

    Can someone help me with code customization for input size 1920x1080 image sizes. Working on a passion project right now, this would be really helpful. I do not wish to pad dummy background on image to make it 1920X1920(not losing any data in conversion to square shape, also would heavily increase the load on the GPU). Can someone help me with the Code customization required for the same?

    Thanks a ton.

    opened by kjhanjee 0
Owner
zhixuhao
zhixuhao
Unet network with mean teacher for altrasound image segmentation

Unet network with mean teacher for altrasound image segmentation

null 5 Nov 21, 2022
Implementation of UNET architecture for Image Segmentation.

Semantic Segmentation using UNET This is the implementation of UNET on Carvana Image Masking Kaggle Challenge About the Dataset This dataset contains

Anushka agarwal 4 Dec 21, 2021
Using pytorch to implement unet network for liver image segmentation.

Using pytorch to implement unet network for liver image segmentation.

zxq 1 Dec 17, 2021
A unet implementation for Image semantic segmentation

Unet-pytorch a unet implementation for Image semantic segmentation 参考网上的Unet做分割的代码,做了一个针对kaggle地盐识别的,请去以下地址获取数据集: https://www.kaggle.com/c/tgs-salt-id

Rabbit 3 Jun 29, 2022
The open source code of SA-UNet: Spatial Attention U-Net for Retinal Vessel Segmentation.

SA-UNet: Spatial Attention U-Net for Retinal Vessel Segmentation(ICPR 2020) Overview This code is for the paper: Spatial Attention U-Net for Retinal V

Changlu Guo 151 Dec 28, 2022
Hippocampal segmentation using the UNet network for each axis

Hipposeg Hippocampal segmentation using the UNet network for each axis, inspired by https://github.com/MICLab-Unicamp/e2dhipseg Red: False Positive Gr

Juan Carlos Aguirre Arango 0 Sep 2, 2021
Unofficial implementation of Point-Unet: A Context-Aware Point-Based Neural Network for Volumetric Segmentation

Point-Unet This is an unofficial implementation of the MICCAI 2021 paper Point-Unet: A Context-Aware Point-Based Neural Network for Volumetric Segment

Namt0d 9 Dec 7, 2022
The undersampled DWI image using Slice-Interleaved Diffusion Encoding (SIDE) method can be reconstructed by the UNet network.

UNet-SIDE The undersampled DWI image using Slice-Interleaved Diffusion Encoding (SIDE) method can be reconstructed by the UNet network. For Super Reso

TIANTIAN XU 1 Jan 13, 2022
Implementation of Uformer, Attention-based Unet, in Pytorch

Uformer - Pytorch Implementation of Uformer, Attention-based Unet, in Pytorch. It will only offer the concat-cross-skip connection. This repository wi

Phil Wang 72 Dec 19, 2022
Implementation detail for paper "Multi-level colonoscopy malignant tissue detection with adversarial CAC-UNet"

Multi-level-colonoscopy-malignant-tissue-detection-with-adversarial-CAC-UNet Implementation detail for our paper "Multi-level colonoscopy malignant ti

CVSM Group -  email: czhu@bupt.edu.cn 84 Nov 22, 2022
MIMO-UNet - Official Pytorch Implementation

MIMO-UNet - Official Pytorch Implementation This repository provides the official PyTorch implementation of the following paper: Rethinking Coarse-to-

Sungjin Cho 248 Jan 2, 2023
Implementation of UNet on the Joey ML framework

Independent Research Project - Code Joey can be cloned from here https://github.com/devitocodes/joey/. Devito and other dependencies such as PyTorch a

Navjot Kukreja 1 Oct 21, 2021
Official Keras Implementation for UNet++ in IEEE Transactions on Medical Imaging and DLMIA 2018

UNet++: A Nested U-Net Architecture for Medical Image Segmentation UNet++ is a new general purpose image segmentation architecture for more accurate i

Zongwei Zhou 1.8k Jan 7, 2023
This is a file about Unet implemented in Pytorch

Unet this is an implemetion of Unet in Pytorch and it's architecture is as follows which is the same with paper of Unet component of Unet Convolution

Dragon 1 Dec 3, 2021
unet-family: Ultimate version

unet-family: Ultimate version 基于之前my-unet代码,我整理出来了这一份终极版本unet-family,方便其他人阅读。 相比于之前的my-unet代码,代码分类更加规范,有条理 对于clone下来的代码不需要修改各种复杂繁琐的路径问题,直接就可以运行。 并且代码有

null 2 Sep 19, 2022
3D2Unet: 3D Deformable Unet for Low-Light Video Enhancement (PRCV2021)

3DDUNET This is the code for 3D2Unet: 3D Deformable Unet for Low-Light Video Enhancement (PRCV2021) Conference Paper Link Dataset We use SMOID dataset

null 1 Jan 7, 2022
Direct application of DALLE-2 to video synthesis, using factored space-time Unet and Transformers

DALLE2 Video (wip) ** only to be built after DALLE2 image is done and replicated, and the importance of the prior network is validated ** Direct appli

Phil Wang 105 May 15, 2022
Practical Blind Denoising via Swin-Conv-UNet and Data Synthesis

Practical Blind Denoising via Swin-Conv-UNet and Data Synthesis [Paper] [Online Demo] The following results are obtained by our SCUNet with purely syn

Kai Zhang 312 Jan 7, 2023
Segmentation in Style: Unsupervised Semantic Image Segmentation with Stylegan and CLIP

Segmentation in Style: Unsupervised Semantic Image Segmentation with Stylegan and CLIP Abstract: We introduce a method that allows to automatically se

Daniil Pakhomov 134 Dec 19, 2022