예제 #1
0
def test_joint_feature_discrete():
    """
    Testing with a single type of nodes. Must de aw well as EdgeFeatureGraphCRF
    """
    X, Y = generate_blocks_multinomial(noise=2, n_samples=1, seed=1)
    x, y = X[0], Y[0]
    edge_list = make_grid_edges(x, 4, return_lists=True)
    edges = np.vstack(edge_list)
    edge_features = edge_list_to_features(edge_list)
    x = ([x.reshape(-1, 3)], [edges], [edge_features])
    y_flat = y.ravel()
    #for inference_method in get_installed(["lp", "ad3", "qpbo"]):
    if True:
        crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
        joint_feature_y = crf.joint_feature(x, y_flat)
        assert_equal(joint_feature_y.shape, (crf.size_joint_feature,))
        # first horizontal, then vertical
        # we trust the unaries ;)
        n_states = crf.l_n_states[0]
        n_features = crf.l_n_features[0]
        pw_joint_feature_horz, pw_joint_feature_vert = joint_feature_y[n_states *
                                         n_features:].reshape(
                                             2, n_states, n_states)
        assert_array_equal(pw_joint_feature_vert, np.diag([9 * 4, 9 * 4, 9 * 4]))
        vert_joint_feature = np.diag([10 * 3, 10 * 3, 10 * 3])
        vert_joint_feature[0, 1] = 10
        vert_joint_feature[1, 2] = 10
        assert_array_equal(pw_joint_feature_horz, vert_joint_feature)
예제 #2
0
def test_inference_ad3():
    
    x, y, pw_horz, pw_vert, res, n_states = inference_data()
    # same inference through CRF inferface
    crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]]
                                      , inference_method="ad3")   
    crf.initialize(x, y)
    #crf.initialize([x], [y])
    w = np.hstack([np.eye(3).ravel(), -pw_horz.ravel(), -pw_vert.ravel()])
    y_pred = crf.inference(x, w, relaxed=True)
    if isinstance(y_pred, tuple):
        # ad3 produces an integer result if it found the exact solution
        #np.set_printoptions(precision=2, threshold=9999)
        assert_array_almost_equal(res[0], y_pred[0][0].reshape(-1, n_states), 5)
        assert_array_almost_equal(res[1], y_pred[1][0], 5)
        assert_array_equal(y, np.argmax(y_pred[0][0], axis=-1), 5)

        # again, this time discrete predictions only
    crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]]
                                      , inference_method="ad3")   
    #crf.initialize([x], [y])
    w = np.hstack([np.eye(3).ravel(), -pw_horz.ravel(), -pw_vert.ravel()])
    crf.initialize(x)
    y_pred = crf.inference(x, w, relaxed=False)
    assert_array_equal(y, y_pred)
예제 #3
0
def test_energy_discrete():
#     for inference_method in get_installed(["qpbo", "ad3"]):
#         crf = EdgeFeatureGraphCRF(n_states=3,
#                                   inference_method=inference_method,
#                                   n_edge_features=2, n_features=3)
        crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
        
        for i in range(10):
            x = np.random.normal(size=(7, 8, 3))
            edge_list = make_grid_edges(x, 4, return_lists=True)
            edges = np.vstack(edge_list)
            edge_features = edge_list_to_features(edge_list)
            x = ([x.reshape(-1, 3)], [edges], [edge_features])

            unary_params = np.random.normal(size=(3, 3))
            pw1 = np.random.normal(size=(3, 3))
            pw2 = np.random.normal(size=(3, 3))
            w = np.hstack([unary_params.ravel(), pw1.ravel(), pw2.ravel()])
            crf.initialize(x)
            y_hat = crf.inference(x, w, relaxed=False)
            #flat_edges = crf._index_all_edges(x)
            energy = compute_energy(crf._get_unary_potentials(x, w)[0],
                                    crf._get_pairwise_potentials(x, w)[0], edges, #CAUTION: pass the flatened edges!!
                                    y_hat)

            joint_feature = crf.joint_feature(x, y_hat)
            energy_svm = np.dot(joint_feature, w)

            assert_almost_equal(energy, energy_svm)
예제 #4
0
def debug_joint_feature():
    # -------------------------------------------------------------------------------------------
    #print "---MORE COMPLEX GRAPH :) ---------------------------------------------------------------------"
    g = NodeTypeEdgeFeatureGraphCRF(    
                    2                   #how many node type?
                 , [2, 3]               #how many possible labels per node type?
                 , [3, 4]               #how many features per node type?
                 , np.array([  [1, 2]
                             , [2, 3]])      #how many features per node type X node type? 
                    )
 
    l_node_f = [  np.array([ [1,1,1], [2,2,2] ])
                , np.array([ [.11, .12, .13, .14], [.21, .22, .23, .24], [.31, .32, .33, .34]]) 
              ]
    l_edges = [ np.array([[0, 1]]) #type 0 node 0 to type 0 node 0 
              , np.array([[0, 1]])
              , None
              , None
              ]              
    l_edge_f = [  np.array([[.111]])
              , np.array([[.221, .222]])
              , None
              , None
              ]
     
    x = (l_node_f, l_edges, l_edge_f)
    #print "- - - - - - - - - - - - - - - - - - - - - - - - - - - "
    y = np.hstack([   np.array([0, 1]),
                   np.array([0, 1, 2])
            ])
    #print y
    g.initialize(x, y)
    jf = g.joint_feature(x,y)
    #print "joint_feature = \n", `jf`
    #print
    assert_array_equal(jf, jf)
    assert_array_almost_equal(jf
                       , np.array(
      [ 1. , 1., 1.  , 2.,  2., 2.    
       , 0.11 , 0.12 , 0.13 , 0.14 ,   0.21 ,  0.22 ,  0.23 ,  0.24   ,  0.31 ,  0.32 ,  0.33 ,  0.34 
       
       ,  0.   ,  0.111,  0.   ,  0.   ,  0.   ,  0.221,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.222,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.  
       ]))
예제 #5
0
def more_complex_graph():
    g = NodeTypeEdgeFeatureGraphCRF(    
                    2                   #how many node type?
                 , [2, 3]               #how many labels   per node type?
                 , [3, 4]               #how many features per node type?
                 , np.array([  [1, 2]
                             , [2, 3]])      #how many features per node type X node type? 
                 )
 
#     nodes  = np.array( [[0,0], [0,1], [1, 0], [1, 1], [1, 2]] )  
    node_f = [  np.array([ [1,1,1], [2,2,2] ])
              , np.array([ [.11, .12, .13, .14], [.21, .22, .23, .24], [.31, .32, .33, .34]]) 
              ]
    edges  = [ np.array( [  [0,1]   #an edge from 0 to 1
                        ])
              , np.array( [ 
                          [0,0]   #an edge from typ0:0 to typ1:0 
                        ])
              , None
              , None
              ]    
    edge_f = [ np.array([[.111]])
              , np.array([[.221, .222]])
              , None
              , None
              ]
     
    x = (node_f, edges, edge_f)
    y = np.hstack([ np.array([0, 0]) 
                  , 2+np.array([0, 0, 0])
         ])
    return g, x, y
예제 #6
0
def get_simple_graph_structure():
    g = NodeTypeEdgeFeatureGraphCRF(
                    1                   #how many node type?
                 , [4]                  #how many labels   per node type?
                 , [3]                  #how many features per node type?
                 , np.array([[3]])      #how many features per node type X node type?                  
                 )
    return g
    def _getCRFModel(self, clsWeights=None):

        crf = NodeTypeEdgeFeatureGraphCRF(
            self.nbType,  # How many node types?
            self._nbClass,  # How many states per type?
            self.lNodeFeatNb,  # How many node features per type?
            self.lEdgeFeatNb,  # How many edge features per type x type?
            inference_method="ad3",
            l_class_weight=clsWeights)
        return crf
예제 #8
0
def test_energy_continuous():
    # make sure that energy as computed by ssvm is the same as by lp
    np.random.seed(0)
    #for inference_method in get_installed(["lp", "ad3"]):
    if True:
        found_fractional = False
        crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])

        while not found_fractional:
            x = np.random.normal(size=(7, 8, 3))
            edge_list = make_grid_edges(x, 4, return_lists=True)
            edges = np.vstack(edge_list)
            edge_features = edge_list_to_features(edge_list)
            x = ([x.reshape(-1, 3)], [edges], [edge_features])

            unary_params = np.random.normal(size=(3, 3))
            pw1 = np.random.normal(size=(3, 3))
            pw2 = np.random.normal(size=(3, 3))
            w = np.hstack([unary_params.ravel(), pw1.ravel(), pw2.ravel()])
            crf.initialize(x)
            res, energy = crf.inference(x, w, relaxed=True, return_energy=True)
            found_fractional = np.any(np.max(res[0], axis=-1) != 1)
            joint_feature = crf.joint_feature(x, res)
            energy_svm = np.dot(joint_feature, w)

            assert_almost_equal(energy, -energy_svm)
예제 #9
0
def test_checks():
    g = NodeTypeEdgeFeatureGraphCRF(
                    1                   #how many node type?
                 , [4]                  #how many labels   per node type?
                 , [3]                  #how many features per node type?
                 , np.array([[3]])      #how many features per node type X node type? 
                 )

    g = NodeTypeEdgeFeatureGraphCRF(
                    2                   #how many node type?
                 , [2, 3   ]               #how many labels   per node type?
                 , [4, 5]                  #how many features per node type?
                 , np.array([[1, 2], [2,4]])      #how many features per node type X node type? 
                 )
    
    with pytest.raises(ValueError):
        g = NodeTypeEdgeFeatureGraphCRF(
                        3                   #how many node type?
                     , [2, 3   ]               #how many labels   per node type?
                     , [4, 5]                  #how many features per node type?
                     , np.array([[1, 2], [2,4]])      #how many features per node type X node type? 
                     )
 
    with pytest.raises(ValueError):
        g = NodeTypeEdgeFeatureGraphCRF(
                        2                   #how many node type?
                     , [2, 3   ]               #how many labels   per node type?
                     , [4, 5, 3]                  #how many features per node type?
                     , np.array([[1, 2], [2,4]])      #how many features per node type X node type? 
                     )

    with pytest.raises(ValueError):
        g = NodeTypeEdgeFeatureGraphCRF(
                        3                   #how many node type?
                     , [2, 3   ]               #how many labels   per node type?
                     , [4, 5]                  #how many features per node type?
                     , np.array([[1, 2], [2,4]])      #how many features per node type X node type? 
                     )

    with pytest.raises(ValueError):
        g = NodeTypeEdgeFeatureGraphCRF(
                        2                   #how many node type?
                     , [2, 3   ]               #how many labels   per node type?
                     , [4, 5]                  #how many features per node type?
                     , np.array([[1, 2, 3], [2,3,4]])      #how many features per node type X node type? 
                     )

    with pytest.raises(ValueError):
        g = NodeTypeEdgeFeatureGraphCRF(
                        2                   #how many node type?
                     , [2, 3   ]               #how many labels   per node type?
                     , [4, 5]                  #how many features per node type?
                     , np.array([[1, 2], [99,4]])      #how many features per node type X node type? 
                     )
예제 #10
0
def test_joint_feature_continuous():
    """
    Testing with a single type of nodes. Must de aw well as EdgeFeatureGraphCRF
    """
    # FIXME
    # first make perfect prediction, including pairwise part
    X, Y = generate_blocks_multinomial(noise=2, n_samples=1, seed=1)
    x, y = X[0], Y[0]
    n_states = x.shape[-1]
    edge_list = make_grid_edges(x, 4, return_lists=True)
    edges = np.vstack(edge_list)
    edge_features = edge_list_to_features(edge_list)
    #x = (x.reshape(-1, 3), edges, edge_features)
    x = ([x.reshape(-1, 3)], [edges], [edge_features])
    y = y.ravel()

    pw_horz = -1 * np.eye(n_states)
    xx, yy = np.indices(pw_horz.shape)
    # linear ordering constraint horizontally
    pw_horz[xx > yy] = 1

    # high cost for unequal labels vertically
    pw_vert = -1 * np.eye(n_states)
    pw_vert[xx != yy] = 1
    pw_vert *= 10

    # create crf, assemble weight, make prediction
#     for inference_method in get_installed(["lp", "ad3"]):
#         crf = EdgeFeatureGraphCRF(inference_method=inference_method)
    if True:
        crf = NodeTypeEdgeFeatureGraphCRF(1, [3], [3], [[2]])
        
        w = np.hstack([np.eye(3).ravel(), -pw_horz.ravel(), -pw_vert.ravel()])
        #crf.initialize([x], [y])
        #report_model_config(crf)
        crf.initialize(x, y)
        
        y_pred = crf.inference(x, w, relaxed=True)

        # compute joint_feature for prediction
        joint_feature_y = crf.joint_feature(x, y_pred)
        assert_equal(joint_feature_y.shape, (crf.size_joint_feature,))
예제 #11
0
def test_unary_potentials():
    #print "---SIMPLE---------------------------------------------------------------------"
    #g, (node_f, edges, edge_f) = get_simple_graph_structure(), get_simple_graph()

    g = NodeTypeEdgeFeatureGraphCRF(
                    1                   #how many node type?
                 , [4]                  #how many labels   per node type?
                 , [3]                  #how many features per node type?
                 , np.array([[3]])      #how many features per node type X node type?                  
                 )
    node_f = [ np.array([[1,1,1], 
                         [2,2,2]]) 
              ]
    edges  = [ np.array([[0,1]]) 
              ]    #an edge from 0 to 1
    edge_f = [ np.array([[3,3,3]]) 
              ]
    x = (node_f, edges, edge_f)
    #print "- - - - - - - - - - - - - - - - - - - - - - - - - - - "
    y = np.hstack([ np.array([1,2])])
#     y = np.array([1,0])
    #print y
    g.initialize(x, y)
    
    gref = EdgeFeatureGraphCRF(4,3,3)
    xref = (node_f[0], edges[0], edge_f[0])
    wref = np.arange(gref.size_joint_feature)
    potref = gref._get_unary_potentials(xref, wref)
    #print `potref`
    
    w = np.arange(g.size_joint_feature)
    pot = g._get_unary_potentials(x, w)
    #print `pot`
    assert_array_equal(pot, [potref])

    pwpotref = gref._get_pairwise_potentials(xref, wref)
    #print `pwpotref`
    pwpot = g._get_pairwise_potentials(x, w)
    #print `pwpot`
    assert_array_equal(pwpot, [pwpotref])
예제 #12
0
def test_joint_feature3():

    # -------------------------------------------------------------------------------------------
    #print "---MORE COMPLEX GRAPH AGAIN :) ---------------------------------------------------------------------"
    g = NodeTypeEdgeFeatureGraphCRF(    
                    2                   #how many node type?
                 , [2, 3]               #how many labels   per node type?
                 , [3, 4]               #how many features per node type?
                 , np.array([  [0, 2]
                             , [2, 3]])      #how many features per node type X node type? 
                 )
 
#     nodes  = np.array( [[0,0], [0,1], [1, 0], [1, 1], [1, 2]] )  
    node_f = [  np.array([ [1,1,1], [2,2,2] ])
              , np.array([ [.11, .12, .13, .14], [.21, .22, .23, .24], [.31, .32, .33, .34]]) 
              ]
    edges  = [ None
              , np.array( [ 
                          [0,1]   #an edge from typ0:0 to typ1:1 
                        ])
              , None
              , np.array( [ 
                          [0,1],   #an edge from typ0:0 to typ1:1 
                          [1,2]   #an edge from typ1:1 to typ1:2 
                        ])
              ]    
    edge_f = [ None
              , np.array([[.221, .222]])
              , None
              , np.array([[.01,  .02,  .03 ],
                          [.001, .002, .003]])
              ]
     
    x = (node_f, edges, edge_f)
    #print "- - - - - - - - - - - - - - - - - - - - - - - - - - - "
    y = np.hstack([ np.array([0, 0]) 
                  , 2+np.array([0, 0, 0])
         ])
    #print y
    g.initialize(x, y)
    #print g.size_unaries
    #print g.size_pairwise
    jf = g.joint_feature(x,y)
    #print "joint_feature = \n", `jf`
    #print
    assert_array_equal(jf, jf)
    assert_array_almost_equal(jf
                       , np.array([ 3.   ,  3.   ,  3.   ,  0.   ,  0.   ,  0.   ,  
                                   0.63 ,  0.66 ,  0.69 ,  0.72 ,    0. , 0.,  0.,  0.   ,    0.,  0., 0. ,  0.,
                                   #edges 0 to 0 2x2 states
                                   #typ0 typ0 EMPTY
                                   #typ0 typ1
                                   .221, 0., 0.,   0., 0., 0.,
                                   .222, 0., 0.,   0., 0., 0.,
                                   #typ1 typ0
                                    0., 0., 0.,   0., 0., 0.,
                                    0., 0., 0.,   0., 0., 0.,
                                   #typ1 typ1
                                    0.011, 0., 0.,   0., 0., 0.,   0., 0., 0., 
                                    0.022, 0., 0.,   0., 0., 0.,   0., 0., 0.,
                                    0.033, 0., 0.,   0., 0., 0.,   0., 0., 0.
                                   ])
                              )

    #print "- - - - - - - - - - - - - - - - - - - - - - - - - - - "
    y = np.hstack([ np.array([0, 1]) 
                  , 2+np.array([1, 1, 0])
         ])
    #print y
    g.initialize(x, y)
    jf = g.joint_feature(x,y)
    #print "joint_feature = \n", `jf`
    #print
    assert_array_equal(jf, jf)
    assert_array_almost_equal(jf
                       , np.array([ 1.   ,  1.   ,  1.   ,  2.   ,  2.   ,  2.   ,  
                                   .31, .32, .33, .34 ,    .32, .34, .36, .38   ,    0.,  0., 0. ,  0.,
                                   #edges 0 to 0 2x2 states
                                   #typ0 typ0 EMPTY
                                   #typ0 typ1
                                   0., .221, 0.,   0., 0., 0.,
                                   0., .222, 0.,   0., 0., 0.,
                                   #typ1 typ0
                                    0., 0., 0.,   0., 0., 0.,
                                    0., 0., 0.,   0., 0., 0.,
                                   #typ1 typ1
                                    0., 0., 0.,   0.001, 0.01, 0.,   0., 0., 0., 
                                    0., 0., 0.,   0.002, 0.02, 0.,   0., 0., 0.,
                                    0., 0., 0.,   0.003, 0.03, 0.,   0., 0., 0.
                                   ])
                              )

    w = np.array([ 1,1,1, 2,2,2,   10,10,10,10, 20,20,20,20, 30,30,30,30 ]
                  +[1.0]*51, dtype=np.float64
                  )
    #print `w`
    ret_u = g._get_unary_potentials(x, w)
    #print `ret_u`
    assert len(ret_u) == 2
    assert_array_almost_equal(ret_u[0], np.array([   #n_nodes x n_states
                                              [3,  6],
                                              [6, 12]]))

    assert_array_almost_equal(ret_u[1], np.array([   #n_nodes x n_states
                                                [5, 10, 15],
                                                [9, 18, 27],
                                                [13, 26, 39]]))
    
    assert len(w) == g.size_joint_feature
    ret_pw = g._get_pairwise_potentials(x, w)
    #     for _pw in ret_pw:
    #         print "_pw ", `_pw`
    pw00, pw01, pw10, pw11 = ret_pw
    assert len(pw00) == 0
    assert_array_almost_equal(pw01,np.array([   #n_edges, n_states, n_states
                                  [[0.443,  0.443,  0.443],
                                   [0.443,  0.443,  0.443]]
                                               ]))
    assert len(pw10) == 0
    
    assert_array_almost_equal(pw11,np.array([   #n_edges, n_states, n_states
                                  [[0.06 ,  0.06 ,  0.06],
                                   [0.06 ,  0.06 ,  0.06],
                                   [0.06 ,  0.06 ,  0.06]]
                                             ,
                                  [[0.006,  0.006,  0.006],
                                   [0.006,  0.006,  0.006],
                                   [0.006,  0.006,  0.006]]        
                                               ]))
예제 #13
0
def test_joint_feature2():

    # -------------------------------------------------------------------------------------------
    #print "---MORE COMPLEX GRAPH :) ---------------------------------------------------------------------"
    g, x, y = more_complex_graph()
    #print y
    
    
    g.initialize(x, y)
    jf = g.joint_feature(x,y)
    #print "joint_feature = \n", `jf`
    #print
    assert_array_equal(jf, jf)
    assert_array_almost_equal(jf
                       , np.array([ 3.   ,  3.   ,  3.   ,  0.   ,  0.   ,  0.   ,  0.63 ,  0.66 ,
        0.69 ,  0.72 ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.111,  0.   ,  0.   ,  0.   ,  0.221,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.222,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ]))

    #print "---MORE COMPLEX GRAPH :) -- BIS -------------------------------------------------------------------"
    g = NodeTypeEdgeFeatureGraphCRF(    
                    2                   #how many node type?
                 , [2, 3]               #how many labels   per node type?
                 , [3, 4]               #how many features per node type?
                 , np.array([  [1, 2]
                             , [2, 3]])      #how many features per node type X node type? 
                 )
 
    node_f = [  np.array([ [1,1,1], [2,2,2] ])
              , np.array([ [.11, .12, .13, .14], [.21, .22, .23, .24], [.31, .32, .33, .34]]) 
              ]
    edges  = [ np.array( [  [0,1]] ),   #an edge from 0 to 1
               np.array( [  [0,2]] )   #an edge from 0 to 2
               , None, None
                        ]    
    edge_f = [ np.array([[.111]])
              , np.array([[.221, .222]])
              , None
              , None
              ]
     
    x = ( node_f, edges, edge_f)
    #print "- - - - - - - - - - - - - - - - - - - - - - - - - - - "
    y = np.hstack([np.array([0, 1]),
                   2+np.array([0, 1, 2])])
    #print y
    g.initialize(x, y)
    jf = g.joint_feature(x,y)
    #print "joint_feature = \n", `jf`
    #print
    assert_array_equal(jf, jf)
    assert_array_almost_equal(jf
                       , np.array([ 1.   ,  1.   ,  1.   ,  2.   ,  2.   ,  2.   ,  0.11 ,  0.12 ,
        0.13 ,  0.14 ,  0.21 ,  0.22 ,  0.23 ,  0.24 ,  0.31 ,  0.32 ,
        0.33 ,  0.34 ,  0.   ,  0.111,  0.   ,  0.   ,  0.   ,  0.   ,
        0.221,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.222,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ]))
    
    #print "MORE COMPLEX GRAPH :) -- BIS  OK"
    #print "--- REORDERED MORE COMPLEX GRAPH :) ---------------------------------------------------------------------"
    node_f = [  np.array([ [2,2,2], [1,1,1] ])
              , np.array([ [.31, .32, .33, .34], [.11, .12, .13, .14], [.21, .22, .23, .24]]) 
              ]
    edges  = [ np.array( [  [1, 0]] ),
               np.array( [  [1,0]] )   #an edge from 0 to 2
               , None, None
                        ]    
    edge_f = [ np.array([[.111]])
              , np.array([[.221, .222]])
              , None
              , None
              ]
     
    x = ( node_f, edges, edge_f)
    #print "- - - - - - - - - - - - - - - - - - - - - - - - - - - "
    y = np.hstack([np.array([1, 0]),
                   2+np.array([2, 0, 1])])
    #print y
    g.initialize(x, y)
    jf = g.joint_feature(x,y)
    #print "joint_feature = \n", `jf`
    #print
    assert_array_equal(jf, jf)
    assert_array_almost_equal(jf
                       , np.array([ 1.   ,  1.   ,  1.   ,  2.   ,  2.   ,  2.   ,  0.11 ,  0.12 ,
        0.13 ,  0.14 ,  0.21 ,  0.22 ,  0.23 ,  0.24 ,  0.31 ,  0.32 ,
        0.33 ,  0.34 ,  0.   ,  0.111,  0.   ,  0.   ,  0.   ,  0.   ,
        0.221,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.222,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,
        0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  0.   ]))
예제 #14
0
            l_n_states = [NCELL + 1, 2
                          ]  # 11 states for pixel nodes, 2 states for pictures
            l_n_feat = [45, 7]  # 45 features for pixels, 7 for pictures
            ll_n_feat = [
                [
                    180, 45
                ],  # 2 feature between pixel nodes, 1 between pixel and picture
                [45, 0]
            ]  # , nothing between picture nodes (no picture_to_picture edge anyway)

            print " TRAINING MULTI-TYPE MODEL "
            #TRAINING
            crf = NodeTypeEdgeFeatureGraphCRF(
                2,  # How many node types?
                l_n_states,  # How many states per type?
                l_n_feat,  # How many node features per type?
                ll_n_feat,  # How many edge features per type x type?
                inference_method=INFERENCE)
            print crf
            ssvm = OneSlackSSVM(
                crf,
                inference_cache=50,
                C=.1,
                tol=0.1,
                #                                 max_iter=MAXITER,
                n_jobs=N_JOBS)

            print "======================================================================================================"
            print "YY[0].shape", Y_train[0].shape
            XX, YY = convertToTwoType(
                X_train,
예제 #15
0
    return [([nf], [e], [ef]) for (nf, e, ef) in X]


if __name__ == '__main__':
    print("Please be patient. Learning will take 5-20 minutes.")
    snakes = load_snakes()
    X_train, Y_train = snakes['X_train'], snakes['Y_train']

    X_train = [one_hot_colors(x) for x in X_train]
    Y_train_flat = [y_.ravel() for y_ in Y_train]

    X_train_directions, X_train_edge_features = prepare_data(X_train)

    inference = 'ad3+'
    # first, train on X with directions only:
    crf = NodeTypeEdgeFeatureGraphCRF(1, [11], [45], [[2]],
                                      inference_method=inference)
    ssvm = OneSlackSSVM(crf,
                        inference_cache=50,
                        C=.1,
                        tol=.1,
                        max_iter=100,
                        n_jobs=1)
    ssvm.fit(convertToSingleTypeX(X_train_directions), Y_train_flat)

    # Evaluate using confusion matrix.
    # Clearly the middel of the snake is the hardest part.
    X_test, Y_test = snakes['X_test'], snakes['Y_test']
    X_test = [one_hot_colors(x) for x in X_test]
    Y_test_flat = [y_.ravel() for y_ in Y_test]
    X_test_directions, X_test_edge_features = prepare_data(X_test)
    Y_pred = ssvm.predict(convertToSingleTypeX(X_test_directions))