python - 多任务分类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)
解决方案
根据教程(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
请注意,这应该只是给你的想法。我没有测试它,所以它可能会抛出一两个错误。
推荐阅读
- r - 变异的R问题
- swift - 将图像保存到 .documentsDirectory,并作为字符串存储在本地领域中的路径
- c++ - 二分查找的终点
- python - Pandas(本机)是否支持 mypy 类型提示?
- canvas - 是否可以在两个形状之间应用 clip() 或 globalCompositeOperation(),然后停止剪辑/合成?
- python - 如何在不知道有多少页面的情况下抓取所有页面
- php - 如何在php hash_pbkdf2中使用字节数组类型的nodejs crypto pbkdf2 salt
- html - 使用 after 伪类选择器后如何删除多余的元素?
- javascript - NodeJS Mongoose - 不从异步函数返回值
- c# - RichTextBox Select + SelectionColor 不起作用