[For beginners of deep learning] Implementation of simple binary classification by full coupling using Keras

Overview

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.

environment

Directory structure

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

data

This is what you see in each directory.

Learning

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

#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, "%")

result

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

[For beginners of deep learning] Implementation of simple binary classification by full coupling using Keras
Anomaly detection by autoencoder using keras [Implementation example for beginners]
Implementation example of hostile generation network (GAN) by keras [For beginners]
Deep learning learned by implementation 2 (image classification)
Parallel learning of deep learning by Keras and Kubernetes
Implementation of Deep Learning model for image recognition
Deep learning learned by implementation (segmentation) ~ Implementation of SegNet ~
Deep reinforcement learning 2 Implementation of reinforcement learning
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
Deep learning learned by implementation 1 (regression)
I tried using the trained model VGG16 of the deep learning library Keras
Deep Learning beginners tried weather forecasting from meteorological satellite images using Keras
Othello-From the tic-tac-toe of "Implementation Deep Learning" (3)
Machine learning algorithm (implementation of multi-class classification)
Implementation and explanation using XGBoost for beginners
Deep Understanding Object Detection by Deep Learning by Keras
[Deep learning] Nogizaka face detection ~ For beginners ~
Othello-From the tic-tac-toe of "Implementation Deep Learning" (2)
[For beginners of artificial intelligence] Machine learning / Deep Learning Programming Learning path and reference books
[Super Introduction] Machine learning using Python-From environment construction to implementation of simple perceptron-
A memorandum of method often used in machine learning using scikit-learn (for beginners)
Classification of guitar images by machine learning Part 1
Recommended study order for machine learning / deep learning beginners
Deep learning learned by implementation ~ Anomaly detection (unsupervised learning) ~
[Learning memo] Deep Learning from scratch ~ Implementation of Dropout ~
Classification of guitar images by machine learning Part 2
Techniques for understanding the basis of deep learning decisions
Othello ~ From the tic-tac-toe of "Implementation Deep Learning" (4) [End]
[Linux] Basics of authority setting by chmod for beginners
Implementation of Chainer series learning using variable length mini-batch
Collection and automation of erotic images using deep learning