1

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?

Santino
  • 776
  • 2
  • 11
  • 29

2 Answers2

2

ImageNet is a popular database which consists of 1000 different categories.

The dimension of 1000 is natural and to be expected, since for one image the softmax outputs a probability for each of the 1000 classes.

EDIT: For multiple image predictions, you should use predict_generator(). In addition, as of TensorFlow 2.0, if you use the Keras backend, predict_generator() has been deprecated in favor of simple predict, which also allows input data as generators.

E.g. : (from How to use predict_generator with ImageDataGenerator?) :

test_datagen = ImageDataGenerator(rescale=1./255)
#Modify the batch size here
test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=(200, 200),
        color_mode="rgb",
        shuffle = False,
        class_mode='categorical',
        batch_size=1)

filenames = test_generator.filenames
nb_samples = len(filenames)

predict = model.predict_generator(test_generator,steps = nb_samples)

Please bear in mind that it will be highly unlikely to have a lot of predictions at once, since it is constrained to the memory of the video card.

Also, note the difference between predict and predict_on_batch: What is the difference between the predict and predict_on_batch methods of a Keras model?

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
0

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.

Santino
  • 776
  • 2
  • 11
  • 29
  • 1
    Great! Please also note that there is a difference behind the curtains between predict_on_batch and predict https://stackoverflow.com/questions/44972565/what-is-the-difference-between-the-predict-and-predict-on-batch-methods-of-a-ker – Timbus Calin Feb 15 '20 at 13:17