tensorflowkerasimage-recognitionimagenet

Keras/Tensorflow - Generate predictions in batch for imagenet (I get only one result back)


I am generating imagenet tags for all keyframes in a video with a single call and have this code:

 # all keras/tf/mobilenet imports
model_imagenet = MobileNetV2(weights='imagenet')

frames_list = []
for frame in frame_set:
    frame_img = frame.to_image()
    frame_pil = frame_img.resize((224,224), Image.ANTIALIAS)
    ts = int(frame.pts)
    x = image.img_to_array(frame_pil)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    frames_list.append(x)

print(len(frames_list))                

preds_list = model_imagenet.predict_on_batch(frames_list)
print("[*]",preds_list)

The result appears thus:

frames_list count: 125

and the predictions thus, one row of 1000 dimensions (imagenet classes), shouldn't it be 125 rows?:

[[1.15425530e-04 1.83317825e-04 4.28701424e-05 2.87547664e-05
                    :
  7.91769926e-05 1.30803732e-04 4.81895368e-05 3.06891889e-04]]

This is generating prediction for a single row in the batch. I have tried both predict and predict_on_batch with the same result.

How can I get a bulk prediction for say 200 frames at one go with Keras/Tensorflow/Mobilenet?


Solution

  • OK, here is how I solved it, hope this helps someone else:

    preds_list = model_imagenet.predict(np.vstack(frames_list),batch_size=32)
    print("[*]",preds_list)
    

    Please note the np.vstack and adjust the batch_size to whatever your computer is capable of.