Tensorflow加载图像,带有来自Dataframe的“值”

jv4diomz  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(123)

我想将图像及其对应值从Pandas DataFrame加载到Tensorflow数据集中,因此df['image_path']中的图像路径应该是我的X要素,df['followers_like_ratio']中的值应该是我最终想要预测的目标y值。
那么,如何将其加载到Tensorflow中?
我的数据:

j0pj023g

j0pj023g1#

我假设image_path是映像的完整路径,还假设followers_like_path中的wntry是表示类标签的STRINGS,您可以使用ImageDataGenerator.flow_from_datafame创建一个生成器,以便将数据加载到model.fit中。

from tensorflow.keras.preprocessing.image import ImageDataGenerator
target_size=(224,224) # set the size of the images
color_mode='rgb' # set the type of image
class_mode= 'categorical' # set the class mode
batch_size=32  # set the batch size
val_split=.2 # set % of images to use for validation
subset='training' # set to 'training', or 'valiatiom' or leave as None
train_gen=ImageDataGenerator(validation_split=val_split).flow_from_datafram(df, 
          x_col='image_path',
          y_col='followers_like_ratio',target_size=target_size,color_mode=color_mode,
          class_mode=class_mode, batch_size=batch_size,shuffle=True, seed=123,
          subset='training')
valid_gen= ImageDataGenerator(validation_split=val_split).flow_from_datafram(df, 
          x_col='image_path',
          y_col='followers_like_ratio',target_size=target_size,color_mode=color_mode,
          class_mode=class_mode, batch_size=batch_size,shuffle=True, seed=123,
          subset='validation')

如果你有一个test_df,你可以创建一个测试生成器。你可以使用sklearn train_test_split将你的原始数据框分离成train_df和test_df,如下所示

from sklearn.model_selection import train_test_split
train_size=.8 # set the % to use for training
train_df, test_df=train_test_split(df, train_size=train_size, shuffle=True, randoom_state=123)

现在在验证和训练生成器中使用train_df,在测试生成器中使用test_df

test_gen=ImageDataGenerator.flow_from_datafram(test_df,  x_col='image_path',
          y_col='followers_like_ratio',target_size=target_size,color_mode=color_mode,
          class_mode=class_mode, batch_size=batch_size, shuffle=False)
y_true=test_gen.labels

相关问题