I want to find a way to effectively do batch image cropping. The input image is the same. Each crop has different input offsets, height, and width.
Naive code:
img = np.zeros([100, 100, 3])
ofsets_x = np.array([10, 15, 18])
img_w = np.array([10, 12, 15])
ofsets_y = np.array([20, 22, 14])
img_h = np.array([14, 12, 16])
crops= []
for i in range(ofsets_x.shape[0]):
ofset_x = ofsets_x[i]
ofset_y = ofsets_y[i]
w = img_w[i]
h = img_h[i]
crop = img[ofsets_x:ofsets_x + w, ofsets_y:ofsets_y + h, :]
crops.append(crop)
Because of this works very slow both in numpy and tensorflow(in tensorflow I am resizing each crop in the end of the loop to the specific size with tf.image.resize). In tensorflow I have tried also tf.vectorized_map and tf.while_loop - didn't gave me any significant speed boost. All this > 20x more slow then in C++. Crop is a simple memcpy. It should be superfast especially with preallocated memory.
How to this faster in numpy or tensorflow?