Giter Club home page Giter Club logo

kiu-net-pytorch's Introduction

KiU-Net-pytorch PWC

Paper (Conference) | Paper (Journal Extension) | Project Page | Slides

Official Pytorch Code for the paper "KiU-Net: Towards Accurate Segmentation of Biomedical Images using Over-complete Representations" , presented at MICCAI 2020 and its

Journal Extension: "KiU-Net: Overcomplete Convolutional Architectures for Biomedical Image and Volumetric Segmentation" , IEEE Transactions on Medical Imaging (IEEE TMI)

About this repo:

This repo hosts the code for the following networks:

  1. KiU-Net 2D
  2. KiU-Net 3D
  3. Res-KiU-Net
  4. Dense-KiU-Net

It also has dataloaders organized for generic 2D image segmentation and 3D volumetric segmentation for BraTS, LiTS dataset; for easy benchmarking of medical image and volumetric segmentation algorithms.

Introduction

In a generic "encoder-decoder" architecture , the initial few blocks of the encoder learn low-level features of the data while the later blocks learn the high-level features. Eventually, the encoder learns to map the data to lower dimensionality (in the spatial sense). The increasing receptive field size over the depth of the network, constrains the network to focus more on the higher-level features. In our proposed architecture , we introduce Ki-Net where we use overcomplete representations which constraints the receptive field from increasing. This is done by a simple change in the architecture of encoder where max-pooling is replaced by up-sampling. This helps the filters in deep layers focus more on the low-level details helping in fine segmentation. This network, when augmented with U-Net is termed as KiU-Net which results in significant improvements in the case of segmenting small anatomical landmarks and blurred noisy boundaries while obtaining better overall performance. More details can be found in the paper.

Prerequisites:

  • Python 3.6
  • Pytorch 1.4

Pytorch Installation

Links for downloading the public Datasets:

  1. GLAS Dataset - Link (Original) | Link (Resized)
  2. RITE Dataset - Link (Original) | Link (Resized)
  3. BraTS Dataset - Link
  4. LiTS Dataset - Link
  5. Unfortunately, Brain Anatomy US dataset introduced in the paper cannot be made public because of pending IRB approval.

Using the Code for your dataset

  • Clone this repository:
git clone https://github.com/jeya-maria-jose/KiU-Net-pytorch
cd KiU-Net-pytorch

Dataset Preparation

Prepare the dataset in the following format for easy use of the code. The train and test folders should contain two subfolders each: img and label. Make sure the images their corresponding segmentation masks are placed under these folders and have the same name for easy correspondance. Please change the data loaders to your need if you prefer not preparing the dataset in this format.

Train Folder-----
      img----
          0001.png
          0002.png
          .......
      label---
          0001.png
          0002.png
          .......
Validation Folder-----
      img----
          0001.png
          0002.png
          .......
      label---
          0001.png
          0002.png
          .......
Test Folder-----
      img----
          0001.png
          0002.png
          .......
      label---
          0001.png
          0002.png
          .......
  • This code is written for binary segmentation of an grayscale image. For using RGB images, just change the number of channels in the first and last layers in kiunet class found in arch/ae.py . For using KiU-Net for segmentation of more than one class, change the number of channels in the softmax layer in kiunet class found in arch/ae.py . The code is written for images of dimenstions 128x128. Either resize your images to the exact dimension for using the code directly or make changes in the ratios in interpolation functions accordingly in case of using dimensions which are not powers of 2.

  • The ground truth images should have pixels corresponding to the labels. Example: In case of binary segmentation, the pixels in the GT should be 0 or 1.

Training Command:

python train.py --train_dataset "enter train directory" --val_dataset "enter validation directory" --direc 'path for results to be saved' --batch_size 1 --epoch 400 --save_freq 10 --modelname "kiunet" --learning_rate 0.0001
Change modelname to kiunet3d, reskiunet or densekiunet to train those architectures

Testing Command:

python test.py --loaddirec "./saved_model_path/model_name.pth" --val_dataset "test dataset directory" --direc 'path for results to be saved' --batch_size 1 --modelname "kiunet"

The results including predicted segmentations maps will be placed in the results folder along with the model weights. Run the performance metrics code in MATLAB for calculating DICE Coefficient and Jaccard Index.

