Skip to content

Commit 9d2ae19

Browse files
committed
initial python3 / OpenCV 3.1 version
1 parent 16a11a1 commit 9d2ae19

File tree

3 files changed

+567
-0
lines changed

3 files changed

+567
-0
lines changed

neuralnetwork/nnetwork1.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#####################################################################
2+
3+
# Example : Neural Network based learning
4+
# basic illustrative python script
5+
6+
# For use with test / training datasets : spambase.{train | test}
7+
8+
# Author : Toby Breckon, [email protected]
9+
10+
# Copyright (c) 2014 / 16 School of Engineering & Computing Sciences,
11+
# Durham University, UK
12+
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
13+
14+
#####################################################################
15+
16+
import csv
17+
import cv2
18+
import numpy as np
19+
20+
########### Define classes
21+
22+
classes = {1 : 'spam', 0 : 'ham (not spam)'}
23+
24+
#####################################################################
25+
26+
########### construct output layer
27+
28+
# expand training responses defined as class labels {0,1...,N} to output layer
29+
# responses for the OpenCV MLP (Neural Network) implementation such that class
30+
# label c becomes {0,0,0, ... 1, ...0} where the c-th entry is the only non-zero
31+
# entry (equal to "value", conventionally = 1) in the N-length vector
32+
33+
# labels : a row vector of class label transformed to {0,0,0, ... 1, ...0}
34+
# max_classes : maximum class label
35+
# value: value use to label the class response in the output layer vector
36+
# sigmoid : {true | false} - return {-value,....value,....-value} instead for
37+
# optimal use with OpenCV sigmoid function
38+
39+
def class_label_to_nn_output(label, max_classes, is_sigmoid, value):
40+
if (is_sigmoid):
41+
output = np.ones(max_classes).astype(np.float32) * (-1 * value)
42+
output[int(label)] = value
43+
else:
44+
output = np.zeros(max_classes).astype(np.float32)
45+
output[int(label)] = value
46+
47+
return output
48+
49+
#####################################################################
50+
51+
########### Load Training and Testing Data Sets
52+
53+
# load training data set
54+
55+
reader=csv.reader(open("spambase.train","rt", encoding='ascii'),delimiter=',')
56+
57+
58+
attribute_list = []
59+
label_list = []
60+
nn_outputs_list = []
61+
62+
#### N.B there is a change in the loader here (compared to other examples)
63+
64+
for row in reader:
65+
# attributes in columns 0-56, class label in last column,
66+
attribute_list.append(list(row[i] for i in (list(range(0,57)))))
67+
label_list.append(row[57])
68+
nn_outputs_list.append(class_label_to_nn_output(row[57], len(classes), True, 1))
69+
70+
training_attributes=np.array(attribute_list).astype(np.float32)
71+
training_class_labels=np.array(label_list).astype(np.float32)
72+
training_nn_outputs=np.array(nn_outputs_list).astype(np.float32)
73+
74+
# load testing data set
75+
76+
reader=csv.reader(open("spambase.test","rt", encoding='ascii'),delimiter=',')
77+
78+
attribute_list = []
79+
label_list = []
80+
nn_outputs_list = []
81+
82+
for row in reader:
83+
# attributes in columns 0-56, class label in last column,
84+
attribute_list.append(list(row[i] for i in (list(range(0,57)))))
85+
label_list.append(row[57])
86+
87+
testing_attributes=np.array(attribute_list).astype(np.float32)
88+
testing_class_labels=np.array(label_list).astype(np.float32)
89+
90+
############ Perform Training -- Neural Network
91+
92+
# create the network object
93+
94+
nnetwork = cv2.ml.ANN_MLP_create();
95+
96+
# define number of layers, sizes of layers and train neural network
97+
# neural networks only support numerical inputs (convert any categorical inputs)
98+
99+
# set the network to be 2 layer 57->10->2
100+
# - one input node per attribute in a sample
101+
# - 10 hidden nodes
102+
# - one output node per class
103+
# defined by the column vector layer_sizes
104+
105+
layer_sizes = np.int32([57, 10, len(classes)]); # format = [inputs, hidden layer n ..., output]
106+
nnetwork.setLayerSizes(layer_sizes);
107+
108+
# create the network using a sigmoid function with alpha and beta
109+
# parameters = 1 specified respectively (standard sigmoid)
110+
111+
nnetwork.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, 1, 1);
112+
113+
# available activation functions = (cv2.ml.ANN_MLP_SIGMOID_SYM or cv2.ml.ANN_MLP_IDENTITY, cv2.ml.ANN_MLP_GAUSSIAN)
114+
115+
# specify stopping criteria and backpropogation for training
116+
117+
nnetwork.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP);
118+
nnetwork.setBackpropMomentumScale(0.1);
119+
nnetwork.setBackpropWeightScale(0.1);
120+
121+
nnetwork.setTermCriteria((cv2.TERM_CRITERIA_COUNT + cv2.TERM_CRITERIA_EPS, 1000, 0.001))
122+
123+
## N.B. The OpenCV neural network (MLP) implementation does not
124+
## support categorical variable output explicitly unlike the
125+
## other OpenCV ML classes.
126+
## Instead, following the traditional approach of neural networks,
127+
## the output class label is formed by we a binary vector that
128+
## corresponds the desired output layer result for a given class
129+
## e.g. {0, 0 ... 1, 0, 0} components (one element by class) where
130+
## an entry "1" in the i-th vector position correspondes to a class
131+
## label for class i
132+
## for optimal performance with the OpenCV intepretation of the sigmoid
133+
## we use {-1, -1 ... 1, -1, -1}
134+
135+
## prior to training we must construct these output layer responses
136+
## from our conventional class labels (carried out by class_label_to_nn_output()
137+
138+
# train the neural network (using training data)
139+
140+
nnetwork.train(training_attributes, cv2.ml.ROW_SAMPLE, training_nn_outputs);
141+
142+
############ Perform Testing -- Neural Network
143+
144+
tp = 0 # spam
145+
tn = 0 # ham
146+
fp = 0 # classed as spam, but is ham
147+
fn = 0 # classed as ham, but is spam
148+
149+
# for each testing example
150+
151+
for i in range(0, len(testing_attributes[:,0])) :
152+
153+
# perform neural network prediction (i.e. classification)
154+
155+
# (to get around some kind of OpenCV python interface bug, vertically stack the
156+
# example with a second row of zeros of the same size and type which is ignored).
157+
158+
sample = np.vstack((testing_attributes[i,:],
159+
np.zeros(len(testing_attributes[i,:])).astype(np.float32)));
160+
161+
retrval,output_layer_responses = nnetwork.predict(sample);
162+
163+
# the class label c (result) is the index of the most
164+
# +ve of the output layer responses (from the first of the two examples in the stack)
165+
166+
result = np.argmax(output_layer_responses[0]);
167+
168+
print("Test data example : " + str(i + 1) + " : result = " + str(classes[int(result)]))
169+
170+
# record results as tp/tn/fp/fn
171+
172+
if (result == testing_class_labels[i] == 1) : tp+=1
173+
elif (result == testing_class_labels[i] == 0) : tn+=1
174+
elif (result != testing_class_labels[i]) :
175+
if ((result == 1) and (testing_class_labels[i] == 0)) : fp+=1
176+
elif ((result == 0) and (testing_class_labels[i] == 1)) : fn+=1
177+
178+
# output summmary statistics
179+
180+
total = tp + tn + fp + fn
181+
correct = tp + tn
182+
wrong = fp + fn
183+
184+
print()
185+
print("Testing Data Set Performance Summary")
186+
print("TP : " + str(round((tp / float(total)) * 100, 2)) + "%")
187+
print("TN : " + str(round((tn / float(total)) * 100, 2)) + "%")
188+
print("FP : " + str(round((fp / float(total)) * 100, 2)) + "%")
189+
print("FN : " + str(round((fn / float(total)) * 100, 2)) + "%")
190+
print("Total Correct : "+ str(round((correct / float(total)) * 100, 2)) + "%")
191+
print("Total Wrong : "+ str(round((wrong / float(total)) * 100, 2)) + "%")
192+
193+
#####################################################################

