Example #1
0
def main():
  # Connect to experiment db
  if args.host is not None:
    client = MongoClient(host=args.host)
  else:
    client = MongoClient()
  db = client.timb_experiments_db
  fs = gridfs.GridFS(db)

  # Pull out input/output sequence
  o = db.experiments.find_one({'_id': ObjectId(args.id)})
  from pprint import pprint
  pprint(o['tracker_params'])

  out_bmaps = np.empty((100, 100, len(o['log'])), dtype=np.uint32)
  out_true_bmaps = np.empty((100, 100, len(o['log'])), dtype=np.uint32)

  for i, log_entry in enumerate(o['log']):
    entry = cPickle.loads(fs.get(log_entry['entry_file']).read())
    out_true_bmaps[:,:,i] = entry['state']

    trusted = timb.threshold_trusted_for_view2(entry['new_weight'])
    machine_output = timb.sdf_to_zc(np.where(trusted, entry['new_phi'], np.nan))
    out_bmaps[:,:,i] = machine_output

  # out_bmaps = np.array(out_bmaps)
  # out_true_bmaps = np.array(out_true_bmaps)

  # Write i/o sequence to matlab
  input_filename = '/tmp/%s.mat' % str(uuid.uuid4())
  output_filename = '/tmp/%s.mat' % str(uuid.uuid4())
  scipy.io.savemat(input_filename, {'bmaps': out_bmaps, 'true_bmaps': out_true_bmaps})

  # Call matlab function to calculuate PR
  subprocess.call(['matlab', '-nosplash', '-nodesktop', #'-nojvm', '-nodisplay', 
    '-r', "multiPR('%s', '%s'); exit" % (input_filename, output_filename)
  ], cwd=MATLAB_CWD)
  output = scipy.io.loadmat(output_filename)
  os.unlink(input_filename)
  os.unlink(output_filename)

  out_ps = np.squeeze(output['ps'])
  out_rs = np.squeeze(output['rs'])
  out_f1s = np.squeeze(output['f1s'])

  print out_f1s

  print 'average f1', np.mean(out_f1s[np.isfinite(out_f1s)])

  import matplotlib.pyplot as plt
  plt.plot(out_ps, label="precision")
  plt.plot(out_rs, label="recall")
  plt.plot(out_f1s, label="f1")
  plt.legend(loc=4)
  plt.ylim([0, 1])
  plt.show()
Example #2
0
fs = gridfs.GridFS(db)

o = db.experiments.find_one({'_id': ObjectId(args.id)})

entry = cPickle.loads(fs.get(o['log'][args.i]['entry_file']).read())

state = entry['state']


def sdf_to_zc(f):
    p = np.pad(f, (1, 1), 'edge')
    return (f * p[:-2, 1:-1] < 0) | (f * p[2:, 1:-1] < 0) | (
        f * p[1:-1, :-2] < 0) | (f * p[1:-1, 2:] < 0)


trusted = timb.threshold_trusted_for_view2(entry['new_weight'])
output = sdf_to_zc(np.where(trusted, entry['new_phi'], np.nan))

import scipy.io
scipy.io.savemat(args.output, {'machine': output, 'true': state})

print 'state', state, state.shape
print 'output', output, output.shape

import sys
sys.exit(0)

import matplotlib
matplotlib.rcParams.update({'font.size': 8, 'image.origin': 'lower'})
import matplotlib.pyplot as plt
Example #3
0
def main():
    # Connect to experiment db
    if args.host is not None:
        client = MongoClient(host=args.host)
    else:
        client = MongoClient()
    db = client.timb_experiments_db
    fs = gridfs.GridFS(db)

    # Pull out input/output sequence
    o = db.experiments.find_one({'_id': ObjectId(args.id)})
    from pprint import pprint
    pprint(o['tracker_params'])

    out_bmaps = np.empty((100, 100, len(o['log'])), dtype=np.uint32)
    out_true_bmaps = np.empty((100, 100, len(o['log'])), dtype=np.uint32)

    for i, log_entry in enumerate(o['log']):
        entry = cPickle.loads(fs.get(log_entry['entry_file']).read())
        out_true_bmaps[:, :, i] = entry['state']

        trusted = timb.threshold_trusted_for_view2(entry['new_weight'])
        machine_output = timb.sdf_to_zc(
            np.where(trusted, entry['new_phi'], np.nan))
        out_bmaps[:, :, i] = machine_output

    # out_bmaps = np.array(out_bmaps)
    # out_true_bmaps = np.array(out_true_bmaps)

    # Write i/o sequence to matlab
    input_filename = '/tmp/%s.mat' % str(uuid.uuid4())
    output_filename = '/tmp/%s.mat' % str(uuid.uuid4())
    scipy.io.savemat(input_filename, {
        'bmaps': out_bmaps,
        'true_bmaps': out_true_bmaps
    })

    # Call matlab function to calculuate PR
    subprocess.call(
        [
            'matlab',
            '-nosplash',
            '-nodesktop',  #'-nojvm', '-nodisplay', 
            '-r',
            "multiPR('%s', '%s'); exit" % (input_filename, output_filename)
        ],
        cwd=MATLAB_CWD)
    output = scipy.io.loadmat(output_filename)
    os.unlink(input_filename)
    os.unlink(output_filename)

    out_ps = np.squeeze(output['ps'])
    out_rs = np.squeeze(output['rs'])
    out_f1s = np.squeeze(output['f1s'])

    print out_f1s

    print 'average f1', np.mean(out_f1s[np.isfinite(out_f1s)])

    import matplotlib.pyplot as plt
    plt.plot(out_ps, label="precision")
    plt.plot(out_rs, label="recall")
    plt.plot(out_f1s, label="f1")
    plt.legend(loc=4)
    plt.ylim([0, 1])
    plt.show()
