We can set up and train an MLP in OpenCV with the following recipe:
- Instantiate a new MLP object:
In [9]: import cv2... mlp = cv2.ml.ANN_MLP_create()
- Specify the size of every layer in the network. We are free to add as many layers as we want, but we need to make sure that the first layer has the same number of neurons as input features (784 in our case), and that the last layer has the same number of neurons as class labels (10 in our case), while there are two hidden layers each having 512 nodes:
In [10]: mlp.setLayerSizes(np.array([784, 512, 512, 10]))
- Specify an activation function. Here we use the sigmoidal activation function from before:
In [11]: mlp.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, ...