官方代碼 Deeplab v3+ resnet101 做backbone

大年初一我居然在更博客。今年過年由於病毒橫行,沒有串門沒有聚餐,整個人閒的沒事幹。。。醫生真是不容易,忙得團團轉還有生命危險,新希望他們平安。

本篇不屬於初級教程。如果完全看不懂請自行谷歌或搜索作者博客。

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_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="${TRAIN_LOGDIR}" \
  --dataset_dir="${PASCAL_DATASET}" \
  --tf_initial_checkpoint="${RES_WEIGHT}" \

說完了命令再說一下權重,官方給了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" \
 
發佈了287 篇原創文章 · 獲贊 143 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章