KiU-Net 3D:

The model file of KiU-Net 3D can be found at arch/ae.py .

Using the Code for BraTS Dataset

Check out readme in "BRATS/" folder for details on training KiU-Net 3D for BraTS dataset.

Using the Code for LiTS Dataset

Check out readme in "LiTS/" folder for details on training KiU-Net 3D for LiTS dataset.

Acknowledgement:

The dataloader code is inspired from pytorch-UNet , BraTS 2018 dataloader and LiTS dataloader.

Citation:

@inproceedings{valanarasu2020kiu,
  title={KiU-Net: Towards Accurate Segmentation of Biomedical Images Using Over-Complete Representations},
  author={Valanarasu, Jeya Maria Jose and Sindagi, Vishwanath A and Hacihaliloglu, Ilker and Patel, Vishal M},
  booktitle={Medical Image Computing and Computer Assisted Intervention--MICCAI 2020: 23rd International Conference, Lima, Peru, October 4--8, 2020, Proceedings, Part IV 23},
  pages={363--373},
  year={2020},
  organization={Springer}
}
@article{valanarasu2021kiu,
  title={Kiu-net: Overcomplete convolutional architectures for biomedical image and volumetric segmentation},
  author={Valanarasu, Jeya Maria Jose and Sindagi, Vishwanath A and Hacihaliloglu, Ilker and Patel, Vishal M},
  journal={IEEE Transactions on Medical Imaging},
  year={2021},
  publisher={IEEE}
}

Open an issue in case of any queries or mail me directly.

kiu-net-pytorch's People

Contributors

jeya-maria-jose 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

kiu-net-pytorch's Issues

Own dataset

Hi,

I would like to use my own dataset and train the model, but I don know how to create it. Should I label my 3D images? If so, how can I do it? Is there any software like labelme for 2D images?

Sorry for the inconveniences

cudnn_status_mapping error

when I run your code with my own data. I encounter this problem"CUDNN_STATUS_MAPPING_ERROR",my pytorch version is 1.5,cuda 10.2. not the same as yours.

The model predicts pixels as opposed to true pixels

Hello,I have a strange question. When I use the trained model for segmentation, I can split the liver and the background, but the split liver has 0 pixels and the background is 1. In the original test set, the liver has 1 pixel and the background is 0.

when i run GLAS dataset,something has happended,how could i solve it?

