예제 #1
0
    def test_bfgs_madsen(self):
        from ch import SumOfSquares
        import scipy.optimize
        obj = Ch(lambda x : SumOfSquares(Madsen(x = x)) )

        def errfunc(x):
            obj.x = Ch(x)
            return obj.r

        def gradfunc(x):
            obj.x = Ch(x)
            return obj.dr_wrt(obj.x).ravel()

        x0 = np.array((3., 1.))

        # Optimize with built-in bfgs.
        # Note: with 8 iters, this actually requires 14 gradient evaluations.
        # This can be verified by setting "disp" to 1.
        #tm = time.time()
        x1 = scipy.optimize.fmin_bfgs(errfunc, x0, fprime=gradfunc, maxiter=8, disp=0)
        #print 'forward: took %.es' % (time.time() - tm,)
        self.assertLess(obj.r/2., 0.4)

        # Optimize with chumpy's minimize (which uses scipy's bfgs).
        obj.x = x0
        minimize(fun=obj, x0=[obj.x], method='bfgs', options={'maxiter': 8, 'disp': False})
        self.assertLess(obj.r/2., 0.4)
예제 #2
0
def RigidTransformSlow(**kwargs):
    # Returns a Ch object with dterms 'v', 'rt', and 't'

    result = Ch(lambda v, rt, t: v.dot(Rodrigues(rt=rt)) + t)
    if len(kwargs) > 0:
        result.set(**kwargs)
    return result
예제 #3
0
def MeshToScan(scan,
               mesh_verts,
               mesh_faces,
               mesh_template_or_sampler,
               rho=lambda x: x,
               normalize=True,
               signed=False):
    """Returns a Ch object whose only dterm is 'mesh_verts'"""

    sampler, n_samples = construct_sampler(mesh_template_or_sampler,
                                           mesh_verts.size / 3)

    norm_const = np.sqrt(n_samples) if normalize else 1

    if signed:
        fn = lambda x: SignedSqrt(rho(x)) / norm_const
    else:
        fn = lambda x: ch.sqrt(rho(x)) / norm_const

    result = Ch(lambda mesh_verts: fn(
        MeshDistanceSquared(sample_verts=mesh_verts,
                            sample_faces=mesh_faces,
                            reference_verts=scan.v,
                            reference_faces=scan.f,
                            sampler=sampler,
                            signed=signed)))

    result.mesh_verts = mesh_verts
    return result
예제 #4
0
def PtsToMesh(sample_verts,
              reference_verts,
              reference_faces,
              reference_template_or_sampler,
              rho=lambda x: x,
              normalize=True,
              signed=False):
    """Returns a Ch object whose dterms are 'reference_v' and 'sample_v'"""

    sampler = {'point2sample': sp.eye(sample_verts.size, sample_verts.size)}
    n_samples = sample_verts.size / 3

    norm_const = np.sqrt(n_samples) if normalize else 1

    if signed:
        fn = lambda x: SignedSqrt(rho(x)) / norm_const
    else:
        fn = lambda x: ch.sqrt(rho(x)) / norm_const

    result = Ch(lambda sample_v, reference_v: fn(
        MeshDistanceSquared(sample_verts=sample_v,
                            reference_verts=reference_v,
                            reference_faces=reference_faces,
                            sampler=sampler,
                            signed=signed)))

    result.reference_v = reference_verts
    result.sample_v = sample_verts
    return result
예제 #5
0
파일: camera.py 프로젝트: cadik/opendr
def RigidTransformSlow(**kwargs):
    # Returns a Ch object with dterms 'v', 'rt', and 't'

    result = Ch(lambda v, rt, t : v.dot(Rodrigues(rt=rt)) + t)
    if len(kwargs) > 0:
        result.set(**kwargs)
    return result
예제 #6
0
def Rosen():

    args = {
        'x1': Ch(-120.),
        'x2': Ch(-100.)
    }
    r1 = Ch(lambda x1, x2 : (x2 - x1**2.) * 10., args)
    r2 = Ch(lambda x1 : x1 * -1. + 1, args)

    func = [r1, r2]

    return func, [args['x1'], args['x2']]
