原文地址:DeepLab 使用 Cityscapes 数据集训练模型
OS: Ubuntu 16.04 LTS
CPU: Intel® Core™ i7-4790K
GPU: GeForce GTX 1080/PCIe/SSE2
Nvidia Driver Version: 384.130
RAM: 32 GB
Anaconda: 4.6.11
CUDA: 9.0
cuDNN: 7.3.1
python: 3.6.8
tensorflow-gpu: 1.13.1
本文操作路径基于 /home/ai
,使用 ~/
代替
清华大学 TUNA 镜像站 - Anaconda
Anaconda 4.6.11
首先下载安装脚本并赋予执行权限
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2019.03-Linux-x86_64.sh
chmod +x Anaconda3-2019.03-Linux-x86_64.sh
运行安装脚本
./Anaconda3-2019.03-Linux-x86_64.sh
按照提示输入安装信息,建议安装在 /usr/local/anaconda3
目录下,方便管理
最新版的 Anaconda 默认在 base 环境安装 python 3.7,导致很多框架不支持,这里换成 3.6
conda install python=3.6
conda install tensorflow-gpu=1.13.0
conda install cudatoolkit=9.0
git clone https://github.com/tensorflow/models.git
使用 Cityscapes 官方数据集
百度云链接
提取码: 7jgc
在 research/deeplab/datasets/dataset
目录下新建 dataset 文件夹,并将下载的数据集解压至 model-master/research/deeplab/datasets/dataset
mkdir model-master/research/deeplab/datasets/dataset
unzip cityscapes.zip -d model-master/research/deeplab/datasets/dataset
解压 gtFine 文件
cd model-master/research/deeplab/datasets/dataset/cityscapes && \
unzip gtFine.zip
clone 源码并移动至 model-master/research/deeplab/datasets/dataset/cityscapes
git clone https://github.com/mcordts/cityscapesScripts
mv cityscapesScripts model-master/research/deeplab/datasets/dataset/cityscapes
创建用于存放模型的文件夹
mkdir ~/models-master/research/deeplab/model
下载模型并解压至 model 文件夹
wget http://download.tensorflow.org/models/deeplabv3_mnv2_cityscapes_train_2018_02_05.tar.gz
tar zxvf deeplabv3_mnv2_cityscapes_train_2018_02_05.tar.gz ~/models-master/research/deeplab/model
将 Cityscapes 的 JSON 数据转换成 tfrecord
创建用于输出 tfrecord 数据的文件夹
mkdir ~/models-master/research/deeplab/datasets/dataset/cityscapes/tfrecord
修改 models-master/research/deeplab/datasets/convert_cityscapes.sh
中的路径设置
以下直接给出脚本全文
注意:根据实际情况修改路径
# Exit immediately if a command exits with a non-zero status.
set -e
CURRENT_DIR=$(pwd)
WORK_DIR="~/models-master/research/deeplab/datasets"
# Root path for Cityscapes dataset.
CITYSCAPES_ROOT="${WORK_DIR}/dataset/cityscapes"
# Create training labels.
python "${CITYSCAPES_ROOT}/cityscapesscripts/preparation/createTrainIdLabelImgs.py"
# Build TFRecords of the dataset.
# First, create output directory for storing TFRecords.
OUTPUT_DIR="${CITYSCAPES_ROOT}/tfrecord"
mkdir -p "${OUTPUT_DIR}"
BUILD_SCRIPT="${WORK_DIR}/build_cityscapes_data.py"
echo "Converting Cityscapes dataset..."
python "${BUILD_SCRIPT}" \
--cityscapes_root="${CITYSCAPES_ROOT}" \
--output_dir="${OUTPUT_DIR}" \
赋予脚本执行权限
chmod +x models-master/research/deeplab/datasets/convert_cityscapes.sh
为方便文件管理,以上创建的文件结构与项目默认的结构不同,会导致一些脚本找不到 cityscapesScripts 相关模块,需要在 python 脚本中添加路径
~/models-master/research/deeplab/datasets/dataset/cityscapes/cityscapesscripts/preparation/createTrainIdLabelImgs.py
在 from cityscapesscripts 之前添加
sys.path.append('/home/ai/models-master_train-cityscapes/research/deeplab/datasets/dataset/cityscapes')
修改 build_cityscapes_data.py 脚本中的路径设置
~/models-master/research/deeplab/datasets/build_cityscapes_data.py
# Cityscapes 目录
tf.app.flags.DEFINE_string('cityscapes_root',
'./dataset/cityscapes',
'Cityscapes dataset root folder.')
# 输出目录
tf.app.flags.DEFINE_string('output_dir',
'./dataset/cityscapes/tfrecord',
'Path to save converted SSTable of TensorFlow examples.')
执行格式转换脚本
~/models-master/research/deeplab/datasets/convert_cityscapes.sh
创建用于保存训练权重的目录
mkdir ~/models-master/research/deeplab/train
参数
python train.py \
--logtostderr \
--training_number_of_steps=30000 \
--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=321 \
--train_crop_size=321 \
--train_batch_size=4 \
--fine_tune_batch_norm=False \
--dataset="cityscapes" \
--tf_initial_checkpoint="~/models-master/research/deeplab/model/train_fine/model.ckpt" \
--train_logdir="~/models-master/research/deeplab/train" \
--dataset_dir="~/models-master/research/deeplab/datasets/dataset/cityscapes/tfrecord"
创建用于保存输出的目录
mkdir ~/models-master/research/deeplab/vis
参数
python vis.py
–logtostderr
–vis_split=“val”
–model_variant=“xception_65”
–atrous_rates=6
–atrous_rates=12
–atrous_rates=18
–output_stride=16
–decoder_output_stride=4
–vis_crop_size=1025
–vis_crop_size=2049
–dataset=“cityscapes”
–colormap_type=“cityscapes”
–checkpoint_dir="/home/ai/models-master_train-cityscapes/research/deeplab/train"
–vis_logdir="/home/ai/models-master_train-cityscapes/research/deeplab/vis"
–dataset_dir="/home/ai/models-master_train-cityscapes/research/deeplab/datasets/dataset/cityscapes/tfrecord"
测试后生成的原图和分割图存在 vis_logdir/segmentation_results
目录下