Skip to content

Commit 34117c6

Browse files
authored
Merge pull request #1 from tensorflow/master
update from original
2 parents a1a3873 + a1bd019 commit 34117c6

File tree

1,044 files changed

+100110
-123309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,044 files changed

+100110
-123309
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@ ENV/
9090

9191
# PyCharm
9292
.idea/
93+
94+
# For mac
95+
.DS_Store
96+
97+
samples/outreach/blogs/segmentation_blogpost/carvana-image-masking-challenge/

CODEOWNERS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
/research/adv_imagenet_models/ @AlexeyKurakin
77
/research/attention_ocr/ @alexgorban
88
/research/audioset/ @plakal @dpwe
9+
/research/autoaugment/* @barretzoph
910
/research/autoencoders/ @snurkabill
1011
/research/brain_coder/ @danabo
1112
/research/cognitive_mapping_and_planning/ @s-gupta
1213
/research/compression/ @nmjohn
14+
/research/cvt_text/ @clarkkev @lmthang
1315
/research/deep_contextual_bandits/ @rikel
1416
/research/deeplab/ @aquariusjay @yknzhu @gpapan
1517
/research/delf/ @andrefaraujo
@@ -28,6 +30,7 @@
2830
/research/lfads/ @jazcollins @susillo
2931
/research/lm_1b/ @oriolvinyals @panyx0718
3032
/research/lm_commonsense/ @thtrieu
33+
/research/lstm_object_detection/ @dreamdragon @masonliuw @yinxiaoli
3134
/research/marco/ @vincentvanhoucke
3235
/research/maskgan/ @a-dai
3336
/research/morph_net/ @gariel-google
@@ -46,6 +49,7 @@
4649
/research/slim/ @sguada @nathansilberman
4750
/research/steve/ @buckman-google
4851
/research/street/ @theraysmith
52+
/research/struct2depth/ @aneliaangelova
4953
/research/swivel/ @waterson
5054
/research/syntaxnet/ @calberti @andorardo @bogatyy @markomernick
5155
/research/tcn/ @coreylynch @sermanet

official/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ If you are on a version of TensorFlow earlier than 1.4, please [update your inst
1414
## Requirements
1515
Please follow the below steps before running models in this repo:
1616

17-
1. Add the top-level ***/models*** folder to the Python path with the command:
17+
18+
1. TensorFlow [nightly binaries](https://github.com/tensorflow/tensorflow#installation)
19+
20+
2. Add the top-level ***/models*** folder to the Python path with the command:
1821
```
1922
export PYTHONPATH="$PYTHONPATH:/path/to/models"
2023
```
21-
2. Install dependencies:
24+
25+
Using Colab:
26+
```
27+
import os
28+
os.environ['PYTHONPATH'] += ":/path/to/models"
29+
```
30+
31+
3. Install dependencies:
2232
```
2333
pip3 install --user -r official/requirements.txt
2434
```
@@ -54,7 +64,7 @@ our objectives of readable, usable, and maintainable code.
5464
* Runnable from a blank environment with relative ease.
5565
* Trainable on: single GPU/CPU (baseline), multiple GPUs, TPU
5666
* Compatible with Python 2 and 3 (using [six](https://pythonhosted.org/six/) when necessary)
57-
* Conform to [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html)
67+
* Conform to [Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md)
5868

5969
**Implementation guidelines**
6070

official/boosted_trees/data_download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
def _download_higgs_data_and_save_npz(data_dir):
4949
"""Download higgs data and store as a numpy compressed file."""
50-
input_url = os.path.join(URL_ROOT, INPUT_FILE)
50+
input_url = URL_ROOT + "/" + INPUT_FILE
5151
np_filename = os.path.join(data_dir, NPZ_FILE)
5252
if tf.gfile.Exists(np_filename):
5353
raise ValueError("data_dir already has the processed data file: {}".format(

official/boosted_trees/train_higgs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ def train_boosted_trees(flags_obj):
249249
_make_csv_serving_input_receiver_fn(
250250
column_names=feature_names,
251251
# columns are all floats.
252-
column_defaults=[[0.0]] * len(feature_names)))
252+
column_defaults=[[0.0]] * len(feature_names)),
253+
strip_default_attrs=True)
253254

254255

255256
def main(_):

official/datasets/movielens.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
ML_20M: 138493,
7373
}
7474

75-
# Note: Users are indexed [1, k], not [0, k-1]
75+
# Note: Movies are indexed [1, k], not [0, k-1]
7676
# Both the 1m and 20m datasets use the same movie set.
7777
NUM_ITEM_IDS = 3952
7878

@@ -111,12 +111,7 @@ def _download_and_clean(dataset, data_dir):
111111
temp_dir = tempfile.mkdtemp()
112112
try:
113113
zip_path = os.path.join(temp_dir, "{}.zip".format(dataset))
114-
def _progress(count, block_size, total_size):
115-
sys.stdout.write("\r>> Downloading {} {:.1f}%".format(
116-
zip_path, 100.0 * count * block_size / total_size))
117-
sys.stdout.flush()
118-
119-
zip_path, _ = urllib.request.urlretrieve(url, zip_path, _progress)
114+
zip_path, _ = urllib.request.urlretrieve(url, zip_path)
120115
statinfo = os.stat(zip_path)
121116
# A new line to clear the carriage return from download progress
122117
# tf.logging.info is not applicable here
@@ -133,8 +128,12 @@ def _progress(count, block_size, total_size):
133128
_regularize_20m_dataset(temp_dir)
134129

135130
for fname in tf.gfile.ListDirectory(temp_dir):
136-
tf.gfile.Copy(os.path.join(temp_dir, fname),
137-
os.path.join(data_subdir, fname))
131+
if not tf.gfile.Exists(os.path.join(data_subdir, fname)):
132+
tf.gfile.Copy(os.path.join(temp_dir, fname),
133+
os.path.join(data_subdir, fname))
134+
else:
135+
tf.logging.info("Skipping copy of {}, as it already exists in the "
136+
"destination folder.".format(fname))
138137

139138
finally:
140139
tf.gfile.DeleteRecursively(temp_dir)

official/keras_application_models/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ Synthetic dataset is used for the benchmark.
1919
Two custom callbacks are provided for model benchmarking: ExamplesPerSecondCallback and LoggingMetricCallback. For each callback, `epoch_based` and `batch_based` options are available to set the benchmark level. Check [model_callbacks.py](model_callbacks.py) for more details.
2020

2121
## Running Code
22-
To benchmark a model, use `--model` to specify the model name, and issue the following command:
22+
To benchmark a model, use `--model` to specify the model name. To perform the benchmark with eager execution, issue the following command:
2323
```
24-
python benchmark_main.py --model=resnet
24+
python benchmark_main.py --model resnet50 --eager
2525
```
26+
Note that, if eager execution is enabled, only one GPU is utilized even if multiple GPUs are provided and multi_gpu_model is used.
27+
28+
29+
To use distribution strategy in the benchmark, run the following:
30+
```
31+
python benchmark_main.py --model resnet50 --dist_strat
32+
```
33+
Currently, only one of the --eager and --dist_strat arguments can be defined, as DistributionStrategy is not supported in Eager execution now.
34+
2635
Arguments:
2736
* `--model`: Which model to be benchmarked. The model name is defined as the keys of `MODELS` in [benchmark_main.py](benchmark_main.py).
2837
* `--callbacks`: To specify a list of callbacks.

official/keras_application_models/benchmark_main.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from official.keras_application_models import model_callbacks
2929
from official.utils.flags import core as flags_core
3030
from official.utils.logs import logger
31+
from official.utils.misc import distribution_utils
3132

3233
# Define a dictionary that maps model names to their model classes inside Keras
3334
MODELS = {
@@ -41,9 +42,8 @@
4142
"densenet121": tf.keras.applications.DenseNet121,
4243
"densenet169": tf.keras.applications.DenseNet169,
4344
"densenet201": tf.keras.applications.DenseNet201,
44-
# TODO(b/80431378)
45-
# "nasnetlarge": tf.keras.applications.NASNetLarge,
46-
# "nasnetmobile": tf.keras.applications.NASNetMobile,
45+
"nasnetlarge": tf.keras.applications.NASNetLarge,
46+
"nasnetmobile": tf.keras.applications.NASNetMobile,
4747
}
4848

4949

@@ -62,7 +62,6 @@ def run_keras_model_benchmark(_):
6262
# Load the model
6363
tf.logging.info("Benchmark on {} model...".format(FLAGS.model))
6464
keras_model = MODELS[FLAGS.model]
65-
model = keras_model(weights=None)
6665

6766
# Get dataset
6867
dataset_name = "ImageNet"
@@ -73,31 +72,49 @@ def run_keras_model_benchmark(_):
7372
FLAGS.model, FLAGS.batch_size)
7473
val_dataset = dataset.generate_synthetic_input_dataset(
7574
FLAGS.model, FLAGS.batch_size)
75+
model = keras_model(weights=None)
7676
else:
77-
raise ValueError("Only synthetic dataset is supported!")
77+
tf.logging.info("Using CIFAR-10 dataset...")
78+
dataset_name = "CIFAR-10"
79+
ds = dataset.Cifar10Dataset(FLAGS.batch_size)
80+
train_dataset = ds.train_dataset
81+
val_dataset = ds.test_dataset
82+
model = keras_model(
83+
weights=None, input_shape=ds.input_shape, classes=ds.num_classes)
7884

