def test_chlambda1(self): c1, c2, c3 = ch.Ch(1), ch.Ch(2), ch.Ch(3) adder = ch.ChLambda(lambda x, y: x + y) adder.x = c1 adder.y = c2 self.assertTrue(adder.r == 3) adder.x = c2 self.assertTrue(adder.r == 4) adder.x = c1 self.assertTrue(adder.r == 3)
def test_redundancy_removal(self): for MT in [False, True]: x1, x2 = ch.Ch(10), ch.Ch(20) x1_plus_x2_1 = x1 + x2 x1_plus_x2_2 = x1 + x2 redundant_sum = (x1_plus_x2_1 + x1_plus_x2_2) * 2 redundant_sum.MT = MT self.assertTrue(redundant_sum.a.a is not redundant_sum.a.b) redundant_sum.remove_redundancy() self.assertTrue(redundant_sum.a.a is redundant_sum.a.b)
def computeHemisphereTransformation(chAz, chEl, chDist, objCenter): chDistMat = geometry.Translate(x=ch.Ch(0), y=-chDist, z=ch.Ch(0)) chToObjectTranslate = geometry.Translate(x=objCenter[0], y=objCenter[1], z=objCenter[2]) chRotAzMat = geometry.RotateZ(a=chAz) chRotElMat = geometry.RotateX(a=-chEl) chCamModelWorld = ch.dot(chToObjectTranslate, ch.dot(chRotAzMat, ch.dot(chRotElMat, chDistMat))) return chCamModelWorld
def test_indexing(self): big = ch.Ch(np.arange(60).reshape((10, 6))) little = big[1:3, 3:6] self.assertTrue( np.max(np.abs(little.r - np.array([[9, 10, 11], [15, 16, 17]]))) == 0) little = big[5] self.assertTrue(np.max(np.abs(little.r - np.arange(30, 36))) == 0) self.assertTrue( np.max( np.abs( sp.coo_matrix(little.dr_wrt(big)).col - np.arange(30, 36))) == 0) little = big[2, 3] self.assertTrue(little.r[0] == 15.0) little = big[2, 3:5] self.assertTrue(np.max(np.abs(little.r - np.array([15, 16]))) == 0.) _ = little.dr_wrt(big) # Tests assignment through reorderings aa = ch.arange(4 * 4 * 4).reshape((4, 4, 4))[:3, :3, :3] aa[0, 1, 2] = 100 self.assertTrue(aa[0, 1, 2].r[0] == 100) # Tests assignment through reorderings (NaN's are a special case) aa = ch.arange(9).reshape((3, 3)) aa[1, 1] = np.nan self.assertTrue(np.isnan(aa.r[1, 1])) self.assertFalse(np.isnan(aa.r[0, 0]))
def test_caching(self): vals = [10, 20, 30, 40, 50] f = lambda a, b, c, d, e: a + (b * c) - d**e # Set up our objects Cs = [ch.Ch(v) for v in vals] C_result = f(*Cs) # Sometimes residuals should be cached r1 = C_result.r r2 = C_result.r self.assertTrue(r1 is r2) # Other times residuals need refreshing Cs[0].set(x=5) r3 = C_result.r self.assertTrue(r3 is not r2) # Sometimes derivatives should be cached dr1 = C_result.dr_wrt(Cs[1]) dr2 = C_result.dr_wrt(Cs[1]) self.assertTrue(dr1 is dr2) # Other times derivatives need refreshing Cs[2].set(x=5) dr3 = C_result.dr_wrt(Cs[1]) self.assertTrue(dr3 is not dr2)
def test_scalars(self): try: import theano.tensor as T from theano import function except: return # Set up variables and function vals = [1, 2, 3, 4, 5] f = lambda a, b, c, d, e: a + (b * c) - d**e # Set up our objects Cs = [ch.Ch(v) for v in vals] C_result = f(*Cs) # Set up Theano's equivalents Ts = T.dscalars('T1', 'T2', 'T3', 'T4', 'T5') TF = f(*Ts) T_result = function(Ts, TF) # Make sure values and derivatives are equal self.assertEqual(C_result.r, T_result(*vals)) for k in range(len(vals)): theano_derivative = function(Ts, T.grad(TF, Ts[k]))(*vals) #print(C_result.dr_wrt(Cs[k])) our_derivative = C_result.dr_wrt(Cs[k])[0, 0] #print(theano_derivative, our_derivative) self.assertEqual(theano_derivative, our_derivative)
def getCubeData(scale=(2, 2, 2), st=False, rgb=np.array([1.0, 1.0, 1.0])): dataCube, facesCube = create_cube(scale=(1, 1, 1), st=False, rgba=np.array( [rgb[0], rgb[1], rgb[2], 1.0]), dtype='float32', type='triangles') verticesCube = ch.Ch(dataCube[:, 0:3]) UVsCube = ch.Ch(np.zeros([verticesCube.shape[0], 2])) facesCube = facesCube.reshape([-1, 3]) normalsCube = geometry.chGetNormals(verticesCube, facesCube) haveTexturesCube = [[False]] texturesListCube = [[None]] vColorsCube = ch.Ch(dataCube[:, 3:6]) return verticesCube, facesCube, normalsCube, vColorsCube, texturesListCube, haveTexturesCube
def test_ndim(self): vs = [ch.Ch(np.random.randn(6).reshape(2, 3)) for i in range(6)] res = vs[0] + vs[1] - vs[2] * vs[3] / (vs[4]**2)**vs[5] self.assertTrue(res.shape[0] == 2 and res.shape[1] == 3) res = (vs[0] + 1) + (vs[1] - 2) - (vs[2] * 3) * (vs[3] / 4) / (vs[4]**2)**vs[5] self.assertTrue(res.shape[0] == 2 and res.shape[1] == 3) drs = [res.dr_wrt(v) for v in vs]
def test_serialization(self): # The main challenge with serialization is the "_parents" # attribute, which is a nonserializable WeakKeyDictionary. # So we pickle/unpickle, change a child and verify the value # at root, and verify that both children have parentage. from platform import python_version if python_version()[0] == '2': import cPickle as pickle else: import _pickle as pickle tmp = ch.Ch(10) + ch.Ch(20) tmp = pickle.loads(pickle.dumps(tmp)) tmp.b.x = 30 self.assertTrue(tmp.r[0] == 40) self.assertTrue(list(tmp.a._parents.keys())[0] == tmp) self.assertTrue(list(tmp.a._parents.keys())[0] ==\ list(tmp.b._parents.keys())[0])
def test_reorder_caching(self): a = ch.Ch(np.zeros(8).reshape((4, 2))) b = a.T dr0 = b.dr_wrt(a) a.x = a.x + 1. dr1 = b.dr_wrt(a) self.assertTrue(dr0 is dr1) a.x = np.zeros(4).reshape((2, 2)) dr2 = b.dr_wrt(a) self.assertTrue(dr2 is not dr1)
def test_vectors(self): try: import theano.tensor as T from theano import function except: return for MT in [False, True]: # Set up variables and function vals = [np.random.randn(20) for i in range(5)] f = lambda a, b, c, d, e: a + (b * c) - d**e # Set up our objects Cs = [ch.Ch(v) for v in vals] C_result = f(*Cs) C_result.MT = MT # Set up Theano equivalents Ts = T.dvectors('T1', 'T2', 'T3', 'T4', 'T5') TF = f(*Ts) T_result = function(Ts, TF) if False: import theano.gradient which = 1 theano_sse = (TF**2.).sum() theano_grad = theano.gradient.grad(theano_sse, Ts[which]) theano_fn = function(Ts, theano_grad) print(theano_fn(*vals)) C_result_grad = ch.SumOfSquares(C_result).dr_wrt(Cs[which]) print(C_result_grad) # if True: # aaa = np.linalg.solve(C_result_grad.T.dot(C_result_grad), C_result_grad.dot(np.zeros(C_result_grad.shape[1]))) # theano_hes = theano.R_obbb = theano.R_op() import pdb pdb.set_trace() # Make sure values and derivatives are equal np.testing.assert_array_equal(C_result.r, T_result(*vals)) for k in range(len(vals)): theano_derivative = function(Ts, T.jacobian(TF, Ts[k]))(*vals) our_derivative = np.array(C_result.dr_wrt(Cs[k]).todense()) #print(theano_derivative, our_derivative) # Theano produces has more nans than we do during exponentiation. # So we test only on entries where Theano is without NaN's without_nans = np.nonzero( np.logical_not(np.isnan(theano_derivative.flatten())))[0] np.testing.assert_array_equal( theano_derivative.flatten()[without_nans], our_derivative.flatten()[without_nans])
def test_matmatmult(self): from chumpy import dot mtx1 = ch.Ch(np.arange(6).reshape((3, 2))) mtx2 = ch.Ch(np.arange(8).reshape((2, 4)) * 10) mtx3 = dot(mtx1, mtx2) #print(mtx1.r) #print(mtx2.r) #print(mtx3.r) #print(mtx3.dr_wrt(mtx1).todense()) #print(mtx3.dr_wrt(mtx2).todense()) for mtx in [mtx1, mtx2]: oldval = mtx3.r.copy() mtxd = mtx3.dr_wrt(mtx).copy() mtx_diff = np.random.rand(mtx.r.size).reshape(mtx.r.shape) mtx.x = mtx.r + mtx_diff mtx_emp = mtx3.r - oldval mtx_pred = mtxd.dot(mtx_diff.ravel()).reshape(mtx_emp.shape) self.assertTrue(np.max(np.abs(mtx_emp - mtx_pred)) < 1e-11)
def test_shared(self): chs = [ch.Ch(i) for i in range(10)] vrs = [float(i) for i in range(10)] func = lambda a: a[0] * a[1] + (a[2] * a[3]) / a[4] chained_result = func(chs).r regular_result = func(vrs) self.assertTrue(chained_result == regular_result) #print(chained_result) #print(regular_result) chained_func = func(chs) chained_func.replace(chs[0], ch.Ch(50)) vrs[0] = 50 chained_result = chained_func.r regular_result = func(vrs) self.assertTrue(chained_result == regular_result)
def get_cam_params(self): v_raw = np.sin(np.arange(900)).reshape((-1,3)) v_raw[:, 2] -= 2 rt = ch.zeros(3) t = ch.zeros(3) f = ch.array([500,500]) c = ch.array([320,240]) k = ch.zeros(5) cam_params = {'v': ch.Ch(v_raw), 'rt': rt, 't': t, 'f': f, 'c': c, 'k': k} return cam_params
def test_unary(self): fns = [ ch.exp, ch.log, ch.sin, ch.arcsin, ch.cos, ch.arccos, ch.tan, ch.arctan, ch.negative, ch.square, ch.sqrt, ch.abs, ch.reciprocal ] eps = 1e-8 for f in fns: x0 = ch.Ch(.25) x1 = ch.Ch(x0.r + eps) pred = f(x0).dr_wrt(x0) empr = (f(x1).r - f(x0).r) / eps # print(pred) # print(empr) if f is ch.reciprocal: self.assertTrue( 1e-6 > np.abs(pred.ravel()[0] - empr.ravel()[0])) else: self.assertTrue( 1e-7 > np.abs(pred.ravel()[0] - empr.ravel()[0]))
def test_stacking(self): a1 = ch.Ch(np.arange(10).reshape(2, 5)) b1 = ch.Ch(np.arange(20).reshape(4, 5)) c1 = ch.vstack((a1, b1)) c1_check = np.vstack((a1.r, b1.r)) residuals1 = (c1_check - c1.r).ravel() a2 = ch.Ch(np.arange(10).reshape(5, 2)) b2 = ch.Ch(np.arange(20).reshape(5, 4)) c2 = ch.hstack((a2, b2)) c2_check = np.hstack((a2.r, b2.r)) residuals2 = (c2_check - c2.r).ravel() self.assertFalse(np.any(residuals1)) self.assertFalse(np.any(residuals2)) d0 = ch.array(np.arange(60).reshape((10, 6))) d1 = ch.vstack((d0[:4], d0[4:])) d2 = ch.hstack((d1[:, :3], d1[:, 3:])) tmp = d2.dr_wrt(d0).todense() diff = tmp - np.eye(tmp.shape[0]) self.assertFalse(np.any(diff.ravel()))
def test_iteration_cache(self): """ Each time you set an attribute, the cache (of r's and dr's) of ancestors is cleared. Because children share ancestors, this means these can be cleared multiple times unnecessarily; in some cases, where lots of objects exist, this cache clearing can actually be a bottleneck. Therefore, the concept of an iteration was added; intended to be used in an optimization setting (see optimization.py) and in the set() method, it avoids such redundant clearing of cache.""" a, b, c = ch.Ch(1), ch.Ch(2), ch.Ch(3) x = a + b y = x + c self.assertTrue(y.r[0] == 6) a.__setattr__('x', 10, 1) self.assertTrue(y.r == 15) a.__setattr__('x', 100, 1) self.assertTrue(y.r == 15) a.__setattr__('x', 100, 2) self.assertTrue(y.r == 105) a, b, c = ch.array([1]), ch.array([2]), ch.array([3]) x = a + b y = x + c self.assertTrue(y.r[0] == 6) a.__setattr__('x', np.array([10]), 1) self.assertTrue(y.r[0] == 15) a.__setattr__('x', np.array(100), 1) self.assertTrue(y.r[0] == 15) a.__setattr__('x', np.array(100), 2) self.assertTrue(y.r[0] == 105) a.__setitem__(range(0, 1), np.array(200), 2) self.assertTrue(y.r[0] == 105) a.__setitem__(range(0, 1), np.array(200), 3) self.assertTrue(y.r[0] == 205)
def layerPosteriorsRobustCh(image, template, testMask, backgroundModel, layerPrior, variances): sigma = ch.sqrt(variances) mask = testMask if backgroundModel == 'FULL': mask = np.ones(image.shape[0:2]) # mask = np.repeat(mask[..., np.newaxis], 3, 2) repPriors = ch.tile(layerPrior, image.shape[0:2]) probs = ch.exp( - (image - template)**2 / (2 * variances)) * (1/(sigma * np.sqrt(2 * np.pi))) foregroundProbs = probs[:,:,0] * probs[:,:,1] * probs[:,:,2] * layerPrior backgroundProbs = np.ones(image.shape) outlierProbs = ch.Ch(1-repPriors) lik = pixelLikelihoodRobustCh(image, template, testMask, backgroundModel, layerPrior, variances) # prodlik = np.prod(lik, axis=2) # return np.prod(foregroundProbs*mask, axis=2)/prodlik, np.prod(outlierProbs*mask, axis=2)/prodlik return foregroundProbs*mask/lik, outlierProbs*mask/lik
def project_points(self, cls): cam_params = self.get_cam_params() for key, value in list(cam_params.items()): eps = (np.random.random(value.r.size)-.5) * 1e-5 pp_dist = cls(**cam_params) old_val = pp_dist.r.copy() old_dr = pp_dist.dr_wrt(value).dot(eps) tmp = cam_params[key].r.copy() tmp += eps.reshape(tmp.shape) cam_params[key] = ch.Ch(tmp) diff = ((cls(**cam_params).r - old_val)) raw_dr_diff = np.abs(old_dr.flatten() - diff.flatten()) med_diff = np.median(raw_dr_diff) max_diff = np.max(raw_dr_diff) #pct_diff = (100. * max_diff / np.mean(np.abs(old_val.flatten()))) # print 'testing for %s' % (key,) # print 'empirical' + str(diff.flatten()[:5]) # print 'predicted' + str(old_dr[:5]) # print 'med diff: %.2e' % (med_diff,) # print 'max diff: %.2e' % (max_diff,) #print 'pct diff: %.2e%%' % (pct_diff,) self.assertLess(med_diff, 1e-8) self.assertLess(max_diff, 5e-8) pp_dist = cls(**cam_params) # Test to make sure that depends_on is working for name in ('rt', 't', 'f', 'c', 'k'): aa = pp_dist.camera_mtx setattr(pp_dist, name, getattr(pp_dist, name).r + 1) bb = pp_dist.camera_mtx if name in ('f', 'c'): self.assertTrue(aa is not bb) else: self.assertTrue(aa is bb)
def test_transpose(self): from chumpy.utils import row, col from copy import deepcopy for which in ('C', 'F'): # test in fortran and contiguous mode a = ch.Ch( np.require(np.zeros(8).reshape((4, 2)), requirements=which)) b = a.T b1 = b.r.copy() #dr = b.dr_wrt(a).copy() dr = deepcopy(b.dr_wrt(a)) diff = np.arange(a.size).reshape(a.shape) a.x = np.require(a.r + diff, requirements=which) b2 = b.r.copy() diff_pred = dr.dot(col(diff)).ravel() diff_emp = (b2 - b1).ravel() np.testing.assert_array_equal(diff_pred, diff_emp)
def on_changed(self, which): # pylint: disable=access-member-before-definition, attribute-defined-outside-init if not hasattr(self, 'normalized'): self.normalized = True if hasattr(self, 'v') and hasattr(self, 'f'): if 'f' not in which and hasattr( self, 'faces_by_vertex' ) and self.faces_by_vertex.shape[0] / 3 == self.v.shape[0]: self.tns.v = self.v else: # change in f or in size of v. shouldn't happen often. f = self.f IS = f.ravel() JS = np.array([range(f.shape[0])] * 3).T.ravel() data = np.ones(len(JS)) IS = np.concatenate((IS * 3, IS * 3 + 1, IS * 3 + 2)) JS = np.concatenate((JS * 3, JS * 3 + 1, JS * 3 + 2)) data = np.concatenate((data, data, data)) sz = self.v.size self.faces_by_vertex = sp.csc_matrix((data, (IS, JS)), shape=(sz, f.size)) self.tns = ch.Ch(lambda v: CrossProduct( TriEdges(f, 1, 0, v), TriEdges(f, 2, 0, v))) self.tns.v = self.v if self.normalized: tmp = ch.MatVecMult(self.faces_by_vertex, self.tns) self.normals = NormalizedNx3(tmp) else: test = self.faces_by_vertex.dot( np.ones(self.faces_by_vertex.shape[1])) faces_by_vertex = sp.diags([1. / test], [0]).dot( self.faces_by_vertex).tocsc() normals = ch.MatVecMult(faces_by_vertex, self.tns).reshape((-1, 3)) normals = normals / (ch.sum(normals**2, axis=1)** .25).reshape((-1, 1)) self.normals = normals
def test_maximum(self): from chumpy.utils import row, col from chumpy import maximum # Make sure that when we compare the max of two *identical* numbers, # we get the right derivatives wrt both the_max = maximum(ch.Ch(1), ch.Ch(1)) self.assertTrue(the_max.r.ravel()[0] == 1.) self.assertTrue(the_max.dr_wrt(the_max.a)[0, 0] == 1.) self.assertTrue(the_max.dr_wrt(the_max.b)[0, 0] == 1.) # Now test given that all numbers are different, by allocating from # a pool of randomly permuted numbers. # We test combinations of scalars and 2d arrays. rnd = np.asarray(np.random.permutation(np.arange(20)), np.float64) c1 = ch.Ch(rnd[:6].reshape((2, 3))) c2 = ch.Ch(rnd[6:12].reshape((2, 3))) s1 = ch.Ch(rnd[12]) s2 = ch.Ch(rnd[13]) eps = .1 for first in [c1, s1]: for second in [c2, s2]: the_max = maximum(first, second) for which_to_change in [first, second]: max_r0 = the_max.r.copy() max_r_diff = np.max( np.abs(max_r0 - np.maximum(first.r, second.r))) self.assertTrue(max_r_diff == 0) max_dr = the_max.dr_wrt(which_to_change).copy() which_to_change.x = which_to_change.x + eps max_r1 = the_max.r.copy() emp_diff = (the_max.r - max_r0).ravel() pred_diff = max_dr.dot(col( eps * np.ones(max_dr.shape[1]))).ravel() #print('comparing the following numbers/vectors:') #print(first.r) #print(second.r) #print('empirical vs predicted difference:') #print(emp_diff) #print(pred_diff) #print('-----') max_dr_diff = np.max(np.abs(emp_diff - pred_diff)) #print('max dr diff: %.2e' % (max_dr_diff,)) self.assertTrue(max_dr_diff < 1e-14)
import pandas as pd import numpy as np import matplotlib.pyplot as plt import scipy.sparse as sps import chumpy as ch MU = ch.Ch(50.0) EPSILON = ch.Ch(0.001) n = 300 np.random.seed(0) xorig = ch.array(np.random.randn(n, n)) data = np.array([-1 * np.ones(n), np.ones(n)]) diags = np.array([0, 1]) D = sps.spdiags(data, diags, n, n) def f(u): return ch.sqrt(EPSILON**2 + ch.power(u, 2)) - EPSILON xvec = ch.array(ch.zeros(n * n)) x = xvec.reshape(n, n) Ux = D.dot(x) Uy = D.T.dot(x.T).T Uxsum = f(Ux).sum() Uysum = f(Uy).sum() phi_atv = Uxsum + Uysum print(type(phi_atv)) E = xorig - x diff = ch.power(E, 2).sum() print(diff)
width, height = (640, 480) numPixels = width * height shapeIm = [width, height, 3] win = -1 clip_start = 0.01 clip_end = 10 frustum = { 'near': clip_start, 'far': clip_end, 'width': width, 'height': height } ZshiftGT = ch.Ch([0]) ZshiftGT = ch.Ch([-0.5]) gtCamElevation = 0 gtCamHeight = 0. #meters # NOTE: Light parameters # Lighting parameters are ignored in general chCamElGT = ch.Ch([gtCamElevation]) chCamHeightGT = ch.Ch([gtCamHeight]) chCamFocalLengthGT = ch.Ch([1077.836, 1078.189]) # https://docs.opencv.org/3.0-beta/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html # camera model used in YCB video dataset is 'Poly3'
#From http://www.ppsloan.org/publications/StupidSH36.pdf def chZonalHarmonics(a): zl0 = -ch.sqrt(ch.pi) * (-1.0 + ch.cos(a)) zl1 = 0.5 * ch.sqrt(3.0 * ch.pi) * ch.sin(a)**2 zl2 = -0.5 * ch.sqrt( 5.0 * ch.pi) * ch.cos(a) * (-1.0 + ch.cos(a)) * (ch.cos(a) + 1.0) z = [zl0, zl1, zl2] return ch.concatenate(z) # http://cseweb.ucsd.edu/~ravir/papers/envmap/envmap.pdf chSpherical_harmonics = { (0, 0): lambda theta, phi: ch.Ch([0.282095]), (1, -1): lambda theta, phi: 0.488603 * ch.sin(theta) * ch.sin(phi), (1, 0): lambda theta, phi: 0.488603 * ch.cos(theta), (1, 1): lambda theta, phi: 0.488603 * ch.sin(theta) * ch.cos(phi), (2, -2): lambda theta, phi: 1.092548 * ch.sin(theta) * ch.cos(phi) * ch.sin(theta) * ch.sin(phi), (2, -1): lambda theta, phi: 1.092548 * ch.sin(theta) * ch.sin(phi) * ch.cos(theta), (2, 0): lambda theta, phi: 0.315392 * (3 * ch.cos(theta)**2 - 1), (2, 1): lambda theta, phi: 1.092548 * ch.sin(theta) * ch.cos(phi) * ch.cos(theta),
def setupCamera(v, cameraParams, is_ycb=False): chDistMat = geometry.Translate(x=0, y=cameraParams['Zshift'], z=cameraParams['chCamHeight']) #print ('chDistMat', chDistMat) chRotElMat = geometry.RotateX(a=-cameraParams['chCamEl']) chCamModelWorld = ch.dot(chDistMat, chRotElMat) flipZYRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 0, 1.0, 0.0], [0.0, -1.0, 0, 0.0], [0.0, 0.0, 0.0, 1.0]]) chMVMat = ch.dot(chCamModelWorld, flipZYRotation) if is_ycb: chMVMat = ch.Ch(np.eye(4)) # np.save('extrinsics.npy', chMVMat, allow_pickle=False) chInvCam = ch.inv(chMVMat) modelRotation = chInvCam[0:3, 0:3] chRod = opendr.geometry.Rodrigues(rt=modelRotation).reshape(3) chTranslation = chInvCam[0:3, 3] translation, rotation = (chTranslation, chRod) # camera parameters format suitable for YCB video dataset if 'a' in cameraParams.keys(): # NOTE: Focal lenght is represented in mm and a is no.of pixels per mm _f = 1000 * cameraParams['chCamFocalLength'] * cameraParams['a'] else: # NOTE: Focal length is already in terms of pixels if np.any(cameraParams['chCamFocalLength'] < 1): import sys sys.exit( "Camera Focal length 'chCamFocalLength' is represented in number of pixels." ) _f = cameraParams['chCamFocalLength'] if 'k' in cameraParams.keys(): _k = cameraParams['k'] else: _k = ch.zeros(5) print('Using k', _k) camera = ProjectPoints(v=v, rt=rotation, t=translation, f=_f, c=cameraParams['c'], k=_k) # _f = 1000 * cameraParams['chCamFocalLength'] * cameraParams['a'] # _c = cameraParams['c'] #np.save('intrinsics.npy', camera.camera_mtx, allow_pickle=False) ##print ('camera shape', camera.shape) ##print ('camera ', camera) print('camera.camera_mtx', camera.camera_mtx) np.save('projection_matrix', camera.camera_mtx) #import ipdb #ipdb.set_trace() flipXRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0., 0.0], [0.0, 0., -1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) camera.openglMat = flipXRotation #Needed to match OpenGL flipped axis. return camera, modelRotation, chMVMat
image = cv2.imread('opendr_GT.png') image = np.float64(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) / 255.0 nComps = 5 nRecComps = 4 gmm = mixture.GMM(n_components=nComps, covariance_type='spherical') win = 40 colors = image[image.shape[0] / 2 - win:image.shape[0] / 2 + win, image.shape[1] / 2 - win:image.shape[1] / 2 + win, :].reshape( [4 * win * win, 3]) gmm.fit(colors) imshape = [win * 2, win * 2, 3] numPixels = win * 2 * win * 2 chInput = ch.Ch(colors) numVars = chInput.size recSoftmaxW = ch.Ch(np.random.uniform(0, 1, [nRecComps, numVars]) / numVars) chRecLogistic = ch.exp(ch.dot(recSoftmaxW, chInput.reshape([numVars, 1]))) chRecSoftmax = chRecLogistic.ravel() / ch.sum(chRecLogistic) chZRecComps = ch.zeros([numVars, nRecComps]) chZ = ch.zeros([numVars]) recMeans = ch.Ch(np.random.uniform(0, 1, [3, nRecComps])) recCovars = 0.2 chRecLogLikelihoods = -0.5 * (chZ.reshape([numPixels, 3, 1]) - ch.tile( recMeans, [numPixels, 1, 1]))**2 - ch.log(
blender_teapots = [] teapots = [line.strip() for line in open('teapots.txt')] selection = [ teapots[i] for i in renderTeapotsList] scene_io_utils.loadTargetsBlendData() for teapotIdx, teapotName in enumerate(selection): teapot = bpy.data.scenes[teapotName[0:63]].objects['teapotInstance' + str(renderTeapotsList[teapotIdx])] teapot.layers[1] = True teapot.layers[2] = True targetModels = targetModels + [teapot] blender_teapots = blender_teapots + [teapot] v_teapots, f_list_teapots, vc_teapots, vn_teapots, uv_teapots, haveTextures_list_teapots, textures_list_teapots, vflat, varray, center_teapots = scene_io_utils.loadTeapotsOpenDRData(renderTeapotsList, useBlender, unpackModelsFromBlender, targetModels) azimuth = np.pi chCosAz = ch.Ch([np.cos(azimuth)]) chSinAz = ch.Ch([np.sin(azimuth)]) chAz = 2*ch.arctan(chSinAz/(ch.sqrt(chCosAz**2 + chSinAz**2) + chCosAz)) chAz = ch.Ch([np.pi/4]) chObjAz = ch.Ch([np.pi/4]) chAzRel = chAz - chObjAz elevation = 0 chLogCosEl = ch.Ch(np.log(np.cos(elevation))) chLogSinEl = ch.Ch(np.log(np.sin(elevation))) chEl = 2*ch.arctan(ch.exp(chLogSinEl)/(ch.sqrt(ch.exp(chLogCosEl)**2 + ch.exp(chLogSinEl)**2) + ch.exp(chLogCosEl))) chEl = ch.Ch([0.95993109]) chDist = ch.Ch([camDistance]) chObjAzGT = ch.Ch([np.pi*3/2])
def test_chlambda2(self): passthrough = ch.ChLambda(lambda x: x) self.assertTrue(passthrough.dr_wrt(passthrough.x) is not None) passthrough.x = ch.Ch(123) self.assertTrue(passthrough.dr_wrt(passthrough.x) is not None)
def setupTexturedRenderer(renderer, vstack, vch, f_list, vc_list, vnch, uv, haveTextures_list, textures_list, camera, frustum, sharedWin=None): f = [] f_listflat = [item for sublist in f_list for item in sublist] lenMeshes = 0 for mesh_i, mesh in enumerate(f_listflat): polygonLen = 0 for polygons in mesh: f = f + [polygons + lenMeshes] polygonLen += len(polygons) lenMeshes += len(vch[mesh_i]) fstack = np.vstack(f) if len(vnch) == 1: vnstack = vnch[0] else: vnstack = ch.vstack(vnch) if len(vc_list) == 1: vcstack = vc_list[0] else: vcstack = ch.vstack(vc_list) uvflat = [item for sublist in uv for item in sublist] ftstack = np.vstack(uvflat) texturesch = [] textures_listflat = [item for sublist in textures_list for item in sublist] # import ipdb; ipdb.set_trace() for texture_list in textures_listflat: if texture_list != None: for texture in texture_list: if isinstance(texture, np.ndarray): texturesch = texturesch + [ch.array(texture)] elif texture != None: texturesch = texturesch + [ch.array(texture)] if len(texturesch) == 0: texture_stack = ch.Ch([]) elif len(texturesch) == 1: texture_stack = texturesch[0].ravel() else: texture_stack = ch.concatenate([tex.ravel() for tex in texturesch]) haveTextures_listflat = [ item for sublist in haveTextures_list for item in sublist ] renderer.set(camera=camera, frustum=frustum, v=vstack, f=fstack, vn=vnstack, vc=vcstack, ft=ftstack, texture_stack=texture_stack, v_list=vch, f_list=f_listflat, vc_list=vc_list, ft_list=uvflat, textures_list=textures_listflat, haveUVs_list=haveTextures_listflat, bgcolor=ch.ones(3), overdraw=True) renderer.msaa = True renderer.sharedWin = sharedWin