我在python/Keras中创建了一个数据生成器,用于在batchesize=5中引入文件名和标签。我想它得到新的(后续)文件名和标签每次迭代
我看了很多例子,也读了一些文档,但是我想不通
def datagenerator(imgfns, imglabels, batchsize, mode="train"):
while True:
images = []
labels = []
cnt=0
while len(images) < batchsize:
images.append(imgfns[cnt])
labels.append(imglabels[cnt])
cnt=cnt+1
#for ii in range(batchsize):
# #img = np.load(imgfns[ii])
# #images.append(img)
# images.append(imgfns[ii])
# labels.append(imglabels[ii])
#for image, label in zip(imgfns, imglabels):
# #img = np.load(image)
# #images.append(img)
# images.append(image)
# labels.append(label)
print(images)
print(labels)
print('********** cnt = ', cnt)
yield images, labels
train_gen = datagenerator(train_uxo_scrap, train_uxo_scrap_labels, batchsize=BS)
valid_gen = datagenerator(test_uxo_scrap, test_uxo_scrap_labels, batchsize=BS)
# train the network
H = model.fit_generator(
train_gen,
steps_per_epoch=NUM_TRAIN_IMAGES // BS,
validation_data=valid_gen,
validation_steps=NUM_TEST_IMAGES // BS,
epochs=NUM_EPOCHS)
下面是我得到的输出的一个例子。您可以看到,每次通过生成器时,它都会获取相同的数据。“Epoch 1/10”后面的第一行有5个文件名。下一行有5个标签(对应于batchsize=5)。例如,您可以在每个输出中看到第一个文件名是“。。。508.npy”等,并且每个迭代的标签都是相同的
Epoch 1/10
['C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#508.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#1218.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#71.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#551.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#843.npy']
[1, 0, 0, 0, 1]
********** cnt = 5
['C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#508.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#1218.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#71.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#551.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#843.npy']
[1, 0, 0, 0, 1]
********** cnt = 5
['C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#508.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#1218.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#71.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#551.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#843.npy']
[1, 0, 0, 0, 1]
********** cnt = 5
问题是,每次迭代都要设置
cnt=0
。你抓取5个文件名,产生它们,然后重复同样的事情,所以你总是抓取前5个。你想改变吗至
您还需要确保
cnt
保持在列表的限制范围内。比如说相关问题 更多 >
编程相关推荐