类型错误:目标数据丢失。您的模型具有“loss”:binary_crossentropy,因此期望目标数据在“fit()”中传递

2024-01-28

我正在尝试运行一个模型并使用分层 K 折验证来验证它。我已将训练和测试图像一起存储在一个新文件夹中,并将训练和测试的基本事实存储在 CSV 中以用于获取标签。

我正在使用 binary_crossentropy 作为损失函数,因为我正在研究二元分类。

CSV 文件包含 2 列:Image(图像的名称)和 ID(相应图像的标签)。

这是代码:

EPOCHS = 1
N_SPLIT = 3

image_dir = 'path of the folder where all the image is contained'

image_label = pd.read_csv('groundtruths of the images.csv')
df = image_label.copy()
    
main_pred = [] #a list to store the scores of each fold
error = [] #
data_kfold = pd.DataFrame()

train_y = df.ID #stores the label of the images
train_x = df.Image #stores the name of the images

train_datagen=ImageDataGenerator(horizontal_flip=True,vertical_flip=True,rotation_range=90) #data augmentation
validation_datagen = ImageDataGenerator()
kfold = StratifiedKFold(n_splits=N_SPLIT,shuffle=True,random_state=42) #making folds

j = 0 # a variable to count the fold number

for train_idx, val_idx in list(kfold.split(train_x,train_y)):
    x_train_df = df.iloc[train_idx] #training data after split
    x_valid_df = df.iloc[val_idx] #validation data after split
    j+=1
    #loading training images
    training_set = train_datagen.flow_from_dataframe(dataframe=x_train_df, directory=image_dir,
                                                 x_col="Image", y_col="ID",
                                                 class_mode=None,
                                                 target_size=(image_size,image_size), batch_size=batch_size)
    #loading validation images
    validation_set = validation_datagen.flow_from_dataframe(dataframe=x_valid_df, directory=image_dir,
                                                 x_col="Image", y_col="ID",
                                                 class_mode=None,
                                                 target_size=(image_size,image_size), batch_size=batch_size)

    #training THIS IS THE LINE WHERE THE ERROR OCCURS
    history = parallel_model.fit(training_set,
                                 validation_data=validation_set,
                                 epochs = EPOCHS,
                                 steps_per_epoch=x_train_df.shape[0] // batch_size
                                 )

    test_generator = ImageDataGenerator(rescale = 1./255)

    test_set = test_generator.flow_from_dataframe(dataframe=image_label, directory=image_dir,
                                                 x_col="Image",y_col=None,
                                                 class_mode=None,
                                                 target_size=(image_size,image_size))

    pred= parallel_model.predict_generator(test_set, len(image_label) // batch_size)
    predicted_class_indices=np.argmax(pred,axis=1)
    data_kfold[j] = predicted_class_indices
    gc.collect()

我得到的错误:

Found 800 validated image filenames.
Found 400 validated image filenames.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-6b473ab35caf> in <module>()
     20                                  validation_data=validation_set,
     21                                  epochs = EPOCHS,
---> 22                                  steps_per_epoch=x_train_df.shape[0] // batch_size
     23                                  )
     24 

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

TypeError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 813, in train_step
        f'Target data is missing. Your model has `loss`: {self.loss}, '

    TypeError: Target data is missing. Your model has `loss`: binary_crossentropy, and therefore expects target data to be passed in `fit()`.

这是您的数据集的错误。

训练时,Tensorflow 需要模型的输入,以及作为基本事实的输出标签。

尝试迭代你的tf.data.Dataset对象并查看它是否仅变成单个值(导致此错误的原因)或元组中的 2 个值(形式为(model_input, label)).

错误输出示例:

for item in dataset:
    print(item) # OR, in the output below for brevity, item.shape
(64, 64, 64, 1)
(64, 64, 64, 1)
(64, 64, 64, 1)
(64, 64, 64, 1)
(64, 64, 64, 1)
...

正确输出示例:

for item in dataset:
    print([ i.shape for i in item ])
[TensorShape([64, 64, 64, 1]), TensorShape([64, 64, 64, 2])]
[TensorShape([64, 64, 64, 1]), TensorShape([64, 64, 64, 2])]
[TensorShape([64, 64, 64, 1]), TensorShape([64, 64, 64, 2])]
[TensorShape([64, 64, 64, 1]), TensorShape([64, 64, 64, 2])]
...

示例取自我正在研究的模型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:目标数据丢失。您的模型具有“loss”:binary_crossentropy,因此期望目标数据在“fit()”中传递 的相关文章

随机推荐