模型的 flow_from_directory 拟合产生 ValueError:输入 0 与图层模型不兼容

2024-03-10

我有以下模型:

我尝试使用 ImageDataGenerator 与 flow*_*from_directory 和 fit_generator 来拟合模型,但是出现以下错误:

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 256, 256, 1), found shape=(None, 400, 400, 1)

我使用了正确的target_size,所以我不知道为什么会出现错误。我的代码如下:

model_merged.compile(loss='categorical_crossentropy',
          optimizer="adam",
          metrics=['acc'])
train_datagen =ImageDataGenerator(rescale=1./255, validation_split=0.25)

#training data
train_generator = train_datagen.flow_from_directory(
    '/kaggle/working/images/',  # Source directory
    target_size=(256, 256),  # Resizes images
    batch_size=batch_size,
    class_mode='categorical',subset = 'training')

epochs = epochs
#Testing data
validation_generator = train_datagen.flow_from_directory(
    '/kaggle/working/images/',
    target_size=(256, 256),
    batch_size=batch_size,
    class_mode='categorical',
    subset='validation') # set as validation data
#Model fitting for a number of epochs
history = model_merged.fit_generator(
  train_generator,
  steps_per_epoch=steps_train,
  epochs=epochs,
  validation_data = validation_generator,
  validation_steps = steps_val,
  verbose=1)

Update

batch_size = 32
epochs = 32
steps_train = 18
steps_val = 3
img_height = 256
img_width = 256
data_dir='/kaggle/working/images/'
model_merged.compile(loss='categorical_crossentropy',
          optimizer="adam",
          metrics=['acc'])
train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

# scale pixel value between 0 and 1
normalization_layer = tf.keras.layers.Rescaling(1./255)
reshape_layer = Reshape((-1,256,256))
resize_layer = Resizing(1,256)
permute_layer = Permute((2,3,1))

train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y))
train_ds = train_ds.map(lambda x, y: (reshape_layer(x), y))
val_ds = val_ds.map(lambda x, y: (reshape_layer(x), y))
train_ds = train_ds.map(lambda x, y: (resize_layer(x), y))
val_ds = val_ds.map(lambda x, y: (resize_layer(x), y))
train_ds = train_ds.map(lambda x, y: (permute_layer(x), y))
val_ds = val_ds.map(lambda x, y: (permute_layer(x), y))


AUTOTUNE = tf.data.AUTOTUNE

train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

history = model_merged.fit(
  train_ds,
  steps_per_epoch=steps_train,
  epochs=epochs,
  validation_data = val_ds,
  validation_steps = steps_val,
  verbose=1)

错误与上面的错误相同。我添加了一些新图层,以便将输入图层从 (None,256,256,3) 更改为 (None,256,256,1),因为这是最初的错误,但它仍然不起作用。我不确定错误来自哪里,因为训练和验证数据集的尺寸现在是正确的。

Update 2 new model I removed the concatenation layer from the merged model, since I want the output of model A to be passed to the input of model B, however, even with the new merged model the error still appears.


您是否愿意使用更现代的https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory

如果是,这是一个接近您的代码的工作示例

epochs = 32
steps_train = 18
steps_val = 3
img_height = 256
img_width = 256
data_dir='/kaggle/working/images/'
model_merged.compile(loss='categorical_crossentropy',
optimizer="adam",
metrics=['acc'])
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.25,
subset="training",
seed=123,
image_size=(img_height, img_width),
label_mode="categorical",
batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.25,
subset="validation",
seed=123,
image_size=(img_height, img_width),
label_mode="categorical",
batch_size=batch_size)

# scale pixel value between 0 and 1
normalization_layer = tf.keras.layers.Rescaling(1./255)
reshape_layer = Reshape((-1,256,256))
resize_layer = Resizing(1,256)
permute_layer = Permute((2,3,1))

train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y))
train_ds = train_ds.map(lambda x, y: (reshape_layer(x), y))
val_ds = val_ds.map(lambda x, y: (reshape_layer(x), y))
train_ds = train_ds.map(lambda x, y: (resize_layer(x), y))
val_ds = val_ds.map(lambda x, y: (resize_layer(x), y))
train_ds = train_ds.map(lambda x, y: (permute_layer(x), y))
val_ds = val_ds.map(lambda x, y: (permute_layer(x), y))


AUTOTUNE = tf.data.AUTOTUNE

train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

