[PYTHON] Learn how to inflate images from TensorFlow code

Introduction

Anyway, the number of images is required to classify images accurately with deep learning. However, it is difficult to manually prepare and tag a large number of images. Therefore, the number of images is increased (inflated) by processing the tagged images. スクリーンショット 2016-09-07 13.40.40.png

This time, I'd like to learn from the TensorFlow code what to do to inflate.

Specifically, we will learn from the CIFAR-10 code. cifar10/cifar10_input.py

In the actual code, the image was inflated by combining multiple processes as shown below.

# Image processing for training the network. Note the many random
# distortions applied to the image.

# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(reshaped_image, [height, width, 3])

# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)

# Because these operations are not commutative, consider randomizing
# the order their operation.
distorted_image = tf.image.random_brightness(distorted_image, max_delta=63)
distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8)

# Subtract off the mean and divide by the variance of the pixels.
float_image = tf.image.per_image_whitening(distorted_image)

Looking at each one, there were five things in the CIFAR-10 code:

We will understand ** visually ** what each one is doing.

tf.random_crop(value, size, seed=None, name=None) The random_crop function is a function for randomly cropping an image with a given size. The image below is the result of actually cropping a 256x170 image with size = 100x100:

スクリーンショット 2016-09-07 12.26.51.png

The trimming position changes depending on the value given to the seed. If the seed value is the same, the same image will be generated no matter how many times it is executed.

tf.image.random_flip_left_right(image, seed=None)

The random_flip_left_right function is a function for randomly flipping an image horizontally. The image below is the result of actually applying the random_flip_left_right function: スクリーンショット 2016-09-07 13.21.57.png

Since it is probabilistically inverted, it may not be inverted depending on the seed value.

There is also a function similar to random_flip_left_right called random_flip_up_down. While random_flip_left_right flips horizontally, random_flip_up_down flips vertically. ..

tf.image.random_brightness(image, max_delta, seed=None)

The random_brightness function is a function for adjusting the brightness of an image due to a random factor. The image below is the result of actually applying the random_brightness function: スクリーンショット 2016-09-07 13.40.40.png

The degree of brightness changes depending on the values of max_delta and seed.

tf.image.random_contrast(image, lower, upper, seed=None)

The random_contrast function is a function for adjusting the contrast of an image due to a random factor. The image below is the result of actually applying the random_contrast function: スクリーンショット 2016-09-07 13.46.37.png

You can see that Contrast1 has reduced contrast and Contrast2 has enhanced contrast. The lower and upper limits of strength can be adjusted with lower and upper.

tf.image.per_image_whitening(image)

The per_image_whitening function is a function for whitening an image so that the average is 0. The image below is the result of actually applying the per_image_whitening function:

スクリーンショット 2016-09-07 13.50.52.png

Actually, each pixel value is calculated by (x --mean) / adjusted_stddev. mean is the average of all pixel values in the image, and adjusted_stddev is defined as adjusted_stddev = max (stddev, 1.0 / sqrt (image.NumElements ())). Where stddev is the standard deviation of all pixel values in the image.

Other

There are other functions that could be used for padding, though not used in TensorFlow's CIFAR-10 example. I will introduce about 5 of them. tf.image.transpose_image(image) The transpose_image function is a function that transposes an image. The image below is the result of actually applying the transpose_image function: スクリーンショット 2016-09-08 9.08.54.png

Since it is only transposed, the result is the same no matter how many times it is executed. If you transpose the transposed image further, it will return to the original image.

tf.image.rot90(image, k=1) The rot90 function rotates the image counterclockwise every 90 degrees. The image below is the result of actually applying the rot90 function: スクリーンショット 2016-09-08 9.12.28.png

You can specify how many times to rotate by changing the value of k.

tf.image.random_hue(image, max_delta, seed=None) The random_hue function is a function for adjusting the hue of an RGB image due to a random factor. The image below is the result of actually applying the random_hue function: スクリーンショット 2016-09-08 9.17.04.png

max_delta must be in the range 0-0.5.

tf.image.random_saturation(image, lower, upper, seed=None) The random_saturation function is a function for adjusting the saturation of RGB images. The image below is the result of actually applying the random_saturation function: スクリーンショット 2016-09-08 9.21.20.png

Enlargement / reduction

Please refer to the following for scaling.

reference

Recommended Posts

Learn how to inflate images from TensorFlow code
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
[How to!] Learn and play Super Mario with Tensorflow !!
How to create a kubernetes pod from python code
How to learn TensorFlow for liberal arts and Python beginners
How to use SWIG from waf
How to install TensorFlow on CentOS 7
Post images from Python to Tumblr
How to launch Explorer from WSL
How to convert Tensorflow model to Lite
Programming to learn from books May 7
How to access wikipedia from python
How to convert from .mgz to .nii.gz
Migrate from VS Code to PyCharm
How to process camera images with Teams and Zoom Sentiment analysis with Tensorflow
How to create a clone from Github
Reinforcement learning to learn from zero to deep
How to easily convert format from Markdown
Qiita (1) How to write a code name
How to update Google Sheets from Python
How to remove end-of-line code (CR) [^ M]
[TF] How to use Tensorboard from Keras
How to utilize multi-core from multiple languages
Conditional branch to learn from Milk Boy
How to access RDS from Lambda (python)
How to operate Linux from the console
How to use tensorflow under docker environment
How to collect face images relatively easily
How to create a repository from media
How to access the Datastore from the outside
How to view images in Django's Admin
How to draw OpenCV images in Pygame
How to run TensorFlow 1.0 code in 2.0
TensorFlow tutorial tutorial
Learn how to inflate images from TensorFlow code
Preparing to use Tensorflow (Anaconda) with Visual Studio Code
I tried porting the code written for TensorFlow to Theano
[Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format
How to code a drone using image recognition
How to open a web browser from python
Study from Python Hour7: How to use classes
How to get results from id in Celery
[Python] How to read data from CIFAR-10 and CIFAR-100
How to run CNN in 1 system notation in Tensorflow 2
How to generate a Python object from JSON
How to call Cloud API from GCP Cloud Functions
How to operate Linux from the outside Procedure
How to handle Linux commands well from Python
How to extract coefficients from a fractional formula
How to measure line speed from the terminal
How to display images continuously with matplotlib Note
Tensorflow, Tensorflow After all, which one (How to read Tensorflow)
I want to make C ++ code from Python code!
[TF] How to build Tensorflow in Proxy environment
How to Learn Kaldi with the JUST Corpus
How to force build TensorFlow 2.3.0 for CUDA11 + cuDNN8
How to clone github remote repository from atom
How to create a radial profile from astronomical images (Chandra, XMM etc.) using python
Images created with matplotlib shift from dvi to pdf
How to process camera images with Teams or Zoom
[Python] How to remove duplicate values from the list
How to create an article from the command line
I want to detect images of cats from Instagram
How to use Keras ~ From simple model generation to CNN ~
How to scrape image data from flickr with python