【问题标题】:Invalid Argument Error / Graph Execution Error无效参数错误/图形执行错误
【发布时间】:2023-01-16 23:25:03
【问题描述】:

运行此 VGG 训练代码时出现多个错误(代码和错误如下所示)。我不知道是因为我的数据集还是其他原因。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics.pairwise import cosine_similarity
import os
import scipy

train_directory = 'sign_data/train' #To be changed
test_directory = 'sign_data/test' #To be changed

train_datagen = ImageDataGenerator(
    rescale = 1./255,
    rotation_range = 0.1,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    shear_range = 0.1
)

train_generator = train_datagen.flow_from_directory(
    train_directory,
    target_size = (224, 224),
    color_mode = 'rgb',
    shuffle = True,
    batch_size=32
    
)


test_datagen = ImageDataGenerator(
    rescale = 1./255,
)

test_generator = test_datagen.flow_from_directory(
    test_directory,
    target_size = (224, 224),
    color_mode = 'rgb',
    shuffle = True,
    batch_size=32
)

from tensorflow.keras.applications.vgg16 import VGG16   
vgg_basemodel = VGG16(include_top=True)

from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping

early_stopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)

vgg_model = tf.keras.Sequential(vgg_basemodel.layers[:-1])
vgg_model.add(tf.keras.layers.Dense(10, activation = 'softmax'))

# Freezing original layers
for layer in vgg_model.layers[:-1]:
    layer.trainable = False

vgg_model.compile(loss='categorical_crossentropy',
                  optimizer=tf.keras.optimizers.SGD(momentum=0.9, learning_rate=0.001, decay=0.01),
                  metrics=['accuracy'])

history = vgg_model.fit(train_generator,
              epochs=30,
              batch_size=64,
              validation_data=test_generator,
              callbacks=[early_stopping])

# finetuning with all layers set trainable

for layer in vgg_model.layers:
    layer.trainable = True

vgg_model.compile(loss='categorical_crossentropy',
                  optimizer=tf.keras.optimizers.SGD(momentum=0.9, lr=0.0001),
                  metrics=['accuracy'])

history2 = vgg_model.fit(train_generator,
              epochs=5,
              batch_size=64,
              validation_data=test_generator,
              callbacks=[early_stopping])

vgg_model.save('saved_models/vgg_finetuned_model')

第一个错误:无效参数错误

    InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-13-292bf57ef59f> in <module>()
     14               batch_size=64,
     15               validation_data=test_generator,
---> 16               callbacks=[early_stopping])
     17 
     18 # finetuning with all layers set trainable

    /usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/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 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)
    File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
      app.start()
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
      self._run_once()
    File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
      handle._run()
    File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
      handler_func(fileobj, events)
    File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events
      self._handle_recv()
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv
      self._run_callback(callback, msg)
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
      callback(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
      return self.dispatch_shell(stream, msg)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
      handler(stream, idents, msg)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
      user_expressions, allow_stdin)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
      interactivity=interactivity, compiler=compiler, result=result)
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
      if self.run_code(code, result):
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-13-292bf57ef59f>", line 16, in <module>
      callbacks=[early_stopping])
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step
      outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
      loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
      y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
      loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
      losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call
      return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1790, in categorical_crossentropy
      y_true, y_pred, from_logits=from_logits, axis=axis)
    File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5099, in categorical_crossentropy
      labels=target, logits=output, axis=axis)
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[32,10] labels_size=[32,128]
     [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_11227]

我在 google colaboratory 上运行这个。有没有我应该安装的模块?还是纯粹是代码本身的错误?

【问题讨论】:

  • 你在哪里定义 early_stopping ?
  • @AyazKhan 我更新了上面的代码。它在这部分:early_stopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)
  • 现在它显示相同的错误?
  • @AyazKhan 是的,它显示相同的错误

标签: python tensorflow keras


【解决方案1】:

我遇到了同样的错误并试图测试所有没有价值的东西,但我听说你必须使文件夹在里面数据集Dense 中的相同。

我不知道这是否会解决您的特定错误,但请尝试使用您的代码:

vgg_model.add(tf.keras.layers.Dense(10, activation = 'softmax'))

10 替换为训练数据集文件夹的数量,或者可以调用“类”。

【讨论】:

  • 谢谢,我用我正在使用的课程数量替换了 10,然后它就可以工作了。
【解决方案2】:

就我而言,原因是形状不兼容。我的模型采用 [batch_size, 784] 图像形状,但数据采用 [batch_size, 28, 28, 1] 形状。所以我很容易用 tf.reshape(x, [-1]) 修复它。

【讨论】:

    【解决方案3】:

    检查图像大小。 model.add(.., input_shape=(100,100,3)) 中定义的图像大小应与train_gererator 中的 target_size=(100,100)。并检查最后一个密集层中的神经元数量是否等于输出类别的数量。 顺便说一句,不需要安装任何其他模块。这是代码中的一些错误。

    【讨论】:

      【解决方案4】:

      我对这段代码有同样的问题:

          model = Sequential()
      
      # Add the first hidden layer with 1024 nodes and ReLU activation
      model.add(Dense(1024, activation='relu', 
                      name="hidden_layer_1"))
      
      # Add the second hidden layer with 512 nodes and ReLU activation
      model.add(Dense(512, activation='relu', 
                      name="hidden_layer_2"))
      
      # Add the third hidden layer with 256 nodes and ReLU activation
      model.add(Dense(256, activation='relu', 
                      name="hidden_layer_3"))
      
      # Add the fourth hidden layer with 100 nodes and ReLU activation
      model.add(Dense(100, activation='relu', 
                      name="hidden_layer_4"))
      
      # Add the output layer
      model.add(Dense(10, activation='softmax', 
                      name="output_layer"))
      
      # Create the optimizer
      optimizer = SGD(learning_rate=0.1)
      
      # Compile the model with the optimizer and accuracy as the metric
      model.compile(optimizer=optimizer, loss='mse', metrics=['accuracy'])
      

      解决方案是在开头添加一行:

      model = Sequential()
      model.add(layers.Flatten(input_shape=(32,32,3))) #this line
      

      【讨论】:

        猜你喜欢
        • 2023-02-17
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 2016-01-05
        • 2020-10-16
        • 2023-03-20
        • 1970-01-01
        相关资源
        最近更新 更多