python3 train.py --train_dataset ./data/train --val_dataset ./data/val --direc ./output --batch_size 1 --epoch 400 --save_freq 10 --modelname "kiunet" --learning_rate 0.0001
Let's use 2 GPUs!
Total_params: 291243
/home/sc1/miniconda3/lib/python3.7/site-packages/torch/nn/functional.py:2479: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
"See the documentation of nn.Upsample for details.".format(mode))
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [927,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [415,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [799,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [287,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [671,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [159,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [543,0,0] Assertion t >= 0 && t < n_classes failed.
/pytorch/aten/src/THCUNN/SpatialClassNLLCriterion.cu:103: void cunn_SpatialClassNLLCriterion_updateOutput_kernel(T *, T *, T *, long *, T *, int, int, int, int, int, long) [with T = float, AccumT = float]: block: [0,0,0], thread: [31,0,0] Assertion t >= 0 && t < n_classes failed.
Traceback (most recent call last):
File "train.py", line 230, in
loss.backward()
File "/home/sc1/miniconda3/lib/python3.7/site-packages/torch/tensor.py", line 118, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/home/sc1/miniconda3/lib/python3.7/site-packages/torch/autograd/init.py", line 93, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED

About the edgeloss and softmax in KiUnet

Hi,
Thanks for your great work.
You used a threshold value of 0.5 in the code to handle the probability of the output, and whether such an operation can be derivable. In addition, there is processing of softmax in kiunet, the operations of these two parts makes me confused, can you explain it?
Best regards!

Issue with loss function.

Hi,

I think there is an issue with the loss function implementation:

  1. In line https://github.com/jeya-maria-jose/KiU-Net-pytorch/blob/master/arch/ae.py#L242, a softmax operation is in place.
  2. According to the loss function implementation, you used cross-entropy loss which requires raw outputs (no log or softmax).

I think this loss function implementation is not correct. You may get the results as with logsoftmax of the outputs, still, the loss function can do a descent job.

Please investigate.

Resized Glas dataset match Dice 0.71?

When I train and test with the resized Glas dataset,I got the dice result 0.71. Howerver,in author's paper the real result should be 0.83. = = Could some one to tell me how to solve this trouble ?
Perhaps my train model wasn't the best,so can you give me a better one ?

The structure of the 3D KiUNet model is incorrect

There are some problems with your 3D kiunet. I compared the code with your paper and found that the model details do not correspond. Maybe you should explain these questions. E.g: out = F.pad(out,[0,0,0,0,0,1]), scale_factor=(0.5,0.25,0.25),(0.5,0.0625,0.0625), (2,0.25,0.25) and so on.
I hope you can reply or update the correct code, Thanks

High memory consumption

Hi, thanks for the great work you've done. I am trying to use either kiunet or reskiunet models with images of size 512512, but it doesn't work with such size on VRAM=40gb with batch size =1. It works on smaller images (256256, batchsize = 3). It also takes too long to train it... I find it a little bit weird because the model doesn't have too mane parameters (only 300k), I trained much larger models faster (e.g. 3*10^6 parameters), so I am wondering if it is the expected behavior or maybe on new pytorch(i am using 1.7) it isn't efficient? Maybe you know some ways how I can improve the memory consumption and speed?

About the environment

I am having the following problem,can someone give some help?thanks!
image

Here is my CUDA version and cuDNN version:
image

Please help me!

when I run test command:python test.py --loaddirec "./resultmodel.pth" --val_dataset "./dataset/test" --direc './special' --batch_size 1 --modelname "kiunet"
an error occurred:
Traceback (most recent call last):
File "test.py", line 127, in
model.load_state_dict(torch.load(loaddirec))
File "/home/zhang/anaconda3/envs/KiU/lib/python3.6/site-packages/torch/nn/modules/module.py", line 830, in load_state_dict
self.class.name, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for kiunet:
Missing key(s) in state_dict: "encoder1.weight", "encoder1.bias", "en1_bn.weight", "en1_bn.bias", "en1_bn.running_mean", "en1_bn.running_var", "encoder2.weight", "encoder2.bias", "en2_bn.weight", "en2_bn.bias", "en2_bn.running_mean", "en2_bn.running_var", "encoder3.weight", "encoder3.bias", "en3_bn.weight", "en3_bn.bias", "en3_bn.running_mean", "en3_bn.running_var", "decoder1.weight", "decoder1.bias", "de1_bn.we
.......

Can you help me solved this problem?

About the preprocessing data?

Hi!
Follow the code, the train data should be png. I want to run the 2D model.
But I could not find the code to convert nii to png.
The preprocess workflow: First, run LiTs/data_prepare/get_training_set.py; second, convert nii to png. Is it right?
And could you share the code(second step) with me?
Thank you!
QQ图片20210423150341

No module named 'multicrop'

(pytorch) caijinyue@titan:~/桌面/KiU-Net-pytorch-master/BRATS$ python train_kiunet.py
Traceback (most recent call last):
File "train_kiunet.py", line 19, in
from data import datasets
File "/home/caijinyue/桌面/KiU-Net-pytorch-master/BRATS/data/datasets.py", line 1, in
from .dualpath import DualData, DualData25, DualData28
File "/home/caijinyue/桌面/KiU-Net-pytorch-master/BRATS/data/dualpath.py", line 18, in
import multicrop
ModuleNotFoundError: No module named 'multicrop'

CUDA out of memory

Hi, when I trained the model with custom data, it always told that CUDA out of memory. I tried resize image to 608x608 and use bath size 1 but can't solve the problem. What's wrong?

Memmory usage of kiunet3d

Hi,
Could you please give some information about GPU memory is needed for kiunet3d? I couldn't train it on a GeForce gtx 1080 with 11 GB of memory.

Slow training

Hi, thanks for sharing your research with us. I implemented kiUNet-3D for my application, had to modify the architecture so that I can train the model. But the training is exceptionally slow. For an input of size 140 x 140 x 140 and dataset of 40 volumes, it takes more than 30 minutes per epoch. I have played with many other 3d architectures but I have observed KiUNet's training is quite slow. Could you suggest any way to speed-up the training, I have already reduced the model.

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.