예제 #7
0
    def on_changed(self, which):
        if 'ortho' in which:
            w = self.ortho.width
            h = self.ortho.height
            self.glf = OsContext(np.int(w), np.int(h), typ=GL_FLOAT)
            _setup_ortho(self.glf, self.ortho.left.r, self.ortho.right.r,
                         self.ortho.bottom.r, self.ortho.top.r,
                         self.ortho.near, self.ortho.far, self.ortho.view_mtx)
            self.glf.Viewport(0, 0, w, h)
            self.glb = OsContext(np.int(w), np.int(h), typ=GL_UNSIGNED_BYTE)
            self.glb.Viewport(0, 0, w, h)
            _setup_ortho(self.glb, self.ortho.left.r, self.ortho.right.r,
                         self.ortho.bottom.r, self.ortho.top.r,
                         self.ortho.near, self.ortho.far, self.ortho.view_mtx)

        if not hasattr(self, 'num_channels'):
            self.num_channels = 3

        if not hasattr(self, 'bgcolor'):
            self.bgcolor = Ch(np.array([.5] * self.num_channels))
            which.add('bgcolor')

        if not hasattr(self, 'overdraw'):
            self.overdraw = True

        if 'bgcolor' in which:
            self.glf.ClearColor(self.bgcolor.r[0],
                                self.bgcolor.r[1 % self.num_channels],
                                self.bgcolor.r[2 % self.num_channels], 1.)
예제 #8
0
    def test_inv2(self):
        from linalg import Inv

        eps = 1e-8
        idx = 13

        mtx1 = np.random.rand(100).reshape((10,10))
        mtx2 = mtx1.copy()
        mtx2.ravel()[idx] += eps

        diff_emp = (np.linalg.inv(mtx2) - np.linalg.inv(mtx1)) / eps

        mtx1 = Ch(mtx1)
        diff_pred = Inv(mtx1).dr_wrt(mtx1)[:,13].reshape(diff_emp.shape)
        #print(diff_emp)
        #print(diff_pred)
        #print(diff_emp - diff_pred)
        self.assertTrue(np.max(np.abs(diff_pred.ravel()-diff_emp.ravel())) < 1e-4)
예제 #9
0
    def test_svd(self):
        from linalg import Svd
        eps = 1e-3
        idx = 10

        data = np.sin(np.arange(300)*100+10).reshape((-1,3))
        data[3,:] = data[3,:]*0+10
        data[:,1] *= 2
        data[:,2] *= 4
        data = data.copy()
        u,s,v = np.linalg.svd(data, full_matrices=False)
        data = Ch(data)
        data2 = data.r.copy()
        data2.ravel()[idx] += eps
        u2,s2,v2 = np.linalg.svd(data2, full_matrices=False)


        svdu, svdd, svdv = Svd(x=data)

        # test singular values
        diff_emp = (s2-s) / eps
        diff_pred = svdd.dr_wrt(data)[:,idx]
        #print(diff_emp)
        #print(diff_pred)
        ratio = diff_emp / diff_pred
        #print(ratio)
        self.assertTrue(np.max(np.abs(ratio - 1.)) < 1e-4)

        # test V
        diff_emp = (v2 - v) / eps
        diff_pred = svdv.dr_wrt(data)[:,idx].reshape(diff_emp.shape)
        ratio = diff_emp / diff_pred
        #print(ratio)
        self.assertTrue(np.max(np.abs(ratio - 1.)) < 1e-2)

        # test U
        diff_emp = (u2 - u) / eps
        diff_pred = svdu.dr_wrt(data)[:,idx].reshape(diff_emp.shape)
        ratio = diff_emp / diff_pred
        #print(ratio)
        self.assertTrue(np.max(np.abs(ratio - 1.)) < 1e-2)
예제 #10
0
    def test_inv1(self):
        from linalg import Inv

        mtx1 = Ch(np.sin(2**np.arange(9)).reshape((3,3)))
        mtx1_inv = Inv(mtx1)
        dr = mtx1_inv.dr_wrt(mtx1)

        eps = 1e-5
        mtx2 = mtx1.r.copy()
        input_diff = np.sin(np.arange(mtx2.size)).reshape(mtx2.shape) * eps
        mtx2 += input_diff
        mtx2_inv = Inv(mtx2)

        output_diff_emp = (np.linalg.inv(mtx2) - np.linalg.inv(mtx1.r)).ravel()
        output_diff_pred = Inv(mtx1).dr_wrt(mtx1).dot(input_diff.ravel())

        #print(output_diff_emp)
        #print(output_diff_pred)

        self.assertTrue(np.max(np.abs(output_diff_emp - output_diff_pred)) < eps*1e-4)
        self.assertTrue(np.max(np.abs(mtx1_inv.r - np.linalg.inv(mtx1.r)).ravel()) == 0)
예제 #11
0
def pose_numpy_to_pkl(name, out_name):
    """Converts numpy array of (nx105) numpy pose fits to a pickle object of:
	'mean', 'cov', 'pic'"""
    raw = np.load(os.path.join(pose_loc, name))
    n_frames = raw.shape[0]

    data = raw.reshape((n_frames, 102))  # get non global joint rots

    mean = np.mean(data, axis=0)
    cov = np.cov(data.T) + 1e-8 * np.eye(
        102)  # add small amount to prevent matrix not being positive definite
    pic = np.linalg.cholesky(np.linalg.inv((cov)))

    # pre pad correctly to 'include' global rot
    out = {
        'mean_pose': np.pad(mean, (3, 0)),
        'cov': np.pad(cov, ((3, 0), (3, 0))),
        'pic': Ch(np.pad(pic, ((3, 0), (3, 0)))),
    }

    with open(os.path.join(pose_loc, out_name), "wb") as outfile:
        pkl.dump(out, outfile)
예제 #12
0
    def test_inv3(self):
        """Test linalg.inv with broadcasting support."""

        from linalg import Inv

        mtx1 = Ch(np.sin(2**np.arange(12)).reshape((3,2,2)))
        mtx1_inv = Inv(mtx1)
        dr = mtx1_inv.dr_wrt(mtx1)

        eps = 1e-5
        mtx2 = mtx1.r.copy()
        input_diff = np.sin(np.arange(mtx2.size)).reshape(mtx2.shape) * eps
        mtx2 += input_diff
        mtx2_inv = Inv(mtx2)

        output_diff_emp = (np.linalg.inv(mtx2) - np.linalg.inv(mtx1.r)).ravel()
        output_diff_pred = Inv(mtx1).dr_wrt(mtx1).dot(input_diff.ravel())

        # print(output_diff_emp)
        # print(output_diff_pred)

        self.assertTrue(np.max(np.abs(output_diff_emp.ravel() - output_diff_pred.ravel())) < eps*1e-3)
        self.assertTrue(np.max(np.abs(mtx1_inv.r - np.linalg.inv(mtx1.r)).ravel()) == 0)
예제 #13
0
 def errfunc(x):
     obj.x = Ch(x)
     return obj.r
예제 #14
0
    def test_cam_derivatives(self):
        mesh, lightings, camera, frustum, renderers = self.load_basics()

        camparms = {
            'c': {
                'mednz': 2.2e-2,
                'meannz': 4.2e-2,
                'desc': 'center of proj diff',
                'eps0': 4.,
                'eps1': .1
            },
            #'f': {'mednz' : 2.5e-2, 'meannz': 6e-2, 'desc': 'focal diff', 'eps0': 100., 'eps1': .1},
            't': {
                'mednz': 1.2e-1,
                'meannz': 3.0e-1,
                'desc': 'trans diff',
                'eps0': .25,
                'eps1': .1
            },
            'rt': {
                'mednz': 8e-2,
                'meannz': 1.8e-1,
                'desc': 'rot diff',
                'eps0': 0.02,
                'eps1': .5
            },
            'k': {
                'mednz': 7e-2,
                'meannz': 5.1e-1,
                'desc': 'distortion diff',
                'eps0': .5,
                'eps1': .05
            }
        }

        for renderer in renderers:

            im_shape = renderer.shape
            lighting = lightings[renderer.num_channels]

            # Render a rotating mesh
            mesh = get_earthmesh(trans=np.array([0, 0, 5]),
                                 rotation=np.array([math.pi / 2., 0, 0]))
            mesh_verts = Ch(mesh.v.flatten())
            camera.v = mesh_verts
            lighting.v = mesh_verts
            renderer.vc = lighting
            renderer.camera = camera

            for atrname, info in list(camparms.items()):

                # Get pixels and derivatives
                r = renderer.r

                atr = lambda: getattr(camera, atrname)
                satr = lambda x: setattr(camera, atrname, x)

                atr_size = atr().size
                dr = renderer.dr_wrt(atr())

                # Establish a random direction
                tmp = np.random.rand(atr().size) - .5
                direction = (tmp / np.linalg.norm(tmp)) * info['eps0']
                #direction = np.sin(np.ones(atr_size))*info['eps0']
                #direction = np.zeros(atr_size)
                # try:
                #     direction[4] = 1.
                # except: pass
                #direction *= info['eps0']
                eps = info['eps1']

                # Render going forward in that direction
                satr(atr().r + direction * eps / 2.)
                rfwd = renderer.r

                # Render going backward in that direction
                satr(atr().r - direction * eps / 1.)
                rbwd = renderer.r

                # Put back
                satr(atr().r + direction * eps / 2.)

                # Establish empirical and predicted derivatives
                dr_empirical = (np.asarray(rfwd, np.float64) -
                                np.asarray(rbwd, np.float64)).ravel() / eps
                dr_predicted = dr.dot(col(direction.flatten())).reshape(
                    dr_empirical.shape)

                images = OrderedDict()
                images['shifted %s' %
                       (atrname, )] = np.asarray(rfwd, np.float64) - .5
                images[r'empirical %s' % (atrname, )] = dr_empirical
                images[r'predicted %s' % (atrname, )] = dr_predicted
                images[info['desc']] = dr_predicted - dr_empirical

                nonzero = images[info['desc']][np.nonzero(
                    images[info['desc']] != 0)[0]]

                mederror = np.median(np.abs(nonzero))
                meanerror = np.mean(np.abs(nonzero))
                if visualize:
                    matplotlib.rcParams.update({'font.size': 18})
                    plt.figure(figsize=(6 * 3, 2 * 3))
                    for idx, title in enumerate(images.keys()):
                        plt.subplot(1, len(list(images.keys())), idx + 1)
                        im = process(images[title].reshape(im_shape),
                                     vmin=-.5,
                                     vmax=.5)
                        plt.title(title)
                        plt.imshow(im)

                    print('%s: median nonzero %.2e' % (
                        atrname,
                        mederror,
                    ))
                    print('%s: mean nonzero %.2e' % (
                        atrname,
                        meanerror,
                    ))
                    plt.draw()
                    plt.show()

                self.assertLess(meanerror, info['meannz'])
                self.assertLess(mederror, info['mednz'])
예제 #15
0
 def set_and_get_r(self, x_in):
     self.x = Ch(x_in)
     return col(self.r)
예제 #16
0
 def gradfunc(x):
     obj.x = Ch(x)
     return obj.dr_wrt(obj.x).ravel()
예제 #17
0
            print(lista[iters][5:-4] + ' Already Done')
            continue
        else:
            flag = 1

        if not (os.path.exists('./Res2/result2_' + lista[iters][5:-4] +
                               '.mat')):
            print(lista[iters][5:-4] + ': CRITICAL ERROR, MISSIN RES2')
            continue
        a = sio.loadmat('./Res2/result2_' + lista[iters][5:-4] + '.mat')

        if not ('C' in a):
            print(lista[iters][5:-4] + ' Not elaborated yet')
            continue

        W_Joints = Ch(10)
        W_FMP2P = Ch(0.1)
        W_Landmarks = Ch(1)
        W_Norm_B = Ch(0.5)
        W_Norm_T = Ch(1)
        W_NN = Ch(1)
        W_Head = Ch(1)
        W_Hands = Ch(0)
        result.betas[:] = np.zeros(10)
        result.pose[:] = np.zeros(72)
        print lista[iters]

        Target = loadObj(lista[iters])

        #Rigid alignment
        scale = Ch(1)
예제 #18
0
 def set_and_get_dr(self, x_in):
     self.x = Ch(x_in)
     return self.dr_wrt(self.x).flatten()
