Skip to content

Commit f02e601

Browse files
author
Alexander Gorban
committed
Merge remote-tracking branch 'tensorflow/master'
2 parents f5f1e12 + b719165 commit f02e601

File tree

157 files changed

+16775
-582
lines changed

Some content is hidden

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

157 files changed

+16775
-582
lines changed

CODEOWNERS

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
1-
official/* @nealwu @k-w-w
2-
research/adversarial_crypto/* @dave-andersen
3-
research/adversarial_text/* @rsepassi
4-
research/adv_imagenet_models/* @AlexeyKurakin
5-
research/attention_ocr/* @alexgorban
6-
research/audioset/* @plakal @dpwe
7-
research/autoencoders/* @snurkabill
8-
research/cognitive_mapping_and_planning/* @s-gupta
9-
research/compression/* @nmjohn
10-
research/delf/* @andrefaraujo
11-
research/differential_privacy/* @panyx0718
12-
research/domain_adaptation/* @bousmalis @ddohan
13-
research/im2txt/* @cshallue
14-
research/inception/* @shlens @vincentvanhoucke
15-
research/learned_optimizer/* @olganw @nirum
16-
research/learning_to_remember_rare_events/* @lukaszkaiser @ofirnachum
17-
research/lfads/* @jazcollins @susillo
18-
research/lm_1b/* @oriolvinyals @panyx0718
19-
research/namignizer/* @knathanieltucker
20-
research/neural_gpu/* @lukaszkaiser
21-
research/neural_programmer/* @arvind2505
22-
research/next_frame_prediction/* @panyx0718
23-
research/object_detection/* @jch1 @tombstone @derekjchow @jesu9 @dreamdragon
24-
research/pcl_rl/* @ofirnachum
25-
research/ptn/* @xcyan @arkanath @hellojas @honglaklee
26-
research/real_nvp/* @laurent-dinh
27-
research/rebar/* @gjtucker
28-
research/resnet/* @panyx0718
29-
research/skip_thoughts/* @cshallue
30-
research/slim/* @sguada @nathansilberman
31-
research/street/* @theraysmith
32-
research/swivel/* @waterson
33-
research/syntaxnet/* @calberti @andorardo @bogatyy @markomernick
34-
research/textsum/* @panyx0718 @peterjliu
35-
research/transformer/* @daviddao
36-
research/video_prediction/* @cbfinn
37-
samples/* @MarkDaoust
38-
tutorials/embedding/* @zffchen78 @a-dai
39-
tutorials/image/* @sherrym @shlens
40-
tutorials/rnn/* @lukaszkaiser @ebrevdo
1+
/official/ @nealwu @k-w-w
2+
/research/adversarial_crypto/ @dave-andersen
3+
/research/adversarial_text/ @rsepassi
4+
/research/adv_imagenet_models/ @AlexeyKurakin
5+
/research/attention_ocr/ @alexgorban
6+
/research/audioset/ @plakal @dpwe
7+
/research/autoencoders/ @snurkabill
8+
/research/cognitive_mapping_and_planning/ @s-gupta
9+
/research/compression/ @nmjohn
10+
/research/delf/ @andrefaraujo
11+
/research/differential_privacy/ @panyx0718
12+
/research/domain_adaptation/ @bousmalis @dmrd
13+
/research/gan/ @joel-shor
14+
/research/im2txt/ @cshallue
15+
/research/inception/ @shlens @vincentvanhoucke
16+
/research/learned_optimizer/ @olganw @nirum
17+
/research/learning_to_remember_rare_events/ @lukaszkaiser @ofirnachum
18+
/research/lfads/ @jazcollins @susillo
19+
/research/lm_1b/ @oriolvinyals @panyx0718
20+
/research/namignizer/ @knathanieltucker
21+
/research/neural_gpu/ @lukaszkaiser
22+
/research/neural_programmer/ @arvind2505
23+
/research/next_frame_prediction/ @panyx0718
24+
/research/object_detection/ @jch1 @tombstone @derekjchow @jesu9 @dreamdragon
25+
/research/pcl_rl/ @ofirnachum
26+
/research/ptn/ @xcyan @arkanath @hellojas @honglaklee
27+
/research/real_nvp/ @laurent-dinh
28+
/research/rebar/ @gjtucker
29+
/research/resnet/ @panyx0718
30+
/research/skip_thoughts/ @cshallue
31+
/research/slim/ @sguada @nathansilberman
32+
/research/street/ @theraysmith
33+
/research/swivel/ @waterson
34+
/research/syntaxnet/ @calberti @andorardo @bogatyy @markomernick
35+
/research/tcn/ @coreylynch @sermanet
36+
/research/textsum/ @panyx0718 @peterjliu
37+
/research/transformer/ @daviddao
38+
/research/video_prediction/ @cbfinn
39+
/research/fivo/ @dieterichlawson
40+
/samples/ @MarkDaoust
41+
/tutorials/embedding/ @zffchen78 @a-dai
42+
/tutorials/image/ @sherrym @shlens
43+
/tutorials/rnn/ @lukaszkaiser @ebrevdo

official/mnist/README.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This directory builds a convolutional neural net to classify the [MNIST
44
dataset](http://yann.lecun.com/exdb/mnist/) using the
5-
[tf.contrib.data](https://www.tensorflow.org/api_docs/python/tf/contrib/data),
5+
[tf.data](https://www.tensorflow.org/api_docs/python/tf/data),
66
[tf.estimator.Estimator](https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator),
77
and
88
[tf.layers](https://www.tensorflow.org/api_docs/python/tf/layers)
@@ -12,18 +12,51 @@ APIs.
1212
## Setup
1313

1414
To begin, you'll simply need the latest version of TensorFlow installed.
15+
Then to train the model, run the following:
1516

16-
First convert the MNIST data to TFRecord file format by running the following:
17+
```
18+
python mnist.py
19+
```
20+
21+
The model will begin training and will automatically evaluate itself on the
22+
validation data.
23+
24+
Illustrative unit tests and benchmarks can be run with:
1725

1826
```
19-
python convert_to_records.py
27+
python mnist_test.py
28+
python mnist_test.py --benchmarks=.
2029
```
2130

22-
Then to train the model, run the following:
31+
## Exporting the model
32+
33+
You can export the model into Tensorflow [SavedModel](https://www.tensorflow.org/programmers_guide/saved_model) format by using the argument `--export_dir`:
2334

2435
```
25-
python mnist.py
36+
python mnist.py --export_dir /tmp/mnist_saved_model
37+
```
38+
39+
The SavedModel will be saved in a timestamped directory under `/tmp/mnist_saved_model/` (e.g. `/tmp/mnist_saved_model/1513630966/`).
40+
41+
**Getting predictions with SavedModel**
42+
Use [`saved_model_cli`](https://www.tensorflow.org/programmers_guide/saved_model#cli_to_inspect_and_execute_savedmodel) to inspect and execute the SavedModel.
43+
44+
```
45+
saved_model_cli run --dir /tmp/mnist_saved_model/TIMESTAMP --tag_set serve --signature_def classify --inputs image=examples.npy
46+
```
47+
48+
`examples.npy` contains the data from `example5.png` and `example3.png` in a numpy array, in that order. The array values are normalized to values between 0 and 1.
49+
50+
The output should look similar to below:
51+
```
52+
Result for output key classes:
53+
[5 3]
54+
Result for output key probabilities:
55+
[[ 1.53558474e-07 1.95694142e-13 1.31193523e-09 5.47467265e-03
56+
5.85711526e-22 9.94520664e-01 3.48423509e-06 2.65365645e-17
57+
9.78631419e-07 3.15522470e-08]
58+
[ 1.22413359e-04 5.87615965e-08 1.72251271e-06 9.39960718e-01
59+
3.30306928e-11 2.87386645e-02 2.82353517e-02 8.21146413e-18
60+
2.52568233e-03 4.15460236e-04]]
2661
```
2762

28-
The model will begin training and will automatically evaluate itself on the
29-
validation data.

official/mnist/convert_to_records.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

official/mnist/dataset.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""tf.data.Dataset interface to the MNIST dataset."""
15+
16+
from __future__ import absolute_import
17+
from __future__ import division
18+
from __future__ import print_function
19+
20+
import os
21+
import shutil
22+
import gzip
23+
import numpy as np
24+
from six.moves import urllib
25+
import tensorflow as tf
26+
27+
28+
def read32(bytestream):
29+
"""Read 4 bytes from bytestream as an unsigned 32-bit integer."""
30+
dt = np.dtype(np.uint32).newbyteorder('>')
31+
return np.frombuffer(bytestream.read(4), dtype=dt)[0]
32+
33+
34+
def check_image_file_header(filename):
35+
"""Validate that filename corresponds to images for the MNIST dataset."""
36+
with open(filename) as f:
37+
magic = read32(f)
38+
num_images = read32(f)
39+
rows = read32(f)
40+
cols = read32(f)
41+
if magic != 2051:
42+
raise ValueError('Invalid magic number %d in MNIST file %s' % (magic,
43+
f.name))
44+
if rows != 28 or cols != 28:
45+
raise ValueError(
46+
'Invalid MNIST file %s: Expected 28x28 images, found %dx%d' %
47+
(f.name, rows, cols))
48+
49+
50+
def check_labels_file_header(filename):
51+
"""Validate that filename corresponds to labels for the MNIST dataset."""
52+
with open(filename) as f:
53+
magic = read32(f)
54+
num_items = read32(f)
55+
if magic != 2049:
56+
raise ValueError('Invalid magic number %d in MNIST file %s' % (magic,
57+
f.name))
58+
59+
60+
def download(directory, filename):
61+
"""Download (and unzip) a file from the MNIST dataset, if it doesn't already exist."""
62+
if not tf.gfile.Exists(directory):
63+
tf.gfile.MakeDirs(directory)
64+
filepath = os.path.join(directory, filename)
65+
if tf.gfile.Exists(filepath):
66+
return filepath
67+
# CVDF mirror of http://yann.lecun.com/exdb/mnist/
68+
url = 'https://storage.googleapis.com/cvdf-datasets/mnist/' + filename + '.gz'
69+
zipped_filepath = filepath + '.gz'
70+
print('Downloading %s to %s' % (url, zipped_filepath))
71+
urllib.request.urlretrieve(url, zipped_filepath)
72+
with gzip.open(zipped_filepath, 'rb') as f_in, open(filepath, 'wb') as f_out:
73+
shutil.copyfileobj(f_in, f_out)
74+
os.remove(zipped_filepath)
75+
return filepath
76+
77+
78+
def dataset(directory, images_file, labels_file):
79+
images_file = download(directory, images_file)
80+
labels_file = download(directory, labels_file)
81+
82+
check_image_file_header(images_file)
83+
check_labels_file_header(labels_file)
84+
85+
def decode_image(image):
86+
# Normalize from [0, 255] to [0.0, 1.0]
87+
image = tf.decode_raw(image, tf.uint8)
88+
image = tf.cast(image, tf.float32)
89+
image = tf.reshape(image, [784])
90+
return image / 255.0
91+
92+
def one_hot_label(label):
93+
label = tf.decode_raw(label, tf.uint8) # tf.string -> tf.uint8
94+
label = tf.reshape(label, []) # label is a scalar
95+
return tf.one_hot(label, 10)
96+
97+
images = tf.data.FixedLengthRecordDataset(
98+
images_file, 28 * 28, header_bytes=16).map(decode_image)
99+
labels = tf.data.FixedLengthRecordDataset(
100+
labels_file, 1, header_bytes=8).map(one_hot_label)
101+
return tf.data.Dataset.zip((images, labels))
102+
103+
104+
def train(directory):
105+
"""tf.data.Dataset object for MNIST training data."""
106+
return dataset(directory, 'train-images-idx3-ubyte',
107+
'train-labels-idx1-ubyte')
108+
109+
110+
def test(directory):
111+
"""tf.data.Dataset object for MNIST test data."""
112+
return dataset(directory, 't10k-images-idx3-ubyte', 't10k-labels-idx1-ubyte')

official/mnist/example3.png

368 Bytes
Loading

official/mnist/example5.png

367 Bytes
Loading

official/mnist/examples.npy

12.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)