U-Net: Convolutional Networks for Biomedical Image Segmentation

Overview

Deep Learning Tutorial for Kaggle Ultrasound Nerve Segmentation competition, using Keras

This tutorial shows how to use Keras library to build deep neural network for ultrasound image nerve segmentation. More info on this Kaggle competition can be found on https://www.kaggle.com/c/ultrasound-nerve-segmentation.

This deep neural network achieves ~0.57 score on the leaderboard based on test images, and can be a good staring point for further, more serious approaches.

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


Overview

Data

Provided data is processed by data.py script. This script just loads the images and saves them into NumPy binary format files .npy for faster loading later.

Pre-processing

The images are not pre-processed in any way, except resizing to 64 x 80. Since the images are pretty noisy, I expect that some thoughtful pre-processing could yield better performance of the model.

Output images (masks) are scaled to [0, 1] interval.

Model

The provided model is basically a convolutional auto-encoder, but with a twist - it has skip connections from encoder layers to decoder layers that are on the same "level". See picture below (note that image size and numbers of convolutional filters in this tutorial differs from the original U-Net architecture).

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 64 x 80 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 20 epochs, where each epoch took ~30 seconds on Titan X. Memory footprint of the model is ~800MB.

After 20 epochs, calculated Dice coefficient is ~0.68, which yielded ~0.57 score on leaderboard, so obviously this model overfits (cross-validation pull requests anyone? ;)).

Loss function for the training is basically just a negative of Dice coefficient (which is used as evaluation metric on the competition), and this is implemented as custom loss function using Keras backend - check dice_coef() and dice_coef_loss() functions in train.py for more detail. Also, for making the loss function smooth, a factor smooth = 1 factor is added.

The weights are updated by Adam optimizer, with a 1e-5 learning rate. During training, model's weights are saved in HDF5 format.


How to use

Dependencies

This tutorial depends on the following libraries:

  • cv2 (OpenCV)
  • Theano and/or Tensorflow
  • Keras >= 1.0

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

Prepare the data

In order to extract raw images and save them to .npy files, you should first prepare its structure. Make sure that raw dir is located in the root of this project. Also, the tree of raw dir must be like:

-raw
 |
 ---- train
 |    |
 |    ---- 1_1.tif
 |    |
 |    ---- …
 |
 ---- test
      |
      ---- 1.tif
      |
      ---- …
  • Now run python data.py.

Running this script will create train and test images and save them to .npy files.

Define the model

  • Check out get_unet() in train.py to modify the model, optimizer and loss function.

Train the model and generate masks for test images

  • Run python train.py to train the model.

Check out train_predict() to modify the number of iterations (epochs), batch size, etc.

After this script finishes, in imgs_mask_test.npy masks for corresponding images in imgs_test.npy should be generated. I suggest you examine these masks for getting further insight of your model's performance.

Generate submission

  • Run python submission.py to generate the submission file submission.csv for the generated masks.

