Exemplo n.º 1
0
def compute_shifts(x, ctx, extra_vars, default_parameters):
    start = timer()
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        start = timer()
        sess.run(
            tf.group(tf.global_variables_initializer(),
                     tf.local_variables_initializer()))
        feed_dict = {
            ctx.X:
            x,
            #ctx.xi:default_parameters._DEFAULT_PARAMETERS['xi'].reshape(-1,1),
            ctx.alpha:
            default_parameters._DEFAULT_PARAMETERS['alpha'].reshape(-1, 1),
            ctx.beta:
            default_parameters._DEFAULT_PARAMETERS['beta'].reshape(-1, 1),
            ctx.mu:
            default_parameters._DEFAULT_PARAMETERS['mu'].reshape(-1, 1),
            ctx.nu:
            default_parameters._DEFAULT_PARAMETERS['nu'].reshape(-1, 1),
            ctx.gamma:
            default_parameters._DEFAULT_PARAMETERS['gamma'].reshape(-1, 1),
            ctx.delta:
            default_parameters._DEFAULT_PARAMETERS['delta'].reshape(-1, 1)
        }
        if extra_vars['return_var'] == 'I':
            y = sess.run(ctx.out_I, feed_dict=feed_dict)
        elif extra_vars['return_var'] == 'O':
            y = sess.run(ctx.out_O, feed_dict=feed_dict)
        end = timer()
        run_time = end - start

    a, r = model_utils.data_postprocessing(
        x, y, extra_vars)  #maybe return a dict instead?
    return a, y, run_time
Exemplo n.º 2
0
def optimize_model(im, gt, extra_vars, parameters):
    #Get data
    if extra_vars['figure_name'] == 'f4':  #precalculated
        x = im.transpose(0, 2, 3, 1)  #need to copy or what?
        with tf.device('/gpu:0'):
            with tf.variable_scope('aux'):
                aux_ctx = ContextualCircuit(lesions=parameters.lesions)
                aux_ctx.run(
                    extra_vars['aux_data'])  #builds tf graph with shape of x
    elif extra_vars['figure_name'] == 'tbp' or extra_vars[
            'figure_name'] == 'tbtcso' or extra_vars['figure_name'] == 'bw':
        x = im.transpose(0, 2, 3, 1)
    else:
        x = model_utils.get_population2(
            im,
            npoints=extra_vars['npoints'],
            kind=extra_vars['kind'],
            scale=extra_vars['scale']).astype(
                parameters._DEFAULT_FLOATX_NP).transpose(0, 2, 3,
                                                         1)  #transpose to bhwc

    #Build graph
    with tf.device('/gpu:0'):
        with tf.variable_scope('main'):
            ctx = ContextualCircuit(lesions=parameters.lesions)
            ctx.run(x)  #builds tf graph with shape of x

        #Special case for fig 4
        if extra_vars['figure_name'] == 'f4':
            extra_vars['aux_y'], _ = compute_shifts(
                x=extra_vars['aux_data'],
                ctx=aux_ctx,
                extra_vars=extra_vars,
                default_parameters=parameters)
        oy, run_time = compute_shifts(x=x,
                                      ctx=ctx,
                                      extra_vars=extra_vars,
                                      default_parameters=parameters)
        y, aux_data = model_utils.data_postprocessing(x, oy, extra_vars)
        import ipdb
        ipdb.set_trace()
        it_score = get_fits(y, gt, extra_vars)

    #Add to database
    update_data(parameters, extra_vars['figure_name'], parameters._id,
                it_score)
    return
