Skip to content
Advertisement

Is there a way to resize a batch of images?

I am trying to make code of object detection efficient. I currently have multiple sources, Below is the pseudo code:

  1. Take images via cv2.imread() or cap.read() from all sources one by one.

  2. Pre-process them one by one:

    a. Resize

    b. Normalise

  3. Make batch.

  4. Pass to model.

  5. Post process one by one.

Now, since images read my opencv are nothing just numpy tensors. So, is there a way to resize batch of N images at once rather than one by one?

Advertisement

Answer

It depends on your images, but yes.

Tensorflow provides a method to do so, and torch has another one, but images must be already in a tensor or numpy array of shape (batch_size, h, w, n_channels) so you need to stack your images before calling it, and you also need it to stack images of same size.

Images are probably of same size for capture, so it will works, but may be of different sizes for image files (depends on your problem) in such case, to use tensorflow’s resize, first you need to resize them to a common size, so it takes no sense: you can just resize them to your target size and call your model.

In my experience, using torch/tensorflow’s resize worths if you’re using GPU, otherwise using openCV ‘s resize is enough.

Consider also that resize operations are not available for all tensorflow/torch versions, so check if they are available in yours.

That said, if going with openCV’s resize, you should first create an empty batch and then fill it, since it is a good practice because of its efficiency. Here is some code example:

i=0
model_size = 224,224
batch_size = 32

batch=np.empty((batch_size,*model_size,3), style=np.uint8)

while True:
    img = read_image()
    i+=1
    batch[i%batch_size] = cv2.resize(img, model_size)  
    # if images are all the same size
    # and your resize is into the model, then just assign the image 
    if i % batch_size == 0:
        model.predict(pre_process(batch))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement