TensorFlow "tf.image" functions on an image batch
<p>I would like to use this function in TensorFlow, however it operates on 3D tensors rather than 4D tensors: I have an outer dimension of batch_size. </p>

<pre><code>tf.image.random_flip_left_right(input_image_data)
</code></pre>

<p>That said, this function expects a tensor (image) of shape:</p>

<pre><code>(width, height, channels)
</code></pre>

<p>But I have multiple images such as: </p>

<pre><code>(batch_size, width, height, channels)
</code></pre>

<p>How could I map the random flip function to each image in my batch size and get as an output a tensor with the same 4D shape I already have? </p>

<p>My guess is that it would need a reshape at the entry of the function and a reshape after the function, but I am not sure whether or not this would break the data's structure and blend together images in the batch when applying the mirror. Moreover, this approach would do a single randomization on the whole batch rather than on a per-image basis. </p>

<p>Any suggestion appreciated! </p>

<p>You would have to use something like tf.pack and tf.unpack or tf.gather and tf.concatenate to convert from your 4D array to your 3D array.</p>

<p>Depending upon how your loading your data you can do the processing in numpy.  Another alternative is to process each image before you put it into a batch. </p>

<p>Let me know if you need an explanation for how tf.pack or the others work.</p>

python image-processing machine-learning computer-vision tensorflow