首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >所有输入数组(x)应具有相同数量的样本。获取数组形状:[(52,224,224,3),(64,35)]

所有输入数组(x)应具有相同数量的样本。获取数组形状:[(52,224,224,3),(64,35)]
EN

Stack Overflow用户
提问于 2019-10-21 04:26:27
回答 1查看 162关注 0票数 0

我得到了这个错误,非常奇怪的是,第一个纪元已经令人满意地完成了,而第二个纪元的尺寸不匹配。All input arrays (x) should have the same number of samples. Got array shapes: [(52, 224, 224, 3), (64, 35)] see error screenshot

我第一次使用带有flow_from_directory()方法的ImageDataGenerator()来获取数据集中的所有图像。这是代码的一部分,我认为错误应该是:

代码语言:javascript
运行
复制
# generator function - two inputs (images and histogram vector) and one label (OCEAN labels)
def generator(dataset_path, OCEAN_histogram, batch_size):

    gen = ImageDataGenerator()
    gen_faces = gen.flow_from_directory(dataset_path,
                                          target_size = (224, 224),
                                          class_mode = None,
                                          batch_size = batch_size,
                                          shuffle=True)    


    batch_OCEAN = np.zeros((batch_size, 5))
    batch_histogram = np.zeros((batch_size, n_features))

    # loop where we continually feed the nn in batches
    while True:

    # it takes the next batch of images
    batch_faces = gen_faces.next()


    # list with all images names in the current batch
    all_faces = [f.rsplit('.', 1)[0].rsplit('/', 1)[1] for f in gen_faces.filenames]

    for i in range(batch_size):
        batch_OCEAN[i] = OCEAN_histogram[all_faces[i]][0]
        batch_histogram[i] = OCEAN_histogram[all_faces[i]][1]

    # shapes : batch_faces -> (64, 224, 224, 3)  batch_histogram -> (64, 35) batch_OCEAN -> (64, 5)
    yield [ batch_faces, batch_histogram ], batch_OCEAN


train_generator = generator(train_path, train_OCEAN_histogram, batch_size)
validation_generator = generator(validation_path, validation_OCEAN_histogram, batch_size)

# Train model on dataset
print("[INFO] training model...")
custom_vgg_model.fit_generator(train_generator, epochs = 50, steps_per_epoch = train_size//batch_size,
                               validation_data = validation_generator,
                               validation_steps = validation_size//batch_size, verbose = 1)

这个错误怎么可能呢?有什么想法吗?

提前感谢!

EN

Stack Overflow用户

发布于 2019-10-21 08:22:00

原因是因为您的总训练样本是55,796,如果您的批量大小是64,那么您将有871个步骤和52个剩余步骤。在871步之后,你的生成器将在下一次迭代中返回一个形状为(52, 224, 224, 3)的张量。

我建议在每个时期之后,对数据集进行混洗,只在生成器中保留55,796//64样本。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58477017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档