Example #4
0
def main():
    # Connect to experiment db
    if args.host is not None:
        client = MongoClient(host=args.host)
    else:
        client = MongoClient()
    db = client.timb_experiments_db
    fs = gridfs.GridFS(db)

    # Pull out parts of the sequence
    ids = args.id.split(',')
    baseline_ids = args.baseline_ids.split(',')

    import matplotlib
    matplotlib.rcParams.update({'font.size': 8, 'image.origin': 'lower'})
    import matplotlib.pyplot as plt

    ROWS = 3
    plt.figure(figsize=(args.num_steps, ROWS * len(ids)), dpi=100)
    plt.subplots_adjust(wspace=.001, hspace=.001)

    for id_ind, id in enumerate(ids):
        o = db.experiments.find_one({'_id': ObjectId(id)})
        id_baseline = baseline_ids[id_ind]
        o_baseline = db.experiments.find_one({'_id': ObjectId(id_baseline)})

        step_inds = np.floor(np.linspace(1,
                                         len(o['log']) - 2,
                                         args.num_steps)).astype(int)
        entries = [
            cPickle.loads(fs.get(o['log'][i]['entry_file']).read())
            for i in step_inds
        ]
        entries_baseline = [
            cPickle.loads(fs.get(o_baseline['log'][i]['entry_file']).read())
            for i in step_inds
        ]

        # def plot_binary(img):
        #   img = skimage.morphology.skeletonize(img)
        #   plt.imshow(img.T, aspect=1, origin='lower', cmap='binary_r', interpolation='spline36')

        def plot_field(f, img=True, contour=False, colors='k'):
            assert img or contour
            if img:
                plt.imshow(f.T,
                           aspect=1,
                           vmin=-1,
                           vmax=1,
                           cmap='bwr',
                           origin='lower')
            else:
                plt.imshow(np.zeros_like(f),
                           aspect=1,
                           vmin=-1,
                           vmax=1,
                           cmap='bwr',
                           origin='lower')
            if contour:
                x = np.linspace(0, f.shape[0] - 1, f.shape[0])
                y = np.linspace(0, f.shape[1] - 1, f.shape[1])
                X, Y = np.meshgrid(x, y, indexing='ij')
                plt.contour(X, Y, f, levels=[0], colors=colors)

        for i, e in enumerate(entries):
            plt.subplot(ROWS * len(ids), len(entries),
                        i + 1 + len(entries) * (0 + ROWS * id_ind))
            plt.axis('off')
            img = np.empty(e['state'].shape + (3, ), dtype=np.uint8)
            for k in range(3):
                img[:, :, k] = np.where(e['state'], 0, 255)
            plot_field(get_sdf(img), False, True)

            e_baseline = entries_baseline[i]
            plt.subplot(ROWS * len(ids), len(entries),
                        i + 1 + len(entries) * (1 + ROWS * id_ind))
            plt.axis('off')
            trusted = timb.threshold_trusted_for_view2(
                e_baseline['new_weight'])
            baseline_output = np.where(trusted, e_baseline['new_phi'], np.nan)
            plot_field(baseline_output, False, True, colors='r')

            plt.subplot(ROWS * len(ids), len(entries),
                        i + 1 + len(entries) * (2 + ROWS * id_ind))
            plt.axis('off')
            trusted = timb.threshold_trusted_for_view2(e['new_weight'])
            machine_output = np.where(trusted, e['new_phi'], np.nan)
            plot_field(machine_output, False, True, colors='b')

    plt.savefig('out.pdf', bbox_inches='tight')
    plt.show()
