目录
Win10+Anaconda3安装
2. 彻底卸载Anaconda
3. 重新安装anaconda(3.5.2版本,python3.6)
解决pip下载慢问题
修改pip默认安装路径
安装labelme
Labelme标注数据
unexpected keyword argument 'width'.
缺少info.yaml文件
准备数据集
批量转换json为png
标签转为灰度图
转成TFRecord格式
Google Colab使用教程
What is Google Colab
使用Google Colab
运行DeepLabv3+ 测试指令
全部代码
训练DeepLabv3+
注意事项
修改训练文件
数据不平衡问题
Train指令
断点训练
可视化训练log
验证eval指令
测试vis指令
在PASCAL VOC 2012 test数据集上测试
Win10+Anaconda3安装
试了以下链接的方法,无用,最后卸载重装 https://blog.csdn.net/Hungry_Shell/article/details/93602257
参考官方链接https://docs.anaconda.com/anaconda/install/uninstall/
首先运行以下指令
conda install anaconda-clean
anaconda-clean --yes
再运行uninstall.exe文件进行卸载(或者控制面板里卸载anaconda)
进入C:\Users\xx\pip,新建文件pip.ini,并写入以下内容。(其实就是更改源)
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
再运行pip install,下载速度明显提高。
将anaconda安装在D盘(默认是C盘,不希望占用C盘空间)
虽然anaconda安装在D盘,但是用pip指令安装的包默认是放在C盘的,需要修改pip默认安装路径,以下转自CSDN博主「JustinXTT」的原创文章,链接https://blog.csdn.net/mukvintt/article/details/80908951?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
第一步:先查看自己的默认安装路径到底是在哪?列出全局的packages包的安装路径在哪?
1. 按键(win+R) ->打开cmd命令窗口。
python -m site
得到以下结果
sys.path = [
'C:\\Users\\Administrator',
'F:\\Anaconda\\python36.zip',
'F:\\Anaconda\\DLLs',
'F:\\Anaconda\\lib',
'F:\\Anaconda',
'F:\\Anaconda\\lib\\site-packages',
'F:\\Anaconda\\lib\\site-packages\\win32',
'F:\\Anaconda\\lib\\site-packages\\win32\\lib',
'F:\\Anaconda\\lib\\site-packages\\Pythonwin',
]
USER_BASE: 'C:\\Users\\Administrator\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python36\\site-packages' (exists)
ENABLE_USER_SITE: True
①我们看见这里的USER_BASE 和USER_SITE其实就是用户自定义的启用Python脚本和依赖安装包的基础路径。
②其中USER_BASE表示就是在C盘这个目录下的Python.exe启动程序路径以及pip,esay-install,markdown等脚本,而这个C盘的Python.exe启动程序路径其实就是我们在安装Anaconda的时候一个分身,更准确的说,其实就是简单的Python程序,并不是什么IDE这种级别的可以类似Eclipse这样去操作编译丰富的功能窗口,只是简单类似shell的一样的存在。
③其中的USER_SITE其实就是用户如果调用C盘路径下的python.exe中的脚本pip文件去下载,就会将site-package的默认安装到这个C盘路径下。
第二步:修改配置文件
cmd命令行输入指令 python -m site -help,出现
C:\Users\Administrator>python -m site -help
F:\Anaconda\lib\site.py [--user-base] [--user-site]
Without arguments print some useful information
With arguments print the value of USER_BASE and/or USER_SITE separated
by ';'.
Exit codes with --user-base or --user-site:
0 - user site directory is enabled
1 - user site directory is disabled by user
2 - uses site directory is disabled by super user
or for security reasons
>2 - unknown error
找到F:\Anaconda\lib\site.py,并修改USER_SITE(改成想要放置site-packages的路径)和USER_BASE(执行下载的pip等脚本路径)两行的路径:
# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
# set it to False to disable the feature or True to force the feature
ENABLE_USER_SITE = None
# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = "F:\Anaconda3\lib\site-packages"
USER_BASE = "F:\Anaconda3\Scripts"
第三步:保存修改即配置成功。
再运行pip指令,包则默认安装在F:\Anaconda3\lib\site-packages路径下
关于labelme的介绍与安装,参考博客https://blog.csdn.net/u014061630/article/details/88756644
conda create --name=labelme python=3.6
conda activate labelme
pip install pyqt5
pip install labelme
pip install -U pillow
labelme标注数据之后,标签文件是json,但最终我们需要的是单通道的图文件,所以需要进行转换,用labelme自带的工具。
conda activate labelme
labelme_json_to_dataset 1.json
报错:TypeError:rectangle() got an unexpected keyword argument 'width'.
解决办法:安装pillow(在前面labelme安装那里已经给出,但我当时装labelme的时候没有执行该指令,所以出错了),参考链接:https://github.com/wkentaro/labelme/issues/529
pip install -U pillow
再次执行labelme_json_to_dataset即可,会在1.json目录下生成1_json文件夹,里面应该包含五个文件,分别为img.png,label.png,label_names.txt,label_viz.png和info.yaml。然而我运行之后发现缺少yaml文件(真是感觉什么坑都踩着了),解决办法参考链接https://blog.csdn.net/winter616/article/details/104426111。其实是版本问题,该版本的labelme_json_to_dataset.py文件没有生成info.yaml的代码,进行修改即可。
进入labelme_json_to_dataset.py路径D:\Anaconda3\Lib\site-packages\labelme\cli,修改labelme_json_to_dataset.py文件
原来是:
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in label_names:
f.write(lbl_name + '\n')
logger.info('Saved to: {}'.format(out_dir))
修改为:
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in label_names:
f.write(lbl_name + '\n')
logger.warning('info.yaml is being replaced by label_names.txt')
info = dict(label_names=label_names)
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)
logger.info('Saved to: {}'.format(out_dir))
if __name__ == '__main__':
main()
并在开头引入yaml包:import yaml
保存修改之后再运行labelme_json_to_dataset.py,即可生成5个文件。
方法一(不推荐,麻烦)
参考博客:https://blog.csdn.net/heiheiya/article/details/88342597
原labelme_json_to_dataset.py只能转换一个json文件,批量转换代码如下,命名为batch_json_to_dataset.sh:
num=100 //num为待转换json数量
for ((i=1;i<=num;i++))
do
python json_to_dataset.py dataset//$i.json -o output//$i
done
//dataset//$i.json 表示dataset文件下的名为i.json的文件
//output//$i 表示在output文件下生成名为i的文件夹,文件夹名称与json文件名称一一对应的
windows系统下不能直接运行.sh文件,可以用Git来执行.sh文件。
下载Git,运行.sh文件
进入Git官网https://git-scm.com/,下载Windows版本,一路next安装即可。
进入batch_json_to_dataset.sh所在文件夹下,右键点击Git Bash Here(没有的话就是Git没装好),在窗口里输入以下指令即可。
sh batch_json_to_dataset.sh
方法二(推荐,快捷):
参考博客https://blog.csdn.net/malvas/article/details/90776327
在github上下载labelme压缩包:https://github.com/wkentaro/labelme.git
压缩包解压,进入目录labelme-master\examples\semantic_segmentation,里面有将标注数据转换成VOC数据集格式的完整示例(读readme文件),具体为:删除data_dataset_voc文件夹,修改labels.txt,改为自己的类别,注意要保留__ignore__、_background_类别,将data_annotated文件夹下内容替换为自己的图与标签文件(json),在该路径下运行以下指令即可:
python labelme2voc.py data_annotated data_dataset_voc --labels labels.txt
====================================================
# It generates:
# - data_dataset_voc/JPEGImages
# - data_dataset_voc/SegmentationClass
# - data_dataset_voc/SegmentationClassVisualization
deeplab使用单通道的标注图且类别的像素标记应该是0,1,2,3…n(n+1个类别,背景+n个目标)
下载deeplabv3+代码包:https://github.com/tensorflow/models.git (环境为tensorflow2.1.0)
进入models-master/research/deeplab/datasets目录下,运行以下指令:
python remove_gt_colormap.py --original_gt_folder="/你的路径/data_dataset_voc/SegmentationClassPNG" --output_dir="/你的路径/data_dataset_voc/SegmentationClassRaw"
之后会在data_dataset_voc下生成SegmentationClassRaw文件夹,存放转换好的单通道标签png,因为像素值较小,看起来是全黑的,可以将像素值乘以100再显示图片检查一下,可以发现其实是有区分的。
#include //头文件
#include
#include
#include
using namespace cv;
using namespace std;
int main()
{
Mat srcImage = imread("C:\\Users\\....\\3.png", 0), dstImage;
dstImage = srcImage.clone();
int rowNumber = dstImage.rows; //行数
int colNumber = dstImage.cols * dstImage.channels(); //列数*通道数=每一行元素的个数
for (int i = 0; i < rowNumber; i++)
{
uchar* data = dstImage.ptr(i); //获取第i行的首地址
for (int j = 0; j < colNumber; j++)
data[j]*=100;
}
imshow("图",dstImage);
waitKey();
return 0;
}
1.获取JPEGImages文件下的图片名称,并写入txt文件
# 文件名写入txt
import random
import glob
img_path = glob.glob('/你的路径/JPEGImages/*.jpg')
for each in img_path:
with open('/你的路径/all.txt','a')as f:
f.write(each[10:-4]+'\n')# 切片中的10数字需要根据*.jpg前路径的长度来定义
2.将txt文件内容随机分为三组txt文件
# 随机分配训练集测试集验证集
import random
with open('D:/all.txt','r')as f:
lines = f.readlines()
g = [i for i in range(1, 23)]# 23=文件总数+1
random.shuffle(g)
# 设置需要的文件数
train = g[:15] //分配15个给train
trainval = g[15:20] //分配5个给trainval
val = g[20:] //
for index, line in enumerate(lines,1):
if index in train:
with open('D:/train.txt','a')as trainf:
trainf.write(line)
elif index in trainval:
with open('D:/trainval.txt','a')as trainvalf:
trainvalf.write(line)
elif index in val:
with open('D:/val.txt','a')as valf:
valf.write(line)
利用build_voc2012_data.py
转换成tfrecord格式(记得修改路径)
mask是存放单通道标签的文件夹,index是存放三个txt的文件夹,'jpg'是输入图的格式,tfrecord是用来存放tfrecord文件的文件夹。数据目录如下:
# from /root/models/research/deeplab/datasets/
python ./build_voc2012_data.py \
--image_folder="/root/data/image" --semantic_segmentation_folder="/root/data/mask" --list_folder="/root/data/index" --image_format="jpg" --output_dir="/root/data/tfrecord"
然而,运行报错:
>> Converting image 5/15 shard 0
>> Converting image 9/15 shard 1Windows fatal exception: access violation
Current thread 0x00002f70 (most recent call first):
空指针问题,检查一下文件,发现有一个txt对应的mask文件缺失,调整后不再报错。
https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d(需要FQ)
https://zhuanlan.zhihu.com/p/54389036
谷歌Colab是一个免费的云服务,现在它支持免费的GPU!
注册Google Drive账号https://drive.google.com/drive/folders/1Agg0iENh6FQ53s_1vHqZf-r6uStbP9UN。在谷歌硬盘上创建Colab notebook文件夹(名称任意)。
文件内右击->More->Colaboratory
打开建立的.ipynb文件,右上角点连接->链接到托管代码执行程序,左上角点击修改->笔记本设置->硬件加速器选择GPU,保存设置。
检查是否在GPU环境下
import tensorflow as tf
tf.test.gpu_device_name()
==============================
'/device:GPU:0' //表示已在GPU环境下
查看GPU信息
!/opt/bin/nvidia-smi
access the drive
from google.colab import drive
drive.mount('/content/drive')
点开链接,将链接内的code输入框框中即可。通过下面的指令即可列出谷歌网盘所有的文件夹
!ls "/content/drive/My Drive/"
常用指令
以%开头的指令在IPython中称为魔术指令。
%magic //显示所有魔术指令的详细文档
%cd /content/drive/My drive/DeepLabv3+ //进入工作路径
%run p.py //执行p脚本
执行shell命令,前加!
!python p.py //执行p脚本
!ls //显示当前路径所有文件
!pip install matplotlib //pip安装包
%time !pip install matplotlib //pip安装,并显示运行时间
首先在github上下载DeepLabv3+的代码到本地,然后直接拖进Google网盘中上传即可。文件夹下有model-master文件夹和.ipynb文件。
主要难点在于配置环境,参考https://stackoverflow.com/questions/55718980/setting-up-deeplabv3-in-colab
方法一(无用):
%env PYTHONPATH=/content/drive/My Drive/DeepLabv3+/models-master/research/:/content/drive/My Drive/DeepLabv3+/models-master/research/slim
!echo $PYTHONPATH
==========================================================================
无用,仍会报错ModuleNotFoundError: No module named 'deeplab'
方法二(可行):
%cd /content/drive/My Drive/DeepLabv3+/models-master/research
import sys
sys.path.extend(['/content/drive/My Drive/DeepLabv3+/models-master/research/', '/content/drive/My Drive/DeepLabv3+/models-master/research/slim/'])
from google.colab import drive
drive.mount('/content/drive')
%tensorflow_version 1.x //切换成tensorflow 1.x版本,默认是2.x版本
//配置环境变量
%cd /content/drive/My Drive/DeepLabv3+/models-master/research //切换路径
import sys
sys.path.extend(['/content/drive/My Drive/DeepLabv3+/models-master/research/', '/content/drive/My Drive/DeepLabv3+/models-master/research/slim/'])
测试=====
%run deeplab/model_test.py
一开始测试指令用的 !python deeplab/model_test.py 但是一直报错!!
报错1:AttributeError: module 'tensorflow' has no attribute 'app'
解决办法: 因为tensorflow 2.x版本没有这个模块,要将import tensorflow as tf 改成import tensorflow..compat.v1 as tf,或者将tensorflow版本降为1.x版本.但是我的tensorflow版本已经是1.x版本了!还是做了以上修改,报了另一个错
报错2:module 'tensorflow' has no attribute 'contrib'
解决办法:搜了一圈,都说是tensorflow 2.x版本没有contrib,降级为1.x即可
emm 我的tensorflow版本是1.15.2,而且,单独运行from tensorflow.contrib import slim as contrib_slim也不报错!
小朋友,你是否有很多问号!!!! 折磨得我心力交瘁!!!
最终解决办法: 将指令!python deeplab/model_test.py改成%run deeplab/model_test.py即可运行成功!!!
- 标签必须是单通道图像,且像素值从0开始依次递推,0对应background,1对应类别1,以此类推,特别的,255代表ignore_label,即目标轮廓处的白边,PASCAL VOC数据集中有。
- 对于小数据集,Xception过大了,效果反而会不好,可以试试小模型如Mobilenet。
- 对于类别分布不均匀,如二分类,类别1很小,则需要修改类别对应权重。
- 使用预训练的所有权重,设置
initialize_last_layer=True
- 只使用网络的backbone,不包括ASPP和decoder,则设置
initialize_last_layer=False
和last_layers_contain_logits_only=False
- 使用所有的预训练权重,除了
logits
,因为如果是自己的数据集,对应的classes不同(这个我们前面已经设置不加载logits),可设置initialize_last_layer=False
和last_layers_contain_logits_only=True
- crop_size至少要大于321,遵循的公式是(crop_size-1)/4为整数)(因为有ASPP模块,如果图片过小,到feature map时没有扩张卷积的范围大了,所以要求一个最小值。)
- 当 fine_tune_batch_norm=True, batch size 应该 > 12 ( > 16 最好),否则可以用小batch size,但设fine_tune_batch_norm=False. (因为BN在大batch size下才有效)
- 当用模型 'mobilent_v2', 设atrous_rates = decoder_output_stride = None. output_stride=16
- 当用模型'xception_65' or 'resnet_v1', 设atrous_rates = [6, 12, 18] (output stride 16) and decoder_output_stride = 4.
或者 atrous_rates=[12,24,36] (output_stridde =8)
data_generator.py
/deeplab/datasets/data_generater.py,添加对自己数据集的描述。
网上一些博客是修改models-master\research\deeplab\deprecated文件夹下segmentation_dataset.py文件,但亲测不修改data_generator.py的话会报错。
//大概100行的样子,仿照着PACAL VOC写就行了
_ROAD_INFORMATION = DatasetDescriptor(
splits_to_sizes={
'train': 1464, //对应的是训练集数量
'trainval': 2913,
'val': 1449,
},
num_classes=2, //类别数+1(background)
ignore_label=255, //有的数据集标签上是由白边的,如voc,就是所谓的ignore_label
)
//大概110行的样子,添加数据集描述
_DATASETS_INFORMATION = {
'cityscapes': _CITYSCAPES_INFORMATION,
'pascal_voc_seg': _PASCAL_VOC_SEG_INFORMATION,
'ade20k': _ADE20K_INFORMATION,
'road':_ROAD_INFORMATION,
}
train_utils.py
修改models-master\research\deeplab\utils文件下的train_utils.py文件,设置exclude_list参数,作用是在使用预训练权重时候,不加载该logit层(因为对应的类别数量不同,所以不用该层):
//大概再210行的样子
# Variables that will not be restored.
exclude_list = ['global_step','logits']
if not initialize_last_layer:
exclude_list.extend(last_layers)
因为对于一张图,road像素点占比非常小,大部分是背景,所以要考虑样本均衡性的问题,最简单的处理方法就是赋予不同的权值。
参考https://github.com/tensorflow/models/issues/3730#issuecomment-387100419
ignore_weight = 0
label0_weight = 1 # 背景的权重
label1_weight = 20 # 目标的权重
not_ignore_mask = tf.to_float(tf.equal(scaled_labels, 0)) * label0_weight + \
tf.to_float(tf.equal(scaled_labels, 1)) * label1_weight + \
tf.to_float(tf.equal(scaled_labels, ignore_label)) * ignore_weight
train_utils.py中的add_softmax_cross_entropy_loss_for_each_scale()函数,loss_weight代表标签的权重,loss_weight为默认为float类型,值为1,即所有标签权重相同;为list类型时,如loss_weight=[0.1,0.9],则表示标签0对应的权重为0.1,标签1对应的权重为0.9。
loss_weight=model_options.label_weights, label_weights在deeplab\common.py中:
///大概145行左右, 定义
flags.DEFINE_multi_float(
'label_weights', None,
'A list of label weights, each element represents the weight for the label '
'of its index, for example, label_weights = [0.1, 0.5] means the weight '
'for label 0 is 0.1 and the weight for label 1 is 0.5. If set as None, all '
'the labels have the same weight 1.0.')
//252行左右, 赋值
label_weights = FLAGS.label_weights
if label_weights is None:
label_weights = 1.0
可以直接在定义中更改,即改其默认值,
///大概145行左右, 定义
flags.DEFINE_multi_float(
'label_weights', [0.1,0.9],
'A list of label weights, each element represents the weight for the label '
'of its index, for example, label_weights = [0.1, 0.5] means the weight '
'for label 0 is 0.1 and the weight for label 1 is 0.5. If set as None, all '
'the labels have the same weight 1.0.')
python train.py \
--logtostderr \
--dataset="road" 数据集名称(对应data_generator.py中的名字)
--train_split="train" \ 可以选择train/val/trainval 不同的数据集
--model_variant="xception_65" \ 选择的模型backbone如mobilenet_v2,resnet_v1等
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--train_crop_size="513,513" \
--train_batch_size=1 \
--training_number_of_steps=1000 \
--fine_tune_batch_norm=False \(由于batchsize小于12,将其改为false)
--initialize_last_layer=False
--last_layers_contain_logits_only=True
--tf_initial_checkpoint="datasets/dataset/deeplabv3_pascal_train_aug/model.ckpt" \ 预训练模型位置
--train_logdir="datasets/dataset/train" \ 训练过程模型文件存储位置
--dataset_dir="datasets/dataset/tfrecord" \ 数据集路径
--save_summaries_secs=250 \
--save_interval_secs=500
没有列出来的参数
--num_clones=1 显卡即GPU的数量
注意train_batch_size应该是num_clones的整数倍 batch_size more that 16 is better
--base_learning_rate
ues 0.007 when training on PASCAL, when fine-tuning on PASCAL, use 0.0001
对于eval_crop_size: 应该等于output_stride * k+1,k为整数。对于PASCAL图片最大维度为512,所以设size为513x513,对于Cityscapes图片最大维度为1024x2048,所以设其size为1025x2049。
如果出现某某flag被define twice的报错,那么restart kernel,然后重新运行即可。
将train_util.py中修改的exclude_list改回来,去掉logits
//大概再210行的样子
# Variables that will not be restored.
exclude_list = ['global_step']
if not initialize_last_layer:
exclude_list.extend(last_layers)
训练指令改为:
python train.py \
--logtostderr \
--dataset="road" \
--train_split="train" \
--model_variant="xception_65" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--train_crop_size="513,513" \
--train_batch_size=6 \
--training_number_of_steps=100000 \
--fine_tune_batch_norm=False \
--initialize_last_layer=True \
--tf_initial_checkpoint="datasets/roaddata/train/model.ckpt" \ 断点训练的model
--train_logdir="datasets/roaddata/train" \
--dataset_dir="datasets/roaddata/tfrecord" \
--save_summaries_secs=250 \
--save_interval_secs=500
可一边训练一边可视化训练的log
tensorboard --logdir train/ //可视化训练日志
tensorboard --logdir eval/ --port 6007 //可视化验证日志 此处另开了一个端口
tensorboard --logdir . //可视化训练+验证日志
模型存储问题、挂载问题、断点训练问题
tenorboard存储训练记录问题
python deeplab/eval.py \
--logtostderr \
--eval_split="val" \
--model_variant="xception_65" \
--dataset="road" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--eval_crop_size="height+1,weight+1" \
--checkpoint_dir="/XXXh/road/train" \
--eval_logdir="/XXX/road/eval" \
--dataset_dir="/XXX/road/tfrecord" \
--max_number_of_evaluations=1
--eval_crop_size参数容易出错
crop_width=self.crop_size[1], IndexError: list index out of range
解决方法:改成--eval_crop_size="513,513"
Invalid argument: padded_shape[0]=73 is not divisible by block_shape[0]=2
解决方法:将height, weight 改为自己数据的大小,而不是train_crop_size的大小,注意 --eval_crop_size="height+1,weight+1"
遇到的问题: 显示 Starting evaluation at 2020-04-13-13:35:35 ,之后就停止了,没有进行验证,why?也没有报错信息。
python deeplab/vis.py \
--logtostderr \
--vis_split="val" \
--model_variant="xception_65" \
--dataset="road" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--vis_crop_size="height+1,weight+1" \
--checkpoint_dir="/XXXh/road/train" \
--vis_logdir="/XXX/road/vis" \
--dataset_dir="/XXX/road/tfrecord" \
--colormap_type="pascal" \
--also_save_raw_predictions = true //除了保存彩色分割图外,也保存raw分割图
//将vic.py中的eval_scales改成[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],变为multi-scale testing,能够提高精度
//大概在69行
//flags.DEFINE_multi_float('eval_scales', [0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
'The scales to resize images for evaluation.')
//将vis.py中的add_flipped_images改为true,则加flipped image input,提高test精度
//大概在73行
//flags.DEFINE_bool('add_flipped_images', true,
'Add flipped images for evaluation or not.')
//当然这些可以直接在上面的指令中指定
//如果加入multi-scale之后,报错
'Tensor' object does not support item assignment ==> model.py中165行
//这是因为tensor分量是不能赋值的,用的代码可能是以前的版本,有错,将165行的prediction[]=XX 该长城outputs_to_predictions[]=XX 即可。在新版的代码中已经修改过来了。
在vis文件夹下生成测试图(两个文件,一个彩色,一个raw)。
1.注册官网账号,下载test数据集 (test数据集的标签是没有公开的)
2.在test上进行测试
给test数据集造一个标签集mask(将原图转为灰度图作为标签即可),这一步是为用官方代码能够生成tfrecord(需要有mask),再用vis来生成raw结果图(需要数据作为tfrecord格式传入),最终的raw结果图就是官网要求的格式。
//批量转灰度图
from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir):
try:
image_file = Image.open(jpgfile) # open colour image
image_file = image_file.convert('L') # convert image to black and white
image_file.save(os.path.join(outdir, os.path.basename(jpgfile)))
except Exception as e:
print(e)
for jpgfile in glob.glob("JPEGImages/*.png"):
# print(jpgfile)
convertjpg(jpgfile,"gray")
造mask用vis.py是为了偷懒,不用另写一个批量测试代码......
3.官网上传测试结果图进行评估
results/VOC20112/Segmentation/comp6_test_cls/XX.png (png即为raw测试图)
生成压缩包results.tar.gz
tar -czvf results.tar.gz results
上传之后等待测试结果即可。