Exemplo n.º 3
0
def optimize_model(gt,extra_vars,parameters,lesions):
    hp_sets = gather_data(lesions)

    print('Running hyperparameter optimizations after lesioning: ' + ' '.join(parameters.lesions))
    build_graph = True
    stimuli = np.load(parameters.f7_stimuli_file)
    for idx in range(parameters.iterations):
        random_parameters = prepare_hps(parameters,hp_sets,idx)
        shift_phase = sp.zeros((extra_vars['n_cps'],))
        shift_anti = sp.zeros((extra_vars['n_cps'],))
        for idx, col in enumerate(extra_vars['test_colors']):
            x = stimuli[idx] #make_stimulus(col,extra_vars)
            extra_vars['x'] = x
            extra_vars['test_color'] = col
            if build_graph:
                with tf.device('/gpu:0'):
                    with tf.variable_scope('main'):
                        ctx = ContextualCircuit(lesions=parameters.lesions, parameters=parameters._DEFAULT_PARAMETERS)
                        ctx.run(x) #builds tf graph with shape of x
                build_graph = False

            y, run_time = compute_shifts(x=x, ctx=ctx, 
                 extra_vars=extra_vars, default_parameters=random_parameters)
            phase,anti = model_utils.data_postprocessing(x,y,extra_vars)
            shift_phase += phase / float(extra_vars['n_col'])
            shift_anti += anti / float(extra_vars['n_col'])

        phase_score = np.corrcoef(shift_phase,gt[0])[0,1] ** 2
        anti_score = np.corrcoef(shift_anti,gt[1])[0,1] ** 2
        it_score = np.mean([phase_score,anti_score])

        #Add to database
        update_data(random_parameters,extra_vars['figure_name'],hp_sets['idx'][idx],it_score)

        #Store the rest of the stuff
        #scores = np.append(scores,it_score)
        #params.append(random_parameters._DEFAULT_PARAMETERS)
        printProgress(idx, parameters.iterations, 
            prefix = 'Progress:', 
            suffix = 'Iteration time: ' + str(np.around(run_time,2)) + '; Rsquared: ' + str(np.around(it_score,2)), 
            bar_length = 30)

    print('\n')
    return #scores,params
Exemplo n.º 4
0
def optimize_model(gt, extra_vars, parameters):
    print('Running hyperparameter optimizations after lesioning: ' +
          ' '.join(parameters.lesions))
    build_graph = True
    stimuli = np.load(parameters.f7_stimuli_file)
    shift_phase = sp.zeros((extra_vars['n_cps'], ))
    shift_anti = sp.zeros((extra_vars['n_cps'], ))
    for idx, col in enumerate(extra_vars['test_colors']):
        x = stimuli[idx]  #make_stimulus(col,extra_vars)
        extra_vars['x'] = x
        extra_vars['test_color'] = col
        if build_graph:
            with tf.device('/gpu:0'):
                with tf.variable_scope('main'):
                    ctx = ContextualCircuit(
                        lesions=parameters.lesions,
                        parameters=parameters._DEFAULT_PARAMETERS)
                    ctx.run(x)  #builds tf graph with shape of x
            build_graph = False

        y, run_time = compute_shifts(x=x,
                                     ctx=ctx,
                                     extra_vars=extra_vars,
                                     default_parameters=parameters)
        phase, anti = model_utils.data_postprocessing(x, y, extra_vars)
        shift_phase += phase / float(extra_vars['n_col'])
        shift_anti += anti / float(extra_vars['n_col'])

    phase_score = np.corrcoef(shift_phase, gt[0])[0, 1]**2
    anti_score = np.corrcoef(shift_anti, gt[1])[0, 1]**2
    it_score = np.mean([phase_score, anti_score])

    #Add to database
    update_data(parameters, extra_vars['figure_name'], parameters._id,
                it_score)
    return
