예제 #1
0
파일: test_npbased.py 프로젝트: jooh/sana
def test_euclideansq():
    responses = util.responses(3)
    rdv = npbased.euclideansq(responses)
    util.assert_shape(rdv, [3, 1])
    np.testing.assert_allclose(rdv[0],
                               np.sum((responses[0, :] - responses[1, :])**2),
                               rtol=RTOL)
예제 #2
0
def test_predict(Model, layers, target):
    outs = Model(layers).feed_forward(u.CNN.INPUTS)
    assert len(list(outs)) == 8
    W, H = u.CNN.NUM_WIDTH, u.CNN.NUM_HEIGHT
    w, h = u.CNN.FILTER_WIDTH, u.CNN.FILTER_HEIGHT
    assert_shape(outs['in:out'].shape, W, H, u.NUM_INPUTS)
    assert_shape(outs['hid1:out'].shape, W - w + 1, H - h + 1, u.NUM_HID1)
    assert_shape(outs['hid2:out'].shape, W - 2 * w + 2, H - 2 * h + 2, u.NUM_HID2)
    u.assert_shape(outs['out:out'].shape, target)
예제 #3
0
def test_predict(Model, layers, target):
    outs = Model(layers).feed_forward(u.CNN.INPUTS)
    assert len(list(outs)) == 8
    W, H = u.CNN.NUM_WIDTH, u.CNN.NUM_HEIGHT
    w, h = u.CNN.FILTER_WIDTH, u.CNN.FILTER_HEIGHT
    assert_shape(outs['in:out'].shape, W, H, u.NUM_INPUTS)
    assert_shape(outs['hid1:out'].shape, W - w + 1, H - h + 1, u.NUM_HID1)
    assert_shape(outs['hid2:out'].shape, W - 2 * w + 2, H - 2 * h + 2,
                 u.NUM_HID2)
    u.assert_shape(outs['out:out'].shape, target)
def test_feed_forward(Model, layers, target):
    outs = Model(layers).feed_forward(u.INPUTS)
    assert len(list(outs)) == 7
    u.assert_shape(outs['in:out'].shape, u.NUM_INPUTS)
    u.assert_shape(outs['hid1:out'].shape, u.NUM_HID1)
    u.assert_shape(outs['hid2:out'].shape, u.NUM_HID2)
    u.assert_shape(outs['out:out'].shape, target)
예제 #5
0
def test_feed_forward(Model, layers, target):
    outs = Model(layers).feed_forward(u.INPUTS)
    assert len(list(outs)) == 7
    u.assert_shape(outs['in:out'].shape, u.NUM_INPUTS)
    u.assert_shape(outs['hid1:out'].shape, u.NUM_HID1)
    u.assert_shape(outs['hid2:out'].shape, u.NUM_HID2)
    u.assert_shape(outs['out:out'].shape, target)
예제 #6
0
def test_decode_from_multiple_layers():
    net = theanets.Regressor([u.NUM_INPUTS, u.NUM_HID1, u.NUM_HID2, dict(
        size=u.NUM_OUTPUTS, inputs=('hid2:out', 'hid1:out'))])
    outs = net.feed_forward(u.INPUTS)
    assert len(list(outs)) == 7
    u.assert_shape(outs['in:out'].shape, u.NUM_INPUTS)
    u.assert_shape(outs['hid1:out'].shape, u.NUM_HID1)
    u.assert_shape(outs['hid2:out'].shape, u.NUM_HID2)
    u.assert_shape(outs['out:out'].shape, u.NUM_OUTPUTS)
def test_decode_from_multiple_layers():
    net = theanets.Regressor([
        u.NUM_INPUTS, u.NUM_HID1, u.NUM_HID2,
        dict(size=u.NUM_OUTPUTS, inputs=('hid2:out', 'hid1:out'))
    ])
    outs = net.feed_forward(u.INPUTS)
    assert len(list(outs)) == 7
    u.assert_shape(outs['in:out'].shape, u.NUM_INPUTS)
    u.assert_shape(outs['hid1:out'].shape, u.NUM_HID1)
    u.assert_shape(outs['hid2:out'].shape, u.NUM_HID2)
    u.assert_shape(outs['out:out'].shape, u.NUM_OUTPUTS)
 def test_encode_hid2(self, net):
     z = net.encode(u.INPUTS, 'hid2')
     u.assert_shape(z.shape, u.NUM_HID2)
 def test_predict_proba(self, net):
     u.assert_shape(net.predict_proba(u.INPUTS).shape, u.NUM_CLASSES)
