Update letter_recog sample to current version of opencv interfaces

This commit is contained in:
Vladislav Sovrasov 2016-02-03 11:22:32 +03:00
parent d579f08093
commit e90dc20361

View File

@ -65,13 +65,12 @@ class RTrees(LetterStatModel):
def train(self, samples, responses):
sample_n, var_n = samples.shape
var_types = np.array([cv2.ml.VAR_NUMERICAL] * var_n + [cv2.ml.VAR_CATEGORICAL], np.uint8)
#CvRTParams(10,10,0,false,15,0,true,4,100,0.01f,CV_TERMCRIT_ITER));
params = dict(max_depth=10 )
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses, varType = var_types, params = params)
self.model.setMaxDepth(20)
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses.astype(int))
def predict(self, samples):
return [self.model.predict(s) for s in samples]
ret, resp = self.model.predict(samples)
return resp.ravel()
class KNearest(LetterStatModel):
@ -79,10 +78,10 @@ class KNearest(LetterStatModel):
self.model = cv2.ml.KNearest_create()
def train(self, samples, responses):
self.model.train(samples, responses)
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
def predict(self, samples):
retval, results, neigh_resp, dists = self.model.find_nearest(samples, k = 10)
retval, results, neigh_resp, dists = self.model.findNearest(samples, k = 10)
return results.ravel()
@ -95,15 +94,15 @@ class Boost(LetterStatModel):
new_samples = self.unroll_samples(samples)
new_responses = self.unroll_responses(responses)
var_types = np.array([cv2.ml.VAR_NUMERICAL] * var_n + [cv2.ml.VAR_CATEGORICAL, cv2.ml.VAR_CATEGORICAL], np.uint8)
#CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0 )
params = dict(max_depth=5) #, use_surrogates=False)
self.model.train(new_samples, cv2.ml.ROW_SAMPLE, new_responses, varType = var_types, params=params)
self.model.setMaxDepth(5)
self.model.train(cv2.ml.TrainData_create(new_samples, cv2.ml.ROW_SAMPLE, new_responses.astype(int), varType = var_types))
def predict(self, samples):
new_samples = self.unroll_samples(samples)
pred = np.array( [self.model.predict(s, returnSum = True) for s in new_samples] )
pred = pred.reshape(-1, self.class_n).argmax(1)
return pred
ret, resp = self.model.predict(new_samples)
return resp.ravel().reshape(-1, self.class_n).argmax(1)
class SVM(LetterStatModel):
@ -111,13 +110,14 @@ class SVM(LetterStatModel):
self.model = cv2.ml.SVM_create()
def train(self, samples, responses):
params = dict( kernel_type = cv2.ml.SVM_LINEAR,
svm_type = cv2.ml.SVM_C_SVC,
C = 1 )
self.model.train(samples, responses, params = params)
self.model.setType(cv2.ml.SVM_C_SVC)
self.model.setC(1)
self.model.setKernel(cv2.ml.SVM_LINEAR)
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses.astype(int))
def predict(self, samples):
return self.model.predict_all(samples).ravel()
ret, resp = self.model.predict(samples)
return resp.ravel()
class MLP(LetterStatModel):
@ -127,22 +127,23 @@ class MLP(LetterStatModel):
def train(self, samples, responses):
sample_n, var_n = samples.shape
new_responses = self.unroll_responses(responses).reshape(-1, self.class_n)
layer_sizes = np.int32([var_n, 100, 100, self.class_n])
self.model.create(layer_sizes)
# CvANN_MLP_TrainParams::BACKPROP,0.001
params = dict( term_crit = (cv2.TERM_CRITERIA_COUNT, 300, 0.01),
train_method = cv2.ml.ANN_MLP_TRAIN_PARAMS_BACKPROP,
bp_dw_scale = 0.001,
bp_moment_scale = 0.0 )
self.model.train(samples, np.float32(new_responses), None, params = params)
self.model.setLayerSizes(layer_sizes)
self.model.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
self.model.setBackpropMomentumScale(0)
self.model.setBackpropWeightScale(0.001)
self.model.setTermCriteria((cv2.TERM_CRITERIA_COUNT, 300, 0.01))
self.model.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
self.model.train(samples, cv2.ml.ROW_SAMPLE, np.float32(new_responses))
def predict(self, samples):
ret, resp = self.model.predict(samples)
return resp.argmax(-1)
if __name__ == '__main__':
import getopt
import sys
@ -155,7 +156,7 @@ if __name__ == '__main__':
args, dummy = getopt.getopt(sys.argv[1:], '', ['model=', 'data=', 'load=', 'save='])
args = dict(args)
args.setdefault('--model', 'rtrees')
args.setdefault('--model', 'svm')
args.setdefault('--data', '../data/letter-recognition.data')
print('loading data %s ...' % args['--data'])
@ -173,8 +174,8 @@ if __name__ == '__main__':
model.train(samples[:train_n], responses[:train_n])
print('testing...')
train_rate = np.mean(model.predict(samples[:train_n]) == responses[:train_n])
test_rate = np.mean(model.predict(samples[train_n:]) == responses[train_n:])
train_rate = np.mean(model.predict(samples[:train_n]) == responses[:train_n].astype(int))
test_rate = np.mean(model.predict(samples[train_n:]) == responses[train_n:].astype(int))
print('train rate: %f test rate: %f' % (train_rate*100, test_rate*100))