datasets_test.py 12.1 KB
Newer Older
Dieterich Lawson's avatar
Dieterich Lawson committed
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# 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.
# ==============================================================================

"""Tests for fivo.data.datasets."""

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

import pickle
import os

import numpy as np
import tensorflow as tf

from fivo.data import datasets

FLAGS = tf.app.flags.FLAGS


class DatasetsTest(tf.test.TestCase):

  def test_sparse_pianoroll_to_dense_empty_at_end(self):
    sparse_pianoroll = [(0, 1), (1, 0), (), (1,), (), ()]
    dense_pianoroll, num_timesteps = datasets.sparse_pianoroll_to_dense(
        sparse_pianoroll, min_note=0, num_notes=2)
    self.assertEqual(num_timesteps, 6)
    self.assertAllEqual([[1, 1],
                         [1, 1],
                         [0, 0],
                         [0, 1],
                         [0, 0],
                         [0, 0]], dense_pianoroll)

  def test_sparse_pianoroll_to_dense_with_chord(self):
    sparse_pianoroll = [(0, 1), (1, 0), (), (1,)]
    dense_pianoroll, num_timesteps = datasets.sparse_pianoroll_to_dense(
        sparse_pianoroll, min_note=0, num_notes=2)
    self.assertEqual(num_timesteps, 4)
    self.assertAllEqual([[1, 1],
                         [1, 1],
                         [0, 0],
                         [0, 1]], dense_pianoroll)

  def test_sparse_pianoroll_to_dense_simple(self):
    sparse_pianoroll = [(0,), (), (1,)]
    dense_pianoroll, num_timesteps = datasets.sparse_pianoroll_to_dense(
        sparse_pianoroll, min_note=0, num_notes=2)
    self.assertEqual(num_timesteps, 3)
    self.assertAllEqual([[1, 0],
                         [0, 0],
                         [0, 1]], dense_pianoroll)

  def test_sparse_pianoroll_to_dense_subtracts_min_note(self):
    sparse_pianoroll = [(4, 5), (5, 4), (), (5,), (), ()]
    dense_pianoroll, num_timesteps = datasets.sparse_pianoroll_to_dense(
        sparse_pianoroll, min_note=4, num_notes=2)
    self.assertEqual(num_timesteps, 6)
    self.assertAllEqual([[1, 1],
                         [1, 1],
                         [0, 0],
                         [0, 1],
                         [0, 0],
                         [0, 0]], dense_pianoroll)

  def test_sparse_pianoroll_to_dense_uses_num_notes(self):
    sparse_pianoroll = [(4, 5), (5, 4), (), (5,), (), ()]
    dense_pianoroll, num_timesteps = datasets.sparse_pianoroll_to_dense(
        sparse_pianoroll, min_note=4, num_notes=3)
    self.assertEqual(num_timesteps, 6)
    self.assertAllEqual([[1, 1, 0],
                         [1, 1, 0],
                         [0, 0, 0],
                         [0, 1, 0],
                         [0, 0, 0],
                         [0, 0, 0]], dense_pianoroll)

  def test_pianoroll_dataset(self):
    pianoroll_data = [[(0,), (), (1,)],
                      [(0, 1), (1,)],
                      [(1,), (0,), (), (0, 1), (), ()]]
    pianoroll_mean = np.zeros([3])
    pianoroll_mean[-1] = 1
    data = {"train": pianoroll_data, "train_mean": pianoroll_mean}
    path = os.path.join(tf.test.get_temp_dir(), "test.pkl")
    pickle.dump(data, open(path, "wb"))
    with self.test_session() as sess:
      inputs, targets, lens, mean = datasets.create_pianoroll_dataset(
          path, "train", 2, num_parallel_calls=1,
          shuffle=False, repeat=False,
          min_note=0, max_note=2)
      i1, t1, l1 = sess.run([inputs, targets, lens])
      i2, t2, l2 = sess.run([inputs, targets, lens])
      m = sess.run(mean)
      # Check the lengths.
      self.assertAllEqual([3, 2], l1)
      self.assertAllEqual([6], l2)
      # Check the mean.
      self.assertAllEqual(pianoroll_mean, m)
      # Check the targets. The targets should not be mean-centered and should
      # be padded with zeros to a common length within a batch.
      self.assertAllEqual([[1, 0, 0],
                           [0, 0, 0],
                           [0, 1, 0]], t1[:, 0, :])
      self.assertAllEqual([[1, 1, 0],
                           [0, 1, 0],
                           [0, 0, 0]], t1[:, 1, :])
      self.assertAllEqual([[0, 1, 0],
                           [1, 0, 0],
                           [0, 0, 0],
                           [1, 1, 0],
                           [0, 0, 0],
                           [0, 0, 0]], t2[:, 0, :])
      # Check the inputs. Each sequence should start with zeros on the first
      # timestep. Each sequence should be padded with zeros to a common length
      # within a batch. The mean should be subtracted from all timesteps except
      # the first and the padding.
      self.assertAllEqual([[0, 0, 0],
                           [1, 0, -1],
                           [0, 0, -1]], i1[:, 0, :])
      self.assertAllEqual([[0, 0, 0],
                           [1, 1, -1],
                           [0, 0, 0]], i1[:, 1, :])
      self.assertAllEqual([[0, 0, 0],
                           [0, 1, -1],
                           [1, 0, -1],
                           [0, 0, -1],
                           [1, 1, -1],
                           [0, 0, -1]], i2[:, 0, :])

  def test_human_pose_dataset(self):
    pose_data = [
        [[0, 0], [2, 2]],
        [[2, 2]],
        [[0, 0], [0, 0], [2, 2], [2, 2], [0, 0]],
    ]
    pose_data = [np.array(x, dtype=np.float64) for x in pose_data]
    pose_data_mean = np.array([1, 1], dtype=np.float64)
    data = {
        "train": pose_data,
        "train_mean": pose_data_mean,
    }
    path = os.path.join(tf.test.get_temp_dir(), "test_human_pose_dataset.pkl")
    with open(path, "wb") as out:
      pickle.dump(data, out)
    with self.test_session() as sess:
      inputs, targets, lens, mean = datasets.create_human_pose_dataset(
          path, "train", 2, num_parallel_calls=1, shuffle=False, repeat=False)
      i1, t1, l1 = sess.run([inputs, targets, lens])
      i2, t2, l2 = sess.run([inputs, targets, lens])
      m = sess.run(mean)
      # Check the lengths.
      self.assertAllEqual([2, 1], l1)
      self.assertAllEqual([5], l2)
      # Check the mean.
      self.assertAllEqual(pose_data_mean, m)
      # Check the targets. The targets should not be mean-centered and should
      # be padded with zeros to a common length within a batch.
      self.assertAllEqual([[0, 0], [2, 2]], t1[:, 0, :])
      self.assertAllEqual([[2, 2], [0, 0]], t1[:, 1, :])
      self.assertAllEqual([[0, 0], [0, 0], [2, 2], [2, 2], [0, 0]], t2[:, 0, :])
      # Check the inputs. Each sequence should start with zeros on the first
      # timestep. Each sequence should be padded with zeros to a common length
      # within a batch. The mean should be subtracted from all timesteps except
      # the first and the padding.
      self.assertAllEqual([[0, 0], [-1, -1]], i1[:, 0, :])
      self.assertAllEqual([[0, 0], [0, 0]], i1[:, 1, :])
      self.assertAllEqual([[0, 0], [-1, -1], [-1, -1], [1, 1], [1, 1]],
                          i2[:, 0, :])

  def test_speech_dataset(self):
    with self.test_session() as sess:
      path = os.path.join(
          os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
          "test_data",
          "tiny_speech_dataset.tfrecord")
      inputs, targets, lens = datasets.create_speech_dataset(
          path, 3, samples_per_timestep=2, num_parallel_calls=1,
          prefetch_buffer_size=3, shuffle=False, repeat=False)
      inputs1, targets1, lengths1 = sess.run([inputs, targets, lens])
      inputs2, targets2, lengths2 = sess.run([inputs, targets, lens])
      # Check the lengths.
      self.assertAllEqual([1, 2, 3], lengths1)
      self.assertAllEqual([4], lengths2)
      # Check the targets. The targets should be padded with zeros to a common
      # length within a batch.
      self.assertAllEqual([[[0., 1.], [0., 1.], [0., 1.]],
                           [[0., 0.], [2., 3.], [2., 3.]],
                           [[0., 0.], [0., 0.], [4., 5.]]],
                          targets1)
      self.assertAllEqual([[[0., 1.]],
                           [[2., 3.]],
                           [[4., 5.]],
                           [[6., 7.]]],
                          targets2)
      # Check the inputs. Each sequence should start with zeros on the first
      # timestep. Each sequence should be padded with zeros to a common length
      # within a batch.
      self.assertAllEqual([[[0., 0.], [0., 0.], [0., 0.]],
                           [[0., 0.], [0., 1.], [0., 1.]],
                           [[0., 0.], [0., 0.], [2., 3.]]],
                          inputs1)
      self.assertAllEqual([[[0., 0.]],
                           [[0., 1.]],
                           [[2., 3.]],
                           [[4., 5.]]],
                          inputs2)

  def test_chain_graph_raises_error_on_wrong_steps_per_observation(self):
    with self.assertRaises(ValueError):
      datasets.create_chain_graph_dataset(
          batch_size=4,
          num_timesteps=10,
          steps_per_observation=9)

  def test_chain_graph_single_obs(self):
    with self.test_session() as sess:
      np.random.seed(1234)
      num_observations = 1
      num_timesteps = 5
      batch_size = 2
      state_size = 1
      observations, lengths = datasets.create_chain_graph_dataset(
          batch_size=batch_size,
          num_timesteps=num_timesteps,
          state_size=state_size)
      out_observations, out_lengths = sess.run([observations, lengths])
      self.assertAllEqual([num_observations, num_observations], out_lengths)
      self.assertAllClose(
          [[[1.426677], [-1.789461]]],
          out_observations)

  def test_chain_graph_multiple_obs(self):
    with self.test_session() as sess:
      np.random.seed(1234)
      num_observations = 3
      num_timesteps = 6
      batch_size = 2
      state_size = 1
      observations, lengths = datasets.create_chain_graph_dataset(
          batch_size=batch_size,
          num_timesteps=num_timesteps,
          steps_per_observation=num_timesteps/num_observations,
          state_size=state_size)
      out_observations, out_lengths = sess.run([observations, lengths])
      self.assertAllEqual([num_observations, num_observations], out_lengths)
      self.assertAllClose(
          [[[0.40051451], [1.07405114]],
           [[1.73932898], [3.16880035]],
           [[-1.98377144], [2.82669163]]],
          out_observations)

  def test_chain_graph_state_dims(self):
    with self.test_session() as sess:
      np.random.seed(1234)
      num_observations = 1
      num_timesteps = 5
      batch_size = 2
      state_size = 3
      observations, lengths = datasets.create_chain_graph_dataset(
          batch_size=batch_size,
          num_timesteps=num_timesteps,
          state_size=state_size)
      out_observations, out_lengths = sess.run([observations, lengths])
      self.assertAllEqual([num_observations, num_observations], out_lengths)
      self.assertAllClose(
          [[[1.052287, -4.560759, 3.07988],
            [2.008926, 0.495567, 3.488678]]],
          out_observations)

  def test_chain_graph_fixed_obs(self):
    with self.test_session() as sess:
      np.random.seed(1234)
      num_observations = 3
      num_timesteps = 6
      batch_size = 2
      state_size = 1
      observations, lengths = datasets.create_chain_graph_dataset(
          batch_size=batch_size,
          num_timesteps=num_timesteps,
          steps_per_observation=num_timesteps/num_observations,
          state_size=state_size,
          fixed_observation=4.)
      out_observations, out_lengths = sess.run([observations, lengths])
      self.assertAllEqual([num_observations, num_observations], out_lengths)
      self.assertAllClose(
          np.ones([num_observations, batch_size, state_size]) * 4.,
          out_observations)

if __name__ == "__main__":
  tf.test.main()