fork本项目, 运行后, 可以看到全部代码及相关方见多文件
项目简介:
Image Cascade Network(ICNet)主要用于图像实时语义分割。相较于其它压缩计算的方法,ICNet即考虑了速度,也考虑了准确性。 ICNet的主要思想是将输入图像变换为不同的分辨率,然后用不同计算复杂度的子网络计算不同分辨率的输入,然后将结果合并。ICNet由三个子网络组成,计算复杂度高的网络处理低分辨率输入,计算复杂度低的网络处理分辨率高的网络,通过这种方式在高分辨率图像的准确性和低复杂度网络的效率之间获得平衡。
整个网络结构如下: 阅读本项目时可以参考一下链接来帮助理解
论文链接:ICNet for Real-Time Semantic Segmentation on High-Resolution Images
博客链接:ICNet for Real-Time Semantic Segmentation on High-Resolution Images
文件结构:
root/ 根目录
|--data/ 数据目录
|--|--iccv09Data/ 数据集
|--|--|--images/ 数据集中的图像
|--|--|--|--1.jpg
|--|--|--labels/ 数据集中的标签
|--|--|--|--1.regions.txt
|--|--train_list.txt
|--|--eval_list.txt
|--work/ 工作目录
|--|--utils.py 各种工具类
|--|--model.py 定义网络的文件
|--|--reader.py 数据读取
|--|--loss.py 定义模型的loss
|--|--DataPartition.py 数据预处理,生成train_list.txt和eval_list.txt文件
|--logs/ 存放log文件的目录
|--train model/ 训练过程中暂存模型参数的目录
|--infer model/ 固化后模型参数的目录
|--pretrained model/ model zoo中预训练模型参数
|--train.py 训练模型
|--eval.py 测试模型的性能
|--infer.py 使用训练好的模型进行分割
|--freeze.py 将train model中存储的模型固化并保存到infer model目录中
|--config/py 保存网络的各个参数,可以根据需要自己调整
In[1]
# 解压数据文件
!tar xf data/data9270/iccv09Data.tar -C data
数据格式:
|--data/ 数据目录
|--|--iccv09Data/ 数据集
|--|--|--images/ 数据集中的图像
|--|--|--|--1.jpg
|--|--|--labels/ 数据集中的标签
|--|--|--|--1.regions.txt
|--|--train_list.txt 每行代表一个样本,分别为图像和标签的路径,中间以空格隔开,例:path/to/image path/to/labels
|--|--eval_list.txt 格式与train_list.txt相同
In[2]
# 图像预处理,生成train_list.txt 和eval_list.txt
!cd work/ && python DataPartition.py
In[3]
# 下载预训练的ICNet模型,并保存在pretrained model目录下
!wget https://paddle-icnet-models.bj.bcebos.com/model_1000.tar.gz
!mkdir pretrained\ model
!tar xzf model_1000.tar.gz -C pretrained\ model
!rm model_1000.tar.gz
--2020-03-31 15:24:20-- https://paddle-icnet-models.bj.bcebos.com/model_1000.tar.gz Resolving paddle-icnet-models.bj.bcebos.com (paddle-icnet-models.bj.bcebos.com)... 182.61.200.195, 182.61.200.229 Connecting to paddle-icnet-models.bj.bcebos.com (paddle-icnet-models.bj.bcebos.com)|182.61.200.195|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 25005084 (24M) [application/x-gzip] Saving to: ‘model_1000.tar.gz’ model_1000.tar.gz 100%[===================>] 23.85M 42.9MB/s in 0.6s 2020-03-31 15:24:20 (42.9 MB/s) - ‘model_1000.tar.gz’ saved [25005084/25005084]
In[4]
# 模型训练,训练中具体需要用的超参数可以在config.py脚本中进行修改,这里为了展示方便
# 加载了paddle model zoo提供的预训练的参数,并且只训练了一个epoch
!python train.py
2020-03-31 15:24:31,687-INFO: train params:{'data_dir': 'data/iccv09Data/', 'batch_size': 10, 'use_gpu': False, 'train_model_dir': 'train model', 'infer_model_dir': 'infer model', 'pretrained_model_dir': 'pretrained model/model_1000', 'eval_file_path': 'data/eval_list.txt', 'continue_train': True, 'paddle_flag': True, 'num_classes': 8, 'weight_decay': 4e-05, 'base_lr': 0.001, 'num_epochs': 1, 'total_step': 4000, 'image_shape': (3, 240, 320), 'enable_ce': False, 'bn_momentum': 0.9997, 'dropout_keep_prop': 0.9, 'default_norm_type': 'gn', 'decode_channel': 48, 'encode_channel': 256, 'default_epsilon': 0.001, 'default_group_number': 32, 'depthwise_use_cudnn': False, 'is_train': True, 'data_augmentation_config': {'use_augmentation': False, 'min_resize': 0.8, 'max_resize': 4, 'crop_size': 240}} 2020-03-31 15:24:31,687 - train.py[line:94] - INFO: train params:{'data_dir': 'data/iccv09Data/', 'batch_size': 10, 'use_gpu': False, 'train_model_dir': 'train model', 'infer_model_dir': 'infer model', 'pretrained_model_dir': 'pretrained model/model_1000', 'eval_file_path': 'data/eval_list.txt', 'continue_train': True, 'paddle_flag': True, 'num_classes': 8, 'weight_decay': 4e-05, 'base_lr': 0.001, 'num_epochs': 1, 'total_step': 4000, 'image_shape': (3, 240, 320), 'enable_ce': False, 'bn_momentum': 0.9997, 'dropout_keep_prop': 0.9, 'default_norm_type': 'gn', 'decode_channel': 48, 'encode_channel': 256, 'default_epsilon': 0.001, 'default_group_number': 32, 'depthwise_use_cudnn': False, 'is_train': True, 'data_augmentation_config': {'use_augmentation': False, 'min_resize': 0.8, 'max_resize': 4, 'crop_size': 240}} 2020-03-31 15:24:31,792-INFO: ***************已读入预先训练的模型,并准备继续训练*************** 2020-03-31 15:24:31,792 - train.py[line:40] - INFO: ***************已读入预先训练的模型,并准备继续训练*************** 2020-03-31 15:24:31,792-INFO: ***************训练开始*************** 2020-03-31 15:24:31,792 - train.py[line:99] - INFO: ***************训练开始*************** 2020-03-31 15:25:02,903-INFO: epoch: 0 step: 100/400 loss:3.8958182334899902 loss_sub4:2.100172519683838 loss_sub24:2.630321979522705 loss_sub124:2.507661819458008 period:2.61 sec, 2020-03-31 15:25:02,903 - train.py[line:125] - INFO: epoch: 0 step: 100/400 loss:3.8958182334899902 loss_sub4:2.100172519683838 loss_sub24:2.630321979522705 loss_sub124:2.507661819458008 period:2.61 sec, 2020-03-31 15:25:32,771-INFO: epoch: 0 step: 200/400 loss:3.2978177070617676 loss_sub4:1.9303418397903442 loss_sub24:2.2254467010498047 loss_sub124:2.0987844467163086 period:2.51 sec, 2020-03-31 15:25:32,771 - train.py[line:125] - INFO: epoch: 0 step: 200/400 loss:3.2978177070617676 loss_sub4:1.9303418397903442 loss_sub24:2.2254467010498047 loss_sub124:2.0987844467163086 period:2.51 sec, 2020-03-31 15:26:02,700-INFO: epoch: 0 step: 300/400 loss:3.4040658473968506 loss_sub4:2.045391798019409 loss_sub24:2.357220411300659 loss_sub124:2.1339149475097656 period:2.47 sec, 2020-03-31 15:26:02,700 - train.py[line:125] - INFO: epoch: 0 step: 300/400 loss:3.4040658473968506 loss_sub4:2.045391798019409 loss_sub24:2.357220411300659 loss_sub124:2.1339149475097656 period:2.47 sec, 2020-03-31 15:26:32,632-INFO: epoch: 0 step: 400/400 loss:3.0009219646453857 loss_sub4:2.1474075317382812 loss_sub24:2.130000114440918 loss_sub124:1.8053367137908936 period:2.50 sec, 2020-03-31 15:26:32,632 - train.py[line:125] - INFO: epoch: 0 step: 400/400 loss:3.0009219646453857 loss_sub4:2.1474075317382812 loss_sub24:2.130000114440918 loss_sub124:1.8053367137908936 period:2.50 sec, 2020-03-31 15:26:39,906-INFO: 0's epoch_total_time: 107.08 sec && mean_loss: 3.516557527142902 2020-03-31 15:26:39,906 - train.py[line:130] - INFO: 0's epoch_total_time: 107.08 sec && mean_loss: 3.516557527142902 2020-03-31 15:26:39,906-INFO: 暂时存储第0个epoch的训练结果 2020-03-31 15:26:39,906 - train.py[line:133] - INFO: 暂时存储第0个epoch的训练结果 2020-03-31 15:26:40,134-INFO: ***************训练完成*************** 2020-03-31 15:26:40,134 - train.py[line:135] - INFO: ***************训练完成***************
In[5]
# 模型固化
!python freeze.py
In[6]
# 使用test数据集的一副图像进行模型推断,并将分割的结果保存在根目录下
# 展示了原始图像以及分割后的结果,实际应用中只使用result124作为最终的分割结果
# 由于本示例中仅仅微调了一个epoch,所以infer的分割结果不是很好
!python infer.py
%matplotlib inline
import matplotlib.pyplot as plt
img1 = plt.imread('origin.png')
img2 = plt.imread('result124.png')
img3 = plt.imread('result24.png')
img4 = plt.imread('result4.png')
fig = plt.figure()
ax = fig.add_subplot(221)
ax.imshow(img1)
ax.set_title('origin')
ax.axis('off')
ax = fig.add_subplot(222)
ax.imshow(img2)
ax.set_title('result124')
ax.axis('off')
ax = fig.add_subplot(223)
ax.imshow(img3)
ax.set_title('result24')
ax.axis('off')
ax = fig.add_subplot(224)
ax.imshow(img4)
ax.set_title('result4')
ax.axis('off')
plt.show()
In[7]
!python eval.py
2020-03-31 15:28:25,440-WARNING: Caution! paddle.fluid.memory_optimize() is deprecated and not maintained any more, since it is not stable! This API would not take any memory optimizations on your Program now, since we have provided default strategies for you. The newest and stable memory optimization strategies (they are all enabled by default) are as follows: 1. Garbage collection strategy, which is enabled by exporting environment variable FLAGS_eager_delete_tensor_gb=0 (0 is the default value). 2. Inplace strategy, which is enabled by setting build_strategy.enable_inplace=True (True is the default value) when using CompiledProgram or ParallelExecutor. eval done! total image number is 107; mean iou:0.17085047038030848
使用AI Studio一键上手实践项目吧:https://aistudio.baidu.com/aistudio/projectdetail/124368
来源:oschina
链接:https://my.oschina.net/u/4067628/blog/3227939