|
|
|
@ -64,12 +64,11 @@ class RTrees(LetterStatModel): |
|
|
|
|
self.model = cv2.ml.RTrees_create() |
|
|
|
|
|
|
|
|
|
def train(self, samples, responses): |
|
|
|
|
sample_n, var_n = samples.shape |
|
|
|
|
self.model.setMaxDepth(20) |
|
|
|
|
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses.astype(int)) |
|
|
|
|
|
|
|
|
|
def predict(self, samples): |
|
|
|
|
ret, resp = self.model.predict(samples) |
|
|
|
|
_ret, resp = self.model.predict(samples) |
|
|
|
|
return resp.ravel() |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -81,7 +80,7 @@ class KNearest(LetterStatModel): |
|
|
|
|
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses) |
|
|
|
|
|
|
|
|
|
def predict(self, samples): |
|
|
|
|
retval, results, neigh_resp, dists = self.model.findNearest(samples, k = 10) |
|
|
|
|
_retval, results, _neigh_resp, _dists = self.model.findNearest(samples, k = 10) |
|
|
|
|
return results.ravel() |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -90,7 +89,7 @@ class Boost(LetterStatModel): |
|
|
|
|
self.model = cv2.ml.Boost_create() |
|
|
|
|
|
|
|
|
|
def train(self, samples, responses): |
|
|
|
|
sample_n, var_n = samples.shape |
|
|
|
|
_sample_n, var_n = samples.shape |
|
|
|
|
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) |
|
|
|
@ -101,7 +100,7 @@ class Boost(LetterStatModel): |
|
|
|
|
|
|
|
|
|
def predict(self, samples): |
|
|
|
|
new_samples = self.unroll_samples(samples) |
|
|
|
|
ret, resp = self.model.predict(new_samples) |
|
|
|
|
_ret, resp = self.model.predict(new_samples) |
|
|
|
|
|
|
|
|
|
return resp.ravel().reshape(-1, self.class_n).argmax(1) |
|
|
|
|
|
|
|
|
@ -118,7 +117,7 @@ class SVM(LetterStatModel): |
|
|
|
|
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses.astype(int)) |
|
|
|
|
|
|
|
|
|
def predict(self, samples): |
|
|
|
|
ret, resp = self.model.predict(samples) |
|
|
|
|
_ret, resp = self.model.predict(samples) |
|
|
|
|
return resp.ravel() |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -127,7 +126,7 @@ class MLP(LetterStatModel): |
|
|
|
|
self.model = cv2.ml.ANN_MLP_create() |
|
|
|
|
|
|
|
|
|
def train(self, samples, responses): |
|
|
|
|
sample_n, var_n = samples.shape |
|
|
|
|
_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]) |
|
|
|
|
|
|
|
|
@ -141,7 +140,7 @@ class MLP(LetterStatModel): |
|
|
|
|
self.model.train(samples, cv2.ml.ROW_SAMPLE, np.float32(new_responses)) |
|
|
|
|
|
|
|
|
|
def predict(self, samples): |
|
|
|
|
ret, resp = self.model.predict(samples) |
|
|
|
|
_ret, resp = self.model.predict(samples) |
|
|
|
|
return resp.argmax(-1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|