예제 #19
0
    def test_spherical_harmonics(self):
        global visualize
        if visualize:
            plt.ion()
    
        # Get mesh
        v, f = get_sphere_mesh()

        from geometry import VertNormals
        vn = VertNormals(v=v, f=f)
        #vn =  Ch(mesh.estimate_vertex_normals())

        # Get camera
        cam, frustum = getcam()
    
        # Get renderer
        from renderer import ColoredRenderer
        cam.v = v
        cr = ColoredRenderer(f=f, camera=cam, frustum=frustum, v=v)
    
        sh_red = SphericalHarmonics(vn=vn, light_color=np.array([1,0,0]))
        sh_green = SphericalHarmonics(vn=vn, light_color=np.array([0,1,0]))
    
        cr.vc = sh_red + sh_green
        
        ims_baseline = []
        for comp_idx, subplot_idx in enumerate([3,7,8,9,11,12,13,14,15]):
        
            sh_comps = np.zeros(9)
            sh_comps[comp_idx] = 1
            sh_red.components =  Ch(sh_comps)
            sh_green.components =  Ch(-sh_comps)
            
            newim = cr.r.reshape((frustum['height'], frustum['width'], 3))
            ims_baseline.append(newim)

            if visualize:
                plt.subplot(3,5,subplot_idx)
                plt.imshow(newim)
                plt.axis('off')
            
        offset = row(.4 * (np.random.rand(3)-.5))
        #offset = row(np.array([1.,1.,1.]))*.05
        vn_shifted = (vn.r + offset)
        vn_shifted = vn_shifted / col(np.sqrt(np.sum(vn_shifted**2, axis=1)))
        vn_shifted = vn_shifted.ravel()
        vn_shifted[vn_shifted>1.] = 1
        vn_shifted[vn_shifted<-1.] = -1
        vn_shifted = Ch(vn_shifted)
        cr.replace(sh_red.vn, vn_shifted)
        if True:
            for comp_idx in range(9):
                if visualize:
                    plt.figure(comp_idx+2)
        
                sh_comps = np.zeros(9)
                sh_comps[comp_idx] = 1
                sh_red.components =  Ch(sh_comps)
                sh_green.components =  Ch(-sh_comps)
        
                pred = cr.dr_wrt(vn_shifted).dot(col(vn_shifted.r.reshape(vn.r.shape) - vn.r)).reshape((frustum['height'], frustum['width'], 3))
                if visualize:
                    plt.subplot(1,2,1)
                    plt.imshow(pred)
                    plt.title('pred (comp %d)' % (comp_idx,))        
                    plt.subplot(1,2,2)
                    
                newim = cr.r.reshape((frustum['height'], frustum['width'], 3))
                emp = newim - ims_baseline[comp_idx]
                if visualize:
                    plt.imshow(emp)
                    plt.title('empirical (comp %d)' % (comp_idx,))
                pred_flat = pred.ravel()
                emp_flat = emp.ravel()
                nnz = np.unique(np.concatenate((np.nonzero(pred_flat)[0], np.nonzero(emp_flat)[0])))
                
                if comp_idx != 0:
                    med_diff = np.median(np.abs(pred_flat[nnz]-emp_flat[nnz]))
                    med_obs = np.median(np.abs(emp_flat[nnz]))
                    if comp_idx == 4 or comp_idx == 8:
                        self.assertTrue(med_diff / med_obs < .6)
                    else:
                        self.assertTrue(med_diff / med_obs < .3)
                if visualize:
                    plt.axis('off')