history = model_merged.fit(
train_ds,
steps_per_epoch=steps_train,
epochs=epochs,
validation_data = val_ds,
validation_steps = steps_val,
verbose=1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

模型的 flow_from_directory 拟合产生 ValueError:输入 0 与图层模型不兼容 的相关文章

随机推荐

  • 如果我尝试打开 PWM 引脚,Windows 10 Iot Core 应用程序会崩溃

    我想为我的蜂鸣器打开一个 PWM 引脚 但如果我尝试打电话给pwmController OpenPin 6 方法 应用程序崩溃并显示System Runtime InteropServices SEHException 我已经仔细检查过示例
  • 尝试复制大文件时出现 NIO 错误

    我有将文件复制到另一个位置的代码 public static void copyFile String sourceDest String newDest throws IOException File sourceFile new Fil
  • 如何获取终端结果并设置 JTextArea 来读取终端?

    我最近完成了一个 GUI 用户可以在其中输入标准 并获得符合所述条件的结果 该程序的工作结果明智 但我无法让我的 GUI 中的文本字段读取我的终端窗口结果 我的 GUI 代码如下 package project205 import java
  • iOS 静默推送通知仅在连接到 xcode 时有效

    我有一个奇怪的问题 我最近将 Xcode 更新到 6 1 1 以对我之前使用 Xcode 5 发布的应用程序进行更改 运行良好 现在 由于某种原因 当我使用新的 Xcode 测试后台静默推送通知时 它只有在我的 iPhone 连接到 Mac
  • 在 Dart 中合并两个对象列表

    我有具有参数名称和参数计数器的对象 这些对象存储在列表中 我的列表中的某些项目具有重复的参数 名称 我想删除列表内的重复项 并将该重复项的计数器添加到重复对象参数中 class Person Person this name this co
  • 对于大输入返回负数的阶乘函数

    我的阶乘函数似乎适用于 1 到 6 之间的数字 但不适用于大于 6 的数字 例如从 21 开始 结果是否定的 我不明白为什么 这是我的功能 factorial Int gt Int factorial 0 1 factorial 1 1 f
  • Apache Flink 检查点卡住

    我们正在运行一个 ListState 介于 300GB 到 400GB 之间的作业 并且有时该列表可能会增加到数千 在我们的用例中 每个项目都必须有自己的 TTL 因此我们使用 S3 上的 RocksDB 后端为此 ListState 的每
  • for 循环后的 Python 语法错误(在解释器中)

    我正在从控制台运行一些 python 代码 粘贴 并得到意外的结果 代码如下所示 parentfound False structfound False instruct False wordlist fileHandle open con
  • 如何获取 ImageView 中 Drawable 的尺寸? [复制]

    这个问题在这里已经有答案了 检索 ImageView 中 Drawable 尺寸的最佳方法是什么 My ImageView有一个初始化方法 我在其中创建ImageView private void init coverImg new Ima
  • 在 C++ 中使用 new 创建引用

    我有以下代码 似乎用 new 创建引用是可以的 但是当用 new 创建对象时 当我尝试重新收集分配的内存时 它会崩溃 float f new float 1 3 delete f float f1 new float delete f1 我
  • “<-”是否意味着在Haskell中分配变量?

    刚开始Haskell 据说Haskell中除了IO包之外的所有东西都是 不可变的 那么当我将名称绑定到某个东西时 它总是不可变的 问题 如下 Prelude gt let removeLower x c c lt x c elem A Z
  • Web API 序列化从小写字母开始的属性

    如何配置要使用的 Web API 的序列化camelCase 从小写字母开始 属性名称而不是PascalCase就像 C 中一样 我可以在整个项目的全球范围内进行吗 如果您想更改 Newtonsoft Json 又名 JSON NET 中的
  • 为重复的索引值添加后缀

    这是一个 df 0 01 0 029064 0 01 0 032876 0 01 0 040795 0 02 0 027003 0 02 0 0315 需要将其与另一个框架连接 但出现错误 无法从重复轴重新索引 我想要的是这样的 df 0
  • vs2015不断添加project.lock.json到tfs

    我的文件夹结构是 解决方案文件夹 tfignore 文件 每个项目的文件夹 但 vs2015 继续将我的 project lock json 文件包含在源代码管理中 在我的 tfignore 文件中 我添加了 project lock js
  • C 中的 case 标签不会减少为整数常量?

    我正在开发一个游戏 我运行了我的代码并收到错误 case 标签不会减少为整数常量 我想我知道这意味着什么 但是我该如何解决它呢 这是我的代码 include
  • 如何将列表列表从宽变为长

    我有一个具有共同结构的列表列表 require data table l lt list a1 list b data table rnorm 3 c data table rnorm 3 d data table rnorm 3 a2 l
  • C++ 与 Python 精度

    尝试查找 num num 的前 k 位数字的问题我用 C 和 Python 编写了相同的程序 C long double intpart num f digit k cin gt gt num gt gt k f digit pow 10
  • 元组比较

    我有一本定义如下的字典 d date tuple date open tuple open close tuple close min tuple min max tuple max MA tuple ma 这些元组中的每一个都包含一个值列
  • 在 iOS 中实现 Google 自定义搜索 API

    我浏览了几个链接 以便找到在 ios 应用程序中实现 google customsearchapi 的正确步骤 并在此过程中花费了大约 6 7 个小时 Links https developers google com custom sea
  • 模型的 flow_from_directory 拟合产生 ValueError:输入 0 与图层模型不兼容

    我有以下模型 我尝试使用 ImageDataGenerator 与 flow from directory 和 fit generator 来拟合模型 但是出现以下错误 ValueError Input 0 is incompatible