# Image classification with different number of training image for each class

I'm trying to train neural network for image classification with 4 different classes:

1. Cars (22k training examples)
2. Building (8k training examples)
3. Pedestrian (5k training examples)
4. Trees (1k training examples)

The problem is that the number of training images is biased toward one/two class(es).

I'm wondering if there is a way to train neural network in terms of the number of training examples per class?

Do I have to limit the number of training examples per class to the minimum number of all classes?

## 2 Answers

It is not important that you don't have data of different classes which are unbalanced. What is important is that your data should have a real distribution. The distribution of your training data should be the same as your test environment. As you can read here, your data should be suited well for the task it is going to be used. Consequently, if the distribution of your samples is real, there will be no problem. Consider the point that for unbalanced data-sets we should use appropriate evaluation metrics like F1 score.

Finally, if this is the real distribution of your data, I highly recommend you not to change the real distribution even if you want to augment your data. You should not change the relative ratio of different classes.

• Thank you for your answer. I thought that there is a problem training NN with unbalanced dataset, because the the network would learn to classify one of the classes much more then the other. the distribution of my dataset in real life changes between different datasets. – TripleS Aug 14 '19 at 6:33

Sometimes it could be the case that one class is simply more common than others. That really bears thinking about, usually. I would guess that is not really the case for what you describe (i.e. it's rather arbitrary what kind of things people would want to classify images of in production later), so let's ignore that case (although it may affect performance on a test set depending on what's the most common in the test set). In general though, you would try out some strategies (e.g. no oversampling whether some extent of oversampling with data augmentation) and test their performance on a realistic test set.

Definitely don't throw away any images. Normally we struggle to create realistic images in data augmentation - here you already have them! Thus, a much more appealing approach is to use a data generator that sample evenly (or in whatever proportions make the most sense) from each class to create batches of training data (with some data augmentation such as slight rotations, changes in colors/saturation etc.).