【问题标题】:InvalidArgumentError: Incompatible shapes: [40,5] vs. [40,4]InvalidArgumentError:不兼容的形状:[40,5] 与 [40,4]
【发布时间】:2021-12-29 20:14:34
【问题描述】:

我正在构建一个带有边界框的对象检测模型,构建成功后,我想将它与 vgg16 分类器合并。

我已经用谷歌搜索了错误。但是还是不能调试。

我知道 40 是我的 batch_size,5 是我的标签数量,4 是我的模型的输出。

下面是我的模型:

vgg16_model = keras.applications.vgg16.VGG16()
model_1 = Sequential()
for layer in vgg16_model.layers[:-4]:
  model_1.add(layer)
for layer in model_1.layers:
  layer.trainable = False

flatten = model_1.output
flatten = Flatten()(flatten)
bboxhead = Dense(128,activation="relu")(flatten)
bboxhead = Dense(64,activation="relu")(bboxhead)
bboxhead = Dense(32,activation="relu")(bboxhead)
bboxhead = Dense(4,activation="relu")(bboxhead)
box_model = Model(inputs = model_1.input,outputs = bboxhead)

数据生成器:

batch_size = 40

train_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2,
                                   rotation_range=40, horizontal_flip=True,
                                   fill_mode='nearest')

train_gen = train_datagen.flow_from_directory(path, target_size=(224,224),
                    class_mode='categorical', batch_size=batch_size, 
                                              subset='training')

val_gen = train_datagen.flow_from_directory(path, target_size=(224,224),
                class_mode='categorical', batch_size=batch_size,
                                            subset='validation')

我的模特:

from tensorflow.keras.optimizers import Adam

opt = Adam(1e-4)

box_model.compile(loss='mse',optimizer=opt)

steps, val_steps = train_gen.n/batch_size, val_gen.n/batch_size
num_epochs = 30

history = box_model.fit(train_gen,validation_data=val_gen,epochs=30,verbose=1)

错误信息是:

Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: closure mismatch, requested ('self', 'step_function'), but source function had ()
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING: AutoGraph could not transform <function Model.make_train_function.<locals>.train_function at 0x7efbbdd35b90> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: closure mismatch, requested ('self', 'step_function'), but source function had ()
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-78-48e34ab17b66> in <module>()
      1 #history = box_model.fit(train_gen, validation_data=val_gen, epochs=num_epochs,steps_per_epoch=steps, validation_steps=val_steps)
      2 
----> 3 history = box_model.fit(train_gen,validation_data=val_gen,epochs=30,verbose=1)

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)
     57   try:
     58     ctx.ensure_initialized()
---> 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:

InvalidArgumentError:  Incompatible shapes: [40,5] vs. [40,4]
     [[node mean_squared_error/SquaredDifference
 (defined at /usr/local/lib/python3.7/dist-packages/keras/losses.py:1204)
]] [Op:__inference_train_function_15076]

Errors may have originated from an input operation.
Input Source operations connected to node mean_squared_error/SquaredDifference:
In[0] model_8/dense_27/Relu (defined at /usr/local/lib/python3.7/dist-packages/keras/backend.py:4867)   
In[1] IteratorGetNext (defined at /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:866)

Operation 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-78-48e34ab17b66>", line 3, in <module>
>>>     history = box_model.fit(train_gen,validation_data=val_gen,epochs=30,verbose=1)
>>> 
>>>   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 1216, in fit
>>>     """The logic for one evaluation step.
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function
>>>     - A TensorFlow tensor, or a list of tensors
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function
>>>     validation_batch_size=None,
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step
>>>     validation_data=None,
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 810, in train_step
>>>     """Runs a single training step."""
>>> 
>>>   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__
>>>     call_fn = tf.__internal__.autograph.tf_convert(self.call, tf.__internal__.autograph.control_status_ctx())
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call
>>>     ag_fn = tf.__internal__.autograph.tf_convert(self.fn, tf.__internal__.autograph.control_status_ctx())
>>> 
>>>   File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1204, in mean_squared_error

【问题讨论】:

    标签: python machine-learning deep-learning classification object-detection


    【解决方案1】:

    问题很简单,因为最后一个必须是这样的

    bboxhead = Dense(5,activation="relu")(bboxhead)
    

    bc 输出的数量必须与您要分类的类的数量相同

    【讨论】:

    • 但它应该是 4 吗?因为我的目标是得到 4 个值,x、y、盒子的高度和宽度?或者我可以拒绝第五个值?
    • 是的,只需将一个类从您的输入中排除,因此您不需要输入一个包含 5 个值的数组,而是需要决定将哪一个留在外面,然后使用包含 4 个值的数组。我的意思是,如果你真的想要 4,那么这是要走的路,但我认为这会增加它的复杂性,就像我说你必须选择要跳过的输入一样
    猜你喜欢
    • 2021-12-31
    • 2021-09-18
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多