Python image Processing (14): Neural network classifier

Source: Internet
Author: User

Happy Shrimp

http://blog.csdn.net/lights_joy/

Welcome reprint, but please keep the author information


in the OpenCV The neural network classifier is supported. This article attempts to invoke it in Python.


Same as the previous Bayesian classifier. Neural networks also follow the method of training and re-use, we directly in the Bayesian classifier test code to make simple changes, complete the classification of two types of data points.


The first is to create the data for the training first:


# training Points train_pts = 30# Create test data points, Class 2 # with (-1.5,-1.5) as Center Rand1 = np.ones ((train_pts,2)) * ( -2) + Np.random.rand (train_pts, 2) PR Int (' RAND1: ') print (RAND1) # with (1.5, 1.5) as Center Rand2 = np.ones ((train_pts,2)) + Np.random.rand (train_pts, 2) print (' Rand2: ') Print (rand2) # Merge random points, get 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 ')) # Show Training Data plt.figure (1) plt.plot (rand1[:,0], rand1[:,1], ' O ') plt.plot (rand2[:,0], rand2[:,1], ' O ')

Similar to this data:



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


# Create Network Ann = Cv2.ml.ANN_MLP_create () ann.setlayersizes (Np.array ([2, ten, 1])  # This line ann.setactivationfunction (Cv2.ml.ANN_MLP_SIGMOID_SYM) Ann.settrainmethod (Cv2.ml.ANN_MLP_BACKPROP) must be run first Ann.setbackpropweightscale (0.1) Ann.setbackpropmomentumscale (0.1)

because our input is the coordinate value of the data point, the output is the category to which this data point belongs. So the input layer of this network has 2 nodes, and the output has only one node. There are two hidden layers in the middle. Each has a total of ten nodes.


We then train on this network:

# TRAINING ret = Ann.train (Train_data, Cv2.ml.ROW_SAMPLE, Train_label)

After training, the test data can be used to predict:

# test data, 20 dots [ -2,2]pt = Np.array (Np.random.rand (20,2) * 4-2, dtype= ' float32 ') (ret, res) = Ann.predict (PT)

predict through Res return to get a 20x1 Array, one input point for each line. Because we chose sigmoid as the activation function. So the computed value is a floating-point number between [0,1] , and we take 0.5 as a threshold to classify and display the result:

# Categorized by label Plt.figure (2) res = Np.hstack (res, RES) # First Class 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 ') # Second Class Type_data = pt[res >= 0.5]type_data = NP.R Eshape (Type_data, (Type_data.shape[0]/2, 2)) Plt.plot (type_data[:,0], type_data[:,1], ' O ')

Look at the final result:













??

Python image Processing (14): Neural network classifier

Related Article

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.