项目作者: baudcode

项目描述 :
Semantic Segmentation using Tensorflow on popular Datasets like Ade20k, Camvid, Coco, PascalVoc
高级语言: Python
项目地址: git://github.com/baudcode/tf-semantic-segmentation.git
创建时间: 2020-01-09T11:13:04Z
项目社区:https://github.com/baudcode/tf-semantic-segmentation

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

下载


TF Semantic Segmentation

Build Status
PyPI Status Badge
codecov
Open in Colab
Documentation Status

Quick Start

See GETTING_STARTED, or the Colab Notebook.

Learn more at our documentation.
See upcoming features on our roadmap.

Features

  • Distributed Training on Multiple GPUs
  • Hyper Parameter Optimization using WandB
  • WandB Integration
  • Easily create TFRecord from Directory
  • Tensorboard visualizations
  • Ensemble inference

Datasets

  • Ade20k
  • Camvid
  • Cityscapes
  • MappingChallenge
  • MotsChallenge
  • Coco
  • PascalVoc2012
  • Taco
  • Shapes (randomly creating triangles, rectangles and circles)
  • Toy (Overlaying TinyImageNet with MNIST)
  • ISIC2018
  • CVC-ClinicDB

Models

  • Unet
  • Erfnet
  • MultiResUnet
  • PSP (experimental)
  • FCN (experimental)
  • NestedUnet (Unet++) (experimental)
  • U2Net / U2NetP (experimental)
  • SatelliteUnet
  • MobilenetUnet (unet with mobilenet encoder pre-trained on imagenet)
  • InceptionResnetV2Unet (unet with inception-resnet v2 encoder pre-trained on imagenet)
  • ResnetUnet (unet with resnet50 encoder pre-trained on imagenet)
  • AttentionUnet

Losses

  • Catagorical Crossentropy
  • Binary Crossentropy
  • Crossentropy + SSIM
  • Dice
  • Crossentropy + Dice
  • Tversky
  • Focal
  • Focal + Tversky

Activations

  • mish
  • swish
  • relu6

Optimizers

  • Ranger
  • RAdam

Normalization

  • Instance Norm
  • Batch Norm

On the fly Augmentations

  • flip left/right
  • flip up/down
  • rot 180
  • color

Getting Started

Requirements

  1. sudo apt-get install libsm6 libxext6 libxrender-dev libyaml-dev libpython3-dev

Tensorflow (2.x) & Tensorflow Addons (optional)

  1. pip install tensorflow-gpu==2.4.0 --upgrade
  2. pip install tensorflow-addons==0.12.0 --upgrade

Installation

  1. pip install tf-semantic-segmentation

Run tensorboard

  • Hint: To see train/test/val images you have to start tensorboard like this
  1. tensorboard --logdir=logs/ --reload_multifile=true

Train on inbuild datasets (generator)

  1. python -m tf_semantic_segmentation.bin.train -ds 'tacobinary' -bs 8 -e 100 \
  2. -logdir 'logs/taco-binary-test' -o 'adam' -lr 5e-3 --size 256,256 \
  3. -l 'binary_crossentropy' -fa 'sigmoid' \
  4. --train_on_generator --gpus='0' \
  5. --tensorboard_train_images --tensorboard_val_images

Create a tfrecord from a dataset

  1. # create a tfrecord from the toy dataset and resize to 128x128
  2. tf-semantic-segmentation-tfrecord-writer -d 'toy' -c /hdd/datasets/ -s '128,128'

Train using a fixed record path

  1. python -m tf_semantic_segmentation.bin.train --record_dir=records/cityscapes-512x256-rgb/ \
  2. -bs 4 -e 100 -logdir 'logs/cityscapes-bs8-e100-512x256' -o 'adam' -lr 1e-4 -l 'categorical_crossentropy' \
  3. -fa 'softmax' -bufsize 50 --metrics='iou_score,f1_score' -m 'erfnet' --gpus='0' -a 'mish' \
  4. --tensorboard_train_images --tensorboard_val_images

Multi GPU training

  1. python -m tf_semantic_segmentation.bin.train --record_dir=records/cityscapes-512x256-rgb/ \
  2. -bs 4 -e 100 -logdir 'logs/cityscapes-bs8-e100-512x256' -o 'adam' -lr 1e-4 -l 'categorical_crossentropy' \
  3. -fa 'softmax' -bufsize 50 --metrics='iou_score,f1_score' -m 'erfnet' --gpus='0,1,2,3' -a 'mish'

Using Code

  1. from tf_semantic_segmentation.bin.train import train_test_model, get_args
  2. # get the default args
  3. args = get_args({})
  4. # change some parameters
  5. # !rm -r logs/
  6. args.model = 'erfnet'
  7. # args['color_mode'] = 0
  8. args.batch_size = 8
  9. args.size = [128, 128] # resize input dataset to this size
  10. args.epochs = 10
  11. args.learning_rate = 1e-4
  12. args.optimizer = 'adam' # ['adam', 'radam', 'ranger']
  13. args.loss = 'dice'
  14. args.logdir = 'logs'
  15. args.record_dir = "datasets/shapes/records"
  16. args.final_activation = 'softmax'
  17. # train and test
  18. results, model = train_test_model(args)

Models

  • Erfnet
  • Unet
  1. from tf_semantic_segmentation import models
  2. # print all available models
  3. print(list(modes.models_by_name.keys()))
  4. # returns a model (without the final activation function)
  5. model = models.get_model_by_name('erfnet', {"input_shape": (128, 128, 3), "num_classes": 5})
  6. # call models directly
  7. model = models.erfnet(input_shape=(128, 128), num_classes=5)

Use your own dataset

  • Accepted file types are: jpg(jpeg) and png

If you already have a train/test/val split then use the following data structure:

  1. dataset/
  2. labels.txt
  3. test/
  4. images/
  5. masks/
  6. train/
  7. images/
  8. masks/
  9. val/
  10. images/
  11. masks/

or use

  1. dataset/
  2. labels.txt
  3. images/
  4. masks/

The labels.txt should contain a list of labels separated by newline [/n]. For instance it looks like this:

  1. background
  2. car
  3. pedestrian
  • To create a tfrecord using the original image size and color use the script like this:
  1. INPUT_DIR = ...
  2. tf-semantic-segmentation-tfrecord-writer -dir $INPUT_DIR -r $INPUT_DIR/records

There are the following addition arguments:

  • -s [—size] ‘$width,$height’ (f.e. “512,512”)
  • -rm [—resize_method] (‘resize’, ‘resize_with_pad’, ‘resize_with_crop_or_pad)
  • cm [—color_mode] (0=RGB, 1=GRAY, 2=NONE (default))

Datasets

  1. from tf_semantic_sementation.datasets import get_dataset by name, datasets_by_name, DataType, get_cache_dir
  2. # print availiable dataset names
  3. print(list(datasets_by_name.keys()))
  4. # get the binary (waste or not) dataset
  5. data_dir = '/hdd/data/'
  6. name = 'tacobinary'
  7. cache_dir = get_cache_dir(data_dir, name.lower())
  8. ds = get_dataset_by_name(name, cache_dir)
  9. # print labels and classes
  10. print(ds.labels)
  11. print(ds.num_classes)
  12. # print number of training examples
  13. print(ds.num_examples(DataType.TRAIN))
  14. # or simply print the summary
  15. ds.summary()

Debug datasets

  1. python -m tf_semantic_segmentation.debug.dataset_vis -d ade20k

TFRecords

This library simplicifies the process of creating a tfrecord dataset for faster training.

Write tfrecords:

  1. from tf_semantic_segmentation.datasets import TFWriter
  2. ds = ...
  3. writer = TFWriter(record_dir)
  4. writer.write(ds)
  5. writer.validate(ds)

or use simple with this script (will be save with size 128 x 128 (width x height)):

  1. tf-semantic-segmentation-tfrecord-writer -d 'toy' -c /hdd/datasets/ -s '128,128'

Analyse already written tfrecord (with mean)

  1. python -m tf_semantic_segmentation.bin.tfrecord_analyser -r records/ --mean

Docker

  1. docker build -t tf_semantic_segmentation -f docker/Dockerfile ./

or pull the latest release

  1. docker pull baudcode/tf_semantic_segmentation:latest

Prediction

  1. pip install matplotlib

Using Code

  1. from tensorflow.keras.models import load_model
  2. import numpy as np
  3. from tf_semantic_segmentation.processing import dataset
  4. from tf_semantic_segmentation.visualizations import show, masks
  5. model = load_model('logs/model-best.h5', compile=False)
  6. # model parameters
  7. size = tuple(model.input.shape[1:3])
  8. depth = model.input.shape[-1]
  9. color_mode = dataset.ColorMode.GRAY if depth == 1 else dataset.ColorMode.RGB
  10. # define an image
  11. image = np.zeros((256, 256, 3), np.uint8)
  12. # preprocessing
  13. image = image.astype(np.float32) / 255.
  14. image, _ = dataset.resize_and_change_color(image, None, size, color_mode, resize_method='resize')
  15. image_batch = np.expand_dims(image, axis=0)
  16. # predict (returns probabilities)
  17. p = model.predict(image_batch)
  18. # draw segmentation map
  19. num_classes = p.shape[-1] if p.shape[-1] > 1 else 2
  20. predictions_rgb = masks.get_colored_segmentation_mask(p, num_classes, images=image_batch, binary_threshold=0.5)
  21. # show images using matplotlib
  22. show.show_images([predictions_rgb[0], image_batch[0]])

