【问题标题】:Keras ImageGenerator : IndexError: list index out of rangeKeras ImageGenerator:IndexError:列表索引超出范围
【发布时间】:2020-01-17 23:29:33
【问题描述】:

我将 kaggle 内核中的数据加载到我的机器上进行复制,现在代码无法正常工作,但可以在同一 python 环境中的 keras 上运行。

这是代码和错误。

def flow_from_dataframe(img_data_gen, in_df, path_col, y_col, **dflow_args):
    base_dir = os.path.dirname(in_df[[path_col]].values[0])
    print('## Ignore next message from keras, values are replaced anyways')
    df_gen = img_data_gen.flow_from_directory(base_dir, 
                                     class_mode = 'sparse',
                                    **dflow_args)
    df_gen.filenames = in_df[path_col].values
    df_gen.classes = np.stack(in_df[y_col].values)
    df_gen.samples = in_df.shape[0]
    df_gen.n = in_df.shape[0]
    df_gen._set_index_array()
    df_gen.directory = '' # since we have the full path
    print('Reinserting dataframe: {} images'.format(in_df.shape[0]))
    return df_gen


train_gen = flow_from_dataframe(core_idg, train_df, 
                             path_col = 'path',
                            y_col = 'disease_vec', 
                            target_size = IMG_SIZE,
                             color_mode = 'rgb',
                            batch_size = 32)

valid_gen = flow_from_dataframe(core_idg, valid_df, 
                             path_col = 'path',
                            y_col = 'disease_vec', 
                            target_size = IMG_SIZE,
                             color_mode = 'rgb',
                            batch_size = 256) # we can use much larger batches for evaluation
# used a fixed dataset for evaluating the algorithm
test_X, test_Y = next(flow_from_dataframe(core_idg, 
                               valid_df, 
                             path_col = 'path',
                            y_col = 'disease_vec', 
                            target_size = IMG_SIZE,
                             color_mode = 'rgb',
                            batch_size = 1024)) # one big batch

t_x, t_y = next(train_gen)
fig, m_axs = plt.subplots(4, 4, figsize = (16, 16))

我可以看到路径和路径列表,但不确定它来自哪里。

Found 0 images belonging to 0 classes.
Reinserting dataframe: 10000 images
Traceback (most recent call last):
  File "main.py", line 181, in <module>
    batch_size = 32)) # one big batch
  File "/home/user/.conda/envs/test_env/lib/python3.6/site-packages/keras_prepro         cessing/image/iterator.py", line 104, in __next__
    return self.next(*args, **kwargs)
  File "/home/user/.conda/envs/test_env/lib/python3.6/site-packages/keras_prepro         cessing/image/iterator.py", line 116, in next
    return self._get_batches_of_transformed_samples(index_array)
  File "/home/user/.conda/envs/test_env/lib/python3.6/site-packages/keras_prepro         cessing/image/iterator.py", line 227, in _get_batches_of_transformed_samples
    img = load_img(filepaths[j],

【问题讨论】:

  • 你能在flow_from_dataframe里面打印base_dir的值吗?
  • @Anwarvic ../data/images_003/images 当我执行 cd ../data/images_001/images/ 和 ls 时,它会打印所有图像。我有 12 个图像文件夹,例如 images003,每个都有一个文件夹 /images,里面是所有图像
  • base_dir = ../data/images_003/images
  • 好的,太好了.. 你现在可以打印train_gen.directory吗?
  • 它不打印任何内容,无论是在内核中还是在我的机器中

标签: python python-3.x pandas image-processing keras


【解决方案1】:

发生错误是因为filepaths 是空列表。添加到filepaths图片路径:

df_gen.filepaths.extend(df_gen.filenames)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多