30

I'm training the Keras object detection model linked at the bottom of this question, although I believe my problem has to do neither with Keras nor with the specific model I'm trying to train (SSD), but rather with the way the data is passed to the model during training.

Here is my problem (see image below): My training loss is decreasing overall, but it shows sharp regular spikes:

Training loss

The unit on the x-axis is not training epochs, but tens of training steps. The spikes occur precisely once every 1390 training steps, which is exactly the number of training steps for one full pass over my training dataset.

The fact that the spikes always occur after each full pass over the training dataset makes me suspect that the problem is not with the model itself, but with the data it is being fed during the training.

I'm using the batch generator provided in the repository to generate batches during training. I checked the source code of the generator and it does shuffle the training dataset before each pass using sklearn.utils.shuffle.

I'm confused for two reasons:

  1. The training dataset is being shuffled before each pass.
  2. As you can see in this Jupyter notebook, I'm using the generator's ad-hoc data augmentation features, so the dataset should theoretically never be same for any pass: All the augmentations are random.

I made some test predictions to see if the model is actually learning anything, and it is! The predictions get better over time, but of course the model is learning very slowly since those spikes seem to mess up the gradient every 1390 steps.

Any hints as to what this might be are greatly appreciated! I'm using the exact same Jupyter notebook that is linked above for my training, the only variable I changed is the batch size from 32 to 16. Other than that, the linked notebook contains the exact training process I'm following.

Here is a link to the repository that contains the model:

https://github.com/pierluigiferrari/ssd_keras

Alex
  • 3,316
  • 4
  • 26
  • 52
  • 1
    This is hardly a [mcve], I think this question could go on a nice diet, which will increase the probability of getting an answer :) – DJK Dec 15 '17 at 02:43
  • 4
    @djk47463 I agree it's hardly a compact example, but how do you create a compact example if you have a complex object detection model and the problem could lie in any part of the model? Anyways, I've solved it myself, it was a Keras-specific issue after all. Maybe this will be useful for someone at some point. – Alex Dec 15 '17 at 19:48

5 Answers5

24

I've figured it out myself:

TL;DR:

Make sure your loss magnitude is independent of your mini-batch size.

The long explanation:

In my case the issue was Keras-specific after all.

Maybe the solution to this problem will be useful for someone at some point.

It turns out that Keras divides the loss by the mini-batch size. The important thing to understand here is that it's not the loss function itself that averages over the batch size, but rather the averaging happens somewhere else in the training process.

Why does this matter?

The model I am training, SSD, uses a rather complicated multi-task loss function that does its own averaging (not by the batch size, but by the number of ground truth bounding boxes in the batch). Now if the loss function already divides the loss by some number that is correlated with the batch size, and afterwards Keras divides by the batch size a second time, then all of a sudden the magnitude of the loss value starts to depend on the batch size (to be precise, it becomes inversely proportional to the batch size).

Now usually the number of samples in your dataset is not an integer multiple of the batch size you choose, so the very last mini-batch of an epoch (here I implicitly define an epoch as one full pass over the dataset) will end up containing fewer samples than the batch size. This is what messes up the magnitude of the loss if it depends on the batch size, and in turn messes up the magnitude of gradient. Since I'm using an optimizer with momentum, that messed up gradient continues influencing the gradients of a few subsequent training steps, too.

