0

The code is:

def _proc_and_batch(self, ds, batch_size):
      def _process_data(x_):
        img_ = tf.cast(x_['image'], tf.int32)
        img_.set_shape(self._img_shape)
        return pack(image=img_, label=tf.constant(0, dtype=tf.int32))

      ds = ds.map(_process_data, num_parallel_calls=tf.data.experimental.AUTOTUNE)    
      ds = ds.batch(batch_size, drop_remainder=True)
      ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
      return ds

How can _process_data be called without giving it an input? So, what's its input?

For reference, the code is taken from here:

https://github.com/hojonathanho/diffusion/blob/master/diffusion_tf/tpu_utils/datasets.py

Alessandro
  • 103
  • 3
  • 11
  • 2
    You are not calling that function. You are passing it as a reference in map function. It is the map function which calls _process_data and supplies input to it. See example of how a map function operates. https://www.w3schools.com/python/ref_func_map.asp – user47 Jun 04 '22 at 09:13
  • So the map function is using it, right? – Alessandro Jun 04 '22 at 09:14
  • Yes. Map function invokes _process_data. – user47 Jun 04 '22 at 09:19
  • Suggested reading: [Any difference between First Class Function and High Order Function](https://stackoverflow.com/q/10141124/4720957) – user47 Jun 04 '22 at 09:23

1 Answers1

1

You haven't called it yet. When you pass _process_data to ds.map(...) you're giving it the function and then it'll call the function itself with input latter. Try passing in _process_data() (with parentheses) to map, you'll get an error.

Tai Nguyen
  • 31
  • 1
  • 6