def test_samme_proba(): # Test the `_samme_proba` helper function. # Define some example (bad) `predict_proba` output. probs = np.array([[1, 1e-6, 0], [0.19, 0.6, 0.2], [-999, 0.51, 0.5], [1e-6, 1, 1e-9]]) probs /= np.abs(probs.sum(axis=1))[:, np.newaxis] # _samme_proba calls estimator.predict_proba. # Make a mock object so I can control what gets returned. class MockEstimator(object): def predict_proba(self, X): assert_array_equal(X.shape, probs.shape) return probs mock = MockEstimator() samme_proba = weight_boosting._samme_proba(mock, 3, np.ones_like(probs)) assert_array_equal(samme_proba.shape, probs.shape) assert_true(np.isfinite(samme_proba).all()) # Make sure that the correct elements come out as smallest -- # `_samme_proba` should preserve the ordering in each example. assert_array_equal(np.argmin(samme_proba, axis=1), [2, 0, 0, 2]) assert_array_equal(np.argmax(samme_proba, axis=1), [0, 1, 1, 1])
def cascade(): """ :return: Beginning of cascade classification. Still being developed. """ pred = 0 bdt = joblib.load('models/adaboost256.pkl') frame = cv2.imread('images/gradient.jpg') # X = cf.compute_chans(frame).ravel().reshape(1, -1) # X = cf.compute_chans(cv2.resize(frame, (64, 128))).ravel().reshape(1, -1) x = cf.compute_chans(cv2.resize(frame, (64, 128))).ravel() # np.random.shuffle(x) N = 100 pred = [None] * N X = np.array([x for i in range(N)]) start = time.time() for i in range(N): for t, estimator in enumerate(bdt.estimators_): pred += _samme_proba(estimator, 2, X) p_t = pred[0, 1] / (t + 1) print('p_t is: ', p_t) if p_t < -.2 and t > 8: return False return False out = casc.cascade(X, bdt) print((time.time() - start) / N) start = time.time() for i in range(N): out = bdt.predict(X) print((time.time() - start) / N) print(type(bdt))
def predict_proba2(self, X): proba = sum(_samme_proba(est , 2 ,X) for est in self.models ) proba = np.array(proba) proba = proba / sum(self.alphas) proba = np.exp((1. / (2 - 1)) * proba) normalizer = proba.sum(axis=1)[:, np.newaxis] normalizer[normalizer == 0.0] = 1.0 proba = proba / normalizer return proba.astype(float)
def predict_proba2(self, X): # if self.alphas == 'SAMME.R' proba = sum(_samme_proba(est , 2 ,X) for est in self.models ) proba = np.array(proba) proba = proba / sum(self.alphas) proba = np.exp((1. / (2 - 1)) * proba) normalizer = proba.sum(axis=1)[:, np.newaxis] normalizer[normalizer == 0.0] = 1.0 # proba = np.linspace(proba) # proba = np.array(proba).astype(float) proba = proba / normalizer # print('proba = ',proba) return proba.astype(float)