In Improving Model Performance, here are some tips you can try… In this paper, we give several methods to improve the performance of the model, but this paper is an optimization scheme under the premise that the training data set is unchanged. In fact, for deep learning, the amount of data usually has a greater impact on model performance, so expanding the size of data is generally a very effective method.

It’s easy for Google and Facebook to collect millions of images and train their deep learning models on a very large scale. But for individuals or small businesses, collecting real-world data, especially tagged data, can be a time-consuming and laborious endeavor. This article explores a technique for improving model performance by adding data augmentation to existing data sets, increasing the amount of data involved in model training.

What is data enhancement

The so-called data enhancement is to randomly add jitter and disturbance to the original data to generate new training samples with the same labels as the original data. This also makes sense, for an image labeled “dog”, do a certain amount of blur, crop, deformation, etc., will not change the category of the image. Data enhancement is also not limited to image classification applications, such as the data shown below, which satisfies normal distribution:

On the basis of the data set, we add some disturbance processing, and the data distribution is as follows:

The data is multiplied by several times, but it still satisfies the normal distribution. One might say, isn’t this model less accurate than the original one? Considering the complexity of the real world, it is difficult for the data collected to fully meet the normal distribution. Therefore, increasing data disturbance in this way will not reduce the accuracy of the model, but enhance the generalization ability.

For image data, there are many ways to enhance data. The most common methods are:

  • translation
  • rotating
  • The zoom
  • tailoring
  • Shear (shearing)
  • Flip horizontal/vertical
  • .

Shearing may be a bit harder to understand, as one chart shows:

Should we write these data-enhancement algorithms ourselves? Keras, for example, provides a way to batch process image distortions.

Data enhancement methods in KERAS

The ImageDataGenerator class is provided in KerAS and is constructed as follows:

ImageDataGenerator(featurewise_center=False,
    samplewise_center=False,
    featurewise_std_normalization = False,
    samplewise_std_normalization = False,
    zca_whitening = False,
    rotation_range = 0.,
    width_shift_range = 0.,
    height_shift_range = 0.,
    shear_range = 0.,
    zoom_range = 0.,
    channel_shift_range = 0.,
    fill_mode = 'nearest',
    cval = 0.0,
    horizontal_flip = False,
    vertical_flip = False,
    rescale = None,
    preprocessing_function = None,
    data_format = K.image_data_format(),
)
Copy the code

There are many parameters, commonly used parameters are:

  • Rotation_range: Controls the rotation of a random degree range.
  • Width_shift_range and height_shift_range: for horizontal and vertical shifts, respectively.
  • Zoom_range: Evenly “zoom in” or “zoom out” the image according to the range [1-zoom_range, 1 + zoom_range].
  • Horizontal_flip: Controls whether to flip horizontally.

Refer to the Keras documentation for complete parameter descriptions.

The following code expands a given image to 10 images, but you can expand more:

image = load_img(args["image"]) image = img_to_array(image) image = np.expand_dims(image, axis=0) aug = ImageDataGenerator(rotation_range=30, Width_shift_range =0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest")

aug.fit(image)

imageGen = aug.flow(image, batch_size=1, save_to_dir=args["output"], save_prefix=args["prefix"],
                    save_format="jpeg")

total = 0
for image in imageGen:
  # increment out counter
  total += 1

  if total == 10:
    break
Copy the code

The output directory must exist, or the output directory must exist, or the following error will occur:

Traceback (most recent call last):
  File "augmentation_demo.py", line 35, in <module>
    for image in imageGen:
  File "/ data/ai/anaconda3 envs/keras/lib/python3.6 / site - packages/keras_preprocessing/image. Py." ", line 1526, in __next__
    return self.next(*args, **kwargs)
  File "/ data/ai/anaconda3 envs/keras/lib/python3.6 / site - packages/keras_preprocessing/image. Py." ", line 1704, in next
    return self._get_batches_of_transformed_samples(index_array)
  File "/ data/ai/anaconda3 envs/keras/lib/python3.6 / site - packages/keras_preprocessing/image. Py." ", line 1681, in _get_batches_of_transformed_samples
    img.save(os.path.join(self.save_to_dir, fname))
  File "/ data/ai/anaconda3 envs/keras/lib/python3.6 / site - packages/PIL/Image. Py." ", line 1947, in save
    fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: 'output/image_0_1091.jpeg'
Copy the code

Here’s a picture of a dog:

After data enhancement, the result was the following 10 slightly different dog images, which amounted to a tenfold increase in the original data set, but we could expand it as much as possible:

Comparison after data enhancement

We take the MiniVGGNet model as an example to illustrate the effect of training on the 17Flowers dataset. 17Flowers is a very small data set, containing 17 categories of flower patterns, each category contains 80 pictures, which is too small for deep learning. In general, a deep learning model needs at least 1,000 to 5,000 images per category for accuracy. Such data sets illustrate the need for data enhancement techniques.

All the pictures of 17Flowers data downloaded from the website are placed in a directory, and the directory structure we usually train is as follows:

{category name}/{picture file}Copy the code

To do this, I wrote an organize_flowers17.py script.

In the absence of data enhancement, the curves of accuracy and loss on the training data set and validation data set with the change of training rounds are as follows:

It can be seen that after about a dozen rounds of training, the accuracy rate on the training data set soon reached close to 100%, while the accuracy rate on the verification data set could not be further increased, only reaching about 60%. This figure clearly shows that the model has a very serious over-fitting.

What about data enhancement? The graph is as follows:

As can be seen from the figure, although the accuracy of the training data set has decreased, the accuracy of the verification data set has been significantly improved, indicating that the generalization ability of the model has been enhanced.

Perhaps a 10 percent improvement in accuracy, from more than 60 percent to 70 percent, doesn’t seem like a big deal to us. However, considering that the number of data set samples we use is really small, it is very rare to achieve such an improvement. In actual projects, it sometimes takes a lot of effort to improve the accuracy rate by 1%.

conclusion

Data enhancement technology can improve the generalization ability of the model and reduce overfitting to a certain extent, but in practice, if we can collect more real data, we should try to use real data. In addition, data enhancement only applies to training data sets, not validation sets, where we want to test the accuracy of real data.

All of the above examples have complete code, so click to read the original article to jump to the sample code I built on Github.

In addition, I am reading the book Deep Learning for Computer Vision with Python. I reply the key word “Computer Vision” in the background of wechat official account and can download the electronic version of the book for free.

Refer to the reading

To improve model performance, you can try these tips…

Computer vision and deep learning, just read this book