title: Is there a way to pass dictionary in tf.data.Dataset w/ tf.py_func?
body: <p>I'm using tf.data.Dataset in data processing and I want to do apply some python code with tf.py_func.</p>
  
  <p>BTW, I found that in tf.py_func, I cannot return a dictionary. Is there any way to do it or workaround?</p>
  
  <p>I have code which looks like below</p>
  
  <pre><code>def map_func(images, labels):
      """mapping python function"""
      # do something
      # cannot be expressed as a tensor graph
      return {
          'images': images,
          'labels': labels,
          'new_key': new_value}
  def tf_py_func(images, labels):
      return tf.py_func(map_func, [images, labels], [tf.uint8, tf.string], name='blah')
  
  return dataset.map(tf_py_func)
  </code></pre>
  
  <p>===========================================================================</p>
  
  <p>It's been a while and I forgot I asked this question.
  I solved it other way around and it was so easy that I felt I was almost a stupid. The problem was:</p>
  
  <ol>
  <li>tf.py_func cannot return dictionary.</li>
  <li>dataset.map can return dictionary.</li>
  </ol>
  
  <p>And the answer is: map twice.</p>
  
  <pre class="lang-py prettyprint-override"><code>def map_func(images, labels):
      """mapping python function"""
      # do something
      # cannot be expressed as a tensor graph
      return processed_images, processed_labels
  
  def tf_py_func(images, labels):
      return tf.py_func(map_func, [images, labels], [tf.uint8, tf.string], name='blah')
  
  def _to_dict(images, labels):
      return { 'images': images, 'labels': labels }
  
  return dataset.map(tf_py_func).map(_to_dict)
  </code></pre>