TypeError:无法将< keras.losses.SparseCategoricalCrossentropy对象的元素转换为Tensor

kuarbcqp  于 2023-06-30  发布在  Go
关注(0)|答案(1)|浏览(177)

我在用自己的图像训练CNN我已经将class_mode设置为sparse,所以我想我应该使用SparseCategoricalCrossentropy。然而,当我这样做(甚至是CategoricalCrossentropy)时,我得到了这个错误,即元素不属于受支持的类型。
我已经尝试了几个不同的损失函数,但仍然存在相同的错误。我想我的数据格式一定有问题。我已经附上了我的data_gen和NN代码沿着完整的追溯。

train_datagen = ImageDataGenerator(
rescale=1./255,
width_shift_range=.05,
height_shift_range=.05,
zoom_range=0.05,
validation_split=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode="constant",
cval=0)

test_datagen = ImageDataGenerator(
rescale=1./255)
    
#data augmentation
train_ds = train_datagen.flow_from_directory(
train_data_dir,
shuffle=True,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='sparse',
subset='training');

cnn = Sequential()
cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(img_width,img_height,3)))
cnn.add(BatchNormalization())

cnn.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
cnn.add(BatchNormalization())
cnn.add(MaxPooling2D(pool_size=(2, 2)))
cnn.add(Dropout(0.2))

cnn.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
cnn.add(BatchNormalization())

cnn.add(Flatten())

cnn.add(Dense(64, activation='relu'))
cnn.add(Dense(22, activation='softmax'))

cnn.compile(loss=keras.losses.SparseCategoricalCrossentropy,
              optimizer="Adam",
              metrics=['accuracy'])
--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In [24], line 111
    106 cnn.compile(loss=keras.losses.SparseCategoricalCrossentropy,
    107               optimizer="Adam",
    108               metrics=['accuracy'])
    110 epochs=20
--> 111 history = cnn.fit(
    112   train_ds,
    113   validation_data=valid_ds,
    114   steps_per_epoch=math.ceil(len(train_ds) / batch_size),
    115   epochs=epochs)
    117 plt.plot(history.history['loss'], label='train')
    118 plt.plot(history.history['val_loss'], label='validation')

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~\AppData\Local\Temp\__autograph_generated_fileswqeb6ya.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

TypeError: in user code:

    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1160, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1146, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1135, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 994, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1052, in compute_loss
        return self.compiled_loss(
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 158, in __call__
        return losses_utils.compute_weighted_loss(
    File "C:\Users\maiaz\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\losses_utils.py", line 328, in compute_weighted_loss
        losses = tf.convert_to_tensor(losses)

    TypeError: Failed to convert elements of <keras.losses.SparseCategoricalCrossentropy object at 0x0000017523557AC0> to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.
bxgwgixi

bxgwgixi1#

您应该使用loss=keras.losses.sparse_categorical_crossentropy

相关问题