Exemplo n.º 5
0
def optimize_model(gt, extra_vars, parameters):
    #Taking the MurakamiShimojo1996 code and integrating with the hp_optim

    # parameters
    ###########
    nw = len(extra_vars['ws'])
    abs_cohs = sp.linspace(0, 100, extra_vars['ncoh'] // 2 + 1) / 100.
    dirs = sp.linspace(0, 9, extra_vars['ndirs'], dtype=int) / float(
        extra_vars['ndirs'])
    coh50 = (extra_vars['ndirs'] / 2.0 - 1.0) / (extra_vars['ndirs'] - 1.0)
    scoh = coh50

    coherences = sp.linspace(-100, 100, extra_vars['ncoh']) / 100.
    coherences4fit = sp.linspace(-100, 100, extra_vars['ncoh4fit']) / 100.

    print('Running hyperparameter optimizations after lesioning: ' +
          ' '.join(parameters.lesions))
    build_graph = True
    for ol, w in enumerate(extra_vars['ws']):
        # generate random dot stimuli and simulate circuit
        ##################################################
        dec_dir_vote = sp.zeros((extra_vars['ncoh'], extra_vars['ntrials']))
        dec_variance = sp.zeros((extra_vars['ncoh'], extra_vars['ntrials']))
        for il in range(extra_vars['ntrials']):
            # generate stimuli at various center coherences
            im_poscoh = [
                get_rdots(cval=extra_vars['value_down'],
                          ccoh=ccoh,
                          sval=extra_vars['value_down'],
                          scoh=scoh,
                          density=extra_vars['rdd'],
                          size=extra_vars['size'],
                          w=w,
                          dirs=dirs,
                          ndirs=extra_vars['ndirs']) for ccoh in abs_cohs
            ]
            im_negcoh = [
                get_rdots(cval=extra_vars['value_up'],
                          ccoh=ccoh,
                          sval=extra_vars['value_down'],
                          scoh=scoh,
                          density=extra_vars['rdd'],
                          size=extra_vars['size'],
                          w=w,
                          dirs=dirs,
                          ndirs=extra_vars['ndirs']) for ccoh in abs_cohs
            ]
            im = sp.array(im_negcoh[1:][::-1] + im_poscoh)
            im.shape = (extra_vars['ncoh'], 1, extra_vars['size'],
                        extra_vars['size'])

            # get population
            x = model_utils.get_population2(
                im,
                kind=extra_vars['kind'],
                scale=extra_vars['scale'],
                npoints=extra_vars['nunits']).transpose(0, 2, 3,
                                                        1)  #transpose to bhwc
            if build_graph:
                with tf.device('/gpu:0'):
                    with tf.variable_scope('main'):
                        ctx = ContextualCircuit(lesions=parameters.lesions)
                        ctx.run(x)  #builds tf graph with shape of x
                build_graph = False

            oy, run_time = compute_shifts(x=x,
                                          ctx=ctx,
                                          extra_vars=extra_vars,
                                          default_parameters=parameters)
            dec_dir_vote[:,
                         il], dec_variance[:,
                                           il] = model_utils.data_postprocessing(
                                               x, oy, extra_vars
                                           )  #maybe return a dict instead?

        PSE_vote = sp.zeros((nw, ))
        PSE_variance = sp.zeros((nw, ))
        dec_dir_vote_fit = sp.zeros((nw, extra_vars['ncoh4fit']))
        dec_variance_fit = sp.zeros((nw, extra_vars['ncoh4fit']))
        _, PSE_vote[ol], dec_dir_vote_fit[ol] = fit_logistic(
            x=coherences, y=dec_dir_vote[ol].mean(-1), x_eval=coherences4fit)
        _, _, _, dec_variance_fit[ol] = fit_gaussian(
            x=coherences, y=dec_variance[ol].mean(-1), x_eval=coherences4fit)
        PSE_variance[ol] = coherences4fit[dec_variance_fit[ol].argmax()]
    PSE_mixed = (PSE_vote + PSE_variance) / 2.0
    y = PSE_mixed * 100  #this is unnecessary for correlation, but let's keep for plotting purposes
    it_score = np.corrcoef(y, gt)[0, 1]**2

    #Add to database
    import ipdb
    ipdb.set_trace()
    update_data(parameters, extra_vars['figure_name'], parameters._id,
                it_score)
    return  #scores,params
def optimize_model(im, gt, extra_vars, parameters):

    #Do outer loop of lesions -- embed in variable scopes
    for lesion in parameters.lesions:
        outer_parameters = deepcopy(parameters)
        outer_parameters.lesions = lesion
        print('Running optimizations on problem ' + extra_vars['figure_name'] +
              ' after lesioning: ' + ' '.join(outer_parameters.lesions))
        #Optional stuff and data prep
        if extra_vars['figure_name'] == 'f4':  #precalculated
            x = im.transpose(0, 2, 3, 1)  #need to copy or what?
            with tf.device('/gpu:0'):
                with tf.variable_scope('aux_' + lesion):
                    aux_ctx = ContextualCircuit(lesions=lesion)
                    aux_ctx.run(extra_vars['aux_data']
                                )  #builds tf graph with shape of x
        elif extra_vars['figure_name'] == 'tbp' or extra_vars[
                'figure_name'] == 'tbtcso' or extra_vars[
                    'figure_name'] == 'bw' or extra_vars[
                        'figure_name'] == 'cross_orientation_suppression':
            x = im.transpose(0, 2, 3, 1)
        else:
            x = model_utils.get_population2(
                im,
                npoints=extra_vars['npoints'],
                kind=extra_vars['kind'],
                scale=extra_vars['scale']).astype(
                    outer_parameters._DEFAULT_FLOATX_NP).transpose(
                        0, 2, 3, 1)  #transpose to bhwc

        # Build main graph
        with tf.device('/gpu:0'):
            with tf.variable_scope('main_' + lesion):
                ctx = ContextualCircuit(lesions=outer_parameters.lesions)
                ctx.run(x)  # builds tf graph with shape of x

        # Do hp optimization
        num_sets = count_sets(lesion, extra_vars['figure_name'])[0]['count']
        if num_sets > 0:
            idx = 0  # keep a count
            # Initialize the session
            with tf.Session(config=tf.ConfigProto(
                    allow_soft_placement=True)) as sess:
                while 1:  # while we have hp to run on this lesion
                    hp_set = get_lesion_rows_from_db(lesion,
                                                     extra_vars['figure_name'],
                                                     get_one=True)
                    if hp_set is None and parameters.pachaya is not True:
                        break
                    else:
                        if parameters.pachaya:
                            random_parameters = prepare_hps(
                                outer_parameters, None)
                        else:
                            random_parameters = prepare_hps(
                                outer_parameters, hp_set)

                        # Special case for fig 4
                        if extra_vars['figure_name'] == 'f4':
                            extra_vars['aux_y'], _ = compute_shifts(
                                x=extra_vars['aux_data'],
                                sess=sess,
                                ctx=aux_ctx,
                                extra_vars=extra_vars,
                                default_parameters=random_parameters)
                        # Apply the model to the data
                        oy, run_time = compute_shifts(
                            x=x,
                            sess=sess,
                            ctx=ctx,
                            extra_vars=extra_vars,
                            default_parameters=random_parameters)

                        # Postprocess the model's outputs
                        y, aux_data = model_utils.data_postprocessing(
                            x, oy, extra_vars)  # maybe return a dict instead?

                        # Correlate simulation and ground truth
                        it_score = get_fits(y, gt, extra_vars)

                        # Add to database
                        if parameters.pachaya and idx == 1:
                            break
                        else:
                            update_data(random_parameters,
                                        extra_vars['figure_name'],
                                        hp_set['_id'], it_score)
                        printProgress(idx,
                                      num_sets,
                                      prefix=extra_vars['figure_name'] +
                                      ' progress on lesion ' + lesion + ':',
                                      suffix='Iteration time: ' +
                                      str(np.around(run_time, 2)) +
                                      '; Correlation: ' +
                                      str(np.around(it_score, 2)),
                                      bar_length=30)
                    if parameters.gaussian_spatial or parameters.gaussian_channel:
                        break
                    else:
                        idx += 1
Exemplo n.º 7
0
def optimize_model(im, gt, extra_vars, parameters):

    # Do outer loop of lesions -- embed in variable scopes
    for lesion in parameters.lesions:

        # outer_parameters = deepcopy(parameters)
        if 'hp_file' in extra_vars.keys():
            hps = np.load(extra_vars['hp_file'])['max_row'].item()[lesion]
            it_parameters = prepare_hps(parameters, hps)
        else:
            it_parameters = deepcopy(parameters)
        # it_parameters = prepare_hps(outer_parameters, hps[lesion])
        it_parameters.lesions = lesion
        print('Running optimizations on problem ' + extra_vars['figure_name'] +
              ' after lesioning: ' + ' '.join(it_parameters.lesions))

        # Optional stuff and data prep
        if extra_vars['figure_name'] == 'f4':  # precalculated
            x = im.transpose(0, 2, 3, 1)  # need to copy or what?
            with tf.device('/gpu:0'):
                with tf.variable_scope('aux_' + lesion):
                    aux_ctx = ContextualCircuit(lesions=lesion)
                    aux_ctx.run(extra_vars['aux_data'])
                    # builds tf graph with shape of x
        elif extra_vars['figure_name'] == 'tbp' or\
                extra_vars['figure_name'] == 'tbtcso' or\
                extra_vars['figure_name'] == 'bw' or\
                extra_vars['figure_name'] == 'cross_orientation_suppression' or\
                extra_vars['figure_name'] == 'size_tuning' or\
                extra_vars['figure_name'] == 'cnn_features':
            x = im.transpose(0, 2, 3, 1)
        else:
            x = model_utils.get_population2(
                im,
                npoints=extra_vars['npoints'],
                kind=extra_vars['kind'],
                scale=extra_vars['scale']).astype(  # transpose to bhwc
                    it_parameters._DEFAULT_FLOATX_NP).transpose(0, 2, 3, 1)

        # Build main graph
        with tf.device('/gpu:0'):
            with tf.variable_scope('main_' + lesion):
                ctx = ContextualCircuit(lesions=it_parameters.lesions)
                ctx.run(x)  # builds tf graph with shape of x

        # Special case for fig 4
        with tf.Session(config=tf.ConfigProto(
                allow_soft_placement=True)) as sess:
            if extra_vars['figure_name'] == 'f4':
                extra_vars['aux_y'], _ = compute_shifts(
                    x=extra_vars['aux_data'],
                    sess=sess,
                    ctx=aux_ctx,
                    extra_vars=extra_vars,
                    default_parameters=it_parameters)
            oy, run_time = compute_shifts(x=x,
                                          sess=sess,
                                          ctx=ctx,
                                          extra_vars=extra_vars,
                                          default_parameters=it_parameters)

        y, aux_data = model_utils.data_postprocessing(x, oy, extra_vars)
        if 'save_file' in extra_vars.keys():
            np.save(extra_vars['save_file'], y)

        if gt is not None:
            it_score = get_fits(y, gt, extra_vars)
            return it_score
        else:
            produce_plots(y, it_parameters.lesions, extra_vars, parameters)
def optimize_model(gt, extra_vars, parameters):

    # parameters
    ###########
    nw = len(extra_vars['ws'])
    abs_cohs = sp.linspace(0, 100, extra_vars['ncoh'] // 2 + 1) / 100.
    dirs = sp.linspace(0, 9, extra_vars['ndirs'], dtype=int) / float(
        extra_vars['ndirs'])
    coh50 = (extra_vars['ndirs'] / 2.0 - 1.0) / (extra_vars['ndirs'] - 1.0)
    scoh = coh50

    coherences = sp.linspace(-100, 100, extra_vars['ncoh']) / 100.
    coherences4fit = sp.linspace(-100, 100, extra_vars['ncoh4fit']) / 100.
    #Taking the MurakamiShimojo1996 code and integrating with the hp_optim

    #Do outer loop of lesions -- embed in variable scopes
    for lesion in parameters.lesions:
        outer_parameters = deepcopy(parameters)
        outer_parameters.lesions = lesion
        print('Running optimizations on problem ' + extra_vars['figure_name'] +
              ' after lesioning: ' + ' '.join(outer_parameters.lesions))
        build_graph = True

        #Do hp optimization
        num_sets = count_sets(lesion, extra_vars['figure_name'])[0]['count']
        idx = 0
        #keep a count
        while 1:  #while we have hp to run on this lesion
            hp_set = get_lesion_rows_from_db(lesion,
                                             extra_vars['figure_name'],
                                             get_one=True)
            if hp_set is None:
                break
            else:
                random_parameters = prepare_hps(outer_parameters, hp_set)
                for ol, w in enumerate(extra_vars['ws']):
                    # generate stimuli at various center coherences
                    im_poscoh = [
                        get_rdots(cval=extra_vars['value_down'],
                                  ccoh=ccoh,
                                  sval=extra_vars['value_down'],
                                  scoh=scoh,
                                  density=extra_vars['rdd'],
                                  size=extra_vars['size'],
                                  w=w,
                                  dirs=dirs,
                                  ndirs=extra_vars['ndirs'])
                        for ccoh in abs_cohs
                    ]
                    im_negcoh = [
                        get_rdots(cval=extra_vars['value_up'],
                                  ccoh=ccoh,
                                  sval=extra_vars['value_down'],
                                  scoh=scoh,
                                  density=extra_vars['rdd'],
                                  size=extra_vars['size'],
                                  w=w,
                                  dirs=dirs,
                                  ndirs=extra_vars['ndirs'])
                        for ccoh in abs_cohs
                    ]
                    im = sp.array(im_negcoh[1:][::-1] + im_poscoh)
                    im.shape = (extra_vars['ncoh'], 1, extra_vars['size'],
                                extra_vars['size'])

                    # get population
                    x = model_utils.get_population2(
                        im,
                        kind=extra_vars['kind'],
                        scale=extra_vars['scale'],
                        npoints=extra_vars['nunits']).transpose(
                            0, 2, 3, 1)  #transpose to bhwc

                    if build_graph:
                        with tf.device('/gpu:0'):
                            with tf.variable_scope('main_' + lesion):
                                ctx = ContextualCircuit(lesions=lesion)
                                ctx.run(x)  #builds tf graph with shape of x
                        build_graph = False

                    # generate random dot stimuli and simulate circuit
                    ##################################################
                    dec_dir_vote = sp.zeros(
                        (extra_vars['ncoh'], extra_vars['ntrials']))
                    dec_variance = sp.zeros(
                        (extra_vars['ncoh'], extra_vars['ntrials']))
                    for il in range(extra_vars['ntrials']):
                        oy, run_time = compute_shifts(
                            x=x,
                            ctx=ctx,
                            extra_vars=extra_vars,
                            default_parameters=random_parameters)
                        dec_dir_vote[:,
                                     il], dec_variance[:, il] = model_utils.data_postprocessing(
                                         x, oy, extra_vars
                                     )  #maybe return a dict instead?

                    PSE_vote = sp.zeros((nw, ))
                    PSE_variance = sp.zeros((nw, ))
                    dec_dir_vote_fit = sp.zeros((nw, extra_vars['ncoh4fit']))
                    dec_variance_fit = sp.zeros((nw, extra_vars['ncoh4fit']))
                    _, PSE_vote[ol], dec_dir_vote_fit[ol] = fit_logistic(
                        x=coherences,
                        y=dec_dir_vote[ol].mean(-1),
                        x_eval=coherences4fit)
                    _, _, _, dec_variance_fit[ol] = fit_gaussian(
                        x=coherences,
                        y=dec_variance[ol].mean(-1),
                        x_eval=coherences4fit)
                    PSE_variance[ol] = coherences4fit[
                        dec_variance_fit[ol].argmax()]
                PSE_mixed = (PSE_vote + PSE_variance) / 2.0
                y = PSE_mixed * 100  #this is unnecessary for correlation, but let's keep for plotting purposes
                import ipdb
                ipdb.set_trace()
                it_score = np.corrcoef(y, gt)[0, 1]  # ** 2

                #Add to database
                update_data(random_parameters, extra_vars['figure_name'],
                            hp_set['_id'], it_score)
                printProgress(idx,
                              num_sets,
                              prefix=extra_vars['figure_name'] +
                              ' progress on lesion ' + lesion + ':',
                              suffix='Iteration time: ' +
                              str(np.around(run_time, 2)) + '; Correlation: ' +
                              str(np.around(it_score, 2)),
                              bar_length=30)
                idx += 1
Exemplo n.º 9
0
def optimize_model(gt, extra_vars, parameters):

    #Do outer loop of lesions -- embed in variable scopes
    for lesion in parameters.lesions:
        outer_parameters = deepcopy(parameters)
        outer_parameters.lesions = lesion
        print('Running optimizations on problem ' + extra_vars['figure_name'] +
              ' after lesioning: ' + ' '.join(outer_parameters.lesions))
        build_graph = True
        stimuli = np.load(outer_parameters.f7_stimuli_file)

        #Do hp optimization
        num_sets = count_sets(lesion, extra_vars['figure_name'])[0]['count']
        if num_sets > 0:
            idx = 0
            #keep a count
            #Initialize the session
            with tf.Session(config=tf.ConfigProto(
                    allow_soft_placement=True)) as sess:
                while idx < max_its:  #while we have hp to run on this lesion
                    start = timer()
                    hp_set = get_lesion_rows_from_db(lesion,
                                                     extra_vars['figure_name'],
                                                     get_one=True)
                    if hp_set is None:
                        break
                    else:
                        random_parameters = prepare_hps(
                            outer_parameters, hp_set)
                        shift_phase = sp.zeros((extra_vars['n_cps'], ))
                        shift_anti = sp.zeros((extra_vars['n_cps'], ))
                        for il, col in enumerate(extra_vars['test_colors']):
                            x = stimuli[il]
                            extra_vars['x'] = x
                            extra_vars['test_color'] = col
                            if build_graph:
                                with tf.device('/gpu:0'):
                                    with tf.variable_scope('main_' + lesion):
                                        ctx = ContextualCircuit(
                                            lesions=lesion,
                                            parameters=random_parameters.
                                            _DEFAULT_PARAMETERS)
                                        ctx.run(
                                            x
                                        )  #builds tf graph with shape of x
                                build_graph = False

                            y = compute_shifts(
                                x=x,
                                sess=sess,
                                ctx=ctx,
                                extra_vars=extra_vars,
                                default_parameters=random_parameters)

                            phase, anti = model_utils.data_postprocessing(
                                x, y, extra_vars)
                            shift_phase += phase / float(extra_vars['n_col'])
                            shift_anti += anti / float(extra_vars['n_col'])

                        phase_score = np.corrcoef(shift_phase * -1,
                                                  gt[0])[0, 1]  # ** 2
                        anti_score = np.corrcoef(shift_anti * -1,
                                                 gt[1])[0, 1]  # ** 2
                        it_score = np.mean([phase_score, anti_score])

                        #Add to database
                        update_data(random_parameters,
                                    extra_vars['figure_name'], hp_set['_id'],
                                    it_score)
                        end = timer()
                        printProgress(idx,
                                      num_sets,
                                      prefix=extra_vars['figure_name'] +
                                      ' progress on lesion ' + lesion + ':',
                                      suffix='Iteration time: ' +
                                      str(np.around(end - start, 2)) +
                                      '; Correlation: ' +
                                      str(np.around(it_score, 2)) + '\n',
                                      bar_length=30)
                    if parameters.gaussian_spatial or parameters.gaussian_channel:
                        break
                    else:
                        idx += 1
def optimize_model(im, gt, extra_vars, parameters):
    #Do outer loop of lesions -- embed in variable scopes
    for lesion in parameters.lesions:
        outer_parameters = deepcopy(parameters)
        outer_parameters.lesions = lesion
        print('Running optimizations on problem ' + extra_vars['figure_name'] +
              ' after lesioning: ' + ' '.join(outer_parameters.lesions))
        #Optional stuff and data prep
        if extra_vars['figure_name'] == 'f4':  #precalculated
            x = im.transpose(0, 2, 3, 1)  #need to copy or what?
            with tf.device('/gpu:0'):
                with tf.variable_scope('aux_' + lesion):
                    aux_ctx = ContextualCircuit(lesions=lesion)
                    aux_ctx.run(extra_vars['aux_data']
                                )  #builds tf graph with shape of x
        elif extra_vars['figure_name'] == 'tbp' or extra_vars[
                'figure_name'] == 'tbtcso' or extra_vars[
                    'figure_name'] == 'bw' or extra_vars[
                        'figure_name'] == 'cross_orientation_suppression':
            x = im.transpose(0, 2, 3, 1)
        else:
            x = model_utils.get_population2(
                im,
                npoints=extra_vars['npoints'],
                kind=extra_vars['kind'],
                scale=extra_vars['scale']).astype(
                    outer_parameters._DEFAULT_FLOATX_NP).transpose(
                        0, 2, 3, 1)  #transpose to bhwc
        # Build main graph
        with tf.device('/gpu:0'):
            with tf.variable_scope('main_' + lesion):
                ctx = ContextualCircuit(lesions=outer_parameters.lesions)
                ctx.run(x)  # builds tf graph with shape of x

        # Do hp optimization
        num_sets = count_sets(lesion, extra_vars['figure_name'])[0]['count']

        if num_sets > 0:
            idx = 0  # keep a count
            # Initialize the session
            with tf.Session(config=tf.ConfigProto(
                    allow_soft_placement=True)) as sess:
                while idx < max_its:  # while we have hp to run on this lesion
                    start = timer()
                    hp_set = get_lesion_rows_from_db(lesion,
                                                     extra_vars['figure_name'],
                                                     get_one=True)
                    if hp_set is None and parameters.pachaya is not True and to_opt > 0:
                        dat = np.array(
                            get_all_lesion_data(
                                lesion, table_name=defaults['table_name']))
                        hist = dat[:, 2:8]
                        perf = -np.array([[perf_it] for perf_it in dat[:, 8]])
                        bds = defaults['_DEFAULT_DOMAINS']
                        # bds = [{'name': 'alpha', 'type': 'continuous', 'domain': (0,1)},
                        #        {'name': 'beta', 'type': 'continuous', 'domain': (0,1)},
                        #        {'name': 'mu', 'type': 'continuous', 'domain': (0,1)},
                        #        {'name': 'nu', 'type': 'continuous', 'domain': (0,1)},
                        #        {'name': 'gamma', 'type': 'continuous', 'domain': (0,1)},
                        #        {'name': 'delta', 'type': 'continuous', 'domain': (0,1)}]
                        opt_me = GPyOpt.methods.BayesianOptimization(
                            f=None,
                            X=hist,
                            Y=perf,
                            domain=bds,
                            evaluator_type='local_penalization')
                        next_samp = opt_me.suggested_sample[0].tolist()
                        opt_list = [
                            'alpha', 'beta', 'mu', 'nu', 'gamma', 'delta'
                        ]
                        hp_set = dict(zip(opt_list, next_samp))
                        random_parameters = prepare_hps(
                            outer_parameters, hp_set)
                        hp_set['_id'] = max(dat[:, 0]) + 1
                        add_row_to_db(hp_set, opt_list)
                    else:
                        if parameters.pachaya:
                            random_parameters = prepare_hps(
                                outer_parameters, None)
                        else:
                            random_parameters = prepare_hps(
                                outer_parameters, hp_set)
                    # Special case for fig 4
                    if extra_vars['figure_name'] == 'f4':
                        extra_vars['aux_y'] = compute_shifts(
                            x=extra_vars['aux_data'],
                            sess=sess,
                            ctx=aux_ctx,
                            extra_vars=extra_vars,
                            default_parameters=random_parameters)
                    # Apply the model to the data
                    oy = compute_shifts(x=x,
                                        sess=sess,
                                        ctx=ctx,
                                        extra_vars=extra_vars,
                                        default_parameters=random_parameters)

                    # Postprocess the model's outputs
                    y, aux_data = model_utils.data_postprocessing(
                        x, oy, extra_vars)  # maybe return a dict instead?

                    # Correlate simulation and ground truth
                    it_score = get_fits(y, gt, extra_vars)

                    # Add to database
                    if parameters.pachaya and idx == 1:
                        break
                    else:
                        update_data(random_parameters,
                                    extra_vars['figure_name'], hp_set['_id'],
                                    it_score)
                    end = timer()
                    printProgress(idx,
                                  max_its,
                                  prefix=extra_vars['figure_name'] +
                                  ' progress on lesion ' + lesion + ':',
                                  suffix='Iteration time: ' +
                                  str(np.around(end - start, 2)) +
                                  '; Correlation: ' +
                                  str(np.around(it_score, 5)) + '\n',
                                  bar_length=30)
                    if parameters.gaussian_spatial or parameters.gaussian_channel:
                        break
                    else:
                        idx += 1
Exemplo n.º 11
0
def optimize_model(im, gt, extra_vars, parameters):

    #Do outer loop of lesions -- embed in variable scopes
    for lesion in parameters.lesions:
        print('Running hyperparameter optimizations after lesioning: ' +
              ' '.join(parameters.lesions))

        #Optional stuff and data prep
        if extra_vars['figure_name'] == 'f4':  #precalculated
            x = im.transpose(0, 2, 3, 1)  #need to copy or what?
            with tf.device('/gpu:0'):
                with tf.variable_scope('aux_' + lesion):
                    aux_ctx = ContextualCircuit(lesions=lesion)
                    aux_ctx.run(extra_vars['aux_data']
                                )  #builds tf graph with shape of x
        elif extra_vars['figure_name'] == 'tbp' or extra_vars[
                'figure_name'] == 'tbtcso' or extra_vars['figure_name'] == 'bw':
            x = im.transpose(0, 2, 3, 1)
        else:
            x = model_utils.get_population2(
                im,
                npoints=extra_vars['npoints'],
                kind=extra_vars['kind'],
                scale=extra_vars['scale']).astype(
                    parameters._DEFAULT_FLOATX_NP).transpose(
                        0, 2, 3, 1)  #transpose to bhwc

        #Build main graph
        with tf.device('/gpu:0'):
            with tf.variable_scope('main_' + lesion):
                ctx = ContextualCircuit(lesions=parameters.lesions)
                ctx.run(x)  #builds tf graph with shape of x

        #Do hp optimization
        for idx in range(parameters.iterations):
            hp_sets = get_lesion_row_from_db(lesion)
            import ipdb
            ipdb.set_trace()
            random_parameters = prepare_hps(parameters, hp_sets, idx)

            #Special case for fig 4
            if extra_vars['figure_name'] == 'f4':
                extra_vars['aux_y'], _ = compute_shifts(
                    x=extra_vars['aux_data'],
                    ctx=aux_ctx,
                    extra_vars=extra_vars,
                    default_parameters=random_parameters)
            oy, run_time = compute_shifts(x=x,
                                          ctx=ctx,
                                          extra_vars=extra_vars,
                                          default_parameters=random_parameters)
            y, aux_data = model_utils.data_postprocessing(
                x, oy, extra_vars)  #maybe return a dict instead?

            it_score = get_fits(y, gt, extra_vars)

            #Add to database
            update_data(random_parameters, extra_vars['figure_name'],
                        hp_sets['idx'][idx], it_score)
            printProgress(idx,
                          parameters.iterations,
                          prefix='Progress on lesion ' + lesion + ':',
                          suffix='Iteration time: ' +
                          str(np.around(run_time, 2)) + '; Rsquared: ' +
                          str(np.around(it_score, 2)),
                          bar_length=30)

        print('\n')