简体   繁体   English

Keras错误中的flow_from_dataframe方法:目标数据丢失,

[英]flow_from_dataframe method in Keras error: target data is missing,

I want to do an image segmentation having the following structure folder我想做一个具有以下结构文件夹的图像分割

--img 
    -------0001.slice.png 
--mask
    -------0001.slice.png

I try to make a dataframe, and also ImageDataGenerator .我尝试制作一个 dataframe 和ImageDataGenerator

X_train_data consist of a list of all the absolute path to all the image files, while the Y_train_label is also the same, consisting of a list of all absolute path of the masks. X_train_data包含所有图像文件的所有绝对路径列表,而Y_train_label也一样,包含所有掩码绝对路径的列表。 Then I make a dictionary to bound them altogether然后我制作一本字典将它们全部绑定

data = OrderedDict()                    

data = {"image":X_train_data, "label":Y_train_label}
df = pd.DataFrame(data=data)  

then I put them into the ImageDatagenerator with flow_from_dataframe然后我用flow_from_dataframe将它们放入ImageDatagenerator

train_data_gen=train_datagen.flow_from_dataframe(dataframe=training_data,
                                                 directory=None,
                                                 x_col="image", 
                                                 y_col="label",
                                                 validate_filenames=(True),
                                                 color_mode='rgb',
                                                 target_size=(IMAGE_HEIGHT, IMAGE_WIDTH),
                                                 batch_size=32, 
                                                 class_mode=None,
                                                 shuffle=(False))

then I fit model as below:然后我适合 model 如下:

history = model.fit(train_data_gen, 
                    steps_per_epoch=train_data_gen.n//train_data_gen.batch_size,   
                    validation_data= validation_data_gen,
                    validation_steps=validation_data_gen.n//validation_data_gen.batch_size,epochs=5)

but then I get the error但后来我得到了错误

ValueError: Target data is missing. Your model was compiled with loss=binary_crossentropy, and therefore expects target data to be provided in fit().

Why the y_col which contain all the absolute path can not be read by that flow_from_dataframe ?为什么包含所有绝对路径的y_col不能被那个flow_from_dataframe读取?

I have tried to change the Y_train_data value by storing all the NumPy array of the masks but still get the same error.我试图通过存储掩码的所有 NumPy 数组来更改Y_train_data值,但仍然出现相同的错误。 I also have used the keras-preprocessing from GitHub.我还使用了 GitHub 的keras-preprocessing

history = model.fit(generator=train_data_gen, 
                    steps_per_epoch=train_data_gen.n//train_data_gen.batch_size,   
                    validation_data=validation_data_gen,
                    validation_steps=validation_data_gen.n//validation_data_gen.batch_size,
                    epochs=5)

This probably occurs because you are using labels named as Y_train_data then raises ValueError because is it wrong, i suggest to structure your data files to perform a "clean" training enviroment.这可能是因为您正在使用名为Y_train_data的标签然后引发ValueError因为它是错误的,我建议构建您的数据文件以执行“干净”的培训环境。

/img /图像
-------0001.slice.png ------0001.切片.png
/mask /面具
-------0001.slice.png ------0001.切片.png

import os
from random import randint

import pandas 

input_dir = "img/"
target_dir = "mask/" 
img_size = (IMAGE_HEIGHT, IMAGE_WIDTH)
num_classes = 2
batch_size = 3 

# replace image format ".bmp" or anoter like jpg, png, etc ...

X_train_data = [image for image in os.listdir(input_dir) if image.endswith(".bmp")]
 
Y_train_label = [image for image in os.listdir(target_dir) if image.endswith(".bmp")] 

labels = [randint(0, 1) for i in range(len(X_train_data))]

df = pd.DataFrame(data=[X_train_data, Y_train_label, labels], columns=["X_train", "y_train", "label"])

At this point it is similar to your example, at least you have to implement in your script在这一点上它类似于你的例子,至少你必须在你的脚本中实现

generator = image.ImageDataGenerator()

train_data_gen = generator.flow_from_dataframe(dataframe=df,
                                                directory=None,
                                                x_col="X_train", 
                                                y_col="y_train",
                                                validate_filenames=(True),
                                                color_mode='rgb',
                                                target_size=(IMAGE_HEIGHT, IMAGE_WIDTH),
                                                batch_size=batch_size, 
                                                class_mode=None,
                                                shuffle=(False))

history = model.fit(train_data_gen, 
                steps_per_epoch=train_data_gen.n//train_data_gen.batch_size,   
                validation_data= validation_data_gen,
                validation_steps=validation_data_gen.n//validation_data_gen.batch_size,epochs=5)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM