我正在训练一个模型来识别图像中的异物。 这是我的数据生成器:
train_data_gen = train_image_generator.flow_from_dataframe(
traincsv,
directory=basepath,
x_col='image_name',
y_col='class',
target_size=IMG_SHAPE,
color_mode='rgb',
class_mode='binary',
batch_size=BATCH_SIZE,
shuffle=True)
#save_to_dir='/content/drive/My Drive/Results')
validation_data_gen = validation_image_generator.flow_from_dataframe(
valcsv,
directory=valpath,
x_col='image_name',
y_col='class',
target_size=IMG_SHAPE,
color_mode='rgb',
class_mode='binary',
batch_size=BATCH_SIZE,
shuffle=True)
#save_to_dir='/content/drive/My Drive/Results')
我已加载Resnet并尝试进行转学。 这是模型的创建:
model = tf.keras.Sequential([
feature_extractor,
layers.Dense(2)
])
当我使用准确性指标进行编译时:
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
并尝试适应它:
history = model.fit(train_data_gen,
epochs=EPOCHS,
validation_data=validation_data_gen)
它成功运行并给出准确性结果。
但是当我更改“将度量标准编译为AUC”时
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['AUC'])
我得到一个错误:
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-909955559916> in <module>()
13 history = model.fit(train_data_gen,
14 epochs=EPOCHS,
---> 15 validation_data=validation_data_gen)
16
17 t=time.time()
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:543 train_step **
self.compiled_metrics.update_state(y, y_pred, sample_weight)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:411 update_state
metric_obj.update_state(y_t, y_p)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/metrics_utils.py:90 decorated
update_op = update_state_fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/metrics.py:2083 update_state
label_weights=label_weights)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/metrics_utils.py:351 update_confusion_matrix_variables
y_pred.shape.assert_is_compatible_with(y_true.shape)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py:1117 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 2) and (None, 1) are incompatible
有人可以协助解决该问题吗?
答案 0 :(得分:0)
由于您的问题只有两个类,因此应使用二进制交叉熵,并且模型的输出应为单个神经元:
model = tf.keras.Sequential([
feature_extractor,
layers.Dense(1)
])
model.compile(
optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['AUC']
)