【问题标题】:how can reslove : InvalidArgumentError: Graph execution error?reslove : InvalidArgumentError: Graph execution error 怎么办?
【发布时间】:2022-06-17 21:13:26
【问题描述】:

大家好,我是计算机视觉和分类方面的专家,我正在尝试使用带有 tensorflow 和 keras 的 cnn 方法训练模型,但我不断收到此代码下方的错误,谁能帮助我或至少给我一个和平建议?

model = keras.models.Sequential([
    keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu',input_shape=(IMG_HEIGHT,IMG_WIDTH,channels)),
    keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
    keras.layers.MaxPool2D(pool_size=(2,2)),
    keras.layers.BatchNormalization(axis=-1),

    keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
    keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),
    keras.layers.MaxPool2D(pool_size=(2,2)),
    keras.layers.BatchNormalization(axis=-1),

    keras.layers.Flatten(),
    keras.layers.Dense(512,activation='relu'),
    keras.layers.BatchNormalization() ,
    keras.layers.Dropout(rate=0.5),

    keras.layers.Dense(3,activation='softmax')

])

learning_rate = 0.001
    epochs=30
    opt= Adam(learning_rate=learning_rate , decay=learning_rate/(epochs*0.5))
    model.compile(loss='sparse_categorical_crossentropy',optimizer=opt,metrics=['accuracy'])


aug = ImageDataGenerator(
          rotation_range=10,
          zoom_range=0.15,
          width_shift_range=0.1,
          height_shift_range=0.1,
          shear_range=0.15,
          horizontal_flip= False,
          vertical_flip= False,
          fill_mode="nearest"
          )
          
    
    history = model.fit(aug.flow(X_train, y_train,batch_size=32), epochs=epochs,validation_data=(X_val,y_val) )

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-15-15df12cd6846> in <module>()
     11 
     12 
---> 13 history = model.fit(aug.flow(X_train, y_train,batch_size=32), epochs=epochs,validation_data=(X_val,y_val) )

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     53     ctx.ensure_initialized()
     54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:
     57     if name is not None:

InvalidArgumentError: Graph execution error:

Detected at node 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)

【问题讨论】:

  • y_train的形状是什么
  • y_train.shape (830,)

标签: python tensorflow keras deep-learning


【解决方案1】:

您只需确保您的标签从 0 到 2 从零开始,因为您的输出层有 3 个节点和一个softmax 激活函数,并且您使用的是sparse_categorical_crossentropy。这是一个工作示例:

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu',input_shape=(256, 256, 3)),
    tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.BatchNormalization(axis=-1),

    tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
    tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),
    tf.keras.layers.MaxPool2D(pool_size=(2,2)),
    tf.keras.layers.BatchNormalization(axis=-1),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512,activation='relu'),
    tf.keras.layers.BatchNormalization() ,
    tf.keras.layers.Dropout(rate=0.5),

    tf.keras.layers.Dense(3,activation='softmax')

])

learning_rate = 0.001
epochs=2
opt= tf.keras.optimizers.Adam(learning_rate=learning_rate , decay=learning_rate/(epochs*0.5))
model.compile(loss='sparse_categorical_crossentropy',optimizer=opt,metrics=['accuracy'])


aug = tf.keras.preprocessing.image.ImageDataGenerator(
          rotation_range=10,
          zoom_range=0.15,
          width_shift_range=0.1,
          height_shift_range=0.1,
          shear_range=0.15,
          horizontal_flip= False,
          vertical_flip= False,
          fill_mode="nearest"
          )
          

X_train = tf.random.normal((50, 256, 256, 3))
y_train = tf.random.uniform((50, ), maxval=3, dtype=tf.int32)
history = model.fit(aug.flow(X_train, y_train, batch_size=2), epochs=epochs)

使用虚拟数据作为真实数据的方向。

【讨论】:

    【解决方案2】:

    否则,如果您使用 Colab。请更改运行时类型,方法是选择运行时 -> 更改运行时类型 -> GPU

    【讨论】:

      【解决方案3】:

      同样的问题发生在我身上,所以你需要确保它们都是相同的图像纵横比(例如:1:1、4:3、16:9 等)。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2022-12-26
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 2012-03-18
      相关资源
      最近更新 更多