为我的 CNN 运行微调模型:值错误

发布于 2025-01-11 22:10:21 字数 2969 浏览 0 评论 0原文

因此,我尝试在我的数据集上使用预先训练的模型,然后将其与我自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岛歌少女 2025-01-18 22:10:22

问题在于编译模型时使用的损失函数。

将编译替换为以下代码:

model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

The Issue is with the loss function used when you compile the model.

Replace the compile with below code:

model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
凉城已无爱 2025-01-18 22:10:22

使用 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.

乱了心跳 2025-01-18 22:10:22

将编译中的损失函数替换为SparseCategoricalCrossentropy即可纠正错误。

model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])

Replace the loss function in compile with SparseCategoricalCrossentropy then you can rectify the error.

model.compile(optimizer='adam',loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
乙白 2025-01-18 22:10:22

首先定义一个损失函数来编译,如下所示:

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate= 0.001 ),
          loss=tf.keras.losses.categorical_crossentropy,
          metrics=['accuracy'],)

如果仍然出现错误,请为 Resnet 模型定义池化:

model_r=tf.keras.applications.ResNet50V2(
    input_shape=(width,height,3),
    include_top=False,
    weights='imagenet',
    pooling='avg'
    )

这对我有用

First define a loss function to compile like this:

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate= 0.001 ),
          loss=tf.keras.losses.categorical_crossentropy,
          metrics=['accuracy'],)

If you still get an error , define pooling for Resnet model :

model_r=tf.keras.applications.ResNet50V2(
    input_shape=(width,height,3),
    include_top=False,
    weights='imagenet',
    pooling='avg'
    )

This works for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文