Exemplo n.º 1
0
        # params are not changing from hypothesis to hypothesis.
        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 mcmclib.proposal
    from i3d import i3d_proposal, vision_forward_model as vfm

    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,
Exemplo n.º 2
0
    """
    run = MCMCRun.load(run_file)

    samples = run.samples.samples[5:]
    for sample in samples:
        sample.forward_model = forward_model

    best_samples = run.best_samples.samples
    for sample in best_samples:
        sample.forward_model = forward_model

    return samples, best_samples


if __name__ == "__main__":
    fwm = vfm.VisionForwardModel()

    data_folder = "./data"
    samples_folder = "./results"

    # we would like to calculate the similarity between two objects: target and comparison
    target_object = 'test1'
    comparison_object = 'test2'

    # load the images for target and comparison
    target_data = np.load('{0:s}/{1:s}.npy'.format(data_folder, target_object))
    comparison_data = np.load('{0:s}/{1:s}.npy'.format(data_folder,
                                                       comparison_object))

    # load the samples from the chains for target
    target_run_file = "{0:s}/{1:s}.pkl".format(samples_folder, target_object)
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
            offscreen_rendering (bool): If True, renders offscreen.
            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']
        offscreen_rendering = kwargs['offscreen_rendering']
        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
    from i3d import i3d_proposal
    from i3d import vision_forward_model as vfm

    # read the data file
    viewpoint = [[np.sqrt(8.0), -45.0, 45.0]]
    data = np.load("{0:s}/{1:s}.npy".format(data_folder, input_file))
    custom_lighting = True

    render_size = data.shape[1:]
    fwm = vfm.VisionForwardModel(render_size=render_size,
                                 custom_lighting=custom_lighting,
                                 offscreen_rendering=offscreen_rendering)

    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
    }

    from bdaooss 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:
        h = bdaooss.BDAoOSSShape(forward_model=fwm,
                                 viewpoint=viewpoint,
                                 params=shape_params)

    else:
        from bdaooss import bdaooss_shape_maxd as bdaooss_maxd
        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=results_folder,
                        temperatures=temperatures)

    return results
Exemplo n.º 4
0
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
Exemplo n.º 5
0
        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 mcmclib.proposal
    from i3d import i3d_proposal, vision_forward_model as vfm
    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,
    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 mcmclib.proposal
    from i3d import i3d_proposal, i3d_hypothesis as hyp, vision_forward_model as vfm

    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