简体   繁体   English

使用fit_generator时,Keras模型的批量大小为1

[英]Keras model is doing batch size of 1 when using fit_generator

When I fit_generator with my batch generator it uses a batch size of 1, it does 1 step increment for each epoch. 当我使用我的批量生成器fit_generator时,它使用批量大小为1,它为每个时期执行1步增量。 What am I doing wrong. 我究竟做错了什么。

I have tried changing batch size arguments for my batch generator but still the same. 我已经尝试更改我的批处理生成器的批量大小参数但仍然相同。

My batch generator function: 我的批量生成器功能:

def batchGenerator(imgs, steerings, batchSize, isTraining):

    while True:
        batchImg = []
        batchSteering = []

        for i in range(batchSize):
            randIndex = random.randint(0, len(imgs) - 1)
            if isTraining:
                img, steering = randomAugment(imgs[randIndex], steerings[randIndex])
            else:
                img = imgs[randIndex]
                steering = steerings[randIndex]

            img = imgPreprocess(img)

            batchImg.append(img)
            batchSteering.append(steering)

        yield (np.asarray(batchImg), np.asarray(batchSteering))

This is my fit_generator arguments: 这是我的fit_generator参数:

history = model.fit_generator(batchGenerator(X_train, y_train, 300, 1),
                              steps_per_epoch = 300,
                              epochs = 10,
                              validation_data = batchGenerator(X_valid, y_valid, 200, 0),
                              validation_steps = 200,
                              verbose = 1,
                              shuffle = 1)

When I run this the batch size seems to be 1, as for each epoch it is being incremented by 1. For each epoch it does 0/300, 1/300, 2/300, 3/300, etc. 当我运行这个时,批量大小似乎是1,因为每个时期它增加1.对于每个时期它做0 / 300,1 / 300,2 / 300,3 / 300等。

What is going on? 到底是怎么回事?

Your steps_per_epoch should always be length of training data divided by batch_size , ie in this case X_train.shape[0]//batch_size . 您的steps_per_epoch应始终为训练数据的长度除以batch_size ,即在此情况下为X_train.shape[0]//batch_size

Also, the way you are shuffling your data with the random index, it will mean some samples might be selected more than once and some, never. 此外,您使用随机索引对数据进行混洗的方式,将意味着可能会多次选择一些样本,而不会选择一些样本。 You can also think about random shuffling the entire training set first, and then pick sequential batches of data for training. 您还可以考虑先对整个训练集进行随机抽样,然后选择连续批量的数据进行训练。 I just wanted to point this out, if you missed. 我只是想指出这一点,如果你错过了。

Edit 编辑

def batchGenerator(imgs, steerings, batchsize, isTraining):
    while True:
        start = 0
        end = batchsize

        while start  < len(imgs): 
            x = imgs[start:end]
            y = steerings[start:end]
            if isTraining:            
                x , y = randomAugment(x, y)

            yield x, y

            start += batchsize
            end += batchsize

Maybe try something like this. 也许尝试这样的事情。 You can handle the shuffling later if this works. 如果有效,你可以稍后处理洗牌。

Your generator has no issue and your code is fine too. 您的生成器没有问题,您的代码也很好。 The way you interpret the output is wrong. 解释输出的方式是错误的。

From the docs, you can see 从文档中,您可以看到

steps_per_epoch: Integer or None. steps_per_epoch:整数或无。 Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. 在宣布一个纪元完成并开始下一个纪元之前的步骤总数(样本批次)。 When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined. 使用输入张量(如TensorFlow数据张量)进行训练时,默认的None等于数据集中的样本数除以批量大小,如果无法确定,则为1。

Normally steps_per_epochs is set as X_train.shape[0]//batch_size 通常, steps_per_epochs设置为X_train.shape[0]//batch_size

While training, the training is done for steps_per_epochs batches and one epoch is treated as completed. 在训练时,训练是针对steps_per_epochs批次进行的,并且一个时期被视为已完成。 Since the data is taken in a random order there is no other way to tell the model that one epoch is over. 由于数据是以随机顺序获取的,因此没有其他方法可以告诉模型一个时期结束。

While training you can see 0/300, 1/300, 2/300, 3/300 and so on till 300/300 . 训练时你可以看到0/300, 1/300, 2/300, 3/300 300/300 0/300, 1/300, 2/300, 3/300 300/300等,直到300/300 It is quite normal. 这很正常。 This means your model is trained for 300 steps where there batch size for each step is 300 (since you gave batch size as 300) 这意味着您的模型经过300步骤的培训,每步的批量大小为300 (因为批量大小为300)

If you gave batch size as let's say 10, and steps_per_epoch as 100 you can see while training 1/100, 2/100 so on till 100/100 which means, your model is trained for 100 steps and each step is essentialy a batch of 10 samples 如果您给出批量大小,比如10,并且steps_per_epoch为100您可以看到训练1/100, 2/100等等,直到100/100 ,这意味着,您的模型经过100步训练,每一步基本上都是一批10样本

In contrast to the fit function, the output of fit_generator is the count of batches and not of training examples. fit函数相比, fit_generator的输出是批次计数而不是训练示例。 Consequently,an increment of 1 means that one more batch has been processed. 因此,增量为1意味着已经处理了一个批次。 With steps_per_epoch you define how many batches will be processed per epoch. 使用steps_per_epoch您可以定义每个纪元将处理的批次数。

By definition, one epoch is finished when each training example has been processed once. 根据定义,当每个训练样本被处理一次时,一个时期结束。 This is why people suggest to set steps_per_epoch to: 这就是为什么人们建议将steps_per_epoch设置为:

steps_per_epoch=number_of_examples//batch_size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM