def do_frame(self, agent=-1, past_plan=None, with_scores=True, multi_prediction=False): if not self.cap.isOpened(): raise Exception('Video stream closed.') if agent == -1: agent = self.agent_num adex, displayed_agent = self.get_agent_index(agent) t_plus_one = None t_plus_one2 = None frame_num = int(self.cap.get(POS_FRAMES)) now = int(self.cap.get(POS_MSEC) / 1000) _, frame = self.cap.read() frame_txt = "{:0>2}:{:0>2}".format(now // 60, now % 60) agent_txt = 'Agent: {}'.format(displayed_agent) # Check for end of annotations. if frame_num >= len(self.frames): frame_txt += ' (eof)' agent_txt = '' else: frame_txt += ' (' + str(frame_num) + ')' t = self.frames[frame_num] # There really shouldn't be any empty timesteps but I found some so we'll have to deal with it. if t >= 0 and self.timesteps[t]: # If we've reached a new timestep, recompute the observations. if t != self.last_t: self.last_t = t adex, displayed_agent = self.get_agent_index(agent) agent_txt = 'Agent: {}'.format(displayed_agent) if self.do_predictions: self.predictions = util.make_predictions( t, self.timesteps, self.agents, self.model, agent, past_plan) # self.predictions = util.fake_predictions(t, self.timesteps, self.agents, 10.0) if multi_prediction and past_plan is not None: predictions2 = util.make_predictions( t, self.timesteps, self.agents, self.model, agent, None) if predictions2.plan[adex].shape[0] > 1: t_plus_one2 = predictions2.plan[adex][1] if self.predictions.plan[adex].shape[0] > 1: t_plus_one = self.predictions.plan[adex][1] if with_scores: # ped_scores, IGP_scores = util.calc_nav_scores(self.predictions.true_paths, self.predictions.plan) # plot_nav_metrics(ped_scores, IGP_scores) pred_errs = util.calc_pred_scores( self.predictions.true_paths, self.predictions.plan, util.prediction_errors) path_errs = util.calc_pred_scores( self.predictions.true_paths, self.predictions.plan, util.path_errors) plot_prediction_metrics(pred_errs, path_errs, self.timesteps[t]) else: self.predictions = util.get_past_paths( t, self.timesteps, self.agents) # Draw the obstacles. frame = np.maximum(frame, cv2.cvtColor(self.obs_map, cv2.COLOR_GRAY2BGR)) # Draw destinations. for d in self.destinations: d = np.append(d, 1) cv2.circle(frame, util.to_pixels(self.Hinv, d), 5, (0, 255, 0), -1) # Inform of frame and agent number. pt = (3, frame.shape[0] - 3) ll, ur = draw_text(frame, pt, frame_txt) if agent_txt: pt = (ll[0], ur[1]) ll, ur = draw_text(frame, pt, agent_txt) # Draw pedestrian paths so far. if self.draw_past and self.predictions.past: for path in self.predictions.past: draw_path(frame, path, (192, 192, 192)) draw_waypoints(frame, self.predictions.past[adex], (255, 211, 176)) # Draw predictions, if we have them. if self.predictions.plan: # For each agent, draw... peds_to_draw = range(len( self.predictions.plan)) if self.draw_all_agents else [adex] # The GP samples. if self.draw_samples != NO_SAMPLES: # What kind of sample are we showing? preds = self.predictions.prior if self.draw_samples == PRIOR_SAMPLES else self.predictions.posterior # Which sample(s) are we showing? if self.draw_all_samples: if preds: samples_to_draw = range(preds[0].shape[1]) else: samples_to_draw = [] else: sdex = self.sample_num samples_to_draw = [sdex] pt = (ll[0], ur[1]) ll, ur = draw_text(frame, pt, 'Sample: {}'.format(sdex + 1)) pt = (ll[0], ur[1]) draw_text( frame, pt, 'Weight: {:.1e}'.format( self.predictions.weights[sdex])) # Now actually draw the sample. for ddex in peds_to_draw: for i in samples_to_draw: path = preds[ddex][:, i, :] path = np.column_stack((path, np.ones(path.shape[0]))) draw_path(frame, path, (255, 0, 0)) # The ground truth. if self.draw_truth: for ddex in peds_to_draw: draw_path(frame, self.predictions.true_paths[ddex], (0, 255, 0)) # The final prediction. if self.draw_plan: for ddex in peds_to_draw: draw_path(frame, self.predictions.plan[ddex], (0, 192, 192)) if not self.draw_all_agents and past_plan is not None: draw_path(frame, past_plan, (0, 192, 192)) draw_waypoints(frame, past_plan, (0, 192, 192)) cv2.imshow('frame', frame) return t_plus_one if t_plus_one2 is None else (t_plus_one, t_plus_one2)
def main(): # Parse command-line arguments. args = parse_args() Hfile = os.path.join(args.datadir, "H.txt") obsfile = os.path.join(args.datadir, "obsmat.txt") # Parse homography matrix. H = np.loadtxt(Hfile) Hinv = np.linalg.inv(H) # Parse pedestrian annotations. frames, timeframes, timesteps, agents = ewap.parse_annotations(Hinv, obsfile) test_sequences = np.array([ range(2112, 2395), range(3648, 3769), range(4163, 4374), range(6797, 7164), range(7301, 7608), range(8373, 8620), range(8859, 9544), range(9603, 10096), range(10101, 10528), range(11205, 11500), range(11835, 12172), range(12201, 12382), ]) total_t = 0 for s in test_sequences: total_t += len(s) model = models.model16 print 'Running experiment...' util.reset_timer() num_samples = 5 iters = 0 total_samples = 0 entropies = np.zeros(test_sequences.shape[0]) for i,timeline in enumerate(test_sequences): M = np.zeros((2,2)) for frame in timeline: iters += 1 t = frames[frame] if t == -1: continue for _ in range(num_samples): predictions = util.make_predictions(t, timesteps, agents, model) for a,plan in enumerate(predictions.plan): if plan.shape[0] > 1: error = predictions.true_paths[a][1,0:2] - plan[1,0:2] M += np.outer(error, error) total_samples += 1 print 'frame {}: {:.1%} complete'.format(frame, iters/total_t) M /= total_samples entropies[i] = 0.5*np.log((2*np.pi*np.e)**2 * np.linalg.det(M)) # print 'entropy is', entropy # print 'variance:' # print M print 'EXPERIMENT COMPLETE.' util.report_time() np.savetxt('experiments/hyperparam-test-194-202-joint-2.txt', entropies)
def do_frame(self, agent=-1, past_plan=None, with_scores=True, multi_prediction=False): if not self.cap.isOpened(): raise Exception('Video stream closed.') if agent == -1: agent = self.agent_num adex, displayed_agent = self.get_agent_index(agent) t_plus_one = None t_plus_one2 = None frame_num = int(self.cap.get(POS_FRAMES)) now = int(self.cap.get(POS_MSEC) / 1000) _, frame = self.cap.read() frame_txt = "{:0>2}:{:0>2}".format(now//60, now%60) agent_txt = 'Agent: {}'.format(displayed_agent) # Check for end of annotations. if frame_num >= len(self.frames): frame_txt += ' (eof)' agent_txt = '' else: frame_txt += ' (' + str(frame_num) + ')' t = self.frames[frame_num] # There really shouldn't be any empty timesteps but I found some so we'll have to deal with it. if t >= 0 and self.timesteps[t]: # If we've reached a new timestep, recompute the observations. if t != self.last_t: self.last_t = t adex, displayed_agent = self.get_agent_index(agent) agent_txt = 'Agent: {}'.format(displayed_agent) if self.do_predictions: self.predictions = util.make_predictions(t, self.timesteps, self.agents, self.model, agent, past_plan) # self.predictions = util.fake_predictions(t, self.timesteps, self.agents, 10.0) if multi_prediction and past_plan is not None: predictions2 = util.make_predictions(t, self.timesteps, self.agents, self.model, agent, None) if predictions2.plan[adex].shape[0] > 1: t_plus_one2 = predictions2.plan[adex][1] if self.predictions.plan[adex].shape[0] > 1: t_plus_one = self.predictions.plan[adex][1] if with_scores: # ped_scores, IGP_scores = util.calc_nav_scores(self.predictions.true_paths, self.predictions.plan) # plot_nav_metrics(ped_scores, IGP_scores) pred_errs = util.calc_pred_scores(self.predictions.true_paths, self.predictions.plan, util.prediction_errors) path_errs = util.calc_pred_scores(self.predictions.true_paths, self.predictions.plan, util.path_errors) plot_prediction_metrics(pred_errs, path_errs, self.timesteps[t]) else: self.predictions = util.get_past_paths(t, self.timesteps, self.agents) # Draw the obstacles. frame = np.maximum(frame, cv2.cvtColor(self.obs_map, cv2.COLOR_GRAY2BGR)) # Draw destinations. for d in self.destinations: d = np.append(d, 1) cv2.circle(frame, util.to_pixels(self.Hinv, d), 5, (0,255,0), -1) # Inform of frame and agent number. pt = (3, frame.shape[0]-3) ll, ur = draw_text(frame, pt, frame_txt) if agent_txt: pt = (ll[0], ur[1]) ll, ur = draw_text(frame, pt, agent_txt) # Draw pedestrian paths so far. if self.draw_past and self.predictions.past: for path in self.predictions.past: draw_path(frame, path, (192,192,192)) draw_waypoints(frame, self.predictions.past[adex], (255,211,176)) # Draw predictions, if we have them. if self.predictions.plan: # For each agent, draw... peds_to_draw = range(len(self.predictions.plan)) if self.draw_all_agents else [adex] # The GP samples. if self.draw_samples != NO_SAMPLES: # What kind of sample are we showing? preds = self.predictions.prior if self.draw_samples == PRIOR_SAMPLES else self.predictions.posterior # Which sample(s) are we showing? if self.draw_all_samples: if preds: samples_to_draw = range(preds[0].shape[1]) else: samples_to_draw = [] else: sdex = self.sample_num samples_to_draw = [sdex] pt = (ll[0], ur[1]) ll, ur = draw_text(frame, pt, 'Sample: {}'.format(sdex+1)) pt = (ll[0], ur[1]) draw_text(frame, pt, 'Weight: {:.1e}'.format(self.predictions.weights[sdex])) # Now actually draw the sample. for ddex in peds_to_draw: for i in samples_to_draw: path = preds[ddex][:,i,:] path = np.column_stack((path, np.ones(path.shape[0]))) draw_path(frame, path, (255,0,0)) # The ground truth. if self.draw_truth: for ddex in peds_to_draw: draw_path(frame, self.predictions.true_paths[ddex], (0,255,0)) # The final prediction. if self.draw_plan: for ddex in peds_to_draw: draw_path(frame, self.predictions.plan[ddex], (0,192,192)) if not self.draw_all_agents and past_plan is not None: draw_path(frame, past_plan, (0,192,192)) draw_waypoints(frame, past_plan, (0,192,192)) cv2.imshow('frame', frame) return t_plus_one if t_plus_one2 is None else (t_plus_one, t_plus_one2)