Using scripts

  • On image
  1. python -m tf_semantic_segmentation.evaluation.predict -m model-best.h5 -i image.png
  • On TFRecord (data type ‘val’ is default)
  1. python -m tf_semantic_segmentation.evaluation.predict -m model-best.h5 -r records/camvid/
  • On TFRecord (with export to directory)
  1. python -m tf_semantic_segmentation.evaluation.predict -m model-best.h5 -r records/cubbinary/ -o out/ -rm 'resize_with_pad'
  • On Video
  1. python -m tf_semantic_segmentation.evaluation.predict -m model-best.h5 -v video.mp4
  • On Video (with export to out/p-video.mp4)
  1. python -m tf_semantic_segmentation.evaluation.predict -m model-best.h5 -v video.mp4 -o out/

Prediction using Tensorflow Model Server

  • Installation
  1. # install
  2. echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list && \
  3. curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -
  4. sudo apt-get update && apt-get install tensorflow-model-server
  • Start Model Server
  1. ### using a single model
  2. tensorflow_model_server --rest_api_port=8501 --model_base_path=/home/user/models/mymodel/saved_model
  3. ### or using an ensamble of multiple models
  4. # helper to write the ensamble config yaml file (models/ contains multiple logdirs/, logdir must contain the name 'unet')
  5. python -m tf_semantic_segmentation.bin.model_server_config_writer -d models/ -c 'unet'
  6. # start model server with written models.yaml
  7. tensorflow_model_server --model_config_file=models.yaml --rest_api_port=8501

Compare models and ensemnble

  1. python -m tf_semantic_segmentation.evaluation.compare_models -i logs/ -c 'taco' -data /hdd/datasets/ -d 'tacobinary'

Parameters:

  • -i (directory containing models)
  • -c (model name (directory name) must contain this value)
  • -data (data directory)
  • -d (dataset name)

Use —help to get more help

Using Code

  1. from tf_semantic_segmentation.serving import predict, predict_on_batch, ensamble_prediction, get_models_from_directory
  2. from tf_semantic_segmentation.processing.dataset import resize_and_change_color
  3. image = np.zeros((128, 128, 3))
  4. image_size = (256, 256)
  5. color_mode = 0 # 0=RGB, 1=GRAY
  6. resize_method = 'resize'
  7. scale_mask = False # only scale mask when model output is scaled using sigmoid activation
  8. num_classes = 3
  9. # preprocess image
  10. image = image.astype(np.float32) / 255.
  11. image, _ = resize_and_change_color(image, None, image_size, color_mode, resize_method='resize')
  12. # prediction on 1 image
  13. p = predict(image.numpy(), host='localhost', port=8501, input_name='input_1', model_name='0')
  14. #############################################################################################################
  15. # if the image size should not match, the color mode does not match or the model_name does not match
  16. # you'll most likely get a `400 Client Error: Bad Request for url: http://localhost:8501/v1/models/0:predict`
  17. # hint: if you only started 1 model try using model_name 'default'
  18. #############################################################################################################
  19. # prediction on batch (for faster prediction of multiple images)
  20. p = predict_on_batch([image], host='localhost', port=8501, input_name='input_1', model_name='0')
  21. # ensamble prediction (average the predictions of multiple models)
  22. # either specify models like this:
  23. models = [
  24. {
  25. "name": "0",
  26. "path": "/home/user/models/mymodel/saved_model/",
  27. "version": 0, # optional
  28. "input_name": "input_1"
  29. },
  30. {
  31. "name": "1",
  32. "path": "/home/user/models/mymodel2/saved_model/",
  33. "input_name": "input_1"
  34. }
  35. ]
  36. # or load from models in directory (models/) that contain the name 'unet'
  37. models = get_models_from_directory('models/', contains='unet')
  38. # returns the ensamble and all predictions made
  39. ensamble, predictions = ensamble_prediction(models, image.numpy(), host='localhost', port=8501)

TFLite support

Convert the model

  1. python -m tf_semantic_segmentation.bin.convert_tflite -i logs/mymodel/saved_model/0/ -o model.tflite

Test inference on the model

  1. python -m tf_semantic_segmentation.debug.tflite_test -m model.tflite -i Harris_Sparrow_0001_116398.jpg