项目作者: milesial

项目描述 :
PyTorch implementation of the U-Net for image semantic segmentation with high quality images
高级语言: Python
项目地址: git://github.com/milesial/Pytorch-UNet.git
创建时间: 2017-08-16T12:17:08Z
项目社区:https://github.com/milesial/Pytorch-UNet

开源协议:GNU General Public License v3.0

下载


U-Net: Semantic segmentation with PyTorch




input and output for a random image in the test dataset

Customized implementation of the U-Net in PyTorch for Kaggle’s Carvana Image Masking Challenge from high definition images.

Quick start

Without Docker

  1. Install CUDA

  2. Install PyTorch 1.13 or later

  3. Install dependencies

    1. pip install -r requirements.txt
  4. Download the data and run training:

    1. bash scripts/download_data.sh
    2. python train.py --amp

With Docker

  1. Install Docker 19.03 or later:
    1. curl https://get.docker.com | sh && sudo systemctl --now enable docker
  2. Install the NVIDIA container toolkit:
    1. distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
    2. && curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
    3. && curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
    4. sudo apt-get update
    5. sudo apt-get install -y nvidia-docker2
    6. sudo systemctl restart docker
  3. Download and run the image:

    1. sudo docker run --rm --shm-size=8g --ulimit memlock=-1 --gpus all -it milesial/unet
  4. Download the data and run training:

    1. bash scripts/download_data.sh
    2. python train.py --amp

Description

This model was trained from scratch with 5k images and scored a Dice coefficient of 0.988423 on over 100k test images.

It can be easily used for multiclass segmentation, portrait segmentation, medical segmentation, …

Usage

Note : Use Python 3.6 or newer

Docker

A docker image containing the code and the dependencies is available on DockerHub.
You can download and jump in the container with (docker >=19.03):

  1. docker run -it --rm --shm-size=8g --ulimit memlock=-1 --gpus all milesial/unet

Training

  1. > python train.py -h
  2. usage: train.py [-h] [--epochs E] [--batch-size B] [--learning-rate LR]
  3. [--load LOAD] [--scale SCALE] [--validation VAL] [--amp]
  4. Train the UNet on images and target masks
  5. optional arguments:
  6. -h, --help show this help message and exit
  7. --epochs E, -e E Number of epochs
  8. --batch-size B, -b B Batch size
  9. --learning-rate LR, -l LR
  10. Learning rate
  11. --load LOAD, -f LOAD Load model from a .pth file
  12. --scale SCALE, -s SCALE
  13. Downscaling factor of the images
  14. --validation VAL, -v VAL
  15. Percent of the data that is used as validation (0-100)
  16. --amp Use mixed precision

By default, the scale is 0.5, so if you wish to obtain better results (but use more memory), set it to 1.

Automatic mixed precision is also available with the --amp flag. Mixed precision allows the model to use less memory and to be faster on recent GPUs by using FP16 arithmetic. Enabling AMP is recommended.

Prediction

After training your model and saving it to MODEL.pth, you can easily test the output masks on your images via the CLI.

To predict a single image and save it:

python predict.py -i image.jpg -o output.jpg

To predict a multiple images and show them without saving them:

python predict.py -i image1.jpg image2.jpg --viz --no-save

  1. > python predict.py -h
  2. usage: predict.py [-h] [--model FILE] --input INPUT [INPUT ...]
  3. [--output INPUT [INPUT ...]] [--viz] [--no-save]
  4. [--mask-threshold MASK_THRESHOLD] [--scale SCALE]
  5. Predict masks from input images
  6. optional arguments:
  7. -h, --help show this help message and exit
  8. --model FILE, -m FILE
  9. Specify the file in which the model is stored
  10. --input INPUT [INPUT ...], -i INPUT [INPUT ...]
  11. Filenames of input images
  12. --output INPUT [INPUT ...], -o INPUT [INPUT ...]
  13. Filenames of output images
  14. --viz, -v Visualize the images as they are processed
  15. --no-save, -n Do not save the output masks
  16. --mask-threshold MASK_THRESHOLD, -t MASK_THRESHOLD
  17. Minimum probability value to consider a mask pixel white
  18. --scale SCALE, -s SCALE
  19. Scale factor for the input images

You can specify which model file to use with --model MODEL.pth.

Weights & Biases

The training progress can be visualized in real-time using Weights & Biases. Loss curves, validation curves, weights and gradient histograms, as well as predicted masks are logged to the platform.

When launching a training, a link will be printed in the console. Click on it to go to your dashboard. If you have an existing W&B account, you can link it
by setting the WANDB_API_KEY environment variable. If not, it will create an anonymous run which is automatically deleted after 7 days.

Pretrained model

A pretrained model is available for the Carvana dataset. It can also be loaded from torch.hub:

  1. net = torch.hub.load('milesial/Pytorch-UNet', 'unet_carvana', pretrained=True, scale=0.5)

Available scales are 0.5 and 1.0.

Data

The Carvana data is available on the Kaggle website.

You can also download it using the helper script:

  1. bash scripts/download_data.sh

The input images and target masks should be in the data/imgs and data/masks folders respectively (note that the imgs and masks folder should not contain any sub-folder or any other files, due to the greedy data-loader). For Carvana, images are RGB and masks are black and white.

You can use your own dataset as long as you make sure it is loaded properly in utils/data_loading.py.


Original paper by Olaf Ronneberger, Philipp Fischer, Thomas Brox:

U-Net: Convolutional Networks for Biomedical Image Segmentation

network architecture