在 tensorflow 上遵循 ResNetV250 模型为我的二元分类模型运行迁移学习:值错误

原文标题Running transfer learning for my binary classification model following ResNetV250 model on tensorflow: Value error

我正在尝试将迁移学习(ResNetV250 和 EfficientnetB0)应用于我的二值图像分类模型,但在拟合模型时出现值错误。

我使用以下参数添加最后一层 ->layers.Dense(num_classes, activation=’sigmoid’, name=’output_layer’) 其中使用 num_classes = 1 并使用 loss=’binary_crossentropy 编译模型。

我收到以下错误:ValueError:logitslabels必须具有相同的形状,收到((None,2)vs(None,1))。

欢迎任何帮助/建议:

# Resnet 50 V2 feature vector
resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4"

# Original: EfficientNetB0 feature vector (version 1)
efficientnet_url = "https://tfhub.dev/tensorflow/efficientnet/b0/feature-vector/1"
-----------------------------------------------------------------------------------------
def create_model(model_url, num_classes=1):

  # Download the pretrained model and save it as a Keras layer
  feature_extractor_layer = hub.KerasLayer(model_url,
                                           trainable=False, # freeze the underlying patterns
                                           name='feature_extraction_layer',
                                           input_shape=IMAGE_SHAPE+(3,)) # define the input image shape
  
  # Create our own model
  model = tf.keras.Sequential([
    feature_extractor_layer, # use the feature extraction layer as the base
    layers.Dense(num_classes, activation='sigmoid', name='output_layer') # create our own output layer      
  ])

  return model
-----------------------------------------------------------------------------------------------------
# Create model
resnet_model = create_model(resnet_url, num_classes=train_data.num_classes)

# Compile
resnet_model.compile(loss='binary_crossentropy',
                     optimizer=tf.keras.optimizers.Adam(),
                     metrics=['accuracy'])

------------------------------------------------------------------------------------------------------
# Fit the model
resnet_history = resnet_model.fit(train_data,
                                  epochs=5,
                                  steps_per_epoch=len(train_data),
                                  validation_data=val_data,
                                  validation_steps=len(val_data),
                                  # Add TensorBoard callback to model (callbacks parameter takes a list)
                                  callbacks=[create_tensorboard_callback(dir_name="tensorflow_hub", # save experiment logs here
                                                                         experiment_name="resnet50V2")]) # name of log files

我得到的错误是:

Saving TensorBoard log files to: tensorflow_hub/resnet50V2/20220418-221123
Epoch 1/5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-196f7d30141c> in <module>()
      7                                   # Add TensorBoard callback to model (callbacks parameter takes a list)
      8                                   callbacks=[create_tensorboard_callback(dir_name="tensorflow_hub", # save experiment logs here
----> 9                                                                          experiment_name="resnet50V2")]) # name of log files

1 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, 2) vs (None, 1)).

原文链接:https://stackoverflow.com//questions/71917627/running-transfer-learning-for-my-binary-classification-model-following-resnetv25

回复

我来回复
  • Michael Hodel的头像
    Michael Hodel 评论

    对于二元分类,您不需要在每个类的Dense层中使用unit,因为那将是多余的。在这种情况下,你一开始就不能这样做,因为你使用了binary_crossentropyloss。试着调整layers.Dense(num_classes)layers.Dense(1)

    2年前 0条评论