aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/slim/python/slim/data/dataset_data_provider_test.py
blob: 1bb6fbc5700c7de4ca07e02dd6f462917f323e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright 2016 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.
# ==============================================================================
"""Tests for slim.data.dataset_data_provider."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile

from tensorflow.contrib.slim.python.slim import queues
from tensorflow.contrib.slim.python.slim.data import dataset
from tensorflow.contrib.slim.python.slim.data import dataset_data_provider
from tensorflow.contrib.slim.python.slim.data import test_utils
from tensorflow.contrib.slim.python.slim.data import tfexample_decoder
from tensorflow.python.client import session
from tensorflow.python.framework import dtypes
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import image_ops
from tensorflow.python.ops import io_ops
from tensorflow.python.ops import parsing_ops
from tensorflow.python.platform import gfile
from tensorflow.python.platform import test


def _resize_image(image, height, width):
  image = array_ops.expand_dims(image, 0)
  image = image_ops.resize_bilinear(image, [height, width])
  return array_ops.squeeze(image, [0])


def _create_tfrecord_dataset(tmpdir):
  if not gfile.Exists(tmpdir):
    gfile.MakeDirs(tmpdir)

  data_sources = test_utils.create_tfrecord_files(tmpdir, num_files=1)

  keys_to_features = {
      'image/encoded':
          parsing_ops.FixedLenFeature(
              shape=(), dtype=dtypes.string, default_value=''),
      'image/format':
          parsing_ops.FixedLenFeature(
              shape=(), dtype=dtypes.string, default_value='jpeg'),
      'image/class/label':
          parsing_ops.FixedLenFeature(
              shape=[1],
              dtype=dtypes.int64,
              default_value=array_ops.zeros(
                  [1], dtype=dtypes.int64))
  }

  items_to_handlers = {
      'image': tfexample_decoder.Image(),
      'label': tfexample_decoder.Tensor('image/class/label'),
  }

  decoder = tfexample_decoder.TFExampleDecoder(keys_to_features,
                                               items_to_handlers)

  return dataset.Dataset(
      data_sources=data_sources,
      reader=io_ops.TFRecordReader,
      decoder=decoder,
      num_samples=100,
      items_to_descriptions=None)


class DatasetDataProviderTest(test.TestCase):

  def testTFRecordDataset(self):
    dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(),
                                                       'tfrecord_dataset'))

    height = 300
    width = 280

    with self.test_session():
      test_dataset = _create_tfrecord_dataset(dataset_dir)
      provider = dataset_data_provider.DatasetDataProvider(test_dataset)
      key, image, label = provider.get(['record_key', 'image', 'label'])
      image = _resize_image(image, height, width)

      with session.Session('') as sess:
        with queues.QueueRunners(sess):
          key, image, label = sess.run([key, image, label])
      split_key = key.decode('utf-8').split(':')
      self.assertEqual(2, len(split_key))
      self.assertEqual(test_dataset.data_sources[0], split_key[0])
      self.assertTrue(split_key[1].isdigit())
      self.assertListEqual([height, width, 3], list(image.shape))
      self.assertListEqual([1], list(label.shape))

  def testTFRecordSeparateGetDataset(self):
    dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(),
                                                       'tfrecord_separate_get'))

    height = 300
    width = 280

    with self.test_session():
      provider = dataset_data_provider.DatasetDataProvider(
          _create_tfrecord_dataset(dataset_dir))
    [image] = provider.get(['image'])
    [label] = provider.get(['label'])
    image = _resize_image(image, height, width)

    with session.Session('') as sess:
      with queues.QueueRunners(sess):
        image, label = sess.run([image, label])
      self.assertListEqual([height, width, 3], list(image.shape))
      self.assertListEqual([1], list(label.shape))

  def testConflictingRecordKeyItem(self):
    dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(),
                                                       'tfrecord_dataset'))

    with self.test_session():
      with self.assertRaises(ValueError):
        dataset_data_provider.DatasetDataProvider(
            _create_tfrecord_dataset(dataset_dir), record_key='image')

if __name__ == '__main__':
  test.main()