def main():
    X, Y = generate_blocks(n_samples=1)
    # X, Y = generate_checker()
    # X, Y = generate_easy(n_samples=1)
    # X, Y = generate_big_checker()
    crf = BinaryGridCRF()
    # clf = StructuredPerceptron(problem=crf, max_iter=100)
    clf = StructuredSVM(problem=crf, max_iter=200, C=100, verbose=10, check_constraints=True)
    # clf = SubgradientStructuredSVM(problem=crf, max_iter=2000, C=100)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    i = 0
    loss = 0
    for x, y, y_pred in zip(X, Y, Y_pred):
        loss += np.sum(y != y_pred)
        fig, plots = plt.subplots(2, 2)
        plots[0, 0].set_title("x")
        plots[0, 0].imshow(x[:, :, 0], interpolation="nearest")
        plots[0, 1].set_title("y")
        plots[0, 1].imshow(y, interpolation="nearest")
        plots[1, 0].set_title("unaries")
        w_unaries = np.zeros(4)
        w_unaries[0] = 1.0
        y_unaries = crf.inference(x, w_unaries)
        plots[1, 0].imshow(y_unaries, interpolation="nearest")
        plots[1, 1].set_title("full crf")
        plots[1, 1].imshow(y_pred, interpolation="nearest")
        for plot in plots.ravel():
            plot.set_axis_off()
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)
def main():
    X, Y = make_dataset_big_checker(n_samples=1)
    size_y = Y[0].size
    shape_y = Y[0].shape
    n_labels = len(np.unique(Y))
    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

    crf = MultinomialFixedGraphCRFNoBias(n_states=n_labels, graph=graph)
    #crf = MultinomialGridCRF(n_labels=4)
    #clf = StructuredPerceptron(problem=crf, max_iter=50)
    clf = StructuredSVM(problem=crf, max_iter=20, C=1000000, verbose=20,
            check_constraints=True,
            positive_constraint=np.arange(crf.size_psi - 1))
    #clf = SubgradientStructuredSVM(problem=crf, max_iter=100, C=10000)
    X_flat = [x.reshape(-1, n_labels).copy("C") 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
    for x, y, y_pred in zip(X, Y, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        #loss += np.sum(y != y_pred)
        loss += np.sum(np.logical_xor(y, y_pred))
        if i > 4:
            continue
        fig, plots = plt.subplots(1, 4)
        plots[0].imshow(y, interpolation='nearest')
        plots[0].set_title("gt")
        pw_z = np.zeros((n_labels, n_labels), dtype=np.int32)
        un = np.ascontiguousarray(
                -1000 * x.reshape(-1, n_labels)).astype(np.int32)
        unaries = alpha_expansion_graph(edges, un, pw_z)
        plots[1].imshow(unaries.reshape(y.shape), interpolation='nearest')
        plots[1].set_title("unaries only")
        plots[2].imshow(y_pred, interpolation='nearest')
        plots[2].set_title("prediction")
        loss_augmented = clf.problem.loss_augmented_inference(
                x.reshape(-1, n_labels), y.ravel(), clf.w)
        loss_augmented = loss_augmented.reshape(y.shape)
        plots[3].imshow(loss_augmented, interpolation='nearest')
        plots[3].set_title("loss augmented")
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)
示例#3
0
    def fit(self, X, Y):
        w = np.ones(self.problem.size_psi) * 1e-5
        subsvm = StructuredSVM(self.problem,
                               self.max_iter,
                               self.C,
                               self.check_constraints,
                               verbose=0)
        objectives = []
        ws = []
        tracer()
        H = Y
        Y = [y / self.problem.n_states_per_label for y in Y]

        for iteration in xrange(1):
            print("LATENT SVM ITERATION %d" % iteration)
            # find latent variables for ground truth:
            if iteration == 0:
                pass
            else:
                H = [self.problem.latent(x, y, w) for x, y in zip(X, Y)]
            #X_wide = [np.repeat(x, self.problem.n_states_per_label, axis=1)
            #for x in X]
            subsvm.fit(X, H)
            H_hat = [self.problem.inference(x, subsvm.w) for x in X]
            inds = np.arange(len(H))
            for i, h, h_hat in zip(inds, H, H_hat):
                plt.matshow(h.reshape(18, 18))
                plt.colorbar()
                plt.savefig("figures/h_%03d_%03d.png" % (iteration, i))
                plt.close()
                plt.matshow(h_hat.reshape(18, 18))
                plt.colorbar()
                plt.savefig("figures/h_hat_%03d_%03d.png" % (iteration, i))
                plt.close()
            w = subsvm.w
            ws.append(w)
            objectives.append(subsvm.primal_objective_)
            tracer()
        self.w = w
        plt.figure()
        plt.plot(objectives)
        plt.show()
        tracer()
示例#4
0
    def fit(self, X, Y):
        w = np.ones(self.problem.size_psi) * 1e-5
        subsvm = StructuredSVM(self.problem, self.max_iter, self.C,
                self.check_constraints, verbose=0)
        objectives = []
        ws = []
        tracer()
        H = Y
        Y = [y / self.problem.n_states_per_label for y in Y]

        for iteration in xrange(1):
            print("LATENT SVM ITERATION %d" % iteration)
            # find latent variables for ground truth:
            if iteration == 0:
                pass
            else:
                H = [self.problem.latent(x, y, w) for x, y in zip(X, Y)]
            #X_wide = [np.repeat(x, self.problem.n_states_per_label, axis=1)
            #for x in X]
            subsvm.fit(X, H)
            H_hat = [self.problem.inference(x, subsvm.w) for x in X]
            inds = np.arange(len(H))
            for i, h, h_hat in zip(inds, H, H_hat):
                plt.matshow(h.reshape(18, 18))
                plt.colorbar()
                plt.savefig("figures/h_%03d_%03d.png" % (iteration, i))
                plt.close()
                plt.matshow(h_hat.reshape(18, 18))
                plt.colorbar()
                plt.savefig("figures/h_hat_%03d_%03d.png" % (iteration, i))
                plt.close()
            w = subsvm.w
            ws.append(w)
            objectives.append(subsvm.primal_objective_)
            tracer()
        self.w = w
        plt.figure()
        plt.plot(objectives)
        plt.show()
        tracer()
def main():
    X, Y = generate_blocks(n_samples=1)
    #X, Y = generate_checker()
    #X, Y = generate_easy(n_samples=1)
    #X, Y = generate_big_checker()
    crf = BinaryGridCRF()
    #clf = StructuredPerceptron(problem=crf, max_iter=100)
    clf = StructuredSVM(problem=crf,
                        max_iter=200,
                        C=100,
                        verbose=10,
                        check_constraints=True)
    #clf = SubgradientStructuredSVM(problem=crf, max_iter=2000, C=100)
    clf.fit(X, Y)
    Y_pred = clf.predict(X)
    i = 0
    loss = 0
    for x, y, y_pred in zip(X, Y, Y_pred):
        loss += np.sum(y != y_pred)
        fig, plots = plt.subplots(2, 2)
        plots[0, 0].set_title("x")
        plots[0, 0].imshow(x[:, :, 0], interpolation='nearest')
        plots[0, 1].set_title("y")
        plots[0, 1].imshow(y, interpolation='nearest')
        plots[1, 0].set_title("unaries")
        w_unaries = np.zeros(4)
        w_unaries[0] = 1.
        y_unaries = crf.inference(x, w_unaries)
        plots[1, 0].imshow(y_unaries, interpolation='nearest')
        plots[1, 1].set_title("full crf")
        plots[1, 1].imshow(y_pred, interpolation='nearest')
        for plot in plots.ravel():
            plot.set_axis_off()
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)
示例#6
0
    def fit(self, X, Y):
        w = np.ones(self.problem.size_psi) * 1e-5
        subsvm = StructuredSVM(self.problem, self.max_iter, self.C,
                               self.check_constraints, verbose=self.verbose -
                               1, n_jobs=self.n_jobs,
                               break_on_bad=self.break_on_bad)
        #objectives = []
        constraints = None
        ws = []
        #Y = Y / self.problem.n_states_per_label
        H = self.problem.init_latent(X, Y)
        #kmeans_init(X, Y, edges, self.problem.n_states_per_label)
        self.H_init_ = H
        inds = np.arange(len(H))
        for i, h in zip(inds, H):
            plt.matshow(h, vmin=0, vmax=self.problem.n_states - 1)
            plt.colorbar()
            plt.savefig("figures/h_init_%03d.png" % i)
            plt.close()

        for iteration in xrange(10):
            print("LATENT SVM ITERATION %d" % iteration)
            # find latent variables for ground truth:
            if iteration == 0:
                pass
            else:
                H_new = np.array([self.problem.latent(x, y, w)
                                  for x, y in zip(X, Y)])
                if np.all(H_new == H):
                    print("no changes in latent variables of ground truth."
                          " stopping.")
                    break
                print("changes in H: %d" % np.sum(H_new != H))

                # update constraints:
                constraints = [[] for i in xrange(len(X))]
                for sample, h, i in zip(subsvm.constraints_, H_new,
                                        np.arange(len(X))):
                    for constraint in sample:
                        const = find_constraint(self.problem, X[i], h, w,
                                                constraint[0])
                        y_hat, dpsi, _, loss = const
                        constraints[i].append([y_hat, dpsi, loss])
                H = H_new
            #if initialization weak?
            #if iteration == 0:
                #subsvm.max_iter = 10

            #subsvm.fit(X, H, constraints=new_constraints)
            #constraints = subsvm.constraints_
            subsvm.fit(X, H, constraints=constraints)
            #if iteration == 0:
                #subsvm.max_iter = self.max_iter
            H_hat = Parallel(n_jobs=self.n_jobs)(delayed(inference)
                                                 (self.problem, x, subsvm.w)
                                                 for x in X)
            inds = np.arange(len(H))
            for i, h, h_hat in zip(inds, H, H_hat):
                plt.matshow(h, vmin=0, vmax=self.problem.n_states - 1)
                plt.colorbar()
                plt.savefig("figures/h_%03d_%03d.png" % (iteration, i))
                plt.close()
                plt.matshow(h_hat, vmin=0, vmax=self.problem.n_states - 1)
                plt.colorbar()
                plt.savefig("figures/h_hat_%03d_%03d.png" % (iteration, i))
                plt.close()
            w = subsvm.w
            ws.append(w)
            #objectives.append(subsvm.primal_objective_)
        self.w = w