예제 #20
0
    def test_color_derivatives(self):

        mesh, lightings, camera, frustum, renderers = self.load_basics()

        for renderer in renderers:

            im_shape = renderer.shape
            lighting = lightings[renderer.num_channels]

            # Get pixels and dI/dC
            mesh = get_earthmesh(trans=np.array([0, 0, 5]),
                                 rotation=np.array([math.pi / 2., 0, 0]))
            mesh_verts = Ch(mesh.v)
            mesh_colors = Ch(mesh.vc)

            camera.set(v=mesh_verts)

            # import pdb; pdb.set_trace()
            # print '-------------------------------------------'
            #lighting.set(vc=mesh_colors, v=mesh_verts)

            lighting.vc = mesh_colors[:, :renderer.num_channels]
            lighting.v = mesh_verts

            renderer.set(v=mesh_verts, vc=lighting)

            r = renderer.r
            dr = renderer.dr_wrt(mesh_colors).copy()

            # Establish a random direction
            eps = .4
            direction = (
                np.random.randn(mesh.v.size).reshape(mesh.v.shape) * .1 +
                np.sin(mesh.v * 19) * .1).flatten()

            # Find empirical forward derivatives in that direction
            mesh_colors = Ch(mesh.vc +
                             direction.reshape(mesh.vc.shape) * eps / 2.)
            lighting.set(vc=mesh_colors[:, :renderer.num_channels])
            renderer.set(vc=lighting)
            rfwd = renderer.r

            # Find empirical backward derivatives in that direction
            mesh_colors = Ch(mesh.vc -
                             direction.reshape(mesh.vc.shape) * eps / 2.)
            lighting.set(vc=mesh_colors[:, :renderer.num_channels])
            renderer.set(vc=lighting)
            rbwd = renderer.r

            dr_empirical = (np.asarray(rfwd, np.float64) -
                            np.asarray(rbwd, np.float64)).ravel() / eps
            dr_predicted = dr.dot(col(direction.flatten())).reshape(
                dr_empirical.shape)

            images = OrderedDict()
            images['shifted colors'] = np.asarray(rfwd, np.float64) - .5
            images[
                r'empirical colors $\left(\frac{dI}{dC}\right)$'] = dr_empirical
            images[
                r'predicted colors $\left(\frac{dI}{dC}\right)$'] = dr_predicted
            images['difference colors'] = dr_predicted - dr_empirical

            nonzero = images['difference colors'][np.nonzero(
                images['difference colors'] != 0)[0]]

            if visualize:
                matplotlib.rcParams.update({'font.size': 18})
                plt.figure(figsize=(6 * 3, 2 * 3))
                for idx, title in enumerate(images.keys()):
                    plt.subplot(1, len(list(images.keys())), idx + 1)
                    im = process(images[title].reshape(im_shape),
                                 vmin=-.5,
                                 vmax=.5)
                    plt.title(title)
                    plt.imshow(im)

                plt.show()
                print('color: median nonzero %.2e' %
                      (np.median(np.abs(nonzero)), ))
                print('color: mean nonzero %.2e' %
                      (np.mean(np.abs(nonzero)), ))
            self.assertLess(np.mean(np.abs(nonzero)), 2e-2)
            self.assertLess(np.median(np.abs(nonzero)), 4.5e-3)
예제 #21
0
    def test_distortion(self):
        mesh, lightings, camera, frustum, renderers = self.load_basics()

        renderer = renderers[1]
        lighting = lightings[renderer.num_channels]
        lighting.light_pos = -lighting.light_pos * 100.

        mesh = get_earthmesh(trans=np.array([0, 0, -8]),
                             rotation=np.array([math.pi / 2., 0, 0]))
        mesh_verts = Ch(mesh.v.flatten())
        renderer.camera = camera
        camera.v = mesh_verts
        lighting.v = mesh_verts
        renderer.vc = lighting
        renderer.camera = camera

        camera.rt = np.array([np.pi, 0, 0])

        # Get pixels and derivatives
        im_original = renderer.r.copy()

        #camera.k = np.zeros(5)
        #camera.k = np.arange(8,0,-1)*.1
        #camera.k = np.array([ 0.00249999,  0.42208098,  0.45360267,  0.06808415, -0.38003062])
        camera.k = np.array([5., 25., .3, .4, 1000., 5., 0., 0.])
        im_distorted = renderer.r

        cr = renderer
        cmtx = np.array([[cr.camera.f.r[0], 0, cr.camera.c.r[0]],
                         [0, cr.camera.f.r[1], cr.camera.c.r[1]], [0, 0, 1]])

        from .cvwrap import cv2
        im_undistorted = cv2.undistort(im_distorted, cmtx, cr.camera.k.r)

        d1 = (im_original - im_distorted).ravel()
        d2 = (im_original - im_undistorted).ravel()

        d1 = d1[d1 != 0.]
        d2 = d2[d2 != 0.]

        self.assertGreater(np.mean(d1**2) / np.mean(d2**2), 44.)
        self.assertLess(np.mean(d2**2), 0.0016)
        self.assertGreater(np.median(d1**2) / np.median(d2**2), 650)
        self.assertLess(np.median(d2**2), 1.9e-5)

        if visualize:
            import matplotlib.pyplot as plt
            plt.ion()

            matplotlib.rcParams.update({'font.size': 18})
            plt.figure(figsize=(6 * 3, 2 * 3))
            plt.subplot(1, 4, 1)
            plt.imshow(im_original)
            plt.title('original')

            plt.subplot(1, 4, 2)
            plt.imshow(im_distorted)
            plt.title('distorted')

            plt.subplot(1, 4, 3)
            plt.imshow(im_undistorted)
            plt.title('undistorted by opencv')

            plt.subplot(1, 4, 4)
            plt.imshow(im_undistorted - im_original + .5)
            plt.title('diff')

            plt.draw()
            plt.show()
