def optimization(ratio): lr0 = 0.0001 lr = lr0 folder_name = os.getcwd() + '/progress-b-1-%f/' % ratio if not os.path.isdir(folder_name): os.mkdir(folder_name) filename = os.getcwd() + '/setup/bunny_transient_64_0.mat' setup = scipy.io.loadmat(filename) gt_transient = np.array(setup['gt_transient'], dtype=np.double, order='C') gt_mesh = MESH() gt_mesh.v = igl.eigen.MatrixXd(np.array(setup['gt_v'], dtype=np.double)) gt_mesh.f = igl.eigen.MatrixXd(np.array(setup['gt_f'], dtype=np.double)).castint() opt = OPT(20000) opt.max_distance_bin = gt_transient.shape[1] opt.smooth_weight = 0.001 mesh = MESH() mesh_init_location = os.getcwd() + '/init/cnlos_bunny_threshold_64.obj' igl.readOBJ(mesh_init_location, mesh.v, mesh.f) mesh.v = np.array(mesh.v, dtype=np.float32, order='C') mesh.f = np.array(mesh.f, dtype=np.int32, order='C') opt.resolution = 64 opt.smooth_ratio = ratio rendering.isotropic_remeshing(mesh, .5 / opt.resolution) old_v = np.array(mesh.v) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh) weight = rendering.create_weighting_function(gt_transient, opt.gamma) optimization_alpha = torch.Tensor([mesh.alpha]) optimization_alpha.requires_grad_() optimizer_alpha = optim.Adam([optimization_alpha], lr=opt.alpha_lr) dummy_loss2 = optimization_alpha**2 dummy_loss2.backward() global_counter = 0 for t in range(3): if mesh.f.shape[0] > 250000: break global_counter, l2_record = optimize_parameters.optimize_alpha( mesh, gt_transient, weight, optimization_alpha, optimizer_alpha, opt, 50, global_counter) if t == 0: l2_0 = l2_record lr = (l2_record / l2_0) * lr0 * ((0.99)**(t / 50)) print('new lr %f' % lr) old_v = np.array(mesh.v) global_counter, convergence_flag = optimize_parameters.optimize_shape( mesh, gt_transient, weight, opt, 15, lr, gt_mesh, global_counter) if convergence_flag: if opt.testing_flag == 1: opt.testing_flag = 0 else: opt.testing_flag = 1 rendering.el_topo_gradient(mesh, old_v) rendering.el_topo_remeshing(mesh, .5 / opt.resolution) rendering.isotropic_remeshing(mesh, .5 / opt.resolution) rendering.compute_mesh_affinity(mesh) rendering.removeTriangle(mesh, opt) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh)
def optimization(ratio): lr0 = 0.0001 lr = lr0 folder_name = os.getcwd() + '/progress-b-1-%f/' % ratio if not os.path.isdir(folder_name): os.mkdir(folder_name) filename = os.getcwd() + '/transient.mat' setup = scipy.io.loadmat(filename) gt_transient = np.array(setup['transient'], dtype=np.double, order='C') opt = OPT(20000) opt.max_distance_bin = gt_transient.shape[1] opt.smooth_weight = 0.001 opt.lighting = np.array(setup['lighting'], dtype=np.float32, order='C') opt.sensor = np.array(setup['lighting'], dtype=np.float32, order='C') opt.gt_mesh = False gt_mesh = MESH() mesh = MESH() mesh_init_location = os.getcwd() + '/cnlos_s_threshold.obj' igl.readOBJ(mesh_init_location, mesh.v, mesh.f) mesh.v = np.array(mesh.v, dtype=np.float32, order='C') mesh.f = np.array(mesh.f, dtype=np.int32, order='C') opt.resolution = 64 opt.smooth_ratio = ratio rendering.isotropic_remeshing(mesh, .5 / opt.resolution) old_v = np.array(mesh.v) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh) weight = rendering.create_weighting_function(gt_transient, opt.gamma) mesh.albedo = optimize_parameters.initial_fitting_albedo( mesh, gt_transient, weight, opt) global_counter = 0 l2 = np.empty(400) albedo = np.empty(400) resolution_cnt = 0 for t in range(400): if mesh.f.shape[0] > 250000: break if t < 30 or resolution_cnt < 5: global_counter, l2_record = optimize_parameters.optimize_albedo( mesh, gt_transient, weight, opt, 50, global_counter, folder_name) if t == 0: l2_0 = l2_record opt.albedo_lr = (l2_reccord / l2_0) * opt_albedo_lr0 * ( (0.99)**(t / 5)) lr = (l2_record / l2_0) * lr0 * ((0.99)**(t / 5)) print('new lr %f' % lr) old_v = np.array(mesh.v) global_counter, convergence_flag, l2_record = optimize_parameters.optimize_shape( mesh, gt_transient, weight, opt, 15, lr, gt_mesh, global_counter, folder_name) resolution_cnt += 1 if resolution_cnt == 50: resolution_cnt = 0 opt.resolution *= 1.5 opt.sample_num *= 1.5 if convergence_flag: if opt.testing_flag == 1: opt.testing_flag = 0 opt.smooth_ratio = ratio / 10 + t / 100 print('shading') else: opt.testing_flag = 1 opt.smooth_ratio = ratio + t / 10 rendering.el_topo_gradient(mesh, old_v) rendering.el_topo_remeshing(mesh, .5 / opt.resolution) rendering.isotropic_remeshing(mesh, .5 / opt.resolution) rendering.compute_mesh_affinity(mesh) rendering.removeTriangle(mesh, opt) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh) filename = folder_name + '%05d.mat' % (t) scipy.io.savemat(filename, mdict={ 'v': mesh.v, 'f': mesh.f, 'albedo': mesh.albedo }) l2[t] = l2_record albedo[t] = mesh.albedo filename = folder_name + 'progress.mat' scipy.io.savemat(filename, mdict={'albedo': albedo, 'l2': l2})
def optimization(ratio): lr0 = 0.0005 lr = lr0 folder_name = os.getcwd() + '/progress-b-1-%f/' % ratio if not os.path.isdir(folder_name): os.mkdir(folder_name) filename = os.getcwd() + '/../exp_bunny/setup/bunny_transient.mat' setup = scipy.io.loadmat(filename) gt_transient = np.array(setup['gt_transient'], dtype=np.double, order='C') gt_mesh = MESH() gt_mesh.v = igl.eigen.MatrixXd(np.array(setup['gt_v'], dtype=np.double)) gt_mesh.f = igl.eigen.MatrixXd(np.array(setup['gt_f'], dtype=np.double)).castint() opt = OPT(20000) opt.max_distance_bin = gt_transient.shape[1] opt.smooth_weight = 0.001 mesh = MESH() mesh_init_location = os.getcwd() + '/init/cnlos_bunny_threshold.obj' igl.readOBJ(mesh_init_location, mesh.v, mesh.f) mesh.v = np.array(mesh.v, dtype=np.float32, order='C') mesh.f = np.array(mesh.f, dtype=np.int32, order='C') opt.resolution = 64 opt.smooth_ratio = ratio rendering.isotropic_remeshing(mesh, .5 / opt.resolution) old_v = np.array(mesh.v) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh) weight = rendering.create_weighting_function(gt_transient, opt.gamma) global_counter = 0 old_v = np.array(mesh.v) old_f = np.array(mesh.f) global_counter, convergence_flag, l2_record = optimize_parameters.optimize_shape( mesh, gt_transient, weight, opt, 15, lr, gt_mesh, global_counter, folder_name) grad_v = np.array(mesh.v) rendering.el_topo_gradient(mesh, old_v) rendering.el_topo_remeshing(mesh, .5 / opt.resolution) eltopo_v = np.array(mesh.v) eltopo_f = np.array(mesh.f) rendering.isotropic_remeshing(mesh, .5 / opt.resolution) isotropic_v = np.array(mesh.v) isotropic_f = np.array(mesh.f) filename = folder_name + 'data.mat' scipy.io.savemat(filename, mdict={ 'old_v': old_v, 'old_f': old_f, 'grad_v': grad_v, 'eltopo_v': eltopo_v, 'eltopo_f': eltopo_f, 'isotropic_v': isotropic_v, 'isotropic_f': isotropic_f })
def optimization(ratio): lr0 = 0.0001 lr = lr0 folder_name = os.getcwd() + '/progress-b-3-%f/' % ratio if not os.path.isdir(folder_name): os.mkdir(folder_name) test_num = 1 filename = os.getcwd() + '/setup/bunny_transient_64_0_%d.mat' % test_num setup = scipy.io.loadmat(filename) gt_transient = np.array(setup['gt_transient'], dtype=np.double, order='C') gt_mesh = MESH() gt_mesh.v = igl.eigen.MatrixXd(np.array(setup['gt_v'], dtype=np.double)) gt_mesh.f = igl.eigen.MatrixXd(np.array(setup['gt_f'], dtype=np.double)).castint() opt = OPT(20000) opt.max_distance_bin = gt_transient.shape[1] opt.smooth_weight = 0.001 mesh = MESH() mesh_init_location = os.getcwd( ) + '/init/cnlos_bunny_threshold_64_0_%d.obj' % test_num igl.readOBJ(mesh_init_location, mesh.v, mesh.f) mesh.v = np.array(mesh.v, dtype=np.float32, order='C') mesh.f = np.array(mesh.f, dtype=np.int32, order='C') opt.resolution = 64 opt.smooth_ratio = ratio rendering.isotropic_remeshing(mesh, .5 / opt.resolution) old_v = np.array(mesh.v) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh) weight = rendering.create_weighting_function(gt_transient, opt.gamma) global_counter = 0 l2 = np.empty(500) alpha = np.empty(500) for t in range(500): if mesh.f.shape[0] > 250000: break global_counter, l2_record = optimize_parameters.optimize_alpha( mesh, gt_transient, weight, opt, 50, global_counter) if t == 0: l2_0 = l2_record lr = (l2_record / l2_0) * lr0 * ((0.99)**(t / 2)) print('new lr %f' % lr) old_v = np.array(mesh.v) global_counter, convergence_flag, l2_record = optimize_parameters.optimize_shape( mesh, gt_transient, weight, opt, 15, lr, gt_mesh, global_counter) if convergence_flag: if opt.testing_flag == 1: opt.testing_flag = 0 opt.smooth_ratio = ratio / 10 + t / 100 print('shading') else: opt.testing_flag = 1 opt.smooth_ratio = ratio + t / 10 rendering.el_topo_gradient(mesh, old_v) rendering.el_topo_remeshing(mesh, .5 / opt.resolution) rendering.isotropic_remeshing(mesh, .5 / opt.resolution) rendering.compute_mesh_affinity(mesh) rendering.removeTriangle(mesh, opt) rendering.compute_mesh_affinity(mesh) rendering.border_indicator(mesh) filename = folder_name + '%05d.mat' % (t) scipy.io.savemat(filename, mdict={ 'v': mesh.v, 'f': mesh.f, 'alpha': mesh.alpha }) l2[t] = l2_record alpha[t] = mesh.alpha filename = folder_name + 'progress.mat' scipy.io.savemat(filename, mdict={'alpha': alpha, 'l2': l2})