neuralnetwork/nnetwork2.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#####################################################################
2+
3+
# Example : Neural Network based learning
4+
# basic illustrative python script
5+
6+
# For use with test / training datasets : wdbc.{train | test}
7+
8+
# Author : Toby Breckon, [email protected]
9+
10+
# Copyright (c) 2014 / 16 School of Engineering & Computing Sciences,
11+
# Durham University, UK
12+
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
13+
14+
#####################################################################
15+
16+
import csv
17+
import cv2
18+
import numpy as np
19+
20+
########### Define classes
21+
22+
classes = {'M': 1, 'B': 0}
23+
inv_classes = {v: k for k, v in list(classes.items())}
24+
25+
#####################################################################
26+
27+
########### construct output layer
28+
29+
# expand training responses defined as class labels {0,1...,N} to output layer
30+
# responses for the OpenCV MLP (Neural Network) implementation such that class
31+
# label c becomes {0,0,0, ... 1, ...0} where the c-th entry is the only non-zero
32+
# entry (equal to "value", conventionally = 1) in the N-length vector
33+
34+
# labels : a row vector of class label transformed to {0,0,0, ... 1, ...0}
35+
# max_classes : maximum class label
36+
# value: value use to label the class response in the output layer vector
37+
# sigmoid : {true | false} - return {-value,....value,....-value} instead for
38+
# optimal use with OpenCV sigmoid function
39+
40+
def class_label_to_nn_output(label, max_classes, is_sigmoid, value):
41+
if (is_sigmoid):
42+
output = np.ones(max_classes).astype(np.float32) * (-1 * value)
43+
output[int(label)] = value
44+
else:
45+
output = np.zeros(max_classes).astype(np.float32)
46+
output[int(label)] = value
47+
48+
return output
49+
50+
#####################################################################
51+
52+
########### Load Training and Testing Data Sets
53+
54+
# load training data set
55+
56+
reader=csv.reader(open("wdbc.train","rt", encoding='ascii'),delimiter=',')
57+
58+
attribute_list = []
59+
label_list = []
60+
nn_outputs_list = []
61+
62+
#### N.B there is a change in the loader here (compared to other examples)
63+
64+
for row in reader:
65+
# attributes in columns 2-32, class label in column 1,
66+
# ignore patient ID in column 0
67+
attribute_list.append(list(row[i] for i in (list(range(2,32)))))
68+
label_list.append(classes[row[1]])
69+
nn_outputs_list.append(class_label_to_nn_output(classes[row[1]], len(classes), True, 1))
70+
71+
72+
training_attributes=np.array(attribute_list).astype(np.float32)
73+
training_class_labels=np.array(label_list).astype(np.float32)
74+
training_nn_outputs=np.array(nn_outputs_list).astype(np.float32)
75+
76+
# load testing data set
77+
78+
reader=csv.reader(open("wdbc.test","rt", encoding='ascii'),delimiter=',')
79+
80+
attribute_list = []
81+
label_list = []
82+
83+
for row in reader:
84+
# attributes in columns 2-32, class label in column 1,
85+
# ignore patient ID in column 0
86+
attribute_list.append(list(row[i] for i in (list(range(2,32)))))
87+
label_list.append(classes[row[1]])
88+
89+
testing_attributes=np.array(attribute_list).astype(np.float32)
90+
testing_class_labels=np.array(label_list).astype(np.float32)
91+
92+
############ Perform Training -- Neural Network
93+
94+
# create the network object
95+
96+
nnetwork = cv2.ml.ANN_MLP_create();
97+
98+
# define number of layers, sizes of layers and train neural network
99+
# neural networks only support numerical inputs (convert any categorical inputs)
100+
101+
# set the network to be 2 layer 30->5->2
102+
# - one input node per attribute in a sample
103+
# - 5 hidden nodes
104+
# - one output node per class
105+
# defined by the column vector layer_sizes
106+
107+
layer_sizes = np.int32([30, 5, len(classes)]) # format = [inputs, hidden layer n ..., output]
108+
nnetwork.setLayerSizes(layer_sizes);
109+
110+
# create the network using a sigmoid function with alpha and beta
111+
# parameters = 1 specified respectively (standard sigmoid)
112+
113+
nnetwork.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, 1, 1);
114+
115+
# available activation functions = (cv2.ml.ANN_MLP_SIGMOID_SYM or cv2.ml.ANN_MLP_IDENTITY, cv2.ml.ANN_MLP_GAUSSIAN)
116+
117+
# specify stopping criteria and backpropogation for training
118+
119+
nnetwork.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP);
120+
nnetwork.setBackpropMomentumScale(0.1);
121+
nnetwork.setBackpropWeightScale(0.1);
122+
123+
nnetwork.setTermCriteria((cv2.TERM_CRITERIA_COUNT + cv2.TERM_CRITERIA_EPS, 1000, 0.001))
124+
125+
## N.B. The OpenCV neural network (MLP) implementation does not
126+
## support categorical variable output explicitly unlike the
127+
## other OpenCV ML classes.
128+
## Instead, following the traditional approach of neural networks,
129+
## the output class label is formed by we a binary vector that
130+
## corresponds the desired output layer result for a given class
131+
## e.g. {0, 0 ... 1, 0, 0} components (one element by class) where
132+
## an entry "1" in the i-th vector position correspondes to a class
133+
## label for class i
134+
## for optimal performance with the OpenCV intepretation of the sigmoid
135+
## we use {-1, -1 ... 1, -1, -1}
136+
137+
## prior to training we must construct these output layer responses
138+
## from our conventional class labels (carried out by class_label_to_nn_output()
139+
140+
# train the neural network (using training data)
141+
142+
nnetwork.train(training_attributes, cv2.ml.ROW_SAMPLE, training_nn_outputs);
143+
144+
############ Perform Testing -- Neural Network
145+
146+
tp = 0 # M
147+
tn = 0 # B
148+
fp = 0 # classed as M, but is B
149+
fn = 0 # classed as B, but is M
150+
151+
# for each testing example
152+
153+
for i in range(0, len(testing_attributes[:,0])) :
154+
155+
# perform neural network prediction (i.e. classification)
156+
157+
# (to get around some kind of OpenCV python interface bug, vertically stack the
158+
# example with a second row of zeros of the same size and type which is ignored).
159+
160+
sample = np.vstack((testing_attributes[i,:],
161+
np.zeros(len(testing_attributes[i,:])).astype(np.float32)))
162+
163+
retrval,output_layer_responses = nnetwork.predict(sample)
164+
165+
# the class label c (result) is the index of the most
166+
# +ve of the output layer responses (from the first of the two examples in the stack)
167+
168+
result = np.argmax(output_layer_responses[0])
169+
170+
print("Test data example : " + str(i + 1) + " : result = " + str(inv_classes[int(result)]))
171+
172+
# record results as tp/tn/fp/fn
173+
174+
if (result == testing_class_labels[i] == 1) : tp+=1
175+
elif (result == testing_class_labels[i] == 0) : tn+=1
176+
elif (result != testing_class_labels[i]) :
177+
if ((result == 1) and (testing_class_labels[i] == 0)) : fp+=1
178+
elif ((result == 0) and (testing_class_labels[i] == 1)) : fn+=1
179+
180+
# output summmary statistics
181+
182+
total = tp + tn + fp + fn
183+
correct = tp + tn
184+
wrong = fp + fn
185+
186+
print()
187+
print("Testing Data Set Performance Summary")
188+
print("TP : " + str(round((tp / float(total)) * 100, 2)) + "%")
189+
print("TN : " + str(round((tn / float(total)) * 100, 2)) + "%")
190+
print("FP : " + str(round((fp / float(total)) * 100, 2)) + "%")
191+
print("FN : " + str(round((fn / float(total)) * 100, 2)) + "%")
192+
print("Total Correct : "+ str(round((correct / float(total)) * 100, 2)) + "%")
193+
print("Total Wrong : "+ str(round((wrong / float(total)) * 100, 2)) + "%")
194+
195+
#####################################################################

0 commit comments

Comments
 (0)