Github:Semantic-Segmentation-Suite分割网络集锦--使用小结

最近在做matting的比赛,所以学了一些分割的内容,并且在师姐的推荐下找到了一个非常好的github仓库,里面囊括了绝大多数经典的分割网络的TensorFlow版本实现,而且坑不是很多,仓库地址:
https://github.com/GeorgeSeif/Semantic-Segmentation-Suite

好了今天跑的是deeplabv3,小结一下使用过程:
首先本地建一个仓库 使用命令:

git clone https://github.com/GeorgeSeif/Semantic-Segmentation-Suite.git

大约3个G的文件,因为里面有数据集会比较大,等待下载完毕后就是按照README中步骤:
1、环境准备:
numpy:sudo pip install numpy
OpenCV Python:sudo apt-get install python-opencvpip install opencv-python
TensorFlow: sudo pip install --upgrade tensorflow-gpu

2、代码仓库提供了数据集CamVid,如果需要训练自己的数据集就需要新建一个数据集文件,文件的目录形式如下:
├── “dataset_name”
| ├── train
| ├── train_labels
| ├── val
| ├── val_labels
| ├── test
| ├── test_labels
分别放好对应的训练、验证、测试集图像和label即可,格式jpg、png等都可以,但是要统一起来

3、在上述dataset文件下新建一个.csv文件,内容是语义分割目标的所有类别,和相应的用来表示的rgb值

name,r,g,b
Animal,64,128,64
Archway,192,0,128
Bicyclist,0,128, 192
Bridge,0, 128, 64
Building,128, 0, 0
Car,64, 0, 128
CartLuggagePram,64, 0, 192
Child,192, 128, 64
Column_Pole,192, 192, 128
Fence,64, 64, 128
LaneMkgsDriv,128, 0, 192
LaneMkgsNonDriv,192, 0, 64
Misc_Text,128, 128, 64
MotorcycleScooter,192, 0, 192
OtherMoving,128, 64, 64
ParkingBlock,64, 192, 128
Pedestrian,64, 64, 0
Road,128, 64, 128
RoadShoulder,128, 128, 192
Sidewalk,0, 0, 192
SignSymbol,192, 128, 128
Sky,128, 128, 128
SUVPickupTruck,64, 128,192
TrafficCone,0, 0, 64
TrafficLight,0, 64, 64
Train,192, 64, 128
Tree,128, 128, 0
Truck_Bus,192, 128, 192
Tunnel,64, 0, 64
VegetationMisc,192, 192, 0
Void,0, 0, 0
Wall,64, 192, 0

我们的数据集做人像分割所以是一个二分类问题修改class_dict.csv为如下即可:

name, r, g, b
Human, 255,255,255
void, 0,0,0

按照需要的类别自行设计

4、下面准备预训练模型
可用的前端网络Frontends有:MobileNetV2, ResNet50/101/152, and InceptionV4,在运行train.py时会根据参数设定,自动下载设定的Frontends,但是有个问题是网络原因下载很慢,偶尔连接断了的话程序会异常中断,可以在utils/get_pretrained_checkpoints.py中找自己需要模型的URL,自行下载。将下载好的模型放在models中。

5、下面是运行train.py的参数

[-h] [--num_epochs NUM_EPOCHS]
     [--checkpoint_step CHECKPOINT_STEP]
     [--validation_step VALIDATION_STEP] [--image IMAGE]
     [--continue_training CONTINUE_TRAINING] [--dataset DATASET]
     [--crop_height CROP_HEIGHT] [--crop_width CROP_WIDTH]
     [--batch_size BATCH_SIZE] [--num_val_images NUM_VAL_IMAGES]
     [--h_flip H_FLIP] [--v_flip V_FLIP] [--brightness BRIGHTNESS]
     [--rotation ROTATION] [--model MODEL] [--frontend FRONTEND]

optional arguments:
-h, --help show this help message and exit
–num_epochs NUM_EPOCHS
Number of epochs to train for
–checkpoint_step CHECKPOINT_STEP
How often to save checkpoints (epochs)
–validation_step VALIDATION_STEP
How often to perform validation (epochs)
–image IMAGE The image you want to predict on. Only valid in
“predict” mode.
–continue_training CONTINUE_TRAINING
Whether to continue training from a checkpoint
–dataset DATASET Dataset you are using.
–crop_height CROP_HEIGHT
Height of cropped input image to network
–crop_width CROP_WIDTH
Width of cropped input image to network
–batch_size BATCH_SIZE
Number of images in each batch
–num_val_images NUM_VAL_IMAGES
The number of images to used for validations
–h_flip H_FLIP Whether to randomly flip the image horizontally for
data augmentation
–v_flip V_FLIP Whether to randomly flip the image vertically for data
augmentation
–brightness BRIGHTNESS
Whether to randomly change the image brightness for
data augmentation. Specifies the max bightness change
as a factor between 0.0 and 1.0. For example, 0.1
represents a max brightness change of 10% (±).
–rotation ROTATION Whether to randomly rotate the image for data
augmentation. Specifies the max rotation angle in
degrees.
–model MODEL The model you are using. See model_builder.py for
supported models
–frontend FRONTEND The frontend you are using. See frontend_builder.py
for supported models

我的训练参数:
python train.py --num_epochs 30 --checkpoint_step 6 --validation_step 1 --dataset dataset --crop_width 512 --crop_height 512 --batch_size 1 --num_val_images 700 --h_flip 1 --v_flip 0 --model DeepLabV3 --frontend ResNet101
根据需要可自行更改

6、代码的一些坑,目前遇到的不多,这里小结一下:
1)、文件夹utils里存放utils.py,train.py中导入utils文件夹中的utils.py和helpers.py时用from utils import utils,helpers,这里注意一下,程序会不晓得到底是utils还是utils.py的;哪里报错改一下就好了
2)、另外遇到的一个问题是输入图像全部裁剪成512*512的(utils.py中的random_crop方法),当输入image的长或宽有一边小于512就会执行

Exception('Crop shape (%d, %d) exceeds image dimensions (%d, %d)!' % (crop_height, crop_width, image.shape[0], image.shape[1]))

所以在前面添加如果图像长或宽有小于512,将图像resize成512*512即可:

if (crop_width > image.shape[1]) or (crop_height > image.shape[0]):
    crop_size = (512,512)
    image = cv2.resize(image, crop_size)
    label = cv2.resize(label, crop_size)

3)、如果没有用命令sudo pip install --upgrade tensorflow-gpu更新gpu的TensorFlow版本的话,我的程序选择了cpu,非常慢,后面重新暗转后问题解决。

目前还在训练,deeplabv3出现了梯度爆炸
DeepLabV3的收敛速度较快,所以学习率尽量设置的小一些,默认的0.0001有点大了,设置为0.00001就ok了
现在的训练情况:
在这里插入图片描述
还需要注意我们训练自己数据集时,因为做的是matting任务,标签图像是灰度的,及0-255之间,分割任务的label是mask图即二值化的图像,所以matting的数据集在做分割任务训练时需要一个二值化处理的步骤,这也是之前梯度爆炸的一个最主要的原因。
贴几张BiseNet的训练图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个仓库真的非常棒,真心推荐,后续更新…

猜你喜欢

转载自blog.csdn.net/qq_34606546/article/details/86514306