[PYTHON] How to increase the number of machine learning dataset images

Significance of image padding

In order to improve the accuracy of classification such as CNN of deep learning, a good training data set is required. In order to ensure accuracy, it is necessary to devise the following.

To do these, you just need a number of sheets. Depending on the type, the number of sheets may be biased, and it is difficult to extract and tag all by hand.

Therefore, we will consider increasing the number of images by processing the images that have been tagged to some extent. Here are some of the techniques. In the explanation, based on the opinion that Reni Takagi should be used as a sample for image processing, Reni Takagi To use.

Inflating method

Implemented in OpenCV 3.0 Python. Here is a sauce that you can actually use for padding.

Github

Contrast adjustment

Creates an image with enhanced and reduced contrast. To emphasize, set 0 for low-brightness pixels below a certain level and 255 for high-brightness pixels above a certain level, and adjust those with intermediate brightness. Conversely, to reduce it, adjust the brightness width to a smaller value. This figure is easy to understand. http://livedoor.blogimg.jp/progr-python2/imgs/f/8/f867a2e8.png

#Look-up table generation
min_table = 50
max_table = 205
diff_table = max_table - min_table

LUT_HC = np.arange(256, dtype = 'uint8' )
LUT_LC = np.arange(256, dtype = 'uint8' )

#High contrast LUT creation
for i in range(0, min_table):
    LUT_HC[i] = 0
for i in range(min_table, max_table):
    LUT_HC[i] = 255 * (i - min_table) / diff_table
for i in range(max_table, 255):
    LUT_HC[i] = 255

#Create low contrast LUT
for i in range(256):
    LUT_LC[i] = min_table + i * (diff_table) / 255

#conversion
src = cv2.imread("reni.jpg ", 1)
high_cont_img = cv2.LUT(src, LUT_HC)
low_cont_img = cv2.LUT(src, LUT_LC)

Contrast emphasized reni_takagi_2012_04_15_1.jpg

Reduced contrast reni_takagi_2012_04_15_2.jpg

reference Image density conversion with tone curve

Gamma conversion

This conversion is used when displaying on a display, and changes the brightness value according to the value of γ. http://livedoor.blogimg.jp/progr-python2/imgs/0/4/043dfda9.png

Replace the contrast adjustment source lookup table here.

#Gamma conversion lookup table
gamma1 = 0.75
gamma2 = 1.5
for i in range(256):
    LUT_G1[i] = 255 * pow(float(i) / 255, 1.0 / gamma1)
    LUT_G2[i] = 255 * pow(float(i) / 255, 1.0 / gamma2)

When γ = 1.5 reni_takagi_2012_04_15_3.jpg

When γ = 0.75 reni_takagi_2012_04_15_2.jpg

reference Lookup table (example of gamma correction)

Smoothing

Smooths the image. Here, the averaging filter is applied.

average_square = (10,10)
src = cv2.imread("reni.jpg ", 1)
blur_img = cv2.blur(src, average_square)

10x10 averaging filter reni_takagi_2012_04_15_5.jpg

reference Smoothing (moving average, Gaussian) filter

Noise based on Gaussian distribution

Add noise to each pixel by adding the generated value based on the Gaussian distribution.

Gaussian noise with σ = 15 reni_takagi_2012_04_15_7.jpg

src = cv2.imread("reni.jpg ", 1)
row,col,ch= src.shape
mean = 0
sigma = 15
gauss = np.random.normal(mean,sigma,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
gauss_img = src + gauss

reference Conversion of image information (brightness data)

Salt & Pepper noise

It's called this because it's a noise like salt and pepper. It is also called impulse noise.

src = cv2.imread("reni.jpg ", 1)
row,col,ch = src.shape
s_vs_p = 0.5
amount = 0.004
sp_img = src.copy()

#Salt mode
num_salt = np.ceil(amount * src.size * s_vs_p)
coords = [np.random.randint(0, i-1 , int(num_salt)) for i in src.shape]
sp_img[coords[:-1]] = (255,255,255)

#Pepper mode
num_pepper = np.ceil(amount* src.size * (1. - s_vs_p))
coords = [np.random.randint(0, i-1 , int(num_pepper)) for i in src.shape]
sp_img[coords[:-1]] = (0,0,0)

Noise on 0.4% pixels reni_takagi_2012_04_15_8.jpg

Invert

Flip left and right and flip up and down.

src = cv2.imread("reni.jpg ", 1)
hflip_img = cv2.flip(src, 1)
vflip_img = cv2.flip(src, 0)

Scaling

Enlarges or reduces part of the image.

src = cv2.imread("reni.jpg ", 1)
hight = src.shape[0]
width = src.shape[1]
half_img = cv2.resize(src,(hight/2,width/2))

Recommended Posts

How to increase the number of machine learning dataset images
How to use machine learning for work? 01_ Understand the purpose of machine learning
How to increase the axis
How to know the port number of the xinetd service
How to get the number of digits in Python
How to increase the processing speed of vertex position acquisition
How to find the optimal number of clusters in k-means
Try to evaluate the performance of machine learning / regression model
How to read the SNLI dataset
Try to evaluate the performance of machine learning / classification model
[Machine learning] I tried to summarize the theory of Adaboost
How to collect machine learning data
People memorize learned knowledge in the brain, how to memorize learned knowledge in machine learning
scikit-learn How to use summary (machine learning)
How to check the version of Django
Record the steps to understand machine learning
How to enjoy Coursera / Machine Learning (Week 10)
Did the number of store closures increase due to the impact of the new coronavirus?
How to find out the number of CPUs without using the sar command
How to use machine learning for work? 02_Overview of AI development project
How to put a line number at the beginning of a CSV file
Memorandum of means when you want to make machine learning with 50 images
How to play a video while watching the number of frames (Mac)
How to calculate the volatility of a brand
Classification of guitar images by machine learning Part 1
How to find the area of the Voronoi diagram
Upload a large number of images to Wordpress
About the development contents of machine learning (Example)
Classification of guitar images by machine learning Part 2
How to identify the system call number ausyscall
Arrangement of self-mentioned things related to machine learning
How to count the number of elements in Django and output to a template
I tried to predict the presence or absence of snow by machine learning.
An introduction to data analysis using Python-To increase the number of video views-
Align the number of samples between classes of data for machine learning with Python
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
Introduction to machine learning
python beginners tried to predict the number of criminals
Count the number of parameters in the deep learning model
Impressions of taking the Udacity Machine Learning Engineer Nano-degree
How to visualize the decision tree model of scikit-learn
Try to estimate the number of likes on Twitter
How to display multiple images of galaxies in tiles
Predict the gender of Twitter users with machine learning
[Blender] How to dynamically set the selection of EnumProperty
Summary of the basic flow of machine learning with Python
Record of the first machine learning challenge with Keras
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
How to access the global variable of the imported module
[Selenium] How to specify the relative path of chromedriver?
I want to increase the security of ssh connections
I tried to compress the image using machine learning
The first step of machine learning ~ For those who want to implement with python ~
How to identify the element with the smallest number of characters in a Python list?
Introduction to machine learning ~ Let's show the table of K-nearest neighbor method ~ (+ error handling)
How to count the number of occurrences of each element in the list in Python with weight
How to make a face image data set used in machine learning (2: Frame analysis of video to obtain candidate images)
[Part 4] Use Deep Learning to forecast the weather from weather images
[Ubuntu] How to delete the entire contents of a directory
10. Counting the number of lines