def main():
    X, Y = make_dataset_big_checker(n_samples=1)
    size_y = Y[0].size
    shape_y = Y[0].shape
    n_labels = len(np.unique(Y))
    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

    crf = MultinomialFixedGraphCRFNoBias(n_states=n_labels, graph=graph)
    #crf = MultinomialGridCRF(n_labels=4)
    #clf = StructuredPerceptron(problem=crf, max_iter=50)
    clf = StructuredSVM(problem=crf,
                        max_iter=20,
                        C=1000000,
                        verbose=20,
                        check_constraints=True,
                        positive_constraint=np.arange(crf.size_psi - 1))
    #clf = SubgradientStructuredSVM(problem=crf, max_iter=100, C=10000)
    X_flat = [x.reshape(-1, n_labels).copy("C") 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
    for x, y, y_pred in zip(X, Y, Y_pred):
        y_pred = y_pred.reshape(x.shape[:2])
        #loss += np.sum(y != y_pred)
        loss += np.sum(np.logical_xor(y, y_pred))
        if i > 4:
            continue
        fig, plots = plt.subplots(1, 4)
        plots[0].imshow(y, interpolation='nearest')
        plots[0].set_title("gt")
        pw_z = np.zeros((n_labels, n_labels), dtype=np.int32)
        un = np.ascontiguousarray(-1000 * x.reshape(-1, n_labels)).astype(
            np.int32)
        unaries = alpha_expansion_graph(edges, un, pw_z)
        plots[1].imshow(unaries.reshape(y.shape), interpolation='nearest')
        plots[1].set_title("unaries only")
        plots[2].imshow(y_pred, interpolation='nearest')
        plots[2].set_title("prediction")
        loss_augmented = clf.problem.loss_augmented_inference(
            x.reshape(-1, n_labels), y.ravel(), clf.w)
        loss_augmented = loss_augmented.reshape(y.shape)
        plots[3].imshow(loss_augmented, interpolation='nearest')
        plots[3].set_title("loss augmented")
        fig.savefig("data_%03d.png" % i)
        plt.close(fig)
        i += 1
    print("loss: %f" % loss)