def test_run_chain(self):
        fwm = vfm.VisionForwardModel(render_size=(50, 50))
        s = shape.Shape(forward_model=fwm, viewpoint=[(3.0, 45.0, 45.0)],
                        params={'LL_VARIANCE': 1.0, 'MAX_PIXEL_VALUE': 175.0})

        data = np.zeros((1, 50, 50))
        kernel = proposal.RandomMixtureProposal(moves={'aaa': shape.shape_change_part_size_local},
                                                params={'CHANGE_SIZE_VARIANCE': 1.0})

        params = {'name': 'unittest', 'results_folder': '.', 'sampler': 'xxx', 'burn_in': 0, 'sample_count': 1,
                  'best_sample_count': 1, 'thinning_period': 10, 'data': data, 'kernel': kernel, 'initial_h': s,
                  'report_period': 10}

        # wrong sampler
        self.assertRaises(ValueError, run_chain, **params)

        # need to supply temperatures if sampler is pt
        params['sampler'] = 'pt'
        self.assertRaises(ValueError, run_chain, **params)

        params['temperatures'] = [2.0, 1.0]
        results = run_chain(**params)
        self.assertIn('run_id', results.keys())
        self.assertIn('run_file', results.keys())
        self.assertIn('mean_acceptance_rate', results.keys())
        self.assertIn('start_time', results.keys())
        self.assertIn('end_time', results.keys())
        self.assertIn('duration', results.keys())
        self.assertIn('best_ll', results.keys())
        self.assertIn('best_posterior', results.keys())
        self.assertIn('mse', results.keys())
        self.assertIn('mean_best_ll', results.keys())
        self.assertIn('mean_best_posterior', results.keys())
        self.assertIn('mse_mean', results.keys())
        self.assertIn('mean_sample_posterior', results.keys())
        self.assertIn('mean_sample_ll', results.keys())
        self.assertIn('mse_sample', results.keys())

        # saved the right files
        start = results['start_time']
        time_str = time.strftime("%Y%m%d_%H%M%S", time.localtime(start))
        fname = "{0:s}/{1:s}_{2:s}_{3:06d}.pkl".format(params['results_folder'], params['name'], time_str,
                                                       results['run_id'])
        self.assertTrue(os.path.isfile(fname))
        os.remove(fname)

        fname = "{0:s}/{1:s}/s{2:d}_0.png".format(params['results_folder'], params['name'], 0)
        self.assertTrue(os.path.isfile(fname))
        os.remove(fname)

        fname = "{0:s}/{1:s}/b{2:d}_0.png".format(params['results_folder'], params['name'], 0)
        self.assertTrue(os.path.isfile(fname))
        os.remove(fname)

        folder = "{0:s}/{1:s}".format(params['results_folder'], params['name'])
        os.rmdir(folder)
