【问题标题】:Generator does only 12 iterations - no matter batch size生成器只进行 12 次迭代——无论批量大小
【发布时间】:2019-08-31 02:19:52
【问题描述】:

我有以下数据生成器。它工作并返回预期的数据。除了我设置的 epochs 或 batchsize 等于什么,它只进行 12 次迭代然后给出错误(见下文)

我已尝试更改 epoch 数和批量大小。

# initialize the number of epochs to train for and batch size
NUM_EPOCHS = 10 #100
BS = 32 #64 #32

NUM_TRAIN_IMAGES = len(train_uxo_scrap)
NUM_TEST_IMAGES = len(test_uxo_scrap)
def datagenerator(imgfns, imglabels, batchsize, mode="train", class_mode='binary'):
    cnt=0
    while True:
        images = []
        labels = []
        #cnt=0

        while len(images) < batchsize and cnt < len(imgfns):
            images.append(imgfns[cnt])
            labels.append(imglabels[cnt])
            cnt=cnt+1

        print(images)
        print(labels)
        print('********** cnt = ', cnt)
        yield images, labels
train_gen = datagenerator(train_uxo_scrap, train_uxo_scrap_labels, batchsize=BS, class_mode='binary')

valid_gen = datagenerator(test_uxo_scrap, test_uxo_scrap_labels, batchsize=BS, class_mode='binary')
# 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)

我希望代码在每次迭代中经过 10 个 epoch 和 32 个样本。每次迭代我得到 32 个样本,但我在第一个 epoch 中只得到 12 次迭代,然后我得到以下错误。无论设置什么批量大小或时期,都会发生这种情况。

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-83-26f81894773d> in <module>()
      5     validation_data=valid_gen,
      6     validation_steps=NUM_TEST_IMAGES // BS,
----> 7     epochs=NUM_EPOCHS)

~\AppData\Local\Continuum\anaconda3\envs\dltf1\lib\site-packages\tensorflow\python\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1424         use_multiprocessing=use_multiprocessing,
   1425         shuffle=shuffle,
-> 1426         initial_epoch=initial_epoch)
   1427 
   1428   def evaluate_generator(self,

~\AppData\Local\Continuum\anaconda3\envs\dltf1\lib\site-packages\tensorflow\python\keras\engine\training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, **kwargs)
    182       # `batch_size` used for validation data if validation
    183       # data is NumPy/EagerTensors.
--> 184       batch_size = int(nest.flatten(batch_data)[0].shape[0])
    185 
    186       # Callbacks batch begin.

IndexError: tuple index out of range

这里是打印的示例:

['C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#615.npy', ..., 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#224.npy']
[1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0]
********** cnt =  352
['C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#532.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\uxo_48-81\\JBCC_Norm_Formatted_48-81_#953.npy', 
...
, 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#1081.npy', 'C:\\Users\\jfhauris\\Documents\\xtemp\\ML GEO\\MLGeoCode\\FormattedDataStore\\scrap_48-81\\JBCC_Norm_Formatted_48-81_#1050.npy']
[1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0]
********** cnt =  384

【问题讨论】:

    标签: python keras generator data-generation


    【解决方案1】:

    看看这是否有效:

    def datagenerator(imgfns, imglabels, batchsize, mode="train", class_mode='binary'):
        while True:
            start = 0
            end = batchsize
    
            while start  < len(imgfns): 
                x = imgfns[start:end]
                y = imglabels[start:end]
                yield x, y
    
                start += batchsize
                end += batchsize
    

    假设 imgfns, imglabels 是 numpy 数组。

    【讨论】:

    • 稍作修改以符合我的真实代码 - 效果很好!谢谢。你知道为什么我的代码不起作用吗?乔恩
    • 好吧,我没有深入研究它。但是 Keras 生成器需要一个while True:,其概念是在该外部循环内有一个while 循环,该循环不断产生每个batchsize 的批次。您的内部 while 似乎一次添加一张图像,并在外部循环中产生。
    猜你喜欢
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    相关资源
    最近更新 更多