Once I adjusted the loss function by multiplying the loss by the batch size (thus reverting Keras' subsequent division by the batch size), everything was fine: No more spikes in the loss.

Alex
  • 3,316
  • 4
  • 26
  • 52
  • 1
    Usually it is safer to just drop the last batch in such cases. Even if the loss is independent of the batch size, a very small batch is more likely to contain non-representative data and hence mess up the gradients. When using mini-batch gradient descent the loss *landscape* is not fixed but it's changing with every batch. This can cause typical fluctuations in the loss if the batch size is too small. Only if the batch size is large enough to be representative of the whole data set the loss will stabilize. A small(er) batch at the end of the epoch can thus have a similarly negative effect. – a_guest Sep 11 '19 at 08:24
  • 1
    @a_guest I'm not sure I agree, for three reasons. 1) Any typically used mini batch sizes are so small relative to the overall dataset that the loss manifold fluctuates extremely between mini batches no matter what. The mini batch does not need to be very representative of the whole dataset. 2) Any optimizer that uses momentum or a similar mechanism doesn't depend much on individual batches anyway. Only the average of many batches matters. 3) From an empirical perspective, training with mini batch size 1 or very small batch sizes works very well in practice. – Alex Sep 11 '19 at 12:21
  • To elaborate on the first argument: Whether the last batch has 32 or 7 samples, both is tiny relative to, and therefore neither will be representative of, your dataset of 50k samples (or 500k, or 5 million). – Alex Sep 11 '19 at 12:28
  • I am struggling with a very similar issue, but my data size is an integer multiple of the batch size, so the explanation does not hold for me. I am unable to explain the sharp spikes and the steep drops in training loss at the end of each epoch. – Markus Loecher Nov 12 '21 at 13:45
8

For anyone working in PyTorch, an easy solution which solves this specific problem is to specify in the DataLoader to drop the last batch:

train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=False, 
                                          pin_memory=(torch.cuda.is_available()), 
                                          num_workers=num_workers, drop_last=True)
wprins
  • 846
  • 2
  • 19
  • 35
  • 2
    Sure, this gets the job done, but it's also just a crutch that treats the symptom rather than the cause. The point is that the loss magnitude simply should not depend on the mini batch size, because that makes no sense. And if you get this right, then there is no need to drop the last mini batch or worry about anything else. – Alex Jul 26 '20 at 21:39
2

I would add gradient clipping because this prevents spikes in the gradients to mess up the parameters during training.

Gradient Clipping is a technique to prevent exploding gradients in very deep networks, typically Recurrent Neural Networks.

Most programs allows you to add a gradient clipping parameter to your GD based optimizer.

Henryk Borzymowski
  • 988
  • 1
  • 10
  • 22
  • 6
    Gradient clipping might get the job done, but I'd argue it would not be a great idea in this case, because it would treat the symptom rather than the cause (the loss shouldn't be exploding in the first place). Besides, the solution has already been provided: In most cases like mine above, the problem will be that the loss magnitude depends on the batch size, which it shouldn't. – Alex Aug 27 '18 at 10:13
1

For me, the solution and cause of the issue were a bit different.

The problem would still occur when defining the batch size to 1 or a proper divisor of the total number of samples (so that the last batch would still be full).

TL;DR;

This is a metric artifact due to averaging that doesn't affect training. The default loss metric is the average loss over the entire current epoch, up to the point where you read it. You can see the per-batch loss by using

def on_batch_begin(batch, logs):
    model.reset_metrics()
    return

lambda_callback = tf.keras.callbacks.LambdaCallback(on_batch_begin=on_batch_begin)

and passing it when training with

model.fit(..., callbacks=[lambda_callback])

Note that this will obviously make the all metrics report only the last training batch loss for each epoch.

Explanation

I managed to pinpoint the problem once I noticed this reset_states() call in the model.fit() method. I also realized that it wouldn't happen over each batch in the batch loop code or train_step, so I went to investigate further.

Looking at the LossesContainer class, it's possible to see that the _total_loss_mean is accumulated with in the __call__() function, and weighted by the batch_size as @Alex mentioned. This value is later then reduced to a mean value (as it's a Mean instance) which finally renders the loss metric we see.

The good news is that _total_loss_mean isn't actually fed into the training process, as it's not the value returned by the LossesContainer's __call__().

The image below shows a training experiment with (gray) and without (pink) the 'fix'. It was done on top of the MNIST Tensorflow example. In it, it' possible to see the averaging effect over the actual data. 1

felippeduran
  • 346
  • 3
  • 6
0

I faced a similar problem when using tensorflow. In my case, the issue was not related to mini batch size. The actual problem was that tensorflow wasn't shuffling the data completely due to limitation set by buffer_size as described here https://www.tensorflow.org/api_docs/python/tf/data/Dataset#shuffle For perfect shuffling, the buffer_size should be greater than or equal to the full size of the dataset.