Esempio n. 1
0
def build_factor_graph_model(labels, feats, factor_types, edge_table, infer_alg = GRAPH_CUT):
    """ Build factor graph model

        Args:
            labels: matrix of labels [num_train_samples*len_label]
            feats: maxtrix of feats [num_train_samples*len_feat]
            factory_types: vectors of all factor types
            edge_table: matrix of pairwised edges, each row is a pair of node indeces
            infer_alg: inference algorithm (GRAPH_CUT)

        Returns:
            labels_fg: matrix of labels in factor graph format
            feats_fg: matrix of features in factor graph format
    """

    labels = labels.astype(np.int32)
    num_train_samples = labels.shape[0]
    num_vars = labels.shape[1]
    num_edges = edge_table.shape[0]
    n_stats = 2

    feats_fg = FactorGraphFeatures(num_train_samples)
    labels_fg = FactorGraphLabels(num_train_samples)

    for i in range(num_train_samples):
        cardinaities = np.array([n_stats]*num_vars, np.int32)
        fg = FactorGraph(cardinaities)

        # add unary factors
        for u in range(num_vars):
            data_u = np.array(feats[i,:], np.float64)
            inds_u = np.array([u], np.int32)
            factor_u = Factor(factor_types[u], inds_u, data_u)
            fg.add_factor(factor_u)

        # add pairwise factors
        for v in range(num_edges):
            data_p = np.array([1.0])
            inds_p = np.array(edge_table[v, :], np.int32)
            factor_p = Factor(factor_types[v + num_vars], inds_p, data_p)
            fg.add_factor(factor_p)

        # add factor graph
        feats_fg.add_sample(fg)

        # add corresponding label
        loss_weights = np.array([1.0/num_vars]*num_vars)
        fg_obs = FactorGraphObservation(labels[i,:], loss_weights)
        labels_fg.add_label(fg_obs)

    return (labels_fg, feats_fg)
Esempio n. 2
0
def build_factor_graph_model(labels,
                             feats,
                             factor_types,
                             edge_table,
                             infer_alg=GRAPH_CUT):
    """ Build factor graph model

        Args:
            labels: matrix of labels [num_train_samples*len_label]
            feats: maxtrix of feats [num_train_samples*len_feat]
            factory_types: vectors of all factor types
            edge_table: matrix of pairwised edges, each row is a pair of node indeces
            infer_alg: inference algorithm (GRAPH_CUT)

        Returns:
            labels_fg: matrix of labels in factor graph format
            feats_fg: matrix of features in factor graph format
    """

    labels = labels.astype(np.int32)
    num_train_samples = labels.shape[0]
    num_vars = labels.shape[1]
    num_edges = edge_table.shape[0]
    n_stats = 2

    feats_fg = FactorGraphFeatures(num_train_samples)
    labels_fg = FactorGraphLabels(num_train_samples)

    for i in range(num_train_samples):
        cardinaities = np.array([n_stats] * num_vars, np.int32)
        fg = FactorGraph(cardinaities)

        # add unary factors
        for u in range(num_vars):
            data_u = np.array(feats[i, :], np.float64)
            inds_u = np.array([u], np.int32)
            factor_u = Factor(factor_types[u], inds_u, data_u)
            fg.add_factor(factor_u)

        # add pairwise factors
        for v in range(num_edges):
            data_p = np.array([1.0])
            inds_p = np.array(edge_table[v, :], np.int32)
            factor_p = Factor(factor_types[v + num_vars], inds_p, data_p)
            fg.add_factor(factor_p)

        # add factor graph
        feats_fg.add_sample(fg)

        # add corresponding label
        loss_weights = np.array([1.0 / num_vars] * num_vars)
        fg_obs = FactorGraphObservation(labels[i, :], loss_weights)
        labels_fg.add_label(fg_obs)

    return (labels_fg, feats_fg)
def gen_data(ftype, num_samples, show_data = False):
	from modshogun import Math
	from modshogun import FactorType, Factor, TableFactorType, FactorGraph
	from modshogun import FactorGraphObservation, FactorGraphLabels, FactorGraphFeatures
	from modshogun import MAPInference, TREE_MAX_PROD

	Math.init_random(17)

	samples = FactorGraphFeatures(num_samples)
	labels = FactorGraphLabels(num_samples)

	for i in xrange(num_samples):
		vc = np.array([2,2,2], np.int32)
		fg = FactorGraph(vc)

		data1 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind1 = np.array([0,1], np.int32)
		fac1 = Factor(ftype[0], vind1, data1)
		fg.add_factor(fac1)

		data2 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind2 = np.array([1,2], np.int32)
		fac2 = Factor(ftype[0], vind2, data2)
		fg.add_factor(fac2)

		data3 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind3 = np.array([0], np.int32)
		fac3 = Factor(ftype[1], vind3, data3)
		fg.add_factor(fac3)

		data4 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind4 = np.array([1], np.int32)
		fac4 = Factor(ftype[1], vind4, data4)
		fg.add_factor(fac4)

		data5 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind5 = np.array([2], np.int32)
		fac5 = Factor(ftype[1], vind5, data5)
		fg.add_factor(fac5)

		data6 = np.array([1.0])
		vind6 = np.array([0], np.int32)
		fac6 = Factor(ftype[2], vind6, data6)
		fg.add_factor(fac6)

		data7 = np.array([1.0])
		vind7 = np.array([2], np.int32)
		fac7 = Factor(ftype[2], vind7, data7)
		fg.add_factor(fac7)

		samples.add_sample(fg)
		fg.connect_components()
		fg.compute_energies()

		infer_met = MAPInference(fg, TREE_MAX_PROD)
		infer_met.inference()

		fg_obs = infer_met.get_structured_outputs()
		labels.add_label(fg_obs)

		if show_data:
			state = fg_obs.get_data()
			print state

	return samples, labels
Esempio n. 4
0
def gen_data(ftype, num_samples, show_data=False):
    from modshogun import Math
    from modshogun import FactorType, Factor, TableFactorType, FactorGraph
    from modshogun import FactorGraphObservation, FactorGraphLabels, FactorGraphFeatures
    from modshogun import MAPInference, TREE_MAX_PROD

    Math.init_random(17)

    samples = FactorGraphFeatures(num_samples)
    labels = FactorGraphLabels(num_samples)

    for i in range(num_samples):
        vc = np.array([2, 2, 2], np.int32)
        fg = FactorGraph(vc)

        data1 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind1 = np.array([0, 1], np.int32)
        fac1 = Factor(ftype[0], vind1, data1)
        fg.add_factor(fac1)

        data2 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind2 = np.array([1, 2], np.int32)
        fac2 = Factor(ftype[0], vind2, data2)
        fg.add_factor(fac2)

        data3 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind3 = np.array([0], np.int32)
        fac3 = Factor(ftype[1], vind3, data3)
        fg.add_factor(fac3)

        data4 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind4 = np.array([1], np.int32)
        fac4 = Factor(ftype[1], vind4, data4)
        fg.add_factor(fac4)

        data5 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind5 = np.array([2], np.int32)
        fac5 = Factor(ftype[1], vind5, data5)
        fg.add_factor(fac5)

        data6 = np.array([1.0])
        vind6 = np.array([0], np.int32)
        fac6 = Factor(ftype[2], vind6, data6)
        fg.add_factor(fac6)

        data7 = np.array([1.0])
        vind7 = np.array([2], np.int32)
        fac7 = Factor(ftype[2], vind7, data7)
        fg.add_factor(fac7)

        samples.add_sample(fg)
        fg.connect_components()
        fg.compute_energies()

        infer_met = MAPInference(fg, TREE_MAX_PROD)
        infer_met.inference()

        fg_obs = infer_met.get_structured_outputs()
        labels.add_label(fg_obs)

        if show_data:
            state = fg_obs.get_data()
            print(state)

    return samples, labels