【问题标题】:Keras flow_from_dataframeKeras flow_from_dataframe
【发布时间】:2019-02-24 17:55:28
【问题描述】:

我正在尝试使用 keras flow_from_dataframe 来将输入图像增强到我的网络(我正在使用最新的 keras_preprocessing 版本)。该网络有一个输入图像和一个图像作为标签。我有一个指定输入输出格式的 csv 文件:

Input,Output
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\1.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\2.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\3.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\4.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\5.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\6.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\1\7.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\1.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\104\1.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\104.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\104\10.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\104.JPG
E:\LTM\SCIE_DB\Dataset_Part1\Dataset\104\11.JPG,E:\LTM\SCIE_DB\Dataset_Part1\Label\104.JPG

我正在尝试通过以下代码将 csv 加载到数据框:

df=pd.read_csv(r"E:\LTM\SCIE_DB\dataset.csv",names=("Input","Output"),dtype={"Input":"str","Output":"str"})
df = df.astype(str)
print(df.dtypes)

datagen=image.ImageDataGenerator(rescale=1./255)
train_generator=datagen.flow_from_dataframe(dataframe=df, directory=None, x_col="Input", y_col="Output", class_mode="other", target_size=(32,32), batch_size=32,has_ext = True)

但是,我不断收到以下错误:

TypeError: y_col column/s must be numeric datatypes.

无论我在做什么,我都无法将 y_col 设置为 str 并且它始终是对象类型。有什么想法吗?

【问题讨论】:

  • 问题是str不是数字类型,y_col应该是整数或者浮点数,而不是字符串。
  • 据我了解文档,y_col 可以是字符串。来自:keras.io/preprocessing/imagey_col:字符串或列表,数据框中包含目标数据的列。
  • 我的意思是 y_col 的值可以是一个字符串,但它引用的列必须是数字,因为您将 class_mode 指定为“其他”。

标签: python keras


【解决方案1】:

您的 y_col 值属于“字符串”类型,但在您的代码中,您将其称为“数字”,因为您将 class_mode 指定为:

class_mode="other"

所以将课堂模式改为

class_mode="input"

因此您可以为 y_col 使用“字符串”值

【讨论】:

  • 虽然此代码可能会解决 OP 的问题,但最好添加上下文,或解释为什么这会解决 OP 的问题,以便将来的访问者可以从您的帖子中学习,并将这些知识应用于他们自己的问题。高质量的答案更有可能获得支持,并有助于提升 SO 作为平台的价值。
猜你喜欢
  • 2019-03-20
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 2021-01-04
  • 2021-10-10
  • 2019-07-06
相关资源
最近更新 更多