79-
# If run with multiple GPUs
80-
# If eager execution is enabled, only one GPU is utilized even if multiple
81-
# GPUs are provided.
8285
num_gpus = flags_core.get_num_gpus(FLAGS)
83-
if num_gpus > 1:
86+
87+
distribution = None
88+
# Use distribution strategy
89+
if FLAGS.dist_strat:
90+
distribution = distribution_utils.get_distribution_strategy(
91+
num_gpus=num_gpus)
92+
elif num_gpus > 1:
93+
# Run with multi_gpu_model
94+
# If eager execution is enabled, only one GPU is utilized even if multiple
95+
# GPUs are provided.
8496
if FLAGS.eager:
8597
tf.logging.warning(
8698
"{} GPUs are provided, but only one GPU is utilized as "
8799
"eager execution is enabled.".format(num_gpus))
88100
model = tf.keras.utils.multi_gpu_model(model, gpus=num_gpus)
89101

102+
# Adam optimizer and some other optimizers doesn't work well with
103+
# distribution strategy (b/113076709)
104+
# Use GradientDescentOptimizer here
105+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
90106
model.compile(loss="categorical_crossentropy",
91-
optimizer=tf.train.AdamOptimizer(),
92-
metrics=["accuracy"])
107+
optimizer=optimizer,
108+
metrics=["accuracy"],
109+
distribute=distribution)
93110

