Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13c9de3

Browse files
committed
fix code style problem and add option "last_layers_contain_logits_only"
1 parent 17ba1ca commit 13c9de3

9 files changed

+47
-34
lines changed

research/deeplab/datasets/build_ade20k_data.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515

16-
import glob
1716
import math
1817
import os
1918
import random
2019
import string
2120
import sys
22-
from PIL import Image
2321
import build_data
2422
import tensorflow as tf
2523

2624
FLAGS = tf.app.flags.FLAGS
27-
flags = tf.app.flags
2825

2926
tf.app.flags.DEFINE_string(
3027
'train_image_folder',
@@ -52,18 +49,18 @@
5249
_NUM_SHARDS = 4
5350

5451
def _convert_dataset(dataset_split, dataset_dir, dataset_label_dir):
55-
""" Convert the ADE20k dataset into into tfrecord format (SSTable).
52+
""" Converts the ADE20k dataset into into tfrecord format (SSTable).
5653
5754
Args:
58-
dataset_split: dataset split (e.g., train, val)
59-
dataset_dir: dir in which the dataset locates
60-
dataset_label_dir: dir in which the annotations locates
55+
dataset_split: Dataset split (e.g., train, val).
56+
dataset_dir: Dir in which the dataset locates.
57+
dataset_label_dir: Dir in which the annotations locates.
6158
6259
Raises:
6360
RuntimeError: If loaded image and label have different shape.
6461
"""
6562

66-
img_names = glob.glob(os.path.join(dataset_dir, '*.jpg'))
63+
img_names = tf.gfile.Glob(os.path.join(dataset_dir, '*.jpg'))
6764
random.shuffle(img_names)
6865
seg_names = []
6966
for f in img_names:
@@ -74,7 +71,7 @@ def _convert_dataset(dataset_split, dataset_dir, dataset_label_dir):
7471
seg_names.append(seg)
7572

7673
num_images = len(img_names)
77-
num_per_shard = int(math.ceil(num_images) / float(_NUM_SHARDS))
74+
num_per_shard = int(math.ceil(num_images / float(_NUM_SHARDS)))
7875

7976
image_reader = build_data.ImageReader('jpeg', channels=3)
8077
label_reader = build_data.ImageReader('png', channels=1)

research/deeplab/datasets/build_voc2012_data.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
image/segmentation/class/encoded: encoded semantic segmentation content.
5151
image/segmentation/class/format: semantic segmentation file format.
5252
"""
53-
import glob
5453
import math
5554
import os.path
5655
import sys
@@ -133,7 +132,7 @@ def _convert_dataset(dataset_split):
133132

134133

135134
def main(unused_argv):
136-
dataset_splits = glob.glob(os.path.join(FLAGS.list_folder, '*.txt'))
135+
dataset_splits = tf.gfile.Glob(os.path.join(FLAGS.list_folder, '*.txt'))
137136
for dataset_split in dataset_splits:
138137
_convert_dataset(dataset_split)
139138

research/deeplab/datasets/download_and_convert_ade20k.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,27 @@ set -e
3939

4040
CURRENT_DIR=$(pwd)
4141
WORK_DIR="./ADE20K"
42-
mkdir -p ${WORK_DIR}
43-
cd ${WORK_DIR}
42+
mkdir -p "${WORK_DIR}"
43+
cd "${WORK_DIR}"
4444

4545
# Helper function to download and unpack ADE20K dataset.
4646
download_and_uncompress() {
4747
local BASE_URL=${1}
4848
local FILENAME=${2}
4949

50-
if [ ! -f ${FILENAME} ]; then
50+
if [ ! -f "${FILENAME}" ]; then
5151
echo "Downloading ${FILENAME} to ${WORK_DIR}"
5252
wget -nd -c "${BASE_URL}/${FILENAME}"
5353
fi
5454
echo "Uncompressing ${FILENAME}"
55-
unzip ${FILENAME}
55+
unzip "${FILENAME}"
5656
}
5757

5858
# Download the images.
5959
BASE_URL="http://data.csail.mit.edu/places/ADEchallenge"
6060
FILENAME="ADEChallengeData2016.zip"
6161

62-
download_and_uncompress ${BASE_URL} ${FILENAME}
62+
download_and_uncompress "${BASE_URL}" "${FILENAME}"
6363

6464
cd "${CURRENT_DIR}"
6565

research/deeplab/datasets/download_and_convert_voc2012.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ set -e
3737

3838
CURRENT_DIR=$(pwd)
3939
WORK_DIR="./pascal_voc_seg"
40-
mkdir -p ${WORK_DIR}
41-
cd ${WORK_DIR}
40+
mkdir -p "${WORK_DIR}"
41+
cd "${WORK_DIR}"
4242

4343
# Helper function to download and unpack VOC 2012 dataset.
4444
download_and_uncompress() {
4545
local BASE_URL=${1}
4646
local FILENAME=${2}
4747

48-
if [ ! -f ${FILENAME} ]; then
48+
if [ ! -f "${FILENAME}" ]; then
4949
echo "Downloading ${FILENAME} to ${WORK_DIR}"
5050
wget -nd -c "${BASE_URL}/${FILENAME}"
5151
fi
5252
echo "Uncompressing ${FILENAME}"
53-
tar -xf ${FILENAME}
53+
tar -xf "${FILENAME}"
5454
}
5555

5656
# Download the images.
5757
BASE_URL="http://host.robots.ox.ac.uk/pascal/VOC/voc2012/"
5858
FILENAME="VOCtrainval_11-May-2012.tar"
5959

60-
download_and_uncompress ${BASE_URL} ${FILENAME}
60+
download_and_uncompress "${BASE_URL}" "${FILENAME}"
6161

6262
cd "${CURRENT_DIR}"
6363

research/deeplab/datasets/segmentation_dataset.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
The Cityscapes dataset contains 19 semantic labels (such as road, person, car,
3232
and so on) for urban street scenes.
3333
34+
3. ADE20K dataset (http://groups.csail.mit.edu/vision/datasets/ADE20K)
35+
36+
The ADE20K dataset contains 150 semantic labels both urban street scenes and
37+
indoor scenes.
38+
3439
References:
3540
M. Everingham, S. M. A. Eslami, L. V. Gool, C. K. I. Williams, J. Winn,
3641
and A. Zisserman, The pascal visual object classes challenge a retrospective.
@@ -39,6 +44,9 @@
3944
M. Cordts, M. Omran, S. Ramos, T. Rehfeld, M. Enzweiler, R. Benenson,
4045
U. Franke, S. Roth, and B. Schiele, "The cityscapes dataset for semantic urban
4146
scene understanding," In Proc. of CVPR, 2016.
47+
48+
B. Zhou, H. Zhao, X. Puig, S. Fidler, A. Barriuso, A. Torralba, "Scene Parsing
49+
through ADE20K dataset", In Proc. of CVPR, 2017.
4250
"""
4351
import collections
4452
import os.path
@@ -87,12 +95,10 @@
8795

8896
# These number (i.e., 'train'/'test') seems to have to be hard coded
8997
# You are required to figure it out for your training/testing example.
90-
# Is there a way to automatically figure it out ?
9198
_ADE20K_INFORMATION = DatasetDescriptor(
9299
splits_to_sizes = {
93100
'train': 20210, # num of samples in images/training
94101
'val': 2000, # num of samples in images/validation
95-
'eval': 2,
96102
},
97103
num_classes=150,
98104
ignore_label=255,

research/deeplab/g3doc/ade20k.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ python deeplab/train.py \
6767
--fine_tune_batch_norm=False \
6868
--dataset="ade20k" \
6969
--initialize_last_layer=False \
70+
--last_layers_contain_logits_only=True \
7071
--tf_initial_checkpoint=${PATH_TO_INITIAL_CHECKPOINT} \
7172
--train_logdir=${PATH_TO_TRAIN_DIR}\
7273
--dataset_dir=${PATH_TO_DATASET}
@@ -90,7 +91,7 @@ which the ADE20K dataset resides (the `tfrecord` above)
9091
fine_tune_batch_norm = False.
9192

9293
2. User should fine tune the `min_resize_value` and `max_resize_value` to get
93-
better result. Note that `resize_factor` has to equals to `output_stride`.
94+
better result. Note that `resize_factor` has to be equal to `output_stride`.
9495

9596
2. The users should change atrous_rates from [6, 12, 18] to [12, 24, 36] if
9697
setting output_stride=8.

research/deeplab/model.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,26 @@
6464
_DECODER_SCOPE = 'decoder'
6565

6666

67-
def get_extra_layer_scopes():
67+
def get_extra_layer_scopes(last_layers_contain_logits_only=False):
6868
"""Gets the scopes for extra layers.
6969
70+
Args:
71+
last_layers_contain_logits_only: Boolean, True if only consider logits as
72+
the last layer (i.e., exclude ASPP module, decoder module and so on)
73+
7074
Returns:
7175
A list of scopes for extra layers.
7276
"""
73-
return [
74-
_LOGITS_SCOPE_NAME,
75-
_IMAGE_POOLING_SCOPE,
76-
_ASPP_SCOPE,
77-
_CONCAT_PROJECTION_SCOPE,
78-
_DECODER_SCOPE,
79-
]
77+
if last_layers_contain_logits_only:
78+
return [_LOGITS_SCOPE_NAME]
79+
else:
80+
return [
81+
_LOGITS_SCOPE_NAME,
82+
_IMAGE_POOLING_SCOPE,
83+
_ASPP_SCOPE,
84+
_CONCAT_PROJECTION_SCOPE,
85+
_DECODER_SCOPE,
86+
]
8087

8188

8289
def predict_labels_multi_scale(images,

research/deeplab/train.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@
118118
flags.DEFINE_boolean('initialize_last_layer', True,
119119
'Initialize the last layer.')
120120

121+
flags.DEFINE_boolean('last_layers_contain_logits_only', False,
122+
'Only consider logits as last layers or not.')
123+
121124
flags.DEFINE_integer('slow_start_step', 0,
122125
'Training model with small learning rate for few steps.')
123126

@@ -292,7 +295,7 @@ def main(unused_argv):
292295
summaries.add(tf.summary.scalar('total_loss', total_loss))
293296

294297
# Modify the gradients for biases and last layer variables.
295-
last_layers = model.get_extra_layer_scopes()
298+
last_layers = model.get_extra_layer_scopes(FLAGS.last_layers_contain_logits_only)
296299
grad_mult = train_utils.get_model_gradient_multipliers(
297300
last_layers, FLAGS.last_layer_gradient_multiplier)
298301
if grad_mult:

research/deeplab/utils/train_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def get_model_init_fn(train_logdir,
9999
tf.logging.info('Initializing model from path: %s', tf_initial_checkpoint)
100100

101101
# Variables that will not be restored.
102-
exclude_list = ['global_step', 'logits']
102+
exclude_list = ['global_step']
103103
if not initialize_last_layer:
104104
exclude_list.extend(last_layers)
105105

0 commit comments

Comments
 (0)