Neural network with OpenCV 3 and Python 3

Introduction

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

program

This is the minimum program. Input layer: 9 Hidden layer: 5 Output layer: 9

nn_mini.png

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)

Execution 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.

Activation function

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 f(x)=x
Sigmoid function 1 cv2.ml.ANN_MLP_SIGMOID_SYM f(x)=\beta*(1-e^{-\alpha x})/(1+e^{-\alpha x})
Gaussian function 2 cv2.ml.ANN_MLP_GAUSSIAN f(x)=\beta e^{-\alpha x*x}

Exit conditions

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

Backpropagation settings

  1. Moment coefficient You can set the moment coefficient for the difference between the previous iteration and the previous iteration. If you set this value to some extent, you can prevent it from getting stuck in the local solution and accelerate the convergence. However, if you set it too large, it may not converge easily at the end, or it may diverge in some cases. As a rule of thumb, about 0.1 is good. The default value is set to 0.1.

Specification example:

```momentum_scale.py
ann.setBackpropMomentumScale(0.1)
```
  1. Learning coefficient A derivative that adjusts the learning speed. If it is too small, it will not converge easily, and if it is too large, it may diverge. As a rule of thumb, about 0.1 is good. The default value is set to 0.1.

Specification example:

```weight_scale.py
ann.setBackpropWeightScale(0.1)
```
  1. Resilient backpropagation OpenCV3 supports Resilient backpropagation as well as backpropagation that specifies the above two parameters. Resilient backpropagation is an algorithm that changes the learning coefficient depending on whether the sign of the learning gradient has changed between the previous time and this time.

Click here for details on how to specify (link)

Relationship between 3-layer neural network and image processing

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

Neural network with OpenCV 3 and Python 3
Neural network with Python (scikit-learn)
Shining life with Python and OpenCV
Capturing images with Pupil, python and OpenCV
2. Mean and standard deviation with neural network!
Binarization with OpenCV / Python
Hello World and face detection with OpenCV 4.3 + Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
Template network config generation with Python and Jinja2
Programming with Python and Tkinter
Encryption and decryption with Python
"Apple processing" with OpenCV3 + Python3
Python and hardware-Using RS232C with Python-
Image editing with python OpenCV
Camera capture with Python + OpenCV
3. Normal distribution with neural network!
[Python] Using OpenCV with Python (Basic)
Neural network starting with Chainer
Face detection with Python + OpenCV
Neural network implementation in python
4. Circle parameters with neural network!
python with pyenv and venv
Network programming with Python Scapy
Using OpenCV with Python @Mac
Works with Python and R
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
[Python] Webcam frame size and FPS settings with OpenCV
Reading, displaying and speeding up gifs with python [OpenCV]
Communicate with FX-5204PS with Python and PyUSB
Environment construction of python and opencv
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
[Python] Using OpenCV with Python (Image Filtering)
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Image classification with self-made neural network by Keras and PyTorch
[Python] Using OpenCV with Python (Image transformation)
Measuring network one-way delay with python
Scraping with Python, Selenium and Chromedriver
[Python] Using OpenCV with Python (Edge Detection)
PRML Chapter 5 Neural Network Python Implementation
Scraping with Python and Beautiful Soup
Simple classification model with neural network
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
Easy Python + OpenCV programming with Canopy
[GUI with Python] PyQt5-Drag and drop-
[TensorFlow] [Keras] Neural network construction with Keras
Reading and writing NetCDF with Python
Simple neural network theory and implementation
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
I played with PyQt5 and Python3
Load gif images with Python + OpenCV
Reading and writing CSV with Python
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Multiple integrals with Python and Sympy