94111
# Create benchmark logger for benchmark logging
95112
run_params = {
96113
"batch_size": FLAGS.batch_size,
97114
"synthetic_data": FLAGS.use_synthetic_data,
98115
"train_epochs": FLAGS.train_epochs,
99-
"num_train_images": FLAGS.num_images,
100-
"num_eval_images": FLAGS.num_images,
116+
"num_train_images": FLAGS.num_train_images,
117+
"num_eval_images": FLAGS.num_eval_images,
101118
}
102119

103120
benchmark_logger = logger.get_benchmark_logger()
@@ -118,8 +135,8 @@ def run_keras_model_benchmark(_):
118135
epochs=FLAGS.train_epochs,
119136
callbacks=callbacks,
120137
validation_data=val_dataset,
121-
steps_per_epoch=int(np.ceil(FLAGS.num_images / FLAGS.batch_size)),
122-
validation_steps=int(np.ceil(FLAGS.num_images / FLAGS.batch_size))
138+
steps_per_epoch=int(np.ceil(FLAGS.num_train_images / FLAGS.batch_size)),
139+
validation_steps=int(np.ceil(FLAGS.num_eval_images / FLAGS.batch_size))
123140
)
124141

125142
tf.logging.info("Logging the evaluation results...")
@@ -128,7 +145,7 @@ def run_keras_model_benchmark(_):
128145
"accuracy": history.history["val_acc"][epoch],
129146
"loss": history.history["val_loss"][epoch],
130147
tf.GraphKeys.GLOBAL_STEP: (epoch + 1) * np.ceil(
131-
FLAGS.num_images/FLAGS.batch_size)
148+
FLAGS.num_eval_images/FLAGS.batch_size)
132149
}
133150
benchmark_logger.log_evaluation_result(eval_results)
134151

@@ -157,17 +174,29 @@ def define_keras_benchmark_flags():
157174
"Model to be benchmarked."))
158175

