Constructing a very simple neural network inorder to train Contour Images and predict the classification

asked 2018-11-02 09:06:19 -0500


I have some contours data (in 2D).I would like to construct a neural network using openCv Library, C++. I would like to know about the following:

1) Since,I have only my contours data,I need to generate Images for each of my contour data. So, I could use, drawPolyline() function of openCv and generate the image. Since, each of my contours can be in any orientation as well as of different sizes, I would like to know the preprocessing techniques which needs to be done,while generating each of my Image,from the contour data.

2) I initially have 5 classifications, lets say A,B,C,D and E.So,once I have my Image Inputs ready, I would like to develop a very simple neural network,which needs to train images and classify it as A,B,C,D and E.

3) In case,I have a complete different contour,I donot want to classify it as any of the above 5 categories and just return as "Unknown Entity".

I would be really glad,if someone can help me in providing basic code and also help in preparing the input (like the size of the image, necessary image compressions etc. )

edit retag flag offensive close merge delete


What do you know about the standard feedforward back-propagation artificial neural network? Have you implemented anything like the XOR problem? Are you familiar with the loss function?

sjhalayka gravatar imagesjhalayka ( 2018-11-02 09:49:03 -0500 )edit


This is a code that trains the an to solve the XOR problem.

Also see:

This is a code that trains the ANN to classify images. It doesn't use a loss function, so it's kind of basic.

sjhalayka gravatar imagesjhalayka ( 2018-11-02 10:09:25 -0500 )edit

Definitely look up the loss function.

sjhalayka gravatar imagesjhalayka ( 2018-11-02 10:14:30 -0500 )edit

Thanks a lot! I will look into it! Please suggest some some basic code,which can work correctly in my case. I will try that code and update you! Also,please throw some light regarding the size of the image etc.

Programming_Enthusiast gravatar imageProgramming_Enthusiast ( 2018-11-02 10:15:35 -0500 )edit

It depends on your patience and the amount of memory you have. I suggest using grayscale 64x64 input images, which leads to 4096 input neurons. You can try with one hidden layer of say 64 neurons. Finally, if you're using one-hot encoding you will need 6 output neurons -- one each for the classes A, B, C, D, E, and F (AKA `other'). If you're using binary encoding then you'll need ceiling(ln(number of classes)/ln(2)) = 3 output neurons. The examples I gave you use binary encoding.

Unfortunately, training for images doesn't get much simpler than the code that I already gave you. As I mentioned before, I'm not even checking the loss function.

sjhalayka gravatar imagesjhalayka ( 2018-11-02 10:32:07 -0500 )edit

If you are trying to classify contours, why not do so with contour features as input to the network instead of pixels? OpenCV only offers training for fully connected networks, but with images you're better off using convolutional networks. Try Hu and Flusser moments and other contour features like compactness, fractional fill, etc. as input to the MLP. Pixel inputs are likely not the way to go via OpenCV. Look at Mahalanobis distance as an alternative to a neural network.

Der Luftmensch gravatar imageDer Luftmensch ( 2018-11-02 14:20:59 -0500 )edit

Regarding convolutional neural networks, here is some code by @berak that shows how to use the CNN to classify between cats and dogs. I don't know if it'll help you though:

sjhalayka gravatar imagesjhalayka ( 2018-11-02 15:02:59 -0500 )edit

@Der Luftmensch: There are many problems with building the model with contours.Because, each of my contour has different orientations, starting points.So, feeding the contour data would be a bit difficult. @sjhalayka: Dont you think,I can use cv:polyline and create Images of Line,circle, ellipse and arc? Then train my model using these images and predict the classification. Please do help!

Programming_Enthusiast gravatar imageProgramming_Enthusiast ( 2018-11-02 17:43:31 -0500 )edit

I'm afraid that I am not experienced enough with convolutional neural networks. :(

sjhalayka gravatar imagesjhalayka ( 2018-11-02 18:28:42 -0500 )edit

@Programming_Enthusiast A contour is a shape. You are interested in classifying contours invariant to size and orientation? Think a little longer on my suggestions, they are not misguided. Look at what Hu and Flusser moments actually are before dismissing them. Feeding hand-crafted high-level features to an MLP is simple. I have done exactly this and it was straight-forward and gave great results. Look at the MNIST dataset if you insist on pixel input.

Der Luftmensch gravatar imageDer Luftmensch ( 2018-11-02 22:23:57 -0500 )edit