def train(self, canvas: Canvas, ground_truth, iteration=-1, test=False, initialize=False): sess = canvas.sess if iteration < 0: iteration = self.iter if initialize: sess.run(tf.global_variables_initializer()) for i in range(iteration): self.global_step += 1 self.train_step(sess, ground_truth.get_batch(self.batch_size), i + 1) if (i + 1) % self.show_steps == 0: self.eval_step(sess, ground_truth.get_batch(self.batch_size), i + 1) if self.save_step is not None and self.save_step > 0 and ( i + 1) % self.save_step == 0: self.save_graph() canvas.save_step(self.global_step) if test and ground_truth.batch_manager.epoched: print("an epoch finished") self.test_step(sess, ground_truth.get_test()) ground_truth.batch_manager.epoched = False if test: self.test_step(sess, ground_truth.get_test())
def get_canvas(): global canvas if canvas is None: if load_step >= 0: canvas = Canvas() else: canvas = Canvas() return canvas
def test_mlp(): mlp_canvas = Canvas() sr = ShapeRegressor(mlp_canvas, conf_path('model/mlp/1')) res = sr.gen(np.zeros(10)) print(np.mean(np.square(res))) mlp_canvas.close()
def test_gru(): gru_canvas = Canvas() pr = PoseRegressor(gru_canvas, conf_path('model/sgru/1'), 5) res = pr.gen(np.zeros(10), np.zeros((24, 3))) print(np.mean(np.square(res))) gru_canvas.close()
def serve(): smpl = SMPLModel(conf_path('smpl')) from com.learning.canvas import Canvas from com.learning.mlp import Graph canvas = Canvas() g = Graph() canvas.load_graph(conf_path('temp/mlp/sklt/3'), g) smpl = SMPLModel(conf_path('smpl')) vf = VirtualFitting() def idle(m): m.seek(0) kinect_pos = m.read(size(['k'])) kinect_pos = np.fromstring(kinect_pos, 'f').reshape((21, 3)) axangle = kinect_pos[0] if not math.isnan(axangle[0]) and not np.linalg.norm( np.array(axangle)) == 0: ai, aj, ak = tr.euler.mat2euler( tr.axangles.axangle2mat(axangle, math.radians(np.linalg.norm(axangle)))) root_rot = [ai, aj, ak] else: root_rot = [0, 0, 0] kinect_joints = kinect_pos[1:] pose = kinect_joints_to_smpl(kinect_joints, root_rot, g, smpl) print(pose) vf.pose = pose vf.recalculate_pose_displacement() verts = np.array(vf.cloth.vertices, 'f').tobytes() faces = np.array(vf.cloth.faces, 'i').tobytes() m.seek(size(['k'])) m.write(verts) m.write(faces) m.flush() cn = create_shared_connect(size(['k', 'v', 'f', 'b']), 'lbac', 0.01) cn.idle.insert(0, idle) print('start') try: cn.start() except KeyboardInterrupt: print('stopped!') canvas.close() vf.close() return canvas.close() vf.close()
def serve(): smpl = SMPLModel(conf_path('smpl')) from com.learning.canvas import Canvas from com.learning.mlp import Graph canvas = Canvas() g = Graph() canvas.load_graph(conf_path('temp/mlp/sklt/3'), g) smpl = SMPLModel(conf_path('smpl')) def idle(m): timer = Timer(False) m.seek(0) kinect_pos = m.read(size(['k'])) kinect_pos = np.fromstring(kinect_pos, 'f').reshape((21, 3)) # 坐标系转换 # kinect_pos[1:, 2] *= -1 axangle = kinect_pos[0] print(np.linalg.norm(axangle)) if not math.isnan(axangle[0]): ai, aj, ak = tr.euler.mat2euler( tr.axangles.axangle2mat(axangle, math.radians(np.linalg.norm(axangle)))) root_rot = [ai, aj, ak] else: root_rot = [0, 0, 0] print(root_rot) timer.tick('rot') # for i in range(3): # root_rot[i] = math.radians(root_rot[i]) kinect_joints = kinect_pos[1:] # std = np.array([0, -0.295, 0]) # tmp = kinect_joints[0] - std # for i in range(20): # kinect_joints[i] -= tmp pose = kinect_joints_to_smpl(kinect_joints, root_rot, g, smpl) timer.tick('pose') print(pose) smpl.set_params(pose) verts = np.array(smpl.verts, 'f').tobytes() faces = np.array(smpl.faces, 'i').tobytes() m.seek(size(['k'])) m.write(verts) m.write(faces) timer.tick('mesh') m.flush() cn = create_shared_connect(size(['k', 'v', 'f']), 'pstm', 0.01) cn.idle.insert(0, idle) print('start') cn.start() canvas.close()
def train(module, gt): set_attributes(module) canvas = Canvas() canvas.open() if load_step is not None: canvas.load_step(load_step, output_dir) g = module.Graph() if load_step is not None: g.restore() g.global_step = load_step else: g.generate() canvas.path = output_dir g.train(canvas, gt, test=module.test_flag, initialize=load_step is None) if output_dir is not None: canvas.save_all()
smpl = SMPLModel(conf_path('smpl')) pose = np.zeros((24, 3)) pose[0] = (0, 0, 2) pose[1:] += 0 smpl.set_params(pose) joints = smpl.J joints = apply(smpl, np.eye(24), joints) joints = process_joints(joints, pose[0]) show_joints(joints) if __name__ == '__main__': # show_20_joints() # save_20_joints() sklt = load_json('sklt.json') from com.learning.canvas import Canvas from com.learning.mlp import Graph canvas = Canvas() g = Graph() canvas.load_graph(conf_path('temp/mlp/sklt/3'), g) kinect_joints_to_smpl(sklt[1:], [0, 0, 0], g) canvas.close() # test_process_joints()