Esempio n. 1
0
def ex4(srao_query_string, image_index, gm_method='original', verbose=True):
  """ querygen
  """
  import image_fetch_core as ifc
  import image_fetch_utils as ifu; reload(ifu)
  import image_fetch_plot as ifp; reload(ifp)
  import image_fetch_querygen as ifq; reload(ifq)
  
  vgd, potentials, platt_mod, bin_mod, queries, ifdata = dp.get_all_data()
  
  query = ifq.gen_srao(srao_query_string)
  ifdata.configure(image_index, query)
  
  gm = None
  tracker = None
  if gm_method == 'original':
    gm, tracker = ifc.generate_pgm(ifdata, verbose)
  elif gm_method == 'uniform' or gm_method == 'empirical':
    gm, tracker = ifc.generate_pgm_all_objects(ifdata, verbose=verbose, method=gm_method)
  energy, best_match_ix, marginals = ifc.do_inference(gm)
  
  #ifp.draw_gm(gm)
  
  ifp.draw_best_objects(tracker, best_match_ix, energy, filename = out_path + "mq_i{0}.png".format(image_index))
  file_prefix = "mq_i{}_".format(image_index)
  ifp.draw_all_heatmaps(tracker, ifdata.object_detections, marginals, gm_method, out_path, file_prefix)
def image_batch(query,
                query_id,
                if_data,
                output_path,
                image_ix_subset=[],
                gen_plots=True,
                gm_method='original'):
    """  Run a single query against a batch of images

  Attributes:
    query (obj): the query to use on the images
    query_id (int) : id number of the query
    if_data (obj): ImageFetchData object
    output_path (str): "/home/econser/School/Thesis/data/inference test/"
    image_ix_subset (list): subset of image indices to run in the batch
  Returns:
    list of energy values from inference
  """

    time_list = []
    energy_list = []
    images = if_data.vg_data

    use_subset = False
    n_images = len(images)
    if len(image_ix_subset) > 0:
        use_subset = True
        n_images = len(image_ix_subset)

    for i in range(0, n_images):
        imgnum = i
        if use_subset: imgnum = image_ix_subset[i]

        gm, tracker, energy, best_matches, marginals, duration = inference_pass(
            query, query_id, imgnum, if_data, gm_method)

        energy_list.append(energy)
        time_list.append(duration)
        if gen_plots:
            filename = output_path + "q{:03d}".format(
                query_id) + "i{:03d}".format(imgnum) + ".png"
            ifp.draw_best_objects(tracker, best_matches, energy, filename)
    print "total time: {0}, average time: {1}".format(np.sum(time_list),
                                                      np.average(time_list))

    file = open(output_path + "q{:03d}_energy_values.csv".format(query_id),
                "wb")
    csv_writer = csv.writer(file)
    csv_writer.writerow(("image_ix", "energy"))
    for i in range(0, n_images):
        imgnum = i
        if use_subset: imgnum = image_ix_subset[i]
        csv_writer.writerow((imgnum, energy_list[i]))
    file.close()
Esempio n. 3
0
def ex1(query_index,
        image_index,
        inf_alg='bp',
        gm_method='original',
        do_suppl_plots=True,
        save_gm=False,
        verbose=True):
    """ generate plots for a query/image pair
  """
    import image_fetch_core as ifc
    reload(ifc)
    import image_fetch_plot as ifp
    reload(ifp)

    vgd, potentials, platt_mod, bin_mod, queries, ifdata = dp.get_all_data()

    query = vgd['vg_data_test'][query_index].annotations
    ifdata.configure(image_index, query)

    gm = None
    tracker = None
    if gm_method == 'original':
        gm, tracker = ifc.generate_pgm(ifdata, verbose)
    elif gm_method == 'uniform' or gm_method == 'empirical':
        gm, tracker = ifc.generate_pgm_all_objects(ifdata,
                                                   verbose=verbose,
                                                   method=gm_method)

    file_prefix = "q{0}_i{1}_".format(query_index, image_index)
    energy = None
    best_match_ix = None
    marginals = None
    obj_file = None

    if inf_alg == 'bp':
        obj_file = out_path + file_prefix + gm_method + '_bp_objects.png'
        energy, best_match_ix, marginals = ifc.do_inference(gm)
        if do_suppl_plots:
            ifp.draw_all_heatmaps(tracker, ifdata.object_detections, marginals,
                                  gm_method, out_path, file_prefix)
            #ifp.p_compare(tracker, ifdata.object_detections, marginals, out_path+file_prefix+'sctr.png')
    elif inf_alg == 'astar':
        obj_file = out_path + file_prefix + 'as_objects.png'
        energy, best_match_ix = ifc.do_inference_astar(gm)

    ifp.draw_best_objects(tracker, best_match_ix, energy, filename=obj_file)
    if save_gm: ifp.draw_gm(gm)