With the advent of deep learning, it is now possible to perform AI tasks with better accuracy than conventional machine learning. However, deep learning is still in the development stage, and there is no established method to do this. Also, because it is in the research stage, there are many cases where implementation and control are complicated.
This time, I will introduce a simple binary classification for the purpose of making the implementation as simple as possible and trying to move deep learning.
This time, we will use deep learning to classify two images of apples and oranges. For the DL framework, we adopted Keras, which is difficult to tune but easy to use.
Prepare the execution module and the directory for data storage
├── data
└── exe.py
In the data directory, prepare the training data train and the test data test, and store the images of apples and oranges, respectively.
├── test
│ ├── 00_apple
│ └── 01_orange
└── train
├── 00_apple
└── 01_orange
This is what you see in each directory.
Apple
Orange
from keras.utils.np_utils import to_categorical
from keras.optimizers import Adagrad
from keras.optimizers import Adam
import numpy as np
from PIL import Image
import os
#Teacher data reading
train_path="./data/train/"
test_path="./data/test/"
xsize=25
ysize=25
image_list = []
label_list = []
for dataset_name in os.listdir(train_path):
dataset_path = train_path + dataset_name
label = 0
if dataset_name == "00_apple":
label = 0
elif dataset_name == "01_orange":
label = 1
for file_name in sorted(os.listdir(dataset_path)):
label_list.append(label)
file_path = dataset_path + "/" + file_name
image = np.array(Image.open(file_path).resize((xsize, ysize)))
print(file_path)
#Convert in RGB order,[[Red array],[Green array],[Blue array]]
image = image.transpose(2, 0, 1)
#Convert to a one-dimensional array(25*25*3) Red,Green,The elements of Blue are arranged in order.
image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0]
#Convert to the range 0 to 1
image_list.append(image / 255.)
#numpy conversion.
X = np.array(image_list)
# label=0 -> [1,0], label=1 -> [0,1]Conversion to
Y = to_categorical(label_list)
#Model definition
model = Sequential()
model.add(Dense(200, input_dim=xsize*ysize*3))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(200))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(2))
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=0.001), metrics=["accuracy"])
model.summary()
#Learning
model.fit(X, Y, nb_epoch=1000, batch_size=100, validation_split=0.1)
CNN is often used when classifying images, but for the sake of simplicity, only full combination was used this time. Also, when extracting shape features from an image, it is often grayscaled so that unnecessary information is not included, but it is judged that color information is important because it is a classification of apples and oranges. However, all the RGB information is passed to the input of the neural network without grayscale.
#inference
total = 0.
ok_count = 0.
for testset_name in os.listdir(test_path):
testset_path = test_path + testset_name
label = -1
if testset_name == "00_apple":
label = 0
elif testset_name == "01_orange":
label = 1
else:
print("error : label not exist")
exit()
for file_name in os.listdir(testset_path):
label_list.append(label)
file_path = testset_path + "/" + file_name
image = np.array(Image.open(file_path).resize((25, 25)))
print(file_path)
image = image.transpose(2, 0, 1)
image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0]
result = model.predict_classes(np.array([image / 255.]))
print("label:", label, "result:", result[0])
total += 1.
if label == result[0]:
ok_count += 1
print("accuracy: ", ok_count / total * 100, "%")
accuracy: 100.0 %
Even though deep learning is highly accurate, the accuracy rate is not quite 100%. However, this time it was a simple classification of apples and oranges, so the correct answer rate was 100%.
Recommended Posts