为我的 CNN 运行微调模型:值错误
因此,我尝试在我的数据集上使用预先训练的模型,然后将其与我自己的 cnn 模型进行比较。但是,当我尝试做模型时,我立即看到错误。非常适合 ((None, 4, 4, 1) vs (None,))。这个错误从何而来?我是否应该编辑预调整 cnn.
我使用的模型是ResNET50,除了输入层改为128并且有2个输出外,没有任何修改。
欢迎任何帮助,
代码:
history = modelB.fit_generator(train_data,
validation_data = test_data,
epochs=5,
steps_per_epoch = 1714,)
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-89-89a7f1c1eb60> in <module>()
2 validation_data = test_data,
3 epochs=5,
----> 4 steps_per_epoch = 1714,)
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
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 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 4, 4, 1) vs (None,)).
So I am trying to use a pre-trained model on my data set to then compare it to my own cnn model. However, I see an error as soon as I try to do model. fit so much that ((None, 4, 4, 1) vs (None,)). Where is this error coming from? Am I supposed to edit the pre-tune cnn.
The model that I am using is ResNET50 with no modification except the input layer changed to 128 and there are 2 outputs.
Any help is welcome,
CODE:
history = modelB.fit_generator(train_data,
validation_data = test_data,
epochs=5,
steps_per_epoch = 1714,)
ERROR:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-89-89a7f1c1eb60> in <module>()
2 validation_data = test_data,
3 epochs=5,
----> 4 steps_per_epoch = 1714,)
2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
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 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((None, 4, 4, 1) vs (None,)).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题在于编译模型时使用的损失函数。
将编译替换为以下代码:
The Issue is with the loss function used when you compile the model.
Replace the compile with below code:
使用 tf.keras.utils.plot_model 打印模型的图形表示。输入和输出节点的数量不匹配。
Use
tf.keras.utils.plot_model
to print out a graphic representation of the model. you have a mismatch between the number of input and output nodes.将编译中的损失函数替换为SparseCategoricalCrossentropy即可纠正错误。
Replace the loss function in compile with
SparseCategoricalCrossentropy
then you can rectify the error.首先定义一个损失函数来编译,如下所示:
如果仍然出现错误,请为 Resnet 模型定义池化:
这对我有用
First define a loss function to compile like this:
If you still get an error , define pooling for Resnet model :
This works for me