我得到了这个错误,非常奇怪的是,第一个纪元已经令人满意地完成了,而第二个纪元的尺寸不匹配。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()来获取数据集中的所有图像。这是代码的一部分,我认为错误应该是:
# 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)
这个错误怎么可能呢?有什么想法吗?
提前感谢!
发布于 2019-10-21 08:22:00
原因是因为您的总训练样本是55,796,如果您的批量大小是64,那么您将有871个步骤和52个剩余步骤。在871步之后,你的生成器将在下一次迭代中返回一个形状为(52, 224, 224, 3)
的张量。
我建议在每个时期之后,对数据集进行混洗,只在生成器中保留55,796//64
样本。
https://stackoverflow.com/questions/58477017
复制相似问题