def test_alpha(): P = np.array([[0.7, 0.3], [0.5, 0.5], [0.4, 0.6], [0.1, 0.9], [0.7, 0.3], [0.6, 0.4], [0.6, 0.4], [0.5, 0.5], [0.4, 0.6], [0.4, 0.6]]) A = np.array([[0.35, 0.65], [0.45, 0.55]]) D = np.array([[0.3, 0.4, 0.3], [0.2, 0.5, 0.3]]) alpha = vdhmms.alpha(A, P, D) alpha_true = np.array([[0.7, 0.3], [0.0798, 0.0372], [0.0599, 0.0537]]) np.testing.assert_almost_equal(alpha[:3, :], alpha_true, decimal=3)
patches = split_on(im, segments) descs = np.zeros((len(patches), voc.shape[1])) for i, patch in enumerate(patches): patch = imresize(patch, (h, w)) patch = normalise(patch) descs[i] = patch.flatten() # OK, these are the labels of our observation ! labels = euclidean_distances(descs, voc).argmin(axis=1) # Now, we can compute the emission probability matrices emission_obs = emission[:, labels].T alphas = vdhmms.alpha(transition, emission_obs, occurances, p_init=first_letter.reshape((len(first_letter), ))) betas = vdhmms.beta(transition, emission_obs, occurances, p_init=last_letter.reshape((len(first_letter), ))) show_segments(im, segments) g = alphas * betas chain = g.argmax(axis=1) prob = g.max(axis=1) # careful ! Some labels of the chain "just" correspond to null probabilities letters = 'abcdefghijklmnopqrstuvwxyz' labels = [] for p, l in zip(prob, chain): if p == 0:
segments = find_letters(im) patches = split_on(im, segments) descs = np.zeros((len(patches), voc.shape[1])) for i, patch in enumerate(patches): patch = imresize(patch, (h, w)) patch = normalise(patch) descs[i] = patch.flatten() # OK, these are the labels of our observation ! labels = euclidean_distances(descs, voc).argmin(axis=1) # Now, we can compute the emission probability matrices emission_obs = emission[:, labels].T alphas = vdhmms.alpha(transition, emission_obs, occurances, p_init=first_letter.reshape((len(first_letter), ))) betas = vdhmms.beta(transition, emission_obs, occurances, p_init=last_letter.reshape((len(first_letter), ))) show_segments(im, segments) g = alphas * betas chain = g.argmax(axis=1) prob = g.max(axis=1) # careful ! Some labels of the chain "just" correspond to null probabilities letters = 'abcdefghijklmnopqrstuvwxyz' labels = [] for p, l in zip(prob, chain): if p == 0: labels.append('_') else: labels.append(letters[l])