Image recognition with keras

Last time Create an image recognition program that distinguishes monkeys, wild boars and crows using the created training data

What is keras

https://keras.io/ja/

Keras is a high-level neural network library written in Python that can be run on TensorFlow, CNTK, and Theano.

Source code

import

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.utils import np_utils
import keras
import numpy as np

Main function

The general flow is as follows:

  1. Load the previously saved ʻanimal.npy`
  2. Pass the training data to the training function to create a model
  3. Pass the created model to the evaluation function to verify the classification accuracy.
classes = ["monkey", "boar", "crow"]
num_classes = len(classes)
image_size = 50

def main():
    X_train, X_test, y_train, y_test = np.load("./animal.npy", allow_pickle=True)
    X_train = X_train.astype("float") / 256
    X_test = X_test.astype("float") / 256
    y_train = np_utils.to_categorical(y_train, num_classes)
    y_test = np_utils.to_categorical(y_test, num_classes)

    model = model_train(X_train, y_train)
    model_eval(model, X_test, y_test)

-The reason why X_train and X_test are divided by 256 is normalization (the data is in the range of 0 to 1).

-To_categorical has an array of[0, 1, 2]

[[1, 0, 0]
 [0, 1, 0]
 [0, 0, 1]]

So, convert to an array that contains 1 only in the index of the correct answer and 0 in other places.

Training

Refer to the 34th line and after of the following code Reference: https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py

def model_train(X, y):
    model = Sequential()
    model.add(Conv2D(32, (3,3), padding='same', input_shape=X.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(3))
    model.add(Activation('softmax'))

    opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)

    model.compile(loss='categorical_crossentropy',
                    optimizer=opt, metrics=['accuracy'])

    model.fit(X, y, batch_size=32, epochs=100)

    #Save model
    model.save("./animal_cnn.h5")

    return model

model = Sequential() Create a model object

model.add(Conv2D(...)) Add a layer

model.add(Activation('relu')) Add a layer that uses "ReLU (Rectified Linear Unit)" as the activation function

model.add(MaxPooling2D(pool_size=(2,2))) Add a layer for max pooling (dividing the input into predetermined areas and outputting the maximum value of each area)

model.add(Dropout(0.25)) Discard 25% (what?) To eliminate data bias

opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) Declaration of optimization. With the method of ʻoptimizers.rmsprop`, the learning rate is 0.0001, and the rate of lowering the learning rate is 0.000001 each time.

model.compile() Compiling the model.

model.fit(X, y, batch_size=32, epochs=100) Train with X and y. batch_size is the number of data used in one training. ʻEpochs` How many sets of training do you do?

Evaluation of training results

ʻVerify the model created by the evaluate` method.

def model_eval(model, X, y):
    scores = model.evaluate(X, y, verbose=1)
    print('test Loss: ', scores[0])
    print('test Accuracy: ', scores[1])

Recommended Posts

Image recognition with keras
Image recognition with Keras + OpenCV
Image recognition
Face recognition of anime characters with Keras
I tried simple image recognition with Jupyter
Image processing with MyHDL
I tried image recognition of CIFAR-10 with Keras-Learning-
Face recognition with Edison
I tried image recognition of CIFAR-10 with Keras-Image recognition-
CIFAR-10 tutorial with Keras
Do image recognition with Caffe model Chainer Yo!
Pepper Tutorial (7): Image Recognition
Image processing with Python
Multivariate LSTM with Keras
CNN 1 Image Recognition Basics
Image Processing with PIL
Deep learning image analysis starting with Kaggle and Keras
Identify the name from the flower image with keras (tensorflow)
Challenge image classification with TensorFlow2 + Keras 3 ~ Visualize MNIST data ~
Until you can do simple image recognition with Jupyter
Image download with Flickr API
Image processing with Python (Part 2)
Read image coordinates with Python-matplotlib
Image processing with PIL (Pillow)
Face recognition with Python's OpenCV
Install Keras (used with Anaconda)
Image editing with python OpenCV
Deep learning image recognition 1 theory
Auto Encodder notes with Keras
Face recognition with Amazon Rekognition
Implemented word2vec with Theano + Keras
Sentence generation with GRU (keras)
Image upload & customization with django-ckeditor
Tuning Keras parameters with Keras Tuner
Sorting image files with Python (2)
Sorting image files with Python (3)
Easy image classification with TensorFlow
Create Image Viewer with Tkinter
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Application of CNN2 image recognition
Easily build CNN with Keras
Image processing with Python (Part 3)
Image caption generation with Chainer
Get image features with OpenCV
Implemented Efficient GAN with keras
Face recognition / cutting with OpenCV
Try face recognition with Python
[Python] Image processing with scikit-image
CNN with keras Try it with the image you picked up
Image classification with self-made neural network by Keras and PyTorch
Image recognition with API from zero knowledge using AutoML Vision
Real-time image recognition on mobile devices with TensorFlow learning model
I tried handwriting recognition of runes with CNN using Keras
Number recognition in images with Python
MNIST (DCNN) with Keras (TensorFlow backend)
Cut out an image with python
Real-time image processing basics with opencv
Challenge image classification by TensorFlow2 + Keras 4 ~ Let's predict with trained model ~
[Python] Using OpenCV with Python (Image Filtering)