Move from predict_generator to predict

I am working on moving some code that is using the currently deprecated model.predict_generator. I would like some guidance on how to do that as I couldn’t easily find examples on how to make the change. Here is the relevant snippet of code

Get the data generator, make sure noise and shuffle are off.

    cand_datagen = DataGenerator(
        list_IDs=cands_to_eval,
        labels=[0] * len(cands_to_eval),
        shuffle=False,
        noise=False,
        batch_size=args.batch_size,
    )

    # Let's get predicting
    probs = model.predict_generator(
        generator=cand_datagen,
        verbose=1,
        use_multiprocessing=use_multiprocessing,
        workers=args.nproc,
        steps=len(cand_datagen),
    )

I know that the parameters for predict are different than the ones for predict_generator but I’m unclear on how or if I need these in moving to predict.

Any guidance is greatly appreciated

Hi @Anthony_Weaver, The data generator gets the specific amount of data instead of loading the whole dataset and the predict generator will generate predictions for the input samples from a data generator. whereas the model.predict Generates output predictions for the input samples.
Computation is done in batches. if batch_size is unspecified, batch_size will default to 32.This method is designed for batch processing of large numbers of inputs. It is not intended for use inside of loops that iterate over your data and process small numbers of inputs at a time.

Thank You.

I perhaps was not clear. I am working on some code I did not write and I am a noob when it comes to working with keras. I am attempting to update the code to move from Keras 2 to Keras 3 using this as a starting point: Migrating Keras 2 code to multi-backend Keras 3

The DataGenerator object was a sub-class of keras.utils.Sequence and I changed it to be PyDataSet because I saw somewhere that was now the proper way to do it.

I am stuck tackling the change from predict_generator to predict. I am doing that because predict_generator has been depracated ( Model.predict_generator is deprecated and will be removed in a future version. Please use Model.predict, which supports generators.)

I moved the use_multiprocessing and workers arguments to the cand_datagen call because the PyDataSet documentation indicates that’s where they should since they are not part of predict. Model.predict does not have a generator keyword but it does accept a PyDataSet. I have changed the predict line to look like:

probs = model.predict(cand_datagen, verbose=1, steps=len(cand_datagen))

The code executes without any errors, but it just seems to do nothing on the GPU. I can see stuff gets loaded into the GPU memory but it never actually seems to compute any predictions.

If this code is actually fine, then it might be the model. I had to manually make some changes to the model which was built in keras 2 in order to have it work with keras 3. Mostly I made changes to several places in the model that used SeparableConv2D and included attributes groups, kernel_initializer, kernel_regularizer, and kernel constraint. I removed all of these because it seems like they are no longer part of the SeprableConv2D class. Perhaps that was not the correct thing to do.

Finally I do not have access to the original data to try and train updated models using keras3, otherwise I would have.

Thank you for any guidance