self_copy = ShapeMaxN(forward_model=self.forward_model, maxn=self.maxn, params=self.params) parts_copy = deepcopy(self.parts) viewpoint_copy = deepcopy(self.viewpoint) self_copy.parts = parts_copy self_copy.viewpoint = viewpoint_copy return self_copy if __name__ == "__main__": import vision_forward_model as vfm import mcmclib.proposal import i3d_proposal fwm = vfm.VisionForwardModel(render_size=(200, 200)) max_part_count = 10 h = ShapeMaxN(forward_model=fwm, viewpoint=[(np.sqrt(2.), -np.sqrt(2.), 2.0)], params={'LL_VARIANCE': 0.0001}, maxn=max_part_count) """ moves = {'shape_add_remove_part': shape_add_remove_part, 'shape_move_part': shape_move_part, 'shape_move_part_local': shape_move_part_local, 'shape_change_part_size': shape_change_part_size, 'shape_change_part_size_local': shape_change_part_size_local, 'shape_move_object': shape_move_object, 'change_viewpoint': i3d_proposal.change_viewpoint_z} """ moves = { 'shape_add_remove_part': shape_add_remove_part, 'shape_move_part_local': shape_move_part_local,
def run_voxel_based_shape_experiment(**kwargs): """This method runs the chain with a VoxelBasedShape hypothesis and given parameters. This method is intended to be used in an Experiment instance. This method prepares the necessary data and calls `run_chain`. Parameters: kwargs (dict): Keyword arguments are as follows input_file (str): mame of the data file containing the observed image data_folder (str): folder containing the data files results_folder (str): sampler (str): see `run_chain` function max_depth (int): maximum depth of hypothesis ll_variance (float): variance of the Gaussian likelihood max_pixel_value (float): maximum pixel intensity value change_viewpoint_variance (float): variance for the change viewpoint move scale_space_variance (float): variance for the scale space move burn_in (int): see `run_chain` function sample_count (int): see `run_chain` function best_sample_count (int): see `run_chain` function thinning_period (int): see `run_chain` function report_period (int): see `run_chain` function temperatures (list): see `run_chain` function Returns: dict: run results """ try: input_file = kwargs['input_file'] results_folder = kwargs['results_folder'] data_folder = kwargs['data_folder'] sampler = kwargs['sampler'] max_depth = None if 'max_depth' in kwargs: max_depth = kwargs['max_depth'] ll_variance = kwargs['ll_variance'] max_pixel_value = kwargs['max_pixel_value'] change_viewpoint_variance = kwargs['change_viewpoint_variance'] scale_space_variance = kwargs['scale_space_variance'] burn_in = kwargs['burn_in'] sample_count = kwargs['sample_count'] best_sample_count = kwargs['best_sample_count'] thinning_period = kwargs['thinning_period'] report_period = kwargs['report_period'] temperatures = None if 'temperatures' in kwargs: temperatures = kwargs['temperatures'] except KeyError as e: raise ValueError("All experiment parameters should be provided. Missing parameter {0:s}".format(e.message)) import numpy as np import mcmclib.proposal as proposal import i3d_proposal import vision_forward_model as vfm # read the data file data = np.load("{0:s}/{1:s}_single_view.npy".format(data_folder, input_file)) render_size = data.shape[1:] fwm = vfm.VisionForwardModel(render_size=render_size) shape_params = {'LL_VARIANCE': ll_variance, 'MAX_PIXEL_VALUE': max_pixel_value} kernel_params = {'CHANGE_VIEWPOINT_VARIANCE': change_viewpoint_variance, 'SCALE_SPACE_VARIANCE': scale_space_variance} import voxel_based_shape as vox moves = {'change_viewpoint': i3d_proposal.change_viewpoint_z, 'voxel_flip_full_vs_empty': vox.voxel_based_shape_flip_full_vs_empty, 'voxel_flip_partial_vs_full': vox.voxel_based_shape_flip_full_vs_partial, 'voxel_flip_partial_vs_empty': vox.voxel_based_shape_flip_empty_vs_partial, 'voxel_scale_space': vox.voxel_scale_space} viewpoint = [(np.sqrt(8.0), -45.0, 45.0)] hypothesis_class = 'VoxelBasedShape' if max_depth is None: h = vox.VoxelBasedShape(forward_model=fwm, viewpoint=viewpoint, params=shape_params) else: hypothesis_class = 'VoxelBasedShapeMaxD' h = vox.VoxelBasedShapeMaxD(forward_model=fwm, viewpoint=viewpoint, params=shape_params, max_depth=max_depth) kernel_params['MAX_DEPTH'] = max_depth # form the proposal kernel = proposal.RandomMixtureProposal(moves=moves, params=kernel_params) results = run_chain(name=input_file, sampler=sampler, initial_h=h, data=data, kernel=kernel, burn_in=burn_in, thinning_period=thinning_period, sample_count=sample_count, best_sample_count=best_sample_count, report_period=report_period, results_folder="{0:s}/{1:s}".format(results_folder, hypothesis_class), temperatures=temperatures) return results
for part in sm.spatial_states.values(): if np.any((part.position + change) > 1.0) or np.any((part.position + change) < -1.0): return hp, 1.0, 1.0 # if updated position is in bounds for part in sm.spatial_states.values(): part.position += change # proposal is symmetric; hence, q(hp|h) = q(h|hp) return hp, 1.0, 1.0 if __name__ == "__main__": import vision_forward_model as vfm import mcmclib.proposal import i3d_proposal fwm = vfm.VisionForwardModel(render_size=(200, 200), offscreen_rendering=False) h = BDAoOSSShape(forward_model=fwm, viewpoint=[[np.sqrt(8.0), -45.0, 45.0]], params={'LL_VARIANCE': 0.0001}) """ moves = {'bdaooss_add_remove_part': bdaooss_add_remove_part, 'bdaooss_change_part_size': bdaooss_change_part_size, 'bdaooss_change_part_size_local': bdaooss_change_part_size_local, 'bdaooss_change_part_dock_face': bdaooss_change_part_dock_face, 'bdaooss_move_object': bdaooss_move_object, 'change_viewpoint': i3d_proposal.change_viewpoint_z} """ moves = {'bdaooss_add_remove_part': bdaooss_add_remove_part, 'bdaooss_change_part_size_local': bdaooss_change_part_size_local, 'bdaooss_change_part_dock_face': bdaooss_change_part_dock_face, 'change_viewpoint': i3d_proposal.change_viewpoint_z} params = {'MOVE_OBJECT_VARIANCE': 0.00005,
# NOTE that we are not copying params. This assumes that params do not change from # hypothesis to hypothesis. shape_copy = self.shape.copy() viewpoint_copy = deepcopy(self.viewpoint) return BDAoOSSShapeMaxD(forward_model=self.forward_model, shape=shape_copy, params=self.params, max_depth=self.max_depth, viewpoint=viewpoint_copy) if __name__ == "__main__": import vision_forward_model as vfm import mcmclib.proposal import i3d_proposal import bdaooss_shape as bd max_depth = 3 fwm = vfm.VisionForwardModel(render_size=(200, 200), offscreen_rendering=False, custom_lighting=True) h = BDAoOSSShapeMaxD(forward_model=fwm, viewpoint=[(np.sqrt(2.0), -np.sqrt(2.0), 2.0)], params={'LL_VARIANCE': 0.0001}, max_depth=max_depth) """ moves = {'bdaooss_add_remove_part': bdaooss_add_remove_part, 'bdaooss_change_part_size': bdaooss_change_part_size, 'bdaooss_change_part_size_local': bdaooss_change_part_size_local, 'bdaooss_change_part_dock_face': bdaooss_change_part_dock_face, 'bdaooss_move_object': bdaooss_move_object, 'change_viewpoint': i3d_proposal.change_viewpoint} """ moves = {'bdaooss_add_remove_part': bd.bdaooss_add_remove_part, 'bdaooss_change_part_size_local': bd.bdaooss_change_part_size_local, 'bdaooss_change_part_dock_face': bd.bdaooss_change_part_dock_face, 'change_viewpoint': i3d_proposal.change_viewpoint}
Created on Feb 1, 2016 Goker Erdogan https://github.com/gokererdogan/ """ from Infer3DShape.similarity.calculate_similarity import * if __name__ == "__main__": import numpy as np import cPickle as pkl import pandas as pd import pandasql as psql import vision_forward_model as vfm fwm = vfm.VisionForwardModel() data_folder = "./data/stimuli20150624_144833" # load the 3D shapes for the stimuli gt_shapes = pkl.load(open(data_folder + '/shapes_single_view.pkl')) objects = ['o1', 'o2', 'o3', 'o4', 'o5', 'o6', 'o7', 'o8', 'o9', 'o10'] transformations = [ 't1_cs_d1', 't1_cs_d2', 't2_ap_d1', 't2_ap_d2', 't2_mf_d1', 't2_mf_d2', 't2_rp_d1', 't2_rp_d2' ] comparisons = {o: [o + '_' + t for t in transformations] for o in objects} df = pd.DataFrame(index=np.arange(0, 8 * len(objects)), columns=[
def run_bdaooss_experiment(**kwargs): """This method runs the chain with a BDAoOSSShape hypothesis and given parameters. This method is intended to be used in an Experiment instance. This method prepares the necessary data and calls `run_chain`. Parameters: kwargs (dict): Keyword arguments are as follows input_file (str): mame of the data file containing the observed image data_folder (str): folder containing the data files results_folder (str): sampler (str): see `run_chain` function inverted (bool): whether the input image is inverted, i.e., upside down max_depth (int): maximum depth of the hypothesis trees ll_variance (float): variance of the Gaussian likelihood max_pixel_value (float): maximum pixel intensity value change_size_variance (float): variance for the change part size move change_viewpoint_variance (float): variance for the change viewpoint move burn_in (int): see `run_chain` function sample_count (int): see `run_chain` function best_sample_count (int): see `run_chain` function thinning_period (int): see `run_chain` function report_period (int): see `run_chain` function temperatures (list): see `run_chain` function Returns: dict: run results """ try: input_file = kwargs['input_file'] results_folder = kwargs['results_folder'] data_folder = kwargs['data_folder'] sampler = kwargs['sampler'] inverted = kwargs['inverted'] ll_variance = kwargs['ll_variance'] max_pixel_value = kwargs['max_pixel_value'] max_depth = None if 'max_depth' in kwargs: max_depth = kwargs['max_depth'] change_size_variance = kwargs['change_size_variance'] change_viewpoint_variance = kwargs['change_viewpoint_variance'] burn_in = kwargs['burn_in'] sample_count = kwargs['sample_count'] best_sample_count = kwargs['best_sample_count'] thinning_period = kwargs['thinning_period'] report_period = kwargs['report_period'] temperatures = None if 'temperatures' in kwargs: temperatures = kwargs['temperatures'] except KeyError as e: raise ValueError( "All experiment parameters should be provided. Missing parameter {0:s}" .format(e.message)) import numpy as np import mcmclib.proposal as proposal import i3d_proposal import vision_forward_model as vfm # read the data file if not inverted: viewpoint = [[np.sqrt(8.0), -45.0, 45.0]] data = np.load("{0:s}/{1:s}_single_view.npy".format( data_folder, input_file)) custom_lighting = True else: data = np.load("{0:s}/{1:s}_single_view_inverted.npy".format( data_folder, input_file)) viewpoint = [[np.sqrt(8.0), -45.0, 135.0]] # for inverted images we use VTK's default lighting because in the custom lighting, bottoms of objects are not # well illuminated custom_lighting = False render_size = data.shape[1:] fwm = vfm.VisionForwardModel(render_size=render_size, custom_lighting=custom_lighting, offscreen_rendering=True) shape_params = { 'LL_VARIANCE': ll_variance, 'MAX_PIXEL_VALUE': max_pixel_value } kernel_params = { 'CHANGE_SIZE_VARIANCE': change_size_variance, 'CHANGE_VIEWPOINT_VARIANCE': change_viewpoint_variance } import bdaooss_shape as bdaooss moves = { 'change_viewpoint': i3d_proposal.change_viewpoint_z, 'bdaooss_add_remove_part': bdaooss.bdaooss_add_remove_part, 'bdaooss_change_part_size_local': bdaooss.bdaooss_change_part_size_local, 'bdaooss_change_part_dock_face': bdaooss.bdaooss_change_part_dock_face } if max_depth is None: hypothesis_class = 'BDAoOSShape' h = bdaooss.BDAoOSSShape(forward_model=fwm, viewpoint=viewpoint, params=shape_params) else: import bdaooss_shape_maxd as bdaooss_maxd hypothesis_class = 'BDAoOSSShapeMaxD' h = bdaooss_maxd.BDAoOSSShapeMaxD(forward_model=fwm, max_depth=max_depth, viewpoint=viewpoint, params=shape_params) kernel_params['MAX_DEPTH'] = max_depth # form the proposal kernel = proposal.RandomMixtureProposal(moves=moves, params=kernel_params) results = run_chain(name=input_file, sampler=sampler, initial_h=h, data=data, kernel=kernel, burn_in=burn_in, thinning_period=thinning_period, sample_count=sample_count, best_sample_count=best_sample_count, report_period=report_period, results_folder="{0:s}/{1:s}".format( results_folder, hypothesis_class), temperatures=temperatures) return results
def run_chain(name, sampler, initial_h, data, kernel, burn_in, thinning_period, sample_count, best_sample_count, report_period, results_folder, temperatures=None): """Run an MCMC chain and save results. This function is used by run_experiment scripts to run chains and save results. Parameters: name (str): name of the chain. Used as the folder name to save sample images sampler (str): Sampler to use. 'mh' for Metropolis-Hastings, 'pt' for Parallel Tempering initial_h (I3DHypothesis): Initial hypothesis data (numpy.ndarray): Observed data kernel (mcmclib.Proposal): Transition kernel of the chain burn_in (int): Number of burn in iterations thinning_period (int): Keep every ith sample sample_count (int): Number of samples to take best_sample_count (int): Size of the best samples list report_period (int): Report the status of the chain every report_period iterations results_folder (str): Folder to save the results temperatures (list): Temperatures of each chain for Parallel Tempering sampler Returns: dict: results """ if sampler == 'mh': from mcmclib.mh_sampler import MHSampler sampler = MHSampler(initial_h=initial_h, data=data, proposal=kernel, burn_in=burn_in, sample_count=sample_count, best_sample_count=best_sample_count, thinning_period=thinning_period, report_period=report_period) elif sampler == 'pt': if temperatures is None: raise ValueError('ParallelTempering sampler requires temperatures parameter.') chain_count = len(temperatures) from mcmclib.parallel_tempering_sampler import ParallelTemperingSampler sampler = ParallelTemperingSampler(initial_hs=[initial_h]*chain_count, data=data, proposals=[kernel]*chain_count, temperatures=temperatures, burn_in=burn_in, sample_count=sample_count, best_sample_count=best_sample_count, thinning_period=int(thinning_period / chain_count), report_period=int(report_period / chain_count)) else: raise ValueError('Unknown sampler. Possible choices are mh and pt.') start = time.time() run = sampler.sample() end = time.time() # generate a random run id run_id = np.random.randint(1000000) run_file = "{0:s}/{1:s}_{2:s}_{3:06d}.pkl".format(results_folder, name, time.strftime("%Y%m%d_%H%M%S", time.localtime(start)), run_id) run.save(run_file) # save images of samples to disk fwm2 = vfm.VisionForwardModel(render_size=(300, 300)) try: os.mkdir("{0:s}/{1:s}".format(results_folder, name)) except OSError as e: warnings.warn(e.message) for i, sample in enumerate(run.samples.samples): fwm2.save_render("{0:s}/{1:s}/s{2:d}.png".format(results_folder, name, i), sample) for i, sample in enumerate(run.best_samples.samples): fwm2.save_render("{0:s}/{1:s}/b{2:d}.png".format(results_folder, name, i), sample) sample_lls = [sample.log_likelihood(data) for sample in run.samples.samples] best_lls = [sample.log_likelihood(data) for sample in run.best_samples.samples] mse_best = -2 * initial_h.params['LL_VARIANCE'] * np.max(best_lls) mse_mean = -2 * initial_h.params['LL_VARIANCE'] * np.mean(best_lls) mse_sample = -2 * initial_h.params['LL_VARIANCE'] * np.mean(sample_lls) # form the results dictionary results = {'run_id': run_id, 'run_file': run_file, 'mean_acceptance_rate': run.run_log.IsAccepted.mean(), 'start_time': start, 'end_time': end, 'duration': (end - start) / 60.0, 'best_posterior': np.max(run.best_samples.log_probs), 'best_ll': np.max(best_lls), 'mse': mse_best, 'mean_best_posterior': np.mean(run.best_samples.log_probs), 'mean_best_ll': np.mean(best_lls), 'mse_mean': mse_mean, 'mean_sample_posterior': np.mean(run.samples.log_probs), 'mean_sample_ll': np.mean(sample_lls), 'mse_sample': mse_sample} # add acceptance rate by move to results acc_rate_by_move = run.acceptance_rate_by_move() acc_rates = dict(zip(acc_rate_by_move.MoveType, acc_rate_by_move.AcceptanceRate)) results.update(acc_rates) return results
for vx in v.voxel.subvoxels[0, 1, 0].subvoxels[1, 1, 0].subvoxels: for vy in vx: for vz in vy: vz.status = EMPTY_VOXEL v.voxel.subvoxels[0, 1, 0].subvoxels[1, 1, 0].subvoxels[0, 1, 1].status = FULL_VOXEL v.voxel.subvoxels[0, 1, 0].subvoxels[1, 1, 0].subvoxels[1, 1, 0].status = FULL_VOXEL v.voxel.subvoxels[0, 1, 0].subvoxels[1, 1, 0].subvoxels[1, 0, 1].status = FULL_VOXEL img = fwm.render(v) np.save('./data/test5_single_view.npy', img) fwm.save_render('./data/test5_single_view.png', v) """ # test 6 import vision_forward_model as vfm fwm = vfm.VisionForwardModel(render_size=(200, 200), custom_lighting=False) import paperclip_shape as pc_shape joint_positions = np.array([[-0.54028188, 0.41323616, 0.19661439], [-0.20661808, 0.51499101, -0.1030599], [-0.12298621, 0., 0.], [0.12298621, 0., 0.], [0.84088651, 0.1529245, -0.10760027], [0.4336905, -0.07415333, -1.01534363]]) pc = pc_shape.PaperClipShape(forward_model=fwm, viewpoint=[get_viewpoint(45.0)], joint_positions=joint_positions, min_joints=2, max_joints=8, mid_segment_id=2) img = fwm.render(pc)