首页 > 解决方案 > 多任务分类CNN

问题描述

我有一个像这样构造的df,我有5列,我必须执行多任务或多输出分类

Daytime     Environment   Filename      Weather     
day         wet           2018-10.png   light_fog   [Example of one row]

我的问题是当我从数据框完成流程时,我不知道如何使用 tf.data.dataset 来构建数据集。有人建议我使用 TFRecords,但我从未使用过它。我怎么能不使用它?

train_data_gen = ImageDataGenerator(rescale=1. / 255)

train_gen = train_data_gen.flow_from_dataframe(train_df,
                                               directory=dataset_dir,
                                               x_col="Filename",
                                               y_col=["Daytime", "Weather", "Environment"],
                                               class_mode="multi_output",
                                               target_size=(img_size, img_size),
                                               batch_size=batch_size,
                                               shuffle=True,
                                               seed=SEED)

标签: pythondataframetensorflowkerasconv-neural-network

解决方案


根据教程(https://www.tensorflow.org/tutorials/load_data/pandas_dataframe#with_tfdata),你应该能够做类似的事情:

filenames = df.pop('Filename')
feature_names = ["Daytime", "Weather", "Environment"]
features = df[numeric_feature_names]

shuffle_buffer_size = len(df.index) # Or whatever shuffle size you want to use
ds = tf.data.Dataset.from_tensor_slices((filenames, features))
ds = ds.shuffle(buffer_size=shuffle_buffer_size, reshuffle_each_iteration=True)
# Performs the function for each pair of a filename and its corresponding features
ds = ds.map(prepare_image) 
dataset = ds.batch(batch_size)
# Prefetch is just for optimization
dataset = ds.prefetch(buffer_size=1)

def prepare_image(filepath, features):
    # You can do whatever transformations in here.
    file_content = tf.io.read_file(image_file_path)
    image = tf.io.decode_png(file_content)
    return image, features

请注意,这应该只是给你的想法。我没有测试它,所以它可能会抛出一两个错误。


推荐阅读