This time, I will try to learn a neural network using OpenCV3 and Python3. Since the case of using image processing and neural network in combination is completed only with OpenCV, it is convenient for a little verification. Also, OpenCV's Python bindings are actually C ++, and Python is just a wrapper that calls C ++ methods, so learning works reasonably fast.
■ Execution environment -Python: 3.5.2 ・ OpenCV: 3.1
■ Click here for installation and easy usage Install OpenCV 3 (core + contrib) in Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & simple operation check
This is the minimum program. Input layer: 9 Hidden layer: 5 Output layer: 9
Activation function: sigmoid Learning method: Backpropagation Learning data: ·Input data [[1.2, 1.3, 1.9, 2.2, 2.3, 2.9, 3.0, 3.2, 3.3]] ·output data [[0, 0, 0, 0, 0, 1, 0, 0, 0]] Validation data: ·Input data [[1.4, 1.5, 1.2, 2.0, 2.5, 2.8, 3.0, 3.1, 3.8]]
ann.py
# -*- coding: utf-8 -*-
import cv2
import numpy as np
#Generate neural network
ann = cv2.ml.ANN_MLP_create()
#Input layer, hidden layer, output layer settings
ann.setLayerSizes(np.array([9, 5, 9], dtype = np.uint8))
#Learning method settings
ann.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
#Learning
ann.train(np.array([[1.2, 1.3, 1.9, 2.2, 2.3, 2.9, 3.0, 3.2, 3.3]], dtype=np.float32),
cv2.ml.ROW_SAMPLE,
np.array([[0, 0, 0, 0, 0, 1, 0, 0, 0]], dtype=np.float32))
#Verification
result = ann.predict(np.array([[1.4, 1.5, 1.2, 2.0, 2.5, 2.8, 3.0, 3.1, 3.8]], dtype = np.float32))
print(result)
The execution result is as follows.
(5.0, array([[-0.06419383, -0.13360272, -0.1681568 , -0.18708915, 0.0970564 , 0.89237726, 0.05093023, 0.17537238, 0.13388439]], dtype=float32))
The output for the input [[1.4, 1.5, 1.2, 2.0, 2.5, 2.8, 3.0, 3.1, 3.8]] is [[-0.06419383, -0.13360272, -0.1681568, -0.18708915, 0.0970564, 0.89237726, 0.05093023, 0.17537238, 0.13388439]] It was. Since the output to be trained is [[0, 0, 0, 0, 0, 1, 0, 0, 0]], the input at the time of verification has some fluctuations with respect to the input at the time of learning. The verification result is within the range of -0.1 to +0.1, which is almost the intended output.
The default activation function is the sigmoid function.
setActivationFunction(type, param1, param2)
Specify with.
Specification example:
activate_function.py
ann.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, 0, 0)
argument | meaning | Default |
---|---|---|
type | Activation function | Sigmoid function |
param1 | α | 0 |
param2 | β | 0 |
method | value | constant | Formula |
---|---|---|---|
Identity function | 0 | cv2.ml.ANN_MLP_IDENTITY | |
Sigmoid function | 1 | cv2.ml.ANN_MLP_SIGMOID_SYM | |
Gaussian function | 2 | cv2.ml.ANN_MLP_GAUSSIAN |
The default settings for the end condition are both maximum number of repetitions: 1000 and minimum change amount: 0.01.
setTermCriteria(val)
Specify with.
Specification example:
criteria.py
criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 1000, 0.01)
ann.setTermCriteria(criteria)
criteria sets the values in the order of (type, maxCount, epsilon).
・ Type
constant | meaning | value |
---|---|---|
cv2.TERM_CRITERIA_COUNT | Maximum number of repetitions | 1 |
cv2.TERM_CRITERIA_MAX_ITER | Maximum number of repetitions | 1 |
cv2.TERM_CRITERIA_EPS | Minimum change | 2 |
Specification example:
```momentum_scale.py
ann.setBackpropMomentumScale(0.1)
```
Specification example:
```weight_scale.py
ann.setBackpropWeightScale(0.1)
```
Click here for details on how to specify (link)
Introduced ** Bag Of Visual Words (BOW) ** in "Learning to classify dogs and cats with OpenCV 3" However, it is well known how to create neural network inputs with BOW. I would like to introduce this area again at a later date.
Recommended Posts