我的数据生成器没有获取新数据。每次交互都会得到相同的数据

2024-09-26 17:44:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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

Tags: normmlusersdocumentsgeoscrapcntnpy
1条回答
网友
1楼 · 发布于 2024-09-26 17:44:41

问题是,每次迭代都要设置cnt=0。你抓取5个文件名,产生它们,然后重复同样的事情,所以你总是抓取前5个。你想改变吗

def datagenerator(imgfns, imglabels, batchsize, mode="train"):
  while True:
    images = []
    labels = []
    cnt=0

def datagenerator(imgfns, imglabels, batchsize, mode="train"):
  cnt=0  
  while True:
    images = []
    labels = []

您还需要确保cnt保持在列表的限制范围内。比如说

while len(images) < batchsize and cnt < len(imgfns):
  # blah

相关问题 更多 >

    热门问题