Using Keras ImageDataGenerator in a regression model

后端 未结 4 1567
别跟我提以往
别跟我提以往 2020-12-30 02:29

I want to use the

flow_from_directory

method of the

ImageDataGenerator

to generate training data for a

4条回答
  •  孤城傲影
    2020-12-30 03:01

    With Keras 2.2.4 you can use ".flow_from_dataframe" that solves what you want to do, allowing you to flow images from a directory for regression problems. You should store all your images in a folder and load a dataframe containing in one column the image IDs and in the other column the regression score (labels) and set "class_mode='other'" in ".flow_from_dataframe".

    Here you can find an example where the images are in "image_dir", the dataframe with the image IDs and the regression scores is loaded with pandas from "train file"

    train_label_df = pd.read_csv(train_file, delimiter=' ', header=None, names=['id', 'score'])
    
    train_datagen = ImageDataGenerator(rescale = 1./255, horizontal_flip = True,
                                       fill_mode = "nearest", zoom_range = 0.2,
                                       width_shift_range = 0.2, height_shift_range=0.2,
                                       rotation_range=30) 
    
    train_generator = train_datagen.flow_from_dataframe(dataframe=train_label_df, directory=image_dir, 
                                                  x_col="id", y_col="score", has_ext=True, 
                                                  class_mode="other", target_size=(img_width, img_height), 
                                                  batch_size=bs)
    

提交回复
热议问题