# image-segmentation-keras
**Repository Path**: bees_huang/image-segmentation-keras
## Basic Information
- **Project Name**: image-segmentation-keras
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 2
- **Created**: 2020-03-26
- **Last Updated**: 2021-05-13
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 图像分割Keras:在Keras中实现Segnet、FCN、UNet、PSPNet等模型。
[](https://badge.fury.io/py/keras-segmentation)
[](https://pepy.tech/project/keras-segmentation)
[](https://travis-ci.org/divamgupta/image-segmentation-keras)
[](https://saythanks.io/to/divamgupta)
[](http://perso.crans.org/besson/LICENSE.html)
[](https://twitter.com/divamgupta)
各种深度图像分割模型在keras中的实现
链接到完整的博客文章和教程:https://divamgupta.com/image-segmentation/2019/06/06/deep-learning-semantic-segmentation-keras.html
## 我们的其他仓库
- [Attention based Language Translation in Keras](https://github.com/divamgupta/attention-translation-keras)
- [Ladder Network in Keras](https://github.com/divamgupta/ladder_network_keras) model achives 98% test accuracy on MNIST with just 100 labeled examples
### 贡献者
Divam Gupta : https://divamgupta.com [](https://twitter.com/divamgupta)
[Rounaq Jhunjhunu wala](https://github.com/rjalfa)
## 模型
支持以下型号:
| model_name | Base Model | Segmentation Model |
|------------------|-------------------|--------------------|
| fcn_8 | Vanilla CNN | FCN8 |
| fcn_32 | Vanilla CNN | FCN8 |
| fcn_8_vgg | VGG 16 | FCN8 |
| fcn_32_vgg | VGG 16 | FCN32 |
| fcn_8_resnet50 | Resnet-50 | FCN32 |
| fcn_32_resnet50 | Resnet-50 | FCN32 |
| fcn_8_mobilenet | MobileNet | FCN32 |
| fcn_32_mobilenet | MobileNet | FCN32 |
| pspnet | Vanilla CNN | PSPNet |
| vgg_pspnet | VGG 16 | PSPNet |
| resnet50_pspnet | Resnet-50 | PSPNet |
| unet_mini | Vanilla Mini CNN | U-Net |
| unet | Vanilla CNN | U-Net |
| vgg_unet | VGG 16 | U-Net |
| resnet50_unet | Resnet-50 | U-Net |
| mobilenet_unet | MobileNet | U-Net |
| segnet | Vanilla CNN | Segnet |
| vgg_segnet | VGG 16 | Segnet |
| resnet50_segnet | Resnet-50 | Segnet |
| mobilenet_segnet | MobileNet | Segnet |
提供的预训练模型的示例结果:
输入图像 | 输出分割图像
:-------------------------:|:-------------------------:
 | 
 | 
## 入门
### 先决条件
* Keras 2.0
* opencv for python
* Theano / Tensorflow / CNTK
```shell
apt-get install -y libsm6 libxext6 libxrender-dev
pip install opencv-python
```
### 安装
安装模块
```shell
pip install keras-segmentation
```
或
```shell
pip install git+https://github.com/divamgupta/image-segmentation-keras
```
### 或
```shell
git clone https://github.com/divamgupta/image-segmentation-keras
cd image-segmentation-keras
python setup.py install
```
## 预先培训的模型:
```python
from keras_segmentation.pretrained import pspnet_50_ADE_20K , pspnet_101_cityscapes, pspnet_101_voc12
model = pspnet_50_ADE_20K() # load the pretrained model trained on ADE20k dataset
model = pspnet_101_cityscapes() # load the pretrained model trained on Cityscapes dataset
model = pspnet_101_voc12() # load the pretrained model trained on Pascal VOC 2012 dataset
# load any of the 3 pretrained models
out = model.predict_segmentation(
inp="input_image.jpg",
out_fname="out.png"
)
```
###准备培训数据
你需要做两个文件夹
* Images Folder - 所有的训练图像
* Annotations Folder - 对于相应的地面真值分割图像
注释图像的文件名应与RGB图像的文件名相同。
相应RGB图像的注释图像大小应相同。
对于RGB图像中的每个像素,注释图像中该像素的类标签将是蓝色像素的值。
生成注释图像的示例代码:
```python
import cv2
import numpy as np
ann_img = np.zeros((30,30,3)).astype('uint8')
ann_img[ 3 , 4 ] = 1 # this would set the label of pixel 3,4 as 1
cv2.imwrite( "ann_1.png" ,ann_img )
```
仅对批注图像使用bmp或png格式。
## 下载准备好的样本数据集
Download and extract the following:
https://drive.google.com/file/d/0B0d9ZiqAgFkiOHR1NTJhWVJMNEU/view?usp=sharing
您将得到一个名为dataset1的文件夹/
## 使用python模块
可以在python脚本中导入keras_分段并使用API
```python
from keras_segmentation.models.unet import vgg_unet
model = vgg_unet(n_classes=51 , input_height=416, input_width=608 )
model.train(
train_images = "dataset1/images_prepped_train/",
train_annotations = "dataset1/annotations_prepped_train/",
checkpoints_path = "/tmp/vgg_unet_1" , epochs=5
)
out = model.predict_segmentation(
inp="dataset1/images_prepped_test/0016E5_07965.png",
out_fname="/tmp/out.png"
)
import matplotlib.pyplot as plt
plt.imshow(out)
# 评估模型
print(model.evaluate_segmentation( inp_images_dir="dataset1/images_prepped_test/" , annotations_dir="dataset1/annotations_prepped_test/" ) )
```
## 通过命令行使用
也可以仅使用命令行使用该工具
### 将准备好的数据可视化
您还可以将准备好的注释可视化,以验证准备好的数据。
```shell
python -m keras_segmentation verify_dataset \
--images_path="dataset1/images_prepped_train/" \
--segs_path="dataset1/annotations_prepped_train/" \
--n_classes=50
```
```shell
python -m keras_segmentation visualize_dataset \
--images_path="dataset1/images_prepped_train/" \
--segs_path="dataset1/annotations_prepped_train/" \
--n_classes=50
```
### 训练模型
要训练模型,请运行以下命令:
```shell
python -m keras_segmentation train \
--checkpoints_path="path_to_checkpoints" \
--train_images="dataset1/images_prepped_train/" \
--train_annotations="dataset1/annotations_prepped_train/" \
--val_images="dataset1/images_prepped_test/" \
--val_annotations="dataset1/annotations_prepped_test/" \
--n_classes=50 \
--input_height=320 \
--input_width=640 \
--model_name="vgg_unet"
```
从上表中选择型号名称
### 得到预测
得到训练模型的预测
```shell
python -m keras_segmentation predict \
--checkpoints_path="path_to_checkpoints" \
--input_path="dataset1/images_prepped_test/" \
--output_path="path_to_predictions"
```
### 模型评估
拿到评估分数
```shell
python -m keras_segmentation evaluate_model \
--checkpoints_path="path_to_checkpoints" \
--images_path="dataset1/images_prepped_test/" \
--segs_path="dataset1/annotations_prepped_test/"
```
## 分割模型微调
下面的示例演示如何使用10个类微调模型。
```python
from keras_segmentation.models.model_utils import transfer_weights
from keras_segmentation.pretrained import pspnet_50_ADE_20K
from keras_segmentation.models.pspnet import pspnet_50
pretrained_model = pspnet_50_ADE_20K()
new_model = pspnet_50( n_classes=51 )
transfer_weights( pretrained_model , new_model ) # 将重量从预先训练的模型转移到您的模型
new_model.train(
train_images = "dataset1/images_prepped_train/",
train_annotations = "dataset1/annotations_prepped_train/",
checkpoints_path = "/tmp/vgg_unet_1" , epochs=5
)
```
## 使用keras分段的项目
以下是一些正在使用我们的库的项目:
* https://github.com/SteliosTsop/QF-image-segmentation-keras [paper](https://arxiv.org/pdf/1908.02242.pdf)
* https://github.com/willembressers/bouquet_quality
* https://github.com/jqueguiner/image-segmentation
* https://github.com/theerawatramchuen/Keras_Segmentation
* https://github.com/neheller/labels18
* https://github.com/shsh-a/segmentation-over-web
* https://github.com/chenwe73/deep_active_learning_segmentation
* https://github.com/vigneshrajap/vision-based-navigation-agri-fields
* https://github.com/ronalddas/Pneumonia-Detection
* https://github.com/Aiwiscal/ECG_UNet
* https://github.com/TianzhongSong/Unet-for-Person-Segmentation
* https://github.com/Guyanqi/GMDNN
* https://github.com/kozemzak/prostate-lesion-segmentation
* https://github.com/lixiaoyu12138/fcn-date
* https://github.com/sagarbhokre/LyftChallenge
* https://github.com/TianzhongSong/Person-Segmentation-Keras
* https://github.com/divyanshpuri02/COCO_2018-Stuff-Segmentation-Challenge
* https://github.com/XiangbingJi/Stanford-cs230-final-project
* https://github.com/lsh1994/keras-segmentation
* https://github.com/SpirinEgor/mobile_semantic_segmentation
* https://github.com/LeadingIndiaAI/COCO-DATASET-STUFF-SEGMENTATION-CHALLENGE
* https://github.com/lidongyue12138/Image-Segmentation-by-Keras
* https://github.com/laoj2/segnet_crfasrnn
* https://github.com/rancheng/AirSimProjects
* https://github.com/RadiumScriptTang/cartoon_segmentation
* https://github.com/dquail/NerveSegmentation
* https://github.com/Bhomik/SemanticHumanMatting
* https://github.com/Symefa/FP-Biomedik-Breast-Cancer
* https://github.com/Alpha-Monocerotis/PDF_FigureTable_Extraction
* https://github.com/rusito-23/mobile_unet_segmentation
* https://github.com/Philliec459/ThinSection-image-segmentation-keras
如果您在公共项目中使用我们的代码,请在此处添加链接(通过发布问题或创建PR)