Exemplo n.º 1
0
def main():
    #X, Y = toy.generate_crosses_latent(n_samples=25, noise=10)
    #X, Y = toy.generate_crosses(n_samples=50, noise=10)
    X, Y = toy.generate_easy(n_samples=50, noise=5)
    #X, Y = toy.generate_xs(n_samples=25, noise=5)
    n_labels = 2
    #crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=2,
                        #inference_method='dai')
    crf = LatentGridCRF(n_labels=n_labels, n_states_per_label=3,
                        inference_method='lp')
    clf = StupidLatentSVM(problem=crf, max_iter=50, C=10. ** 5, verbose=2,
                          check_constraints=True, n_jobs=12, break_on_bad=True)
    #clf = StupidLatentSVM(problem=crf, max_iter=50, C=1, verbose=2,
            #check_constraints=True, n_jobs=12)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)

    i = 0
    loss = 0
    for x, y, h_init, y_pred in zip(X, Y, clf.H_init_, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        loss += np.sum(y != y_pred / 2)
        if i > 100:
            continue
        fig, ax = plt.subplots(3, 2)
        ax[0, 0].matshow(y * crf.n_states_per_label,
                         vmin=0, vmax=crf.n_states - 1)
        ax[0, 0].set_title("ground truth")
        w_unaries_only = np.array([1, 1, 1, 1,
                                   0,
                                   0, 0,
                                   0, 0, 0,
                                   0, 0, 0, 0])
        unary_pred = crf.inference(x, w_unaries_only)
        ax[0, 1].matshow(unary_pred, vmin=0, vmax=crf.n_states - 1)
        ax[0, 1].set_title("unaries only")
        ax[1, 0].matshow(h_init, vmin=0, vmax=crf.n_states - 1)
        ax[1, 0].set_title("latent initial")
        ax[1, 1].matshow(crf.latent(x, y, clf.w),
                         vmin=0, vmax=crf.n_states - 1)
        ax[1, 1].set_title("latent final")
        ax[2, 0].matshow(y_pred, vmin=0, vmax=crf.n_states - 1)
        ax[2, 0].set_title("prediction")
        ax[2, 1].matshow((y_pred // crf.n_states_per_label)
                         * crf.n_states_per_label,
                         vmin=0, vmax=crf.n_states - 1)
        ax[2, 1].set_title("prediction")
        for a in ax.ravel():
            a.set_xticks(())
            a.set_yticks(())
        fig.savefig("data_%03d.png" % i, bbox_inches="tight")
        i += 1
    print("loss: %f" % loss)
    print(clf.w)
Exemplo n.º 2
0
def main():
    # X, Y = make_dataset_checker_multinomial()
    # X, Y = make_dataset_big_checker_extended()
    # X, Y = make_dataset_big_checker(n_samples=5)
    X, Y = make_dataset_easy_latent_explicit(n_samples=50)
    # X = X[:, :18, :18]
    # Y = Y[:, :18, :18]
    # X, Y = make_dataset_blocks_multinomial(n_samples=100)
    size_y = Y[0].size
    shape_y = Y[0].shape
    inds = np.arange(size_y).reshape(shape_y)
    horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()]
    vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()]
    downleft = np.c_[inds[:-1, :-1].ravel(), inds[1:, 1:].ravel()]
    downright = np.c_[inds[:-1, 1:].ravel(), inds[1:, :-1].ravel()]
    edges = np.vstack([horz, vert, downleft, downright]).astype(np.int32)
    graph = sparse.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])), shape=(size_y, size_y)).tocsr()
    graph = graph + graph.T

    # n_labels = len(np.unique(Y))
    n_labels = 2
    # crf = LatentFixedGraphCRF(n_labels=2, n_states_per_label=2, graph=graph)
    crf = LatentFixedGraphCRF(n_labels=n_labels, n_states_per_label=2, graph=graph)
    # clf = LatentStructuredPerceptron(problem=crf, max_iter=500)
    clf = StupidLatentSVM(problem=crf, max_iter=10000, C=10000, verbose=2, check_constraints=True)
    X_flat = [x.reshape(-1, n_labels) for x in X]
    Y_flat = [y.ravel() for y in Y]
    clf.fit(X_flat, Y_flat)
    # clf.fit(X, Y)
    Y_pred = clf.predict(X_flat)
    # Y_pred = clf.predict(X)

    i = 0
    loss = 0
    tracer()
    for x, y, y_pred in zip(X, Y, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        loss += np.sum(y / 2 != y_pred / 2)
        if i > 20:
            continue
        plt.subplot(131)
        plt.imshow(y, interpolation="nearest")
        plt.colorbar()
        plt.subplot(132)
        plt.imshow(np.argmin(x, axis=2), interpolation="nearest")
        plt.colorbar()
        plt.subplot(133)
        plt.imshow(y_pred, interpolation="nearest")
        plt.colorbar()
        plt.savefig("data_%03d.png" % i)
        plt.close()
        i += 1
    print("loss: %f" % loss)
Exemplo n.º 3
0
def main():
    #X, Y = make_dataset_checker_multinomial()
    #X, Y = make_dataset_big_checker_extended()
    #X, Y = make_dataset_big_checker(n_samples=5)
    X, Y = make_dataset_easy_latent_explicit(n_samples=50)
    #X = X[:, :18, :18]
    #Y = Y[:, :18, :18]
    #X, Y = make_dataset_blocks_multinomial(n_samples=100)
    size_y = Y[0].size
    shape_y = Y[0].shape
    inds = np.arange(size_y).reshape(shape_y)
    horz = np.c_[inds[:, :-1].ravel(), inds[:, 1:].ravel()]
    vert = np.c_[inds[:-1, :].ravel(), inds[1:, :].ravel()]
    downleft = np.c_[inds[:-1, :-1].ravel(), inds[1:, 1:].ravel()]
    downright = np.c_[inds[:-1, 1:].ravel(), inds[1:, :-1].ravel()]
    edges = np.vstack([horz, vert, downleft, downright]).astype(np.int32)
    graph = sparse.coo_matrix(
        (np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
        shape=(size_y, size_y)).tocsr()
    graph = graph + graph.T

    #n_labels = len(np.unique(Y))
    n_labels = 2
    #crf = LatentFixedGraphCRF(n_labels=2, n_states_per_label=2, graph=graph)
    crf = LatentFixedGraphCRF(n_labels=n_labels,
                              n_states_per_label=2,
                              graph=graph)
    #clf = LatentStructuredPerceptron(problem=crf, max_iter=500)
    clf = StupidLatentSVM(problem=crf,
                          max_iter=10000,
                          C=10000,
                          verbose=2,
                          check_constraints=True)
    X_flat = [x.reshape(-1, n_labels) for x in X]
    Y_flat = [y.ravel() for y in Y]
    clf.fit(X_flat, Y_flat)
    #clf.fit(X, Y)
    Y_pred = clf.predict(X_flat)
    #Y_pred = clf.predict(X)

    i = 0
    loss = 0
    tracer()
    for x, y, y_pred in zip(X, Y, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        loss += np.sum(y / 2 != y_pred / 2)
        if i > 20:
            continue
        plt.subplot(131)
        plt.imshow(y, interpolation='nearest')
        plt.colorbar()
        plt.subplot(132)
        plt.imshow(np.argmin(x, axis=2), interpolation='nearest')
        plt.colorbar()
        plt.subplot(133)
        plt.imshow(y_pred, interpolation='nearest')
        plt.colorbar()
        plt.savefig("data_%03d.png" % i)
        plt.close()
        i += 1
    print("loss: %f" % loss)