def compare_algorithms(): x = np.ones((10, 10)) x[:, 5:] = -1 x_noisy = x + np.random.normal(0, 1.8, size=x.shape) inds = np.arange(x.size).reshape(x.shape).astype(np.int64) horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()] vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()] edges = np.vstack([horz, vert]) #unaries = 2 * x_thresh.astype(np.float).ravel() - 1 unaries = x_noisy.ravel() unaries = np.c_[np.exp(-unaries), np.exp(unaries)] pairwise = np.exp(np.eye(2) * 4.1) # repeat pairwise for each edge pairwise = np.repeat(pairwise[np.newaxis, :, :], len(edges), axis=0) algorithms = ["maxprod", "gibbs", "jt", "trw", "treeep"] fix, axes = plt.subplots(1, len(algorithms)) for ax, alg in zip(axes, algorithms): result_mrf = mrf(unaries, edges, pairwise, verbose=1, alg=alg) ax.matshow(result_mrf.reshape(x.shape)) ax.set_title(alg) ax.set_xticks(()) ax.set_yticks(()) plt.show()
def predict(self, X_prob, blocks): """ X_prob: X_prob[i,j] is the probability that phone i has label j. blocks: X_prob[blocks == i] is sentence i Returns X_decoded which is a (1d) label vector where X_decoded[i] is the label assigned to phone i by viterbi decoding. The model used for decoding a sentence with n phones is a CRF with the following structure: /-------+-------+------------ Edge potentials from language model (this object) | | | v v v X_1 --- X_2 --- ... --- X_n <-- P(X_n=j | Y_n) provided in X_prob ^ ^ ^ | | | P_1 P_2 P_n <-- Phone observations (never seen by this model) Decoding is done separately for each sentence. The potentials for each X_i -- X_{i+1} edge are the same. """ # upper bound on sentence length, +10 because I'm too lazy to think about off-by-1 errors. max_sentence_length = np.max(np.diff( np.nonzero(np.diff(blocks) != 0))) + 10 # Construct the edges and pairwise potentials for a chain CRF. # We construct the largest CRF we'll need here, and use sections of the chain for shorter sentences. edges = np.arange(max_sentence_length) edges = np.c_[edges[:-1], edges[1:]] pairwise = np.repeat(self.Q[np.newaxis, :, :], max_sentence_length, axis=0) decoded_labels = [] for X_prob_sent in iterator.blocks(X_prob, blocks): sentence_length = X_prob_sent.shape[0] decoded = daicrf.mrf(X_prob_sent.astype(np.float), edges[:sentence_length - 1], pairwise[:sentence_length - 1, :, ], verbose=0, alg='jt') decoded_labels.append(decoded) X_decoded = np.concatenate(decoded_labels) return X_decoded
def predict(self, X_prob, blocks): """ X_prob: X_prob[i,j] is the probability that phone i has label j. blocks: X_prob[blocks == i] is sentence i Returns X_decoded which is a (1d) label vector where X_decoded[i] is the label assigned to phone i by viterbi decoding. The model used for decoding a sentence with n phones is a CRF with the following structure: /-------+-------+------------ Edge potentials from language model (this object) | | | v v v X_1 --- X_2 --- ... --- X_n <-- P(X_n=j | Y_n) provided in X_prob ^ ^ ^ | | | P_1 P_2 P_n <-- Phone observations (never seen by this model) Decoding is done separately for each sentence. The potentials for each X_i -- X_{i+1} edge are the same. """ # upper bound on sentence length, +10 because I'm too lazy to think about off-by-1 errors. max_sentence_length = np.max(np.diff(np.nonzero(np.diff(blocks) != 0))) + 10 # Construct the edges and pairwise potentials for a chain CRF. # We construct the largest CRF we'll need here, and use sections of the chain for shorter sentences. edges = np.arange(max_sentence_length) edges = np.c_[edges[:-1], edges[1:]] pairwise = np.repeat(self.Q[np.newaxis,:,:], max_sentence_length, axis=0) decoded_labels = [] for X_prob_sent in iterator.blocks(X_prob, blocks): sentence_length = X_prob_sent.shape[0] decoded = daicrf.mrf( X_prob_sent.astype(np.float), edges[:sentence_length-1], pairwise[:sentence_length-1,:,], verbose=0, alg='jt') decoded_labels.append(decoded) X_decoded = np.concatenate(decoded_labels) return X_decoded
def example_binary(): x = np.ones((10, 10)) x[:, 5:] = -1 x_noisy = x + np.random.normal(0, .8, size=x.shape) x_thresh = x_noisy > .0 fig, axes = plt.subplots(1, 5) axes[0].set_title("Original") axes[0].matshow(x) axes[1].set_title("binary") axes[1].matshow(x_noisy) axes[2].set_title("thresholded") axes[2].matshow(x_thresh) inds = np.arange(x.size).reshape(x.shape).astype(np.int64) horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()] vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()] edges = np.vstack([horz, vert]) #unaries = 2 * x_thresh.astype(np.float).ravel() - 1 unaries = x_noisy.ravel() unaries = np.c_[np.exp(-unaries), np.exp(unaries)] axes[3].set_title("Potts MRF") result = potts_mrf(unaries, edges, 1.1, verbose=1) axes[3].matshow(result.reshape(x.shape)) # repeat pairwise for each edge pairwise = np.exp(np.eye(2) * 1.1) pairwise = np.repeat(pairwise[np.newaxis, :, :], len(edges), axis=0) result_mrf = mrf(unaries, edges, pairwise, verbose=1, alg="trw") axes[4].set_title("MRF") axes[4].matshow(result_mrf.reshape(x.shape)) for ax in axes: ax.set_xticks(()) ax.set_yticks(()) plt.show()
def example_multinomial(): np.random.seed(45) unaries = np.zeros((10, 12, 3)) unaries[:, :4, 0] = 1 unaries[:, 4:8, 1] = 1 unaries[:, 8:, 2] = 1 x = np.argmax(unaries, axis=2) unaries_noisy = unaries + np.random.normal(size=unaries.shape) x_thresh = np.argmax(unaries_noisy, axis=2) unaries_noisy = np.exp(unaries_noisy).reshape(-1, 3) inds = np.arange(x.size).reshape(x.shape).astype(np.int64) horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()] vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()] edges = np.vstack([horz, vert]) result = potts_mrf(unaries_noisy, edges, 1.1) pairwise = np.eye(3) + np.ones((1, 1)) pairwise[-1, 0] = 0 pairwise[0, -1] = 0 print(pairwise) # repeat pairwise for each edge pairwise = np.repeat(pairwise[np.newaxis, :, :], len(edges), axis=0) result_mrf = mrf(unaries_noisy, edges, np.exp(pairwise), alg="jt") plot, axes = plt.subplots(1, 4) axes[0].set_title("original") axes[0].matshow(x) axes[1].set_title("thresholded") axes[1].matshow(x_thresh) axes[1].set_title("Potts MRF") axes[2].matshow(result.reshape(x.shape)) axes[1].set_title("MRF") axes[3].matshow(result_mrf.reshape(x.shape)) for ax in axes: ax.set_xticks(()) ax.set_yticks(()) plt.show()
def example(): img1 = np.asarray(Image.open("scene1.row3.col1.ppm")) / 255. img2 = np.asarray(Image.open("scene1.row3.col2.ppm")) / 255. img1 = img1[180:220, 80:120] img2 = img2[180:220, 80:120] unaries = (stereo_unaries(img1, img2) * 100).astype(np.int32) n_disps = unaries.shape[2] pairwise = .10 * np.eye(n_disps) newshape = unaries.shape[:2] x, y = np.ogrid[:n_disps, :n_disps] #one_d_topology = 5 * np.abs(x - y).astype(np.int32).copy("C") # libdai works in exp space pairwise_exp = np.exp(pairwise) # build edges for max product inference: inds = np.arange(np.prod(newshape)).reshape(newshape).astype(np.int64) horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()] vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()] edges = np.vstack([horz, vert]).copy() #asdf = np.random.permutation(len(edges)) #edges = edges[asdf] start = time() max_product = mrf(np.exp(-unaries.reshape(-1, n_disps)), edges, pairwise_exp, alg='maxprod') time_maxprod = time() - start energy_max_prod = energy(unaries, max_product.reshape(newshape), -pairwise) start = time() trw = mrf(np.exp(-unaries.reshape(-1, n_disps)), edges, pairwise_exp, alg='trw') time_trw = time() - start energy_trw = energy(unaries, trw, -pairwise) start = time() treeep = mrf(np.exp(-unaries.reshape(-1, n_disps)), edges, pairwise, alg='treeep') time_treeep = time() - start energy_treeep = energy(unaries, treeep.reshape(newshape), -pairwise) start = time() gibbs = mrf(np.exp(-unaries.reshape(-1, n_disps)), edges, pairwise_exp, alg='gibbs') time_gibbs = time() - start energy_gibbs = energy(unaries, gibbs.reshape(newshape), -pairwise) fix, axes = plt.subplots(3, 3, figsize=(16, 8)) energy_argmax = energy(unaries, np.argmin(unaries, axis=2), -pairwise) #energies = np.array([energy_argmax, energy_gibbs, energy_max_prod, #energy_treeep, energy_trw]) #(energy_argmax, energy_gibbs, energy_qpbo, #energy_max_prod, energy_treep, energy_trw) = energies - energy_gc axes[0, 0].imshow(img1) axes[0, 1].imshow(img2) axes[0, 2].set_title("unaries only e=%f" % (energy_argmax)) axes[0, 2].matshow(np.argmin(unaries, axis=2), vmin=0, vmax=8) axes[1, 0].set_title("treeep %.2fs, e=%f" % (time_treeep, energy_treeep)) axes[1, 0].matshow(treeep.reshape(newshape), vmin=0, vmax=8) axes[1, 2].set_title("max-product %.2fs, e=%f" % (time_maxprod, energy_max_prod)) axes[1, 2].matshow(max_product.reshape(newshape), vmin=0, vmax=8) axes[2, 0].set_title("trw %.2fs, e=%f" % (time_trw, energy_trw)) axes[2, 0].matshow(trw.reshape(newshape), vmin=0, vmax=8) axes[2, 2].set_title("gibbs %.2fs, e=%f" % (time_gibbs, energy_gibbs)) axes[2, 2].matshow(gibbs.reshape(newshape), vmin=0, vmax=8) for ax in axes.ravel(): ax.set_xticks(()) ax.set_yticks(()) plt.tight_layout() plt.show()
edges = np.arange(max_sentence_length) edges = np.c_[edges[:-1], edges[1:]] pairwise = np.repeat(bigram.Q[np.newaxis,:,:], max_sentence_length, axis=0) + 1e-6 def _block_iterator(X, blocks): for i in xrange(blocks.max()+1): yield X[blocks == i] decoded_labels = [] for Y_sent in _block_iterator(Y, timit_test.sentence_ids): #print Y_sent.sum() #print ".", sentence_length = Y_sent.shape[0] decoded = daicrf.mrf( Y_sent.astype(np.float), edges[:sentence_length-1], pairwise[:sentence_length-1,:,], verbose=0, alg='jt') decoded_labels.append(decoded) #if len(decoded_labels) > 5: # break print "" Y_decoded = np.equal.outer( np.concatenate(decoded_labels), np.arange(timit_test.y.shape[1])) Y_decoded_folded = np.equal.outer( np.argmax(np.dot(Y_decoded, F), axis=1), np.arange(F.shape[1]))