def run_experiment2(**kwargs):
    """This method runs the chain with a PaperClipShape hypothesis and given parameters for the second view-dependency
    experiment we look at.

    This method is intended to be used in an Experiment instance. This method prepares the necessary data and
    calls `Infer3DShape.run_chain`.

    Parameters:
        kwargs (dict): Keyword arguments are as follows
            input_file (str): name 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
            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
            max_joint_count (int): maximum number of joints in the shape. required if shape_type is 'paperclip'
            move_joint_variance (float): variance for the move joint move. required if shape_type is 'paperclip'
            max_new_segment_length (float): maximum new segment length for add joint move. required if shape_type is
                'paperclip'
            max_segment_length_change (float): maximum change ratio for change segment length move. required if
                shape_type is 'paperclip'
            rotate_midsegment_variance (float): variance for the rotate midsegment move. required if shape_type is
                'paperclip'
            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']

        ll_variance = kwargs['ll_variance']
        max_pixel_value = kwargs['max_pixel_value']
        change_viewpoint_variance = kwargs['change_viewpoint_variance']

        max_joint_count = kwargs['max_joint_count']
        move_joint_variance = kwargs['move_joint_variance']
        max_new_segment_length = kwargs['max_new_segment_length']
        max_segment_length_change = kwargs['max_segment_length_change']
        rotate_midsegment_variance = kwargs['rotate_midsegment_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']

        # pid is set by Experiment class
        chain_id = kwargs['pid']

        temperatures = None
        if sampler == 'pt':
            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

    # seed using chain_id to prevent parallel processes from getting the same random seed
    np.random.seed(int((time.time() * 1000) + chain_id) % 2**32)

    # read training data. subjects are trained on two sets of views 75 degrees apart.
    data = np.load("{0:s}/{1:s}_train.npy".format(data_folder, input_file))
    render_size = data.shape[1:]

    # if shape_type == 'paperclip':
    custom_lighting = False

    import Infer3DShape.vision_forward_model as i3d_vfm
    fwm = i3d_vfm.VisionForwardModel(render_size=render_size, offscreen_rendering=True, custom_lighting=custom_lighting)

    shape_params = {'LL_VARIANCE': ll_variance, 'MAX_PIXEL_VALUE': max_pixel_value, 'SEGMENT_LENGTH_VARIANCE': 0.0001}

    # construct viewpoint. during training, subjects see views: theta = 270, 285, 300, 345, 0, 15
    theta = np.random.rand() * 360.0
    viewpoint1 = np.array((np.sqrt(8.0), theta + 270.0, 45.0))
    viewpoint2 = np.array((np.sqrt(8.0), theta + 285.0, 45.0))
    viewpoint3 = np.array((np.sqrt(8.0), theta + 300.0, 45.0))
    viewpoint4 = np.array((np.sqrt(8.0), theta + 345.0, 45.0))
    viewpoint5 = np.array((np.sqrt(8.0), theta + 0.0, 45.0))
    viewpoint6 = np.array((np.sqrt(8.0), theta + 15.0, 45.0))
    viewpoint = [viewpoint1, viewpoint2, viewpoint3, viewpoint4, viewpoint5, viewpoint6]

    # construct initial hypothesis and kernel
    import Infer3DShape.i3d_proposal as i3d_proposal
    kernel_params = {'CHANGE_VIEWPOINT_VARIANCE': change_viewpoint_variance}
    moves = {'change_viewpoint': i3d_proposal.change_viewpoint_z}

    import Infer3DShape.paperclip_shape as i3d_pc
    h = i3d_pc.PaperClipShape(forward_model=fwm, viewpoint=viewpoint, params=shape_params,
                              min_joints=2, max_joints=max_joint_count, joint_count=6, mid_segment_id=2)

    kernel_params['MOVE_JOINT_VARIANCE'] = move_joint_variance
    kernel_params['MAX_NEW_SEGMENT_LENGTH'] = max_new_segment_length
    kernel_params['MAX_SEGMENT_LENGTH_CHANGE'] = max_segment_length_change
    kernel_params['ROTATE_MIDSEGMENT_VARIANCE'] = rotate_midsegment_variance

    moves['paperclip_move_joints'] = i3d_pc.paperclip_shape_move_joint
    moves['paperclip_move_branch'] = i3d_pc.paperclip_shape_move_branch
    moves['paperclip_change_segment_length'] = i3d_pc.paperclip_shape_change_segment_length
    moves['paperclip_change_branch_length'] = i3d_pc.paperclip_shape_change_branch_length
    moves['paperclip_add_remove_joint'] = i3d_pc.paperclip_shape_add_remove_joint
    moves['paperclip_rotate_midsegment'] = i3d_pc.paperclip_shape_rotate_midsegment

    import mcmclib.proposal as mcmc_proposal
    kernel = mcmc_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
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
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
예제 #5
0
def run_experiment1(**kwargs):
    """This method runs the chain with a PaperClipShape hypothesis and given parameters for the canonical view effect
    experiment.

    This method is intended to be used in an Experiment instance. This method prepares the necessary data and
    calls `Infer3DShape.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_joint_count (int): maximum number of joints in the shape
            ll_variance (float): variance of the Gaussian likelihood
            max_pixel_value (float): maximum pixel intensity value
            move_joint_variance (float): variance for the move joint move
            max_new_segment_length (float): maximum new segment length for add joint move
            max_segment_length_change (float): maximum change ratio for change segment length move
            rotate_midsegment_variance (float): variance for the rotate midsegment 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']
        max_joint_count = kwargs['max_joint_count']
        ll_variance = kwargs['ll_variance']
        max_pixel_value = kwargs['max_pixel_value']
        move_joint_variance = kwargs['move_joint_variance']
        max_new_segment_length = kwargs['max_new_segment_length']
        max_segment_length_change = kwargs['max_segment_length_change']
        rotate_midsegment_variance = kwargs['rotate_midsegment_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

    # read the data file
    data = np.load("{0:s}/{1:s}.npy".format(data_folder, input_file))
    render_size = data.shape[1:]

    import Infer3DShape.vision_forward_model as i3d_vfm
    fwm = i3d_vfm.VisionForwardModel(render_size=render_size,
                                     offscreen_rendering=True,
                                     custom_lighting=False)

    shape_params = {
        'LL_VARIANCE': ll_variance,
        'MAX_PIXEL_VALUE': max_pixel_value,
        'SEGMENT_LENGTH_VARIANCE': 0.0001
    }

    viewpoint = np.array((np.sqrt(8.0), np.random.rand() * 360.0, 45.0))

    import Infer3DShape.paperclip_shape as i3d_pc
    h = i3d_pc.PaperClipShape(forward_model=fwm,
                              viewpoint=[viewpoint],
                              params=shape_params,
                              min_joints=2,
                              max_joints=max_joint_count,
                              joint_count=6,
                              mid_segment_id=2)

    kernel_params = {
        'CHANGE_VIEWPOINT_VARIANCE': change_viewpoint_variance,
        'MOVE_JOINT_VARIANCE': move_joint_variance,
        'MAX_NEW_SEGMENT_LENGTH': max_new_segment_length,
        'MAX_SEGMENT_LENGTH_CHANGE': max_segment_length_change,
        'ROTATE_MIDSEGMENT_VARIANCE': rotate_midsegment_variance
    }

    import Infer3DShape.i3d_proposal as i3d_proposal
    moves = {
        'change_viewpoint': i3d_proposal.
        change_viewpoint_z,  # in exp1, only rotations around z are allowed.
        'paperclip_move_joints': i3d_pc.paperclip_shape_move_joint,
        'paperclip_move_branch': i3d_pc.paperclip_shape_move_branch,
        'paperclip_change_segment_length':
        i3d_pc.paperclip_shape_change_segment_length,
        'paperclip_change_branch_length':
        i3d_pc.paperclip_shape_change_branch_length,
        'paperclip_add_remove_joint': i3d_pc.paperclip_shape_add_remove_joint,
        'paperclip_rotate_midsegment': i3d_pc.paperclip_shape_rotate_midsegment
    }

    import mcmclib.proposal as mcmc_proposal
    kernel = mcmc_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