Check out function submission() and run_length_enc() (thanks woshialex) for details.

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
  • How to generate hdf5 file?

    How to generate hdf5 file?

    Q1: How to generate the hdf5 file in order to save the parameters and models?

    print('-'*30)
    print('Loading saved weights...')
    print('-'*30)
    model.load_weights('unet.hdf5')
    

    I found this part. Don't know how to get this file.

    Q2: How to print out the result? After running the train.py, I got "imgs_mask_test.npy". Only shows me: 5508/5508 [==============================] - 271s
    How to print out the result, such as dice coefficient? Can I just add a line " print(dice_coef)" inside train_and_predict()?

    Hope to give me some advices. Thanks.

    opened by lowkeygit 4
  • A problem about loading data

    A problem about loading data

    I got an Error when I try to run data.py.

    When running the create_train_data() function, it returns: imgs = np.ndarray((total, 1, image_rows, image_cols), dtype=np.uint8) TypeError: 'float' object cannot be interpreted as an integer

    How to fix this problem?

    Thanks a lot.

    opened by lowkeygit 2
  • Error during training

    Error during training

    Hi

    I followed the steps.

    1. python data.py generated 4 files (imgs_train.npy, imgs_mask_train.npy, imgs_test.npy & imgs_id_test.npy)
    2. python train.py gave below error. ( train.py.tar.gz )
    Using TensorFlow backend.
    ------------------------------
    Loading and preprocessing train data...
    ------------------------------
    (array([ 1128086, 18525490, 48198109, 50544455, 31160281, 21835121,
           14112760,  6650318,  4792944,  5010836]), array([ -23.21516418,    7.21833038,   37.65182495,   68.08531952,
             98.51881409,  128.95230865,  159.38580322,  189.81929779,
            220.25279236,  250.68628693,  281.11978149]))
    (array([    23720, 199448869,     20222,     17016,     15333,     15023,
               18014,     33247,   2359486,      7470]), array([ -67.14089203,  -27.08612595,   12.96864014,   53.02340622,
             93.0781723 ,  133.13293839,  173.18770447,  213.24247055,
            253.29723663,  293.35200272,  333.4067688 ]))
    ------------------------------
    Creating and compiling model...
    ------------------------------
    train.py:67: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), padding="same", activation="relu")`
      conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs)
    train.py:68: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), padding="same", activation="relu")`
      conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv1)
    Traceback (most recent call last):
      File "train.py", line 210, in <module>
        train_and_predict()
      File "train.py", line 157, in train_and_predict
        model = get_unet()
      File "train.py", line 69, in get_unet
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
      File "/home/gopi/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 602, in __call__
        output = self.call(inputs, **kwargs)
      File "/home/gopi/anaconda2/lib/python2.7/site-packages/keras/layers/pooling.py", line 154, in call
        data_format=self.data_format)
      File "/home/gopi/anaconda2/lib/python2.7/site-packages/keras/layers/pooling.py", line 217, in _pooling_function
        pool_mode='max')
      File "/home/gopi/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 3386, in pool2d
        x = tf.nn.max_pool(x, pool_size, strides, padding=padding)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 1769, in max_pool
        name=name)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1605, in _max_pool
        data_format=data_format, name=name)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
        op_def=op_def)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
        set_shapes_for_outputs(ret)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
        shapes = shape_func(op)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
        return call_cpp_shape_fn(op, require_shape_fn=True)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
        debug_python_shape_fn, require_shape_fn)
      File "/home/gopi/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
        raise ValueError(err.message)
    ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_1/MaxPool' (op: 'MaxPool') with input shapes: [?,1,160,32].
    

    <<<<<<<<<<<<<<<<<<<<<<<<<

    opened by gopi77 2
  • I can not find  imgs_train.npy , where is it?

    I can not find imgs_train.npy , where is it?

    When I download this project, run "data.py", it warning said "No such file or directory: 'imgs_train.npy'", How can I obtain this file? Thank you very much

    opened by jianti 1
  • About dice loss

    About dice loss

    Hi I am using dice loss in my network for batch size 1. sometimes loss is crossing 1 as I am using just same type code but different terminology but basic is same (2. * intersection + smooth) / (K.sum(y_true_fy_true_f) + K.sum(y_pred_fy_pred_f) + smooth)

    But what is meaning of below code which I found in your network ,does it normalize and how to understand it. I mean if Dice loss top is 0.9 and bottom is 0.9 then sum is 1.8 and below equation will give -0.8 def dice_coef_loss(y_true, y_pred): return 1.-dice_coef(y_true, y_pred) Thanks

    opened by sagarhukkire 1
  • ValueError: Negative dimension size caused by subtracting 2 from 1

    ValueError: Negative dimension size caused by subtracting 2 from 1

    Hello,

    I'm not being able to train the network because of the following error, can anybody shed some light on how to fix it?

    Thanks!

    $ python train.py
    Using TensorFlow backend.
    ------------------------------
    Loading and preprocessing train data...
    ------------------------------
    ------------------------------
    Creating and compiling model...
    ------------------------------
    Traceback (most recent call last):
      File "train.py", line 209, in <module>
        train_and_predict()
      File "train.py", line 156, in train_and_predict
        model = get_unet()
      File "train.py", line 72, in get_unet
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
      File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 514, in __call__
        self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
      File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 572, in add_inbound_node
        Node.create_node(self, inbound_layers, node_indices, tensor_indices)
      File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 149, in create_node
        output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
      File "/usr/local/lib/python2.7/site-packages/keras/layers/pooling.py", line 162, in call
        dim_ordering=self.dim_ordering)
      File "/usr/local/lib/python2.7/site-packages/keras/layers/pooling.py", line 212, in _pooling_function
        border_mode, dim_ordering, pool_mode='max')
      File "/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1701, in pool2d
        x = tf.nn.max_pool(x, pool_size, strides, padding=padding)
      File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 850, in max_pool
        name=name)
      File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1440, in _max_pool
        data_format=data_format, name=name)
      File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
        op_def=op_def)
      File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2382, in create_op
        set_shapes_for_outputs(ret)
      File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1783, in set_shapes_for_outputs
        shapes = shape_func(op)
      File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 596, in call_cpp_shape_fn
        raise ValueError(err.message)
    ValueError: Negative dimension size caused by subtracting 2 from 1
    
    opened by waissbluth 0
  • Error while running data.py

    Error while running data.py

    Traceback (most recent call last): File "data.py", line 189, in detseg() File "data.py", line 79, in detseg imgs_train = np.load('imgs_train.npy') File "/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py", line 372, in load fid = open(file, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'imgs_train.npy'

    opened by alaeddine-abbagh 0
  • merge concat_axis=1

    merge concat_axis=1

    up6 = merge([Convolution2D(256, 2, 2,activation='relu', border_mode='same')(UpSampling2D(size=(2, 2))(conv5)), conv4], mode='concat', concat_axis=1)

    concat_axis=1,why not 0? the input size is [channels height width],so I think axis=1 corresponds to height but not channel. Please tell me why?

    opened by RainLKP 0
  • when running data.py,a problem occured.

    when running data.py,a problem occured.

    Creating training images...

    Done: 0/5635 images Done: 100/5635 images Traceback (most recent call last): File "data.py", line 186, in create_train_data() File "data.py", line 51, in create_train_data imgs_mask[i] = img_mask TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

    I have tryed 'int(total)',then this problem occured. "imgs = np.ndarray((int(total), 1, image_rows, image_cols), dtype=np.uint8) imgs_mask = np.ndarray((int(total), 1, image_rows, image_cols), dtype=np.uint8) "

    opened by zhangbanxian123 6
A PyTorch implementation for V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation

A PyTorch implementation of V-Net Vnet is a PyTorch implementation of the paper V-Net: Fully Convolutional Neural Networks for Volumetric Medical Imag

Matthew Macy 606 Dec 21, 2022
MVGCN: a novel multi-view graph convolutional network (MVGCN) framework for link prediction in biomedical bipartite networks.

MVGCN MVGCN: a novel multi-view graph convolutional network (MVGCN) framework for link prediction in biomedical bipartite networks. Developer: Fu Hait

null 13 Dec 1, 2022
Neural networks applied in recognizing guitar chords using python, AutoML.NET with C# and .NET Core

Chord Recognition Demo application The demo application is written in C# with .NETCore. As of July 9, 2020, the only version available is for windows

Andres Mauricio Rondon Patiño 24 Oct 22, 2022
Pre-trained model, code, and materials from the paper "Impact of Adversarial Examples on Deep Learning Models for Biomedical Image Segmentation" (MICCAI 2019).

Adaptive Segmentation Mask Attack This repository contains the implementation of the Adaptive Segmentation Mask Attack (ASMA), a targeted adversarial

Utku Ozbulak 53 Jul 4, 2022
Code for "Multi-Compound Transformer for Accurate Biomedical Image Segmentation"

News The code of MCTrans has been released. if you are interested in contributing to the standardization of the medical image analysis community, plea

null 97 Jan 5, 2023
Flexible-CLmser: Regularized Feedback Connections for Biomedical Image Segmentation

Flexible-CLmser: Regularized Feedback Connections for Biomedical Image Segmentation The skip connections in U-Net pass features from the levels of enc

Boheng Cao 1 Dec 29, 2021
This repository implements and evaluates convolutional networks on the Möbius strip as toy model instantiations of Coordinate Independent Convolutional Networks.

Orientation independent Möbius CNNs This repository implements and evaluates convolutional networks on the Möbius strip as toy model instantiations of

Maurice Weiler 59 Dec 9, 2022
U-2-Net: U Square Net - Modified for paired image training of style transfer

U2-Net: U Square Net Modified for paired image training of style transfer This is an unofficial repo making use of the code which was made available b

Doron Adler 43 Oct 3, 2022
An implementation of the research paper "Retina Blood Vessel Segmentation Using A U-Net Based Convolutional Neural Network"

Retina Blood Vessels Segmentation This is an implementation of the research paper "Retina Blood Vessel Segmentation Using A U-Net Based Convolutional

Srijarko Roy 23 Aug 20, 2022
Code for "FPS-Net: A convolutional fusion network for large-scale LiDAR point cloud segmentation".

FPS-Net Code for "FPS-Net: A convolutional fusion network for large-scale LiDAR point cloud segmentation", accepted by ISPRS journal of Photogrammetry

null 15 Nov 30, 2022
Code for our ICASSP 2021 paper: SA-Net: Shuffle Attention for Deep Convolutional Neural Networks

SA-Net: Shuffle Attention for Deep Convolutional Neural Networks (paper) By Qing-Long Zhang and Yu-Bin Yang [State Key Laboratory for Novel Software T

Qing-Long Zhang 199 Jan 8, 2023
U^2-Net - Portrait matting This repository explores possibilities of using the original u^2-net model for portrait matting.

U^2-Net - Portrait matting This repository explores possibilities of using the original u^2-net model for portrait matting.

Dennis Bappert 104 Nov 25, 2022
The Medical Detection Toolkit contains 2D + 3D implementations of prevalent object detectors such as Mask R-CNN, Retina Net, Retina U-Net, as well as a training and inference framework focused on dealing with medical images.

The Medical Detection Toolkit contains 2D + 3D implementations of prevalent object detectors such as Mask R-CNN, Retina Net, Retina U-Net, as well as a training and inference framework focused on dealing with medical images.

MIC-DKFZ 1.2k Jan 4, 2023
RGBD-Net - This repository contains a pytorch lightning implementation for the 3DV 2021 RGBD-Net paper.

[3DV 2021] We propose a new cascaded architecture for novel view synthesis, called RGBD-Net, which consists of two core components: a hierarchical depth regression network and a depth-aware generator network.

Phong Nguyen Ha 4 May 26, 2022
Realtime segmentation with ENet, the fast and accurate segmentation net.

Enet This is a realtime segmentation net with almost 22 fps on GTX1080 ti, and the model size is very small with only 28M. This repo contains the infe

JinTian 14 Aug 30, 2022
[NeurIPS2021] Code Release of K-Net: Towards Unified Image Segmentation

K-Net: Towards Unified Image Segmentation Introduction This is an official release of the paper K-Net:Towards Unified Image Segmentation. K-Net will a

Wenwei Zhang 423 Jan 2, 2023
Implementation of Uniformer, a simple attention and 3d convolutional net that achieved SOTA in a number of video classification tasks

Uniformer - Pytorch Implementation of Uniformer, a simple attention and 3d convolutional net that achieved SOTA in a number of video classification ta

Phil Wang 90 Nov 24, 2022
CBKH: The Cornell Biomedical Knowledge Hub

Cornell Biomedical Knowledge Hub (CBKH) CBKG integrates data from 18 publicly available biomedical databases. The current version of CBKG contains a t

null 44 Dec 21, 2022
Using pretrained language models for biomedical knowledge graph completion.

LMs for biomedical KG completion This repository contains code to run the experiments described in: Scientific Language Models for Biomedical Knowledg

Rahul Nadkarni 41 Nov 30, 2022