amaiya/ktrain

Image generator for only validation set?

nburner96 opened this issue · 1 comments

Is it possible to use images_from_df to generate a single set of images for independent validation instead of outputting a tuple of a training and validation set?

I am trying to validate my model on a separate set of images from those that were used as validation images during the training phase.

Basically, instead of this:

(train_img, val_img, preproc) = images_from_df(train_df=train, image_column='id', label_columns='DIFF',
                                                       directory=img_dir, suffix='.tif', 
                                                       val_df=val, is_regression=True, target_size=dim, color_mode='rgb')

I am looking for something like this:

val_new = images_from_df(train_df=img_df, image_column='id', label_columns='DIFF',
                                                       directory=img_dir, suffix='.tif', color_mode='rgb')

ktrain is a lightweight wrapper to keras. So, you can use the Keras ImageDataGenerator object stored in the preproc object:

preproc.datagen
# <keras.preprocessing.image.ImageDataGenerator at 0x7f858f7c94f0>

You should be able to use it to load the data however you want and then feed the results to learner.validate or learner.evaluate:

val_new = preproc.datagen.flow_from_dataframe(
                  val_df,
                  directory=d,
                  x_col=image_column,
                  y_col=label_columns,
                  target_size=target_size,
                  class_mode="other",
                  shuffle=False,
                  interpolation="bicubic",
                 color_mode='rgb',
        )
learner.evaluate(test_data=val_new)

The model expects the targets in the dataframe to be one-hot or multi-hot encoded, I believe. You can examine what images_from_df is doing to the target if the target (or other columns) in your dataframe need to be transformed.