I suggest a workflow such as:
TARGET_HEIGHT = 500 TARGET_WIDTH = 500 image = tf.image.decode_jpeg(image_buffer, channels=3) image = tf.image.convert_image_dtype(image, dtype=tf.float32)
cropped_image has a constant tensor size and can then be thrown into a packet at random.
You can dynamically access the size of the decoded image using tf.shape(image) . You can do the calculations on the resulting subelements and then stitch them back using something like bbox_begin = tf.pack([bbox_h_start, bbox_y_start, 0]) . You just need to insert your own logic to determine the starting points of the crop and what you want to do if the image starts smaller than you want for your pipeline.
If you want to increase the size only if the image is smaller than your target size, you need to use tf.control_flow_ops.cond or the equivalent. But you can use the min and max operations to set the size of the cropping window so that you return the full image if it is smaller than the required size, and then unconditionally resize to 500x500. The cropped image will already be 500x500, so resizing should be an effective no-op.
source share