159176
flags.DEFINE_integer(
160-
name="num_images", default=1000,
177+
name="num_train_images", default=1000,
178+
help=flags_core.help_wrap(
179+
"The number of synthetic images for training. The default value is "
180+
"1000."))
181+
182+
flags.DEFINE_integer(
183+
name="num_eval_images", default=50,
161184
help=flags_core.help_wrap(
162-
"The number of synthetic images for training and evaluation. The "
163-
"default value is 1000."))
185+
"The number of synthetic images for evaluation. The default value is "
186+
"50."))
164187

165188
flags.DEFINE_boolean(
166189
name="eager", default=False, help=flags_core.help_wrap(
167190
"To enable eager execution. Note that if eager execution is enabled, "
168191
"only one GPU is utilized even if multiple GPUs are provided and "
169192
"multi_gpu_model is used."))
170193

194+
flags.DEFINE_boolean(
195+
name="dist_strat", default=False, help=flags_core.help_wrap(
196+
"To enable distribution strategy for model training and evaluation. "
197+
"Number of GPUs used for distribution strategy can be set by the "
198+
"argument --num_gpus."))
199+
171200
flags.DEFINE_list(
172201
name="callbacks",
173202
default=["ExamplesPerSecondCallback", "LoggingMetricCallback"],
@@ -176,6 +205,15 @@ def define_keras_benchmark_flags():
176205
"callbacks. For example: `--callbacks ExamplesPerSecondCallback,"
177206
"LoggingMetricCallback`"))
178207

208+
@flags.multi_flags_validator(
209+
["eager", "dist_strat"],
210+
message="Both --eager and --dist_strat were set. Only one can be "
211+
"defined, as DistributionStrategy is not supported in Eager "
212+
"execution currently.")
213+
# pylint: disable=unused-variable
214+
def _check_eager_dist_strat(flag_dict):
215+
return not(flag_dict["eager"] and flag_dict["dist_strat"])
216+
179217

180218
def main(_):
181219
with logger.benchmark_context(FLAGS):

official/keras_application_models/dataset.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from __future__ import division
1818
from __future__ import print_function
1919

20+
import numpy as np
2021
import tensorflow as tf
21-
2222
from official.utils.misc import model_helpers # pylint: disable=g-bad-import-order
2323

2424
# Default values for dataset.
@@ -29,7 +29,7 @@
2929
def _get_default_image_size(model):
3030
"""Provide default image size for each model."""
3131
image_size = (224, 224)
32-
if model in ["inception", "xception", "inceptionresnet"]:
32+
if model in ["inceptionv3", "xception", "inceptionresnetv2"]:
3333
image_size = (299, 299)
3434
elif model in ["nasnetlarge"]:
3535
image_size = (331, 331)
@@ -42,8 +42,33 @@ def generate_synthetic_input_dataset(model, batch_size):
4242
image_shape = (batch_size,) + image_size + (_NUM_CHANNELS,)
4343
label_shape = (batch_size, _NUM_CLASSES)
4444

45-
return model_helpers.generate_synthetic_data(
45+
dataset = model_helpers.generate_synthetic_data(
4646
input_shape=tf.TensorShape(image_shape),
47-
input_dtype=tf.float32,
4847
label_shape=tf.TensorShape(label_shape),
49-
label_dtype=tf.float32)
48+
)
49+
return dataset
50+
51+
52+
class Cifar10Dataset(object):
53+
"""CIFAR10 dataset, including train and test set.
54+
55+
Each sample consists of a 32x32 color image, and label is from 10 classes.
56+
"""
57+
58+
def __init__(self, batch_size):
59+
"""Initializes train/test datasets.
60+
61+
Args:
62+
batch_size: int, the number of batch size.
63+
"""
64+
self.input_shape = (32, 32, 3)
65+
self.num_classes = 10
66+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
67+
x_train, x_test = x_train / 255.0, x_test / 255.0
68+
y_train, y_test = y_train.astype(np.int64), y_test.astype(np.int64)
69+
y_train = tf.keras.utils.to_categorical(y_train, self.num_classes)
70+
y_test = tf.keras.utils.to_categorical(y_test, self.num_classes)
71+
self.train_dataset = tf.data.Dataset.from_tensor_slices(
72+
(x_train, y_train)).shuffle(2000).batch(batch_size).repeat()
73+
self.test_dataset = tf.data.Dataset.from_tensor_slices(
74+
(x_test, y_test)).shuffle(2000).batch(batch_size).repeat()

0 commit comments

Comments
 (0)