大年初一我居然在更博客。今年过年由于病毒横行,没有串门没有聚餐,整个人闲的没事干。。。医生真是不容易,忙得团团转还有生命危险,新希望他们平安。
本篇不属于初级教程。如果完全看不懂请自行谷歌或搜索作者博客。
deeplab官方提供了多种backbone,通过train.py中传递参数,
--model_variant="resnet_v1_101_beta" \
可以更改backbone。(resnet_v1_{50,101}_beta: We modify the original ResNet-101 [10], similar to PSPNet [11] by replacing the first 7x7 convolution with three 3x3 convolutions. See resnet_v1_beta.py for more details.)
这个是官方对beta的说法。就是改了一个卷积核的尺寸。改小了,并且用了三个。
一共有如下可选:
# A map from feature extractor name to the network name scope used in the
# ImageNet pretrained versions of these models.
name_scope = {
'mobilenet_v2': 'MobilenetV2',
'resnet_v1_50': 'resnet_v1_50',
'resnet_v1_50_beta': 'resnet_v1_50',
'resnet_v1_101': 'resnet_v1_101',
'resnet_v1_101_beta': 'resnet_v1_101',
'xception_41': 'xception_41',
'xception_65': 'xception_65',
'xception_71': 'xception_71',
'nas_pnasnet': 'pnasnet',
'nas_hnasnet': 'hnasnet',
}
当然backbone更改后,网络训练的参数比如decay什么的也有所不同。在feature_extractor.py中307行开始就是在改参数,举个例子:
if 'resnet' in model_variant:
arg_scope = arg_scopes_map[model_variant](
weight_decay=weight_decay,
batch_norm_decay=0.95,
batch_norm_epsilon=1e-5,
batch_norm_scale=True)
并且在train.py中:
# For weight_decay, use 0.00004 for MobileNet-V2 or Xcpetion model variants.
# Use 0.0001 for ResNet model variants.
flags.DEFINE_float('weight_decay', 0.00004,
'The value of the weight decay for training.')
所以,在我自己的bash文件中,我也要改
--weight_decay=0.0001 \
于是我完整的bash文件就是:
python "${WORK_DIR}"/train.py \
--logtostderr \
--train_split="train" \
--model_variant="resnet_v1_50_beta" \
--weight_decay=0.0001 \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--train_crop_size="513,513" \
--train_batch_size=12 \
--num_clones=6 \
--training_number_of_steps=30000 \
--fine_tune_batch_norm=True \
--train_logdir="${TRAIN_LOGDIR}" \
--dataset_dir="${PASCAL_DATASET}" \
--tf_initial_checkpoint="${RES_WEIGHT}" \
--base_learning_rate=0.007 \
说完了命令再说一下权重,官方给了resnet101,以及resnet50在imagenet,(https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/model_zoo.md)resnet预训练的权重。从官网下载后,加载的过程中,我发现,如果使用
--model_variant="resnet_v1_101" \
会出现加载错误。网络结构中在bottleneck上的参数设置,与checkpoint训练的网络结构不一样。同时,resnet在论文中提及的时候,作者说自己改过了。所以,这里大年初一更博客的笔者推测,beta版本才是真正的backbone。由于谷歌上不去,不想用镜像,所以笔者使用的是beta。使用后权重加载成功,并且有如下提示:
INFO:tensorflow:Initializing model from path: /home/DATA/liutian/tmp/tfdeeplab/resnet/model.ckpt
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/weights]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/depthwise_weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/depthwise_weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/depthwise_weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/weights]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/weights]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/weights]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/depthwise_weights]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/weights]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/depthwise_weights]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/weights]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/BatchNorm/gamma]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/BatchNorm/beta]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/BatchNorm/moving_mean]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/BatchNorm/moving_variance]
WARNING:tensorflow:Checkpoint is missing variable [logits/semantic/weights]
WARNING:tensorflow:Checkpoint is missing variable [logits/semantic/biases]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [image_pooling/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp0/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/depthwise_weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_depthwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp1_pointwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/depthwise_weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_depthwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp2_pointwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/depthwise_weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_depthwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [aspp3_pointwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [concat_projection/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/feature_projection0/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/depthwise_weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_depthwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv0_pointwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/depthwise_weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_depthwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/BatchNorm/gamma/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [decoder/decoder_conv1_pointwise/BatchNorm/beta/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [logits/semantic/weights/Momentum]
WARNING:tensorflow:Checkpoint is missing variable [logits/semantic/biases/Momentum]
INFO:tensorflow:Create CheckpointSaverHook.
我认为这个说明checkpoint少了decode与aspp的网络。而beta是有的。因为在代码中,aspp是否使用是通过参数空置的(model.py 397行:
model_options.aspp_with_batch_norm
),decode是否使用也是通过参数控制的(
decoder_output_stride如果不给参数就直接删除整个decoder。
),modelvariant只会影响encoder阶段,也就是feature_extractor.
那么我的确认方式是1,debug2,看tensorboard
ValueError: Total size of new array must be unchanged for resnet_v1_101/block1/unit_1/bottleneck_v1/shortcut/weights lh_shape: [(1, 1, 128, 256)], rh_shape: [(1, 1, 64, 256)]
之所以废这么多话是想说,复现可能会有一定问题,因为你需要先用coco预训练,再用voc2012 trainaug set预训练,得到的权重才可以和论文比。
我是没用coco。
更-------------------------
这个病毒越演愈烈,正好在北京直面过非典,实际上非典时期结束有一部分是因为天气变热,病毒传播困难。再加上隔离严格。所以武汉肺炎终究会过去。就是医护人员在湖北人手不足,新闻上全家感染的例子不在少数。致死率没有非典严重,大多数是并发症。但是传染的速度真的是太快了。虽然不能恐慌,但是也要严肃对待。未感染的需要提高免疫力,摄取维生素c,注意保暖,不要感冒发烧,以免给医疗系统增加压力。
这里贴的是resnet101在voc的结果,
这个贴的是xception,可以看到,这里采用了coco以及JFT两个数据集预训练作为变量。而resnet则没有,所以很有可能是没用coco,只用了imagenet。
所以我的总的bash代码是这样的。
#!/bin/bash
# Copyright 2018 The TensorFlow Authors All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#
# This script is used to run local test on PASCAL VOC 2012. Users could also
# modify from this script for their use case.
#
# Usage:
# # From the tensorflow/models/research/deeplab directory.
# sh ./local_test.sh
#
#
# Exit immediately if a command exits with a non-zero status.
set -e
# Move one-level up to tensorflow/models/research directory.
cd ..
# Update PYTHONPATH.
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
# Set up the working environment.
CURRENT_DIR=$(pwd)
WORK_DIR="${CURRENT_DIR}/deeplab"
RES_DIR="${CURRENT_DIR}/resnet"
RES_WEIGHT="${CURRENT_DIR}/resnet/model.ckpt"
#export CUDA_VISIBLE_DEVICES=3
# Run model_test first to make sure the PYTHONPATH is correctly set.
#python "${WORK_DIR}"/model_test.py -v
# Go to datasets folder and download PASCAL VOC 2012 segmentation dataset.
DATASET_DIR="datasets"
cd "${WORK_DIR}/${DATASET_DIR}"
#sh download_and_convert_voc2012.sh
# Go back to original directory.
cd "${CURRENT_DIR}"
# Set up the working directories.
PASCAL_FOLDER="pascal_voc_seg"
EXP_FOLDER="result/offres_freze"
INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models"
COCO_PRE="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/coco_pretrain"
TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train"
VOC_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train_voc"
EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval"
VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis"
EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export"
mkdir -p "${INIT_FOLDER}"
mkdir -p "${TRAIN_LOGDIR}"
mkdir -p "${EVAL_LOGDIR}"
mkdir -p "${VIS_LOGDIR}"
mkdir -p "${EXPORT_DIR}"
# Copy locally the trained checkpoint as the initial checkpoint.
TF_INIT_ROOT="http://download.tensorflow.org/models"
TF_INIT_CKPT="deeplabv3_pascal_train_aug_2018_01_04.tar.gz"
cd "${INIT_FOLDER}"
#wget -nd -c "${TF_INIT_ROOT}/${TF_INIT_CKPT}"
cd "${CURRENT_DIR}"
PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord"
# Train 10 iterations.
NUM_ITERATIONS=20
echo ${PASCAL_DATASET}
python "${WORK_DIR}"/train.py \
--logtostderr \
--train_split="train_aug" \
--model_variant="resnet_v1_101_beta" \
--weight_decay=0.001 \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--train_crop_size="513,513" \
--train_batch_size=32 \
--num_clones=8 \
--training_number_of_steps=30000 \
--fine_tune_batch_norm=True \
--train_logdir="${TRAIN_LOGDIR}" \
--dataset_dir="${PASCAL_DATASET}" \
--tf_initial_checkpoint="${RES_WEIGHT}" \
python "${WORK_DIR}"/train.py \
--logtostderr \
--train_split="train" \
--model_variant="resnet_v1_101_beta" \
--weight_decay=0.0001 \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--decoder_output_stride=4 \
--train_crop_size="513,513" \
--train_batch_size=32 \
--num_clones=8 \
--training_number_of_steps=30000 \
--fine_tune_batch_norm=True \
--train_logdir="${VOC_LOGDIR}" \
--dataset_dir="${PASCAL_DATASET}" \
--tf_initial_checkpoint="${TRAIN_LOGDIR}/model.ckpt-30000" \
论文较真:
由于官方没写resnet作为backbone怎么训练的,所以这里放一个同样用deeplabv3+,resnet,cvpr的文章:
Decoders Matter for Semantic Segmentation:Data-Dependent Decoding Enables Flexible Feature Aggregation
在val的pascal voc数据集大概是72,73的准确率: