在TensorFlow上的RESNETV250模型之后,我的二进制分类模型的运行传输学习:值错误
我正在尝试将转移学习(ResnETV250&效率网络B0)应用于我的二进制图像分类模型,但在拟合模型时遇到了值误差。
我添加了最后一层,其中包括以下参数 - > layers.dense(num_classes,activation ='sigmoid',name ='output_layer')
其中使用num_classes = 1
,并使用lose ='binary_crossentropy
编译模型/代码>。
我有以下错误:
valueerror:
logits
和标签
必须具有相同的形状,接收到 ((无,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)).
I am trying to apply transfer learning (ResNetV250 & EfficientnetB0) to my binary image classification model but got a Value Error while fitting the model.
I add the final layer with the following parameter ->layers.Dense(num_classes, activation='sigmoid', name='output_layer')
where use num_classes = 1
and compile the model using loss='binary_crossentropy
.
I got the following error:
ValueError:
logits
andlabels
must have the same shape, received
((None, 2) vs (None, 1)).
Any help/suggestions are welcome:
# 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
And the error I got:
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)).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于二进制分类,您无需在每个类的
密集
层中使用单位
,因为这是多余的。在这种情况下,您首先无法这样做,因为您使用binary_crossentropy
损失。尝试调整layers.dense(num_classes)
tolayers.dense(1)
。For binary classification you don't need to use a
unit
in theDense
layer for each class, since that would be redundant. And in this case you can't do so in the first place, since you use thebinary_crossentropy
loss. Try adjustinglayers.Dense(num_classes)
tolayers.Dense(1)
.