Esempio n. 1
0
def inference_pass(query, query_id, image_ix, if_data, gm_method='original'):
  """ Run the full inference pass using a query and image
  
  Attributes:
    query (obj): the query to execute
    query_id (int): a query identification number
    image_ix (int): the image to run against the image
    if_data (obj): an ImageFetchData object
  
  Returns:
    (object) opengm graphical model
    (object) DetectionTracker
    (float) energy value
    (numpy array) array of best-match indices
    (numpy array) post-inference marginals
    (float) total pass time
  """
  import time
  gm = None
  tracker = None
  start = time.time()  
  if_data.configure(image_ix, query)
  if gm_method == 'original':
    gm, tracker = ifc.generate_pgm(if_data, verbose=False)
  elif gm_method == 'uniform' or gm_method == 'empirical':
    gm, tracker = ifc.generate_pgm_all_objects(if_data, verbose=False, method=gm_method)
  energy, indices, marginals = ifc.do_inference(gm)
  duration = time.time() - start
  
  print "query {0} on image {1}: {2} sec".format(query_id, image_ix, duration)
  return gm, tracker, energy, indices, marginals, duration
Esempio n. 2
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)
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)
Esempio n. 4
0
def gen_viz_file(query_ixs, image_ixs, output_path):
  import image_fetch_core as ifc; reload(ifc)
  import image_fetch_utils as ifu; reload(ifu)
  
  vgd, potentials, platt_mod, bin_mod, queries, ifdata = dp.get_all_data()
  
  for query_ix in query_ixs:
    filename = 'q_' + str(query_ix) + '.csv'
    f = open(output_path+filename, 'w')
    
    query = queries['simple_graphs'][query_ix]
    query_str = ifu.sg_to_str(query.annotations)
    
    tp_simple = ifu.get_partial_scene_matches(vgd['vg_data_test'], queries['simple_graphs'])
    tp_indices = tp_simple[query_ix]
    
    for i in image_ixs: #range(0, len(image_ixs)):
      ifdata.configure(i, query.annotations)
      gm, tracker = ifc.generate_pgm(ifdata, verbose=False)
      energy, best_match_ix, marginals = ifc.do_inference(gm)
      
      line = '{:03d}, 0, "{}"\n'.format(i, query_str)
      f.write(line)
      line = '{:03d}, 2, {:0.4f}\n'.format(i, energy)
      f.write(line)
      
      if i in tp_indices:
        line = '{:03d}, 3, "match"\n'.format(i)
      else:
        line = '{:03d}, 3, "no match"\n'.format(i)
      f.write(line)
      
      for obj_ix in range(0, len(best_match_ix)):
        obj_name = tracker.object_names[obj_ix]
        box_ix = best_match_ix[obj_ix]
        bc = tracker.box_coords[box_ix]
        line = '{:03d}, 1, {}, "{}", {}, {}, {}, {}\n'.format(i, obj_ix, obj_name, int(bc[0]), int(bc[1]), int(bc[2]), int(bc[3]))
        f.write(line)
    
    f.close()