In [1]:
import cv2
import numpy as np
import glob
import sys
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
print ("OpenCV:",  cv2.__version__)
print ("Numpy : ", np.__version__)
print ("Python:",  sys.version)


OpenCV: 3.4.2
Numpy :  1.15.0
Python: 3.7.0 (default, Jun 28 2018, 13:15:42) 
[GCC 7.2.0]

In [3]:
# load training data
dim = 240*320
X = np.empty((0, dim))
y = np.empty((0, 4))
training_data = glob.glob('data_test.npz')

for single_npz in training_data:
    with np.load(single_npz) as data:
        train = data['train']
        train_labels = data['train_labels']
    X = np.vstack((X, train))
    y = np.vstack((y, train_labels))

print ('Image array shape: ', X.shape)
print ('Label array shape: ', y.shape)


Image array shape:  (10, 76800)
Label array shape:  (10, 4)

In [4]:
plt.imshow(X[0].reshape(240, 320), cmap='gray')


Out[4]:
<matplotlib.image.AxesImage at 0x7fe4a38fe8d0>

In [5]:
# create a neural network
model = cv2.ml.ANN_MLP_create()
layer_sizes = np.int32([dim, 32, 4])
model.setLayerSizes(layer_sizes)
model.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
model.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, 2, 1)
model.setTermCriteria((cv2.TERM_CRITERIA_COUNT, 20, 0.01))

In [6]:
# training
model.train(np.float32(X), cv2.ml.ROW_SAMPLE, np.float32(y))


Out[6]:
True

In [7]:
# evaluate on training data
ret, resp = model.predict(X)
prediction = resp.argmax(-1)
true_labels = y.argmax(-1)

train_rate = np.mean(prediction == true_labels)
print (len(prediction))
print (prediction)
print ('Train accuracy: ', "{0:.2f}%".format(train_rate * 100))


10
[1 1 2 2 2 2 2 2 2 2]
Train accuracy:  80.00%

In [8]:
# save model
model.save('model_test.xml')

In [ ]:


In [ ]:


In [9]:
# load model
model = cv2.ml.ANN_MLP_load('model_test.xml')

In [10]:
# predict
ret, resp = model.predict(X)
print (len(resp))
resp.argmax(-1)


10
Out[10]:
array([1, 1, 2, 2, 2, 2, 2, 2, 2, 2])

In [ ]: