Python 我的数据生成器无法获取新数据。每次交互都会得到相同的数据
我在python/Keras中创建了一个数据生成器,用于在batchesize=5中引入文件名和标签。每次迭代都会得到相同的文件名和标签。我希望每次迭代都能得到新的(后续的)文件名和标签Python 我的数据生成器无法获取新数据。每次交互都会得到相同的数据,python,keras,generator,data-generation,Python,Keras,Generator,Data Generation,我在python/Keras中创建了一个数据生成器,用于在batchesize=5中引入文件名和标签。每次迭代都会得到相同的文件名和标签。我希望每次迭代都能得到新的(后续的)文件名和标签 Epoch 1/10 ['C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#508.npy', 'C:\\Users\\jfhau
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
我看了很多例子并阅读了文档,但我无法理解这一点
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
下面是我得到的输出的一个例子。您可以看到,每次通过生成器时,它都会获取相同的数据。“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个。你想改变吗
def datagenerator(imgfns, imglabels, batchsize, mode="train"):
while True:
images = []
labels = []
cnt=0
到
您还需要确保cnt
保持在列表的限制范围内。大概是
while len(images) < batchsize and cnt < len(imgfns):
# blah
而len(图像)
def datagenerator(imgfns, imglabels, batchsize, mode="train"):
cnt=0
while True:
images = []
labels = []
while len(images) < batchsize and cnt < len(imgfns):
# blah