使用类别交叉熵值的多类别分类错误:形状(3,1)和(1,3)不兼容



我正在22500张图像上构建一个多类分类器。

标签有3个类别-0,1,2

我有一个热编码的y标签如下:

y_train = tf.one_hot(y_train,3)
y_test = tf.one_hot(y_test,3)
y_val = tf.one_hot(y_val,3)

由于数据的大小很大,我使用tf.data.Dataset对象来预处理数据。我已经使用dataset.zip 压缩了数据和标签

#creating zipped tuples ofdata and label
data_set_train = tf.data.Dataset.zip((X_train , y_train))
data_set_test = tf.data.Dataset.zip((X_test , y_test))
data_set_val = tf.data.Dataset.zip((X_val , y_val))

并应用了预处理函数


def pre_process(x,y):
x_norm = (x - mean_Rot_MIP) / Var_Rot_MIP
# Stacking along the last dimension to avoid having to move channel axis
x_norm_3ch = tf.stack((x_norm, x_norm, x_norm), axis=-1)
x_norm_3ch = tf.reshape(x_norm_3ch, [1,224,224,3])
return x_norm_3ch , y

#creating dataset iterable with all transaformations
X_train1 = data_set_train.map(pre_process)
X_test1 = data_set_test.map(pre_process)
X_val1 = data_set_val.map(pre_process)

数据集对象包含数据张量和y标签张量的元组,如:

(<tf.Tensor: shape=(1, 224, 224, 3), dtype=float64, numpy=
array([[[[-1.02143877, -1.02143877, -1.02143877],
[-1.02143877, -1.02143877, -1.02143877],
[-1.02143877, -1.02143877, -1.02143877],
...,
[-1.02143877, -1.02143877, -1.02143877],
[-1.02143877, -1.02143877, -1.02143877],
[-1.02143877, -1.02143877, -1.02143877]]]])>,
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 0., 0.], dtype=float32)>)

每个输入的形状为:(1224224,3(y标签的形状为:(3,(

我使用RESNET50和一些额外的头层来执行分类

baseModel = ResNet50(weights=None, include_top=False, input_tensor=Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(256, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(3, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)

并用分类交叉熵作为损失函数。

# compile the model
INIT_LR = 1e-4
BS = 16
NUM_EPOCHS = 20
opt = Adam(lr=INIT_LR, decay=INIT_LR / NUM_EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])
# train the model
H = model.fit(X_train1, batch_size = BS, validation_data=(X_val1), epochs = NUM_EPOCHS, shuffle =False)

当我拟合模型时,我得到以下错误:

Traceback (most recent call last):
File "<ipython-input-81-eda0da51ce9e>", line 1, in <module>
H = model.fit(X_train1, batch_size = BS, validation_data=(X_val1), epochs = NUM_EPOCHS, shuffle =False)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 814, in _call
results = self._stateful_fn(*args, **kwds)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 2828, in __call__
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 3210, in _maybe_define_function
return self._define_function_with_shape_relaxation(args, kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 3142, in _define_function_with_shape_relaxation
args, kwargs, override_flat_arg_shapes=relaxed_arg_shapes)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerfunction.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworkfunc_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythoneagerdef_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworkfunc_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:806 train_function  *
return step_function(self, iterator)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:796 step_function  **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:789 run_step  **
outputs = model.train_step(data)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginetraining.py:749 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasenginecompile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkeraslosses.py:149 __call__
losses = ag_call(y_true, y_pred)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkeraslosses.py:253 call  **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonutildispatch.py:201 wrapper
return target(*args, **kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkeraslosses.py:1535 categorical_crossentropy
return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonutildispatch.py:201 wrapper
return target(*args, **kwargs)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonkerasbackend.py:4687 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
C:ProgramDataAnaconda3libsite-packagestensorflowpythonframeworktensor_shape.py:1134 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (3, 1) and (1, 3) are incompatible

我做错了什么?我该如何解决?

问题在于y标签的形状。我通过使用tf.reshape(y,[1,3])对其进行整形来修复它我所做的唯一更改是在预处理函数中。

def pre_process(x,y):
x_norm = (x - mean_Rot_MIP) / Var_Rot_MIP
# Stacking along the last dimension to avoid having to move channel axis
x_norm_3ch = tf.stack((x_norm, x_norm, x_norm), axis=-1)
x_norm_3ch = tf.reshape(x_norm_3ch, [1,224,224,3])
y_s = tf.reshape(y,[1,3])
return x_norm_3ch , y_s

我相信可能还有其他方法可以实现我的目标,这一种方法只做了最小的改动。

我认为使用zip()是一种奇怪的方法。为什么不使用from_tensor_slices和批次:

data_set_train = tf.data.Dataset.from_tensor_slices((X_train , y_train)).batch(4)

这应该行得通。您的输出标签形状应该是(4, 3)

更正示例:

import tensorflow as tf
x = tf.random.uniform(minval=0, maxval=1, shape=(100, 224, 224, 3), dtype=tf.float32)
y = tf.random.uniform(minval=0, maxval=3, shape=(100,), dtype=tf.int32)
y = tf.keras.utils.to_categorical(y, num_classes=3)
BS = 16
ds = tf.data.Dataset.from_tensor_slices((x, y)).batch(BS)
baseModel = tf.keras.applications.ResNet50(weights=None, include_top=False,
input_tensor=tf.keras.Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = tf.keras.layers.AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = tf.keras.layers.Flatten(name="flatten")(headModel)
headModel = tf.keras.layers.Dense(256, activation="relu")(headModel)
headModel = tf.keras.layers.Dropout(0.5)(headModel)
headModel = tf.keras.layers.Dense(3, activation="softmax")(headModel)
model = tf.keras.Model(inputs=baseModel.input, outputs=headModel)
INIT_LR = 1e-4
NUM_EPOCHS = 1
opt = tf.keras.optimizers.Adam(lr=INIT_LR, decay=INIT_LR / NUM_EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])
H = model.fit(ds, epochs = NUM_EPOCHS, shuffle=False)

相关内容

  • 没有找到相关文章

最新更新