예제 #22
0
          flag = 1;
          
        if not(os.path.exists('./Res1/result_' + lista[iters][5:-4] +'.mat')):
            print(lista[iters][5:-4] + ':  ERROR, MISSIN RES1')
            continue
        
        a=sio.loadmat('./Res1/result_' + lista[iters][5:-4] +'.mat');
        
        result.betas[:]=np.zeros(10);
        result.pose[:]= np.zeros(72);
        print lista[iters]
 
        Target = loadObj(lista[iters])
    
        #Rigid alignment
        scale=Ch(1);
        trans=ch.array([0,0,0]);
        Tar_shift = Target.vertices+trans;
        indexes=a['pF_lb2'].reshape(6890);
        distances=Tar_shift[indexes-1]-result*scale;   
        
        (t)=ch.minimize(distances, x0 = [trans,result.pose[[0,1,2]]],
            method = 'dogleg', callback = None,
            options = {'maxiter': 50, 'e_3': .0001, 'disp': 1})
        
        #Non-rigid Alignment
        c_pre={};
        if (W_Joints):
            k=a['Joints_Target'];
            j_to_consider = range(0,24)
            J_distances = J_reposed[j_to_consider,:] - (k[j_to_consider,:]+trans);
예제 #23
0
    def test_vert_derivatives(self):

        mesh, lightings, camera, frustum, renderers = self.load_basics()

        for renderer in renderers:

            lighting = lightings[renderer.num_channels]
            im_shape = renderer.shape

            # Render a rotating mesh
            mesh = get_earthmesh(trans=np.array([0, 0, 5]),
                                 rotation=np.array([math.pi / 2., 0, 0]))
            mesh_verts = Ch(mesh.v.flatten())
            camera.set(v=mesh_verts)
            lighting.set(v=mesh_verts)
            renderer.set(camera=camera)
            renderer.set(vc=lighting)

            # Get pixels and derivatives
            r = renderer.r
            dr = renderer.dr_wrt(mesh_verts)

            # Establish a random direction
            direction = (np.random.rand(mesh.v.size).reshape(mesh.v.shape) -
                         .5) * .1 + np.sin(mesh.v * 10) * .2
            direction *= .5
            eps = .2

            # Render going forward in that direction
            mesh_verts = Ch(mesh.v + direction * eps / 2.)
            lighting.set(v=mesh_verts)
            renderer.set(v=mesh_verts, vc=lighting)
            rfwd = renderer.r

            # Render going backward in that direction
            mesh_verts = Ch(mesh.v - direction * eps / 2.)
            lighting.set(v=mesh_verts)
            renderer.set(v=mesh_verts, vc=lighting)
            rbwd = renderer.r

            # Establish empirical and predicted derivatives
            dr_empirical = (np.asarray(rfwd, np.float64) -
                            np.asarray(rbwd, np.float64)).ravel() / eps
            dr_predicted = dr.dot(col(direction.flatten())).reshape(
                dr_empirical.shape)

            images = OrderedDict()
            images['shifted verts'] = np.asarray(rfwd, np.float64) - .5
            images[
                r'empirical verts $\left(\frac{dI}{dV}\right)$'] = dr_empirical
            images[
                r'predicted verts $\left(\frac{dI}{dV}\right)$'] = dr_predicted
            images['difference verts'] = dr_predicted - dr_empirical

            nonzero = images['difference verts'][np.nonzero(
                images['difference verts'] != 0)[0]]

            if visualize:
                matplotlib.rcParams.update({'font.size': 18})
                plt.figure(figsize=(6 * 3, 2 * 3))
                for idx, title in enumerate(images.keys()):
                    plt.subplot(1, len(list(images.keys())), idx + 1)
                    im = process(images[title].reshape(im_shape),
                                 vmin=-.5,
                                 vmax=.5)
                    plt.title(title)
                    plt.imshow(im)

                print('verts: median nonzero %.2e' %
                      (np.median(np.abs(nonzero)), ))
                print('verts: mean nonzero %.2e' %
                      (np.mean(np.abs(nonzero)), ))
                plt.draw()
                plt.show()

            self.assertLess(np.mean(np.abs(nonzero)), 7e-2)
            self.assertLess(np.median(np.abs(nonzero)), 4e-2)