Example #5
0
else:
  client = MongoClient()
db = client.timb_experiments_db
fs = gridfs.GridFS(db)

o = db.experiments.find_one({'_id': ObjectId(args.id)})

entry = cPickle.loads(fs.get(o['log'][args.i]['entry_file']).read())

state = entry['state']

def sdf_to_zc(f):
  p = np.pad(f, (1,1), 'edge')
  return (f*p[:-2,1:-1] < 0) | (f*p[2:,1:-1] < 0) | (f*p[1:-1,:-2] < 0) | (f*p[1:-1,2:] < 0)

trusted = timb.threshold_trusted_for_view2(entry['new_weight'])
output = sdf_to_zc(np.where(trusted, entry['new_phi'], np.nan))

import scipy.io
scipy.io.savemat(args.output, {'machine':output, 'true':state})


print 'state', state, state.shape
print 'output', output, output.shape


import sys
sys.exit(0)

import matplotlib
matplotlib.rcParams.update({'font.size': 8, 'image.origin': 'lower'})
Example #6
0
def main():
  # Connect to experiment db
  if args.host is not None:
    client = MongoClient(host=args.host)
  else:
    client = MongoClient()
  db = client.timb_experiments_db
  fs = gridfs.GridFS(db)

  # Pull out parts of the sequence
  ids = args.id.split(',')
  baseline_ids = args.baseline_ids.split(',')

  import matplotlib
  matplotlib.rcParams.update({'font.size': 8, 'image.origin': 'lower'})
  import matplotlib.pyplot as plt

  ROWS = 3
  plt.figure(figsize=(args.num_steps, ROWS*len(ids)), dpi=100)
  plt.subplots_adjust(wspace=.001, hspace=.001)

  for id_ind, id in enumerate(ids):
    o = db.experiments.find_one({'_id': ObjectId(id)})
    id_baseline = baseline_ids[id_ind]
    o_baseline = db.experiments.find_one({'_id': ObjectId(id_baseline)})

    step_inds = np.floor(np.linspace(1, len(o['log'])-2, args.num_steps)).astype(int)
    entries = [cPickle.loads(fs.get(o['log'][i]['entry_file']).read()) for i in step_inds]
    entries_baseline = [cPickle.loads(fs.get(o_baseline['log'][i]['entry_file']).read()) for i in step_inds]

    # def plot_binary(img):
    #   img = skimage.morphology.skeletonize(img)
    #   plt.imshow(img.T, aspect=1, origin='lower', cmap='binary_r', interpolation='spline36')

    def plot_field(f, img=True, contour=False, colors='k'):
      assert img or contour
      if img:
        plt.imshow(f.T, aspect=1, vmin=-1, vmax=1, cmap='bwr', origin='lower')
      else:
        plt.imshow(np.zeros_like(f), aspect=1, vmin=-1, vmax=1, cmap='bwr', origin='lower')
      if contour:
        x = np.linspace(0, f.shape[0]-1, f.shape[0])
        y = np.linspace(0, f.shape[1]-1, f.shape[1])
        X, Y = np.meshgrid(x, y, indexing='ij')
        plt.contour(X, Y, f, levels=[0], colors=colors)

    for i, e in enumerate(entries):
      plt.subplot(ROWS*len(ids), len(entries), i+1 + len(entries)*(0 + ROWS*id_ind))
      plt.axis('off')
      img = np.empty(e['state'].shape+(3,), dtype=np.uint8)
      for k in range(3):
        img[:,:,k] = np.where(e['state'], 0, 255)
      plot_field(get_sdf(img), False, True)

      e_baseline = entries_baseline[i]
      plt.subplot(ROWS*len(ids), len(entries), i+1 + len(entries)*(1 + ROWS*id_ind))
      plt.axis('off')
      trusted = timb.threshold_trusted_for_view2(e_baseline['new_weight'])
      baseline_output = np.where(trusted, e_baseline['new_phi'], np.nan)
      plot_field(baseline_output, False, True, colors='r')

      plt.subplot(ROWS*len(ids), len(entries), i+1 + len(entries)*(2 + ROWS*id_ind))
      plt.axis('off')
      trusted = timb.threshold_trusted_for_view2(e['new_weight'])
      machine_output = np.where(trusted, e['new_phi'], np.nan)
      plot_field(machine_output, False, True, colors='b')

  plt.savefig('out.pdf', bbox_inches='tight')
  plt.show()