0

For a project, I am using tf.data.Dataset to write the input pipeline. The input is an image RGB The label is a list of 2D coordinates of of objects in the image that used to generate a heatmap

Here is a MWE to reproduce the problem.

 def encode_images(image, label):
        """

        Parameters
        ----------
        image
        label

        Returns
        -------

        """
        # load image
        # here the normal code
        # img_contents = tf.io.read_file(image)
        # # decode the image
        # img = tf.image.decode_jpeg(img_contents, channels=3)
        # img = tf.image.resize(img, (256, 256))
        # img = tf.cast(img, tf.float32)

        # this is just for testing
        image = tf.random.uniform(
            (256, 256, 3), minval=0, maxval=255, dtype=tf.dtypes.float32, seed=None, name=None
        )
        return image, label

    def generate_heatmap(image, label):
        """

        Parameters
        ----------
        image
        label

        Returns
        -------

        """

        start = 0.5
        sigma=3
        img_shape = (image.shape[0] , image.shape[1] )
        density_map = np.zeros(img_shape, dtype=np.float32)
        for center_x, center_y in label[0]:
            for v_y in range(img_shape[0]):
                for v_x in range(img_shape[1]):
                    x = start + v_x
                    y = start + v_y
                    d2 = (x - center_x) * (x - center_x) + (y - center_y) * (y - center_y)
                    exp = d2 / (2.0 * sigma**2)
                    if exp > math.log(100):
                        continue
                    density_map[v_y, v_x] = math.exp(-exp)
        return density_map


    X = ["img1.png", "img2.png", "img3.png", "img4.png", "img5.png"]
    y = [[[2, 3], [100, 120], [100, 120]],
         [[2, 3], [100, 120], [100, 120], [2, 1]],
         [[2, 3], [100, 120], [100, 120], [10, 10], [11, 12]],
         [[2, 3], [100, 120], [100, 120], [10, 10], [11, 12], [10, 2]],
         [[2, 3], [100, 120], [100, 120]]
         ]
    dataset = tf.data.Dataset.from_tensor_slices((X, tf.ragged.constant(y)))
    dataset = dataset.map(encode_images, num_parallel_calls=8)
    dataset = dataset.map(generate_heatmap, num_parallel_calls=8)
    dataset = dataset.batch(1, drop_remainder=False)

The problem is that in generate_heatmap() function, I have used numpy array to modify the elements by indices which is ~ not possible in tensorflow. I try to iterate over the label tensor which is not possible in tensorflow till now. The other things is that the eager mode seems not enabled in tf.data.Dataset!! Any suggestion to deal with that! I think in pytorch such code can be done quickly without suffering :) !

LearnToGrow
  • 1,656
  • 6
  • 30
  • 53
  • Don't condemn me if I am wrong, but I believe it's tf.executing_eagerly(). If you use Jupyter or Colab, it's automatically enabled but that's how you manually enable it. – Jacob Sep 05 '20 at 05:01
  • 1
    Eager execution does not work in the map function. – Aniket Bote Sep 05 '20 at 05:15
  • @AniketBote any deal with this ? – LearnToGrow Sep 05 '20 at 05:17
  • to wrap your python or numpy function in TensorFlow operation,try using [tf.py_function](https://www.tensorflow.org/api_docs/python/tf/py_function) – Shubham Shaswat Sep 05 '20 at 05:39
  • Your `generate_heatmap` function contains errors. See [this](https://colab.research.google.com/drive/1eqVf7HNCU4QGPkkjU-aFkmOXWkiAtBD9?usp=sharing) – Aniket Bote Sep 05 '20 at 05:52
  • @AniketBote, in your example you can just convert label to a list and change label[0] in the generate_heatmap to label. In your example, the eager mode is activated – LearnToGrow Sep 05 '20 at 12:21

0 Answers0