예제 #24
0
    def test_lightpos_derivatives(self):

        mesh, lightings, camera, frustum, renderers = self.load_basics()

        for renderer in renderers:

            im_shape = renderer.shape
            lighting = lightings[renderer.num_channels]

            # Render a rotating mesh
            mesh = get_earthmesh(trans=np.array([0, 0, 5]),
                                 rotation=np.array([math.pi / 2., 0, 0]))
            mesh_verts = Ch(mesh.v.flatten())
            camera.set(v=mesh_verts)

            # Get predicted derivatives wrt light pos
            light1_pos = Ch(np.array([-1000, -1000, -1000]))
            lighting.set(light_pos=light1_pos, v=mesh_verts)
            renderer.set(vc=lighting, v=mesh_verts)

            dr = renderer.dr_wrt(light1_pos).copy()

            # Establish a random direction for the light
            direction = (np.random.rand(3) - .5) * 1000.
            eps = 1.

            # Find empirical forward derivatives in that direction
            lighting.set(light_pos=light1_pos.r + direction * eps / 2.)
            renderer.set(vc=lighting)
            rfwd = renderer.r

            # Find empirical backward derivatives in that direction
            lighting.set(light_pos=light1_pos.r - direction * eps / 2.)
            renderer.set(vc=lighting)
            rbwd = renderer.r

            # Establish empirical and predicted derivatives
            dr_empirical = (np.asarray(rfwd, np.float64) -
                            np.asarray(rbwd, np.float64)).ravel() / eps
            dr_predicted = dr.dot(col(direction.flatten())).reshape(
                dr_empirical.shape)

            images = OrderedDict()
            images['shifted lightpos'] = np.asarray(rfwd, np.float64) - .5
            images[
                r'empirical lightpos $\left(\frac{dI}{dL_p}\right)$'] = dr_empirical
            images[
                r'predicted lightpos $\left(\frac{dI}{dL_p}\right)$'] = dr_predicted
            images['difference lightpos'] = dr_predicted - dr_empirical

            nonzero = images['difference lightpos'][np.nonzero(
                images['difference lightpos'] != 0)[0]]

            if visualize:
                matplotlib.rcParams.update({'font.size': 18})
                plt.figure(figsize=(6 * 3, 2 * 3))
                for idx, title in enumerate(images.keys()):
                    plt.subplot(1, len(list(images.keys())), idx + 1)
                    im = process(images[title].reshape(im_shape),
                                 vmin=-.5,
                                 vmax=.5)
                    plt.title(title)
                    plt.imshow(im)

                plt.show()
                print('lightpos: median nonzero %.2e' %
                      (np.median(np.abs(nonzero)), ))
                print('lightpos: mean nonzero %.2e' %
                      (np.mean(np.abs(nonzero)), ))
            self.assertLess(np.mean(np.abs(nonzero)), 2.4e-2)
            self.assertLess(np.median(np.abs(nonzero)), 1.2e-2)
예제 #25
0
 def test_dogleg_madsen(self):
     obj = Madsen(x = Ch(np.array((3.,1.))))
     minimize(fun=obj, x0=[obj.x], method='dogleg', options={'maxiter': 34, 'disp': False})
     self.assertTrue(np.sum(obj.r**2)/2 < 0.386599528247)