예제 #10
0
def hinge_energy(input_vector, trimesh, gradient_mode, NUM_VERTS):
    """
    The covariance energy for minimizing Gaussian
    curvature and its gradient

    Its expected that you curry this method into a function
    of a single variable `input_vector` to make it optimizable
    see `hinge_energy_and_grad`
    
    input_vector: a 1D array representing a flattened list
    of positions

    trimesh: a TriMesh object (see local trimesh.py) containing
    all the immutable topology of the mesh. This object
    can be muted with updated vertices and used to recompute
    normals and areas.
    
    gradient_mode sets the return value and gates gradient computation:
      0: return energy
      1: return grad
      2: return (energy, grad)

    NUM_VERTS: how many vertices are in the mesh total
    """
    assert_shape(input_vector, (NUM_VERTS * 3, ))

    # reshape input vector into a list of points
    verts = input_vector.reshape(NUM_VERTS, 3)

    # mutate the trimesh w/ the new verts
    # and update the normals and areas (
    # these are the non-topological properties
    # that change w/ vertices)
    trimesh.vs = verts
    trimesh.update_face_normals_and_areas()
    face_areas = trimesh.get_face_areas()
    face_normals = trimesh.get_face_normals()

    energy = []

    jacobian = np.zeros((NUM_VERTS, 3))

    # for every vertex compute an energy
    for v_index in range(0, len(verts)):

        normal_covariance_matrix = np.zeros((3, 3))

        # for every face touching our vertex (vertex star)
        for f_index in trimesh.vertex_face_neighbors(v_index):
            face = trimesh.faces[f_index]

            # get the indices of the face in proper ijk
            # order where is is the center of the vertex star
            fi_index = v_index
            fj_index, fk_index = [j for j in face if fi_index != j]

            fi = verts[fi_index]
            fj = verts[fj_index]
            fk = verts[fk_index]

            # theta is the angle of the corner
            # of the face at a
            eij = np.subtract(fj, fi)
            eik = np.subtract(fk, fi)
            theta = util.angle_between(eij, eik)

            # the normal of the face
            N = face_normals[f_index]

            # NN^T gives us a 3x3 covariance matrix
            # of the vector. Scale it by Theta
            # and add it to the running covariance matrix
            # being built up for every normal
            normal_covariance_matrix += theta * np.outer(N, N)

        # now you have the covariance matrix for every face
        # normal surrounding this vertex star.
        # the first eigenvalue of this matrix is our energy
        #
        #
        # ``` (eq 4)
        # Since Equation 3 is just the
        # variational form of an eigenvalue problem, λi can also be expressed
        # as the smallest eigenvalue of the 3 × 3 normal covariance matrix
        # ```
        eigenvalues, eigenvectors = \
            eigendecomp(normal_covariance_matrix)

        smallest_eigenvalue = eigenvalues[0]
        associated_eigenvector = eigenvectors[:, 0]

        # the first eigenvalue is the smallest one
        energy.append(smallest_eigenvalue)

        # start computing the gradient if it's needed
        if gradient_mode == 1 or gradient_mode == 2:

            x = associated_eigenvector

            # for every face touching our vertex (vertex star)
            for f_index in trimesh.vertex_face_neighbors(v_index):
                face = trimesh.faces[f_index]

                # fi is v
                fi_index = v_index
                fj_index, fk_index = [j for j in face if fi_index != j]

                # the three vertices of the current face
                fi = verts[fi_index]
                fj = verts[fj_index]
                fk = verts[fk_index]

                # theta is the angle of the corner
                # of the face at fi
                eij = np.subtract(fj, fi)
                eik = np.subtract(fk, fi)
                theta = util.angle_between(eij, eik)

                # the face normal
                N = face_normals[f_index]

                # scalar, double the area
                A = face_areas[f_index] * 2

                # oriented edges
                ejk = np.subtract(fk, fj)
                eki = np.subtract(fi, fk)
                eij = np.subtract(fj, fi)
                assert_shape(ejk, (3, ))

                # derivatives of the normal
                dNdi = np.outer(np.cross(ejk, N), N) / A
                dNdj = np.outer(np.cross(eki, N), N) / A
                dNdk = np.outer(np.cross(eij, N), N) / A
                assert_shape(dNdi, (3, 3))

                # derivatives of the angle
                dThetadj = -1 * np.cross(N, eij / norm(eij))
                dThetadk = -1 * np.cross(N, eki / norm(eki))
                dThetadi = np.cross(N, eij + eki)
                assert_shape(dThetadj, (3, ))

                xdotN = x.dot(N)
                assert_shape(xdotN, ())

                # a 3 vector pointing in the directly the i vertex should move
                jacobian[
                    fi_index] += xdotN * xdotN * dThetadi + 2 * theta * xdotN * x.dot(
                        dNdi)
                jacobian[
                    fj_index] += xdotN * xdotN * dThetadj + 2 * theta * xdotN * x.dot(
                        dNdj)
                jacobian[
                    fk_index] += xdotN * xdotN * dThetadk + 2 * theta * xdotN * x.dot(
                        dNdk)

    # squared sum of the energies is our final cost value
    K = np.sum(energy)**2

    # return energy, gradient or both
    if gradient_mode == 0:
        return K
    elif gradient_mode == 1:
        return jacobian.reshape(NUM_VERTS * 3)
    elif gradient_mode == 2:
        return (K, jacobian.reshape(NUM_VERTS * 3))
예제 #11
0
파일: test_npbased.py 프로젝트: jooh/sana
def test_allpairwisecontrasts_shape():
    util.assert_shape(npbased.allpairwisecontrasts(10), np.array([45, 10]))
예제 #12
0
 def test_predict_logit(self, net):
     u.assert_shape(net.predict_logit(u.INPUTS).shape, u.NUM_CLASSES)
예제 #13
0
def test_predict(Model, layers, output):
    u.assert_shape(Model(layers).predict(u.INPUTS).shape, output)
예제 #14
0
 def test_predict_proba(self, net):
     u.assert_shape(net.predict_proba(u.CNN.INPUTS).shape, u.NUM_CLASSES)
 def test_decode_hid2(self, net):
     x = net.decode(net.encode(u.INPUTS, 'hid2'), 'hid2')
     u.assert_shape(x.shape, u.NUM_INPUTS)
예제 #16
0
def test_predict(Model, layers, output):
    u.assert_shape(Model(layers).predict(u.CNN.INPUTS).shape, output)
예제 #17
0
 def test_encode_hid2(self, net):
     z = net.encode(u.INPUTS, 'hid2')
     u.assert_shape(z.shape, u.NUM_HID2)
예제 #18
0
 def test_predict_logit(self, net):
     u.assert_shape(net.predict_logit(u.CNN.INPUTS).shape, u.NUM_CLASSES)
예제 #19
0
 def test_decode_hid2(self, net):
     x = net.decode(net.encode(u.INPUTS, 'hid2'), 'hid2')
     u.assert_shape(x.shape, u.NUM_INPUTS)