I am trying to make code of object detection efficient. I currently have multiple sources, Below is the pseudo code:
Take images via cv2.imread() or cap.read() from all sources one by one.
Pre-process them one by one:
a. Resize
b. Normalise
Make batch.
Pass to model.
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?
CodePudding user response:
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