如何使用Keras ImageDataGenerator处理以图像为目标的数据(语义分割)

问题描述 投票:1回答:1

我有一个熊猫数据框,其中包含所有图像及其对应目标的路径,但我不知道如何加载图像。现在,当我尝试适应时出现此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-8a8446d3863c> in <module>()
      5                                                 y_col='target',
      6                                                 class_mode='other',
----> 7                                                 batch_size=128)
      8 
      9 val_gen = image_generator.flow_from_directory(val_df,

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py in flow_from_dataframe(self, dataframe, directory, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, subset, interpolation, drop_duplicates, **kwargs)
    664             subset=subset,
    665             interpolation=interpolation,
--> 666             drop_duplicates=drop_duplicates
    667         )
    668 

~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras_preprocessing/image/dataframe_iterator.py in __init__(self, dataframe, directory, image_data_generator, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, subset, interpolation, dtype, drop_duplicates)
    144                 y_col = [y_col]
    145             if "object" in list(df[y_col].dtypes):
--> 146                 raise TypeError("y_col column/s must be numeric datatypes.")
    147         self.samples = len(self.filenames)
    148         if class_mode in ["other", "input", None]:

TypeError: y_col column/s must be numeric datatypes.

我也尝试过使用opencv读取图像并将其放入数据框,但是我遇到了同样的错误。

这里有我用来制作数据框和我对flow_from_dataframe的调用的代码:

train_x_fnames = os.listdir(x_train)
train_x_paths = []
train_y_paths = []
for fname in train_x_fnames:
    train_x_paths.append(os.path.join(x_train, fname))
    new_fname = os.path.join(y_train, fname[:-4] + "_train_color.png")
    train_y_paths.append(new_fname)

train_x_paths = np.array(train_x_paths)
train_y_paths = np.array(train_y_paths)

train_df = pd.DataFrame([train_x_paths, train_y_paths]).transpose()


val_x_fnames = os.listdir(x_val)
val_x_paths = []
val_y_paths = []
for fname in val_x_fnames:
    val_x_paths.append(os.path.join(x_val, fname))
    new_fname = os.path.join(y_val, fname[:-4] + "_train_color.png")
    val_y_paths.append(new_fname)

val_x_paths = np.array(val_x_paths)
val_y_paths = np.array(val_y_paths)


val_df = pd.DataFrame([val_x_paths, val_y_paths]).transpose()


train_df.columns = ['sample', 'target']
val_df.columns = ['sample', 'target']
train_gen = image_generator.flow_from_dataframe(train_df,
                                                directory=None,
                                                target_size=(144, 256),
                                                x_col='sample',
                                                y_col='target',
                                                class_mode='other',
                                                batch_size=128)

val_gen = image_generator.flow_from_directory(val_df,
                                              directory=None,
                                              target_size=(144, 256),
                                              x_col='sample',
                                              y_col='target',
                                              class_mode='other',
                                              batch_size=128)
python pandas keras deep-learning image-segmentation
1个回答
-1
投票

您现在可以解决问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.