Training data by CNN

Try with the image you picked up by CNN with keras The basics were created by referring to this article.

It's just a subtle difference.

I investigated the details myself and managed to do it. I haven't studied enough because I'm Yoshi because I moved a lot.

The purpose is to judge OK or NG from a photo of a certain thing. Image data (640x480) is used as the original data. I use 1000 sheets for both OK and NG.

The purpose is to keep a memo of the results that beginners have reached.

environment

Windows10 64bit Anaconda navigator Jupiter notebook Python 3.7.7 tensorflow 2.1.0 tensorflow-gpu 2.1.0 keras 2.3.1 numpy 1.18.1 matplotlib 3.1.3

Build a program in the above environment

Library

cnn.ipynb


import keras
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Activation, Conv2D, Flatten, Dense,Dropout
from sklearn.model_selection import train_test_split
from keras.optimizers import SGD, Adadelta, Adagrad, Adam, Adamax, RMSprop, Nadam
from PIL import Image
import numpy as np
import glob
import matplotlib.pyplot as plt
import time
import os

I only needed ʻAdadelta for from keras.optimizers import` I want to try various things and put it in. (I didn't test it after all ...)

Various settings

cnn.ipynb


#Setting
batch_size = 16
epochs = 200

#Image directory
test_dir ="Specify the directory where images are saved"
folder = os.listdir(test_dir)

#Size setting when resizing the original image 640x480
x_image_size = 200#640→200
y_image_size = 200#480→200
dense_size  = len(folder)

As for batch_size, the setting values such as 1, 4, 32, etc. were rolling to see various sites. At first, I tried turning it at 32, but I got an error and it didn't work, so I set it to 16. I can't say that it's correct because I haven't investigated all the details ... I just turned around, so I'm using it.

I set it separately to try different image sizes vertically and horizontally. As a result, I had to drop it to 200 or it was too heavy to move. So ʻimage_size = 200` is fine. (The later description needs to be partially changed)

Image loading

cnn.ipynb


X = []
Y = []
for index, name in enumerate(folder):
    dir = test_dir + name
    files = glob.glob(dir + "/*.jpg ")
    for i, file in enumerate(files):
        image = Image.open(file)
        image = image.convert("RGB")
        image = image.resize((x_image_size, y_image_size))
        data = np.asarray(image)
        X.append(data)
        Y.append(index)

X = np.array(X)
Y = np.array(Y)
X = X.astype('float32')
X = X / 255.0

Y = np_utils.to_categorical(Y, dense_size)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.15)

This time, the test was set to 15%, which is almost the same as the article I referred to.

CNN model creation

cnn.ipynb


model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',input_shape=X_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(dense_size))
model.add(Activation('softmax'))

model.summary()

To be honest, I'm studying Where and how do you touch it to change it? I don't know about. keras official website I'm looking here, but amateurs can't understand ...

Learning machine creation

cnn.ipynb


optimizers ="Adadelta"
results = {}

model.compile(loss='categorical_crossentropy', optimizer=optimizers, metrics=['accuracy'])
results[0]= model.fit(X_train, y_train, validation_split=0.2, epochs=epochs,batch_size=batch_size)

model_json_str = model.to_json()
open('model.json', 'w').write(model_json_str)
model.save('weights.h5');

To be honest, I couldn't even reach it without Qiita. Thank you very much.

result

Learning results 2020-09-22 (3).png

test results 2020-09-22 (4).png

At first, the result was terrible and the learning result became a horizontal bar. At that time, the image data is as small as about 50 each. The number of epochs was also about 20.

Someone told me that "images are life", and I think that was exactly the result.

After that, I am thrilled to make inferences based on this data and get the results properly.

Summary

For some reason, I was able to create it as data. I realized that the "try various things" part is overwhelmingly lacking. I was able to know again that I can't remember or try to know unless I move my hand.

If you start before the age of 40, it will be difficult because your brain will refuse.

that's all.

Recommended Posts

Training data by CNN
Creating training data
Correlation by data preprocessing
Gzip the data by streaming
Visualization of data by prefecture
Data acquired by Django releted
Depth estimation by CNN (CNN SLAM # 3)
First satellite data analysis by Tellus
Data Scientist Training Course Chapter 2 Day 2
Data Scientist Training Course Chapter 3 Day 3
Data Scientist Training Course Chapter 4 Day 1
Data Scientist Training Course Chapter 3 Day 1 + 2
Pandas Cleansing Labeled Training Data Split
10 selections of data extraction by pandas.DataFrame.query
Animation of geographic data by geopandas
ECG data anomaly detection by Matrix Profile
Organize data divided by folder with Python
I did Python data analysis training remotely