keras-video-generators icon indicating copy to clipboard operation
keras-video-generators copied to clipboard

Predict the trained model

Open pengweiguo123 opened this issue 4 years ago • 16 comments

How can I use the videoframe generator to create a test dataset? I see you have done it in your blog, but without the code. could you please upload that part of code? Thanks.

pengweiguo123 avatar Mar 20 '20 04:03 pengweiguo123

There is a get_test_generator() which returns a test generator. You need to use split_test argument in constructor call to give the proportion of test data, it works like split_val and get_validation_generator()

See code here: https://github.com/metal3d/keras-video-generators/blob/master/src/keras_video/generator.py#L278

e.g.:

# get 20% of data for test
gen = VideoFrameGenerator(glob_pattern='data/{classname}', split_test=.2)
test_gen = gen.get_test_generator()

metal3d avatar Mar 20 '20 09:03 metal3d

Hey,@metal3d Above answer was really helpful, I saw your blog on https://medium.com/smileinnovation/training-neural-network-with-image-sequence-an-example-with-video-as-input-c3407f7a0b0f. It was showing test accuracy along with images. Can you please guide how did you get them?

rehanpunjwani avatar Mar 21 '20 09:03 rehanpunjwani

The test accuracy is made by using split_test parameter to get a testing validator. Then I only get one batch from the testing generator and send it to model.predict method.

I will create a notebook with example for training and evaluation. I'm only a bit busy for a couple of days, sorry for that.

metal3d avatar Mar 24 '20 22:03 metal3d

@metal3d I have implemented your mobile net model with SlidingVideoGenerator. How can I test predictions on the frames of my own video outside of the dataset on an individual set of frames like frame (1-5), frame (2-6), ... and so on to see what model predicts the particular instance of the video? I would be grateful if you could help me with this.

rehanpunjwani avatar Mar 28 '20 17:03 rehanpunjwani

Hi! I've been using your generator and a modified version of your model and its been working amazingly well. I wanted to ask if the evaluation/test notebook is available yet. Something like the code you used to check your model in your blog would be extremely helpful. Or being able to get a prediction on individual video inputs.

BANANAPEEL202 avatar Jul 22 '20 04:07 BANANAPEEL202

@Rehan-Rehman-Punjwani Sorry for the delay, I'm very busy for a while. To make prediction, you need to build your batches and send them to the model. That's not covered by this repository, it's a common task with OpenCV and Keras. If I found some time, maybe I will make an article about that

@BANANAPEEL202 I will make a simple Notebook to show the usage of training / test, you're right, it's missing

metal3d avatar Jul 22 '20 07:07 metal3d

@metal3d. Thank you so much!

BANANAPEEL202 avatar Jul 22 '20 15:07 BANANAPEEL202

@metal3d Thank you for a fantastic notebook, great explanation, and detailed work!

Since you are super busy and did not have a chance (understandably) to create the notebook, I was wondering how to get the y_true_class value from the validation dataset out of your get_validation_generator?

Here is a snippet of your good work

train = VideoFrameGenerator(
    classes=classes,
    glob_pattern=glob_pattern,
    split=.2
)

valid= train.get_validation_generator()
y_pred = model.predict(valid)
y_predicted_class = np.argmax(y_pred, axis=1)

# how to get the `y_true `below ?
y_true_class = np.argmax(y_true, axis=1)

How can we get the y_true values in the last line above in order to predict the accuracy? where y_true is the actual label of the validation data.

for example to use in:

classification_report(y_true_class , y_predicted_class )

Thank you again! :)

LS4203 avatar Nov 09 '20 13:11 LS4203

@BANANAPEEL202

Does this work for you?

gen = VideoFrameGenerator(glob_pattern='data/{classname}', split_test=.2)
test_gen = gen.get_test_generator()

results = model.evaluate(test_gen , verbose=2)
print("The Accuracy score on the Train set is:\t{:0.3f}".format(results[1]))

LS4203 avatar Nov 09 '20 14:11 LS4203

@LS4203 It does. Thanks!

BANANAPEEL202 avatar Nov 09 '20 15:11 BANANAPEEL202

How to use generator for test data when test data is situated in different directory?

shreyanshbehani avatar Dec 24 '20 15:12 shreyanshbehani

There is a get_test_generator() which returns a test generator. You need to use split_test argument in constructor call to give the proportion of test data, it works like split_val and get_validation_generator()

See code here: https://github.com/metal3d/keras-video-generators/blob/master/src/keras_video/generator.py#L278

e.g.:

# get 20% of data for test
gen = VideoFrameGenerator(glob_pattern='data/{classname}', split_test=.2)
test_gen = gen.get_test_generator()

I get this weird issue:

"local variable 'nbtrain' referenced before assignment"

FinAminToastCrunch avatar Mar 02 '21 00:03 FinAminToastCrunch

I will add Codacy check and change the test system. I'm very busy for a while, sorry to not help a lot for now

metal3d avatar Mar 03 '21 16:03 metal3d

The problem is here: https://github.com/metal3d/keras-video-generators/blob/master/src/keras_video/generator.py#L169

I will fix this as soon as I can.

metal3d avatar Mar 05 '21 14:03 metal3d

@metal3d I have trained the model using VideoFrameGenerator, and now want this model to predict in realtime, how can I do that?

rohannaik-3 avatar Mar 09 '21 04:03 rohannaik-3

@rohannaik-3 the prediction is not convered by my package. The package is only a generator to train a model. If I'm not wrong, when you do image recognition, you don't use Image generator from keras package, it's the same for video generator :)

Everything depends on how you capture videos (from file, from webcam, from stream)

metal3d avatar Jul 07 '21 06:07 metal3d