Python Image Processing (14): Neural Network Classifier and python Image Processing

Source: Internet
Author: User

Python Image Processing (14): Neural Network Classifier and python Image Processing

Happy shrimp

Http://blog.csdn.net/lights_joy/

Reprinted, but keep the author information


Opencv supports neural network classifier. This article attempts to call it in python.


Like the Bayesian classifier, the neural network adopts the training method before use. We directly modify the test code of the Bayesian classifier to classify the two types of data points.


The first step is to first create training data:


# Training Points train_pts = 30 # create test data points. For example, type 2 # Use (-1.5,-1.5) as the center rand1 = np. ones (train_pts, 2) * (-2) + np. random. rand (train_pts, 2) print ('rand1: ') print (rand1) # centered on (1.5, 1.5) rand2 = np. ones (train_pts, 2) + np. random. rand (train_pts, 2) print ('rand2: ') print (rand2) # merge random points to obtain training data train_data = np. vstack (rand1, rand2) train_data = np. array (train_data, dtype = 'float32') train_label = np. vstack (np. zeros (train_pts, 1), dtype = 'float32'), np. ones (train_pts, 1), dtype = 'float32') # displays the training data plt. figure (1) plt. plot (rand1 [:, 0], rand1 [:, 1], 'O') plt. plot (rand2 [:, 0], rand2 [:, 1], 'O ')

Similar data:



After obtaining the training data, create a network and configure the training parameters:


# Create a network ann = cv2.ml. ANN_MLP_create () ann. setLayerSizes (np. array ([2, 10, 10, 1]) # You must first execute this line ann. setActivationFunction (cv2.ml. ANN_MLP_SIGMOID_SYM) ann. setTrainMethod (cv2.ml. ANN_MLP_BACKPROP) ann. setBackpropWeightScale (0.1) ann. setBackpropMomentumScale (0.1)

Because our input is the coordinate value of the data point and the output is the category of the data point, the input layer of this network has two nodes, and the output only has one node. There are two hidden layers, each with 10 nodes.


Then we train this network:

# Training ret = ann. train (train_data, cv2.ml. ROW_SAMPLE, train_label)

After training, you can use the test data for prediction:

# Test data, 20 points [-2, 2] pt = np. array (np. random. rand (20, 2) * 4-2, dtype = 'float32') (ret, res) = ann. predict (pt)

Predict returns a 20x1 array through res. Each row corresponds to an input point. Because sigmoid is selected as the activation function, the calculated value is a floating point, we take 0.5 as the threshold value for classification and display the result:

# Display plt by label. figure (2) res = np. hstack (res, res) # type 1 type_data = pt [res <0.5] type_data = np. reshape (type_data, (type_data.shape [0]/2, 2) plt. plot (type_data [:, 0], type_data [:, 1], 'O') # type 2 type_data = pt [res> = 0.5] type_data = np. reshape (type_data, (type_data.shape [0]/2, 2) plt. plot (type_data [:, 0], type_data [:, 1], 'O ')

Check the final result:













Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.