def load_variables(pickle_file_name): if fu.exists(pickle_file_name): with fu.fopen(pickle_file_name, 'r') as f: d = cPickle.load(f) return d else: raise Exception('{:s} does not exists.'.format(pickle_file_name))
def mkdir_if_missing(output_dir): if not fu.exists(output_dir): try: fu.makedirs(output_dir) except: logging.error( "Something went wrong in mkdir_if_missing. " "Probably some other process created the directory already.")
def save_variables(pickle_file_name, var, info, overwrite = False): if fu.exists(pickle_file_name) and overwrite == False: raise Exception('{:s} exists and over write is false.'.format(pickle_file_name)) # Construct the dictionary assert(type(var) == list); assert(type(info) == list); d = {} for i in xrange(len(var)): d[info[i]] = var[i] with fu.fopen(pickle_file_name, 'w') as f: cPickle.dump(d, f, cPickle.HIGHEST_PROTOCOL)
def save_variables(pickle_file_name, var, info, overwrite = False): if fu.exists(pickle_file_name) and overwrite == False: raise Exception('{:s} exists and over write is false.'.format(pickle_file_name)) # Construct the dictionary assert(type(var) == list); assert(type(info) == list); d = {} for i in range(len(var)): d[info[i]] = var[i] with fu.fopen(pickle_file_name, 'wb') as f: pickle.dump(d, f, pickle.HIGHEST_PROTOCOL)
def save_variables(pickle_file_name, var, info, overwrite=False): if fu.exists(pickle_file_name) and overwrite == False: raise Exception( '{:s} exists and over write is false.'.format(pickle_file_name)) # Construct the dictionary assert (type(var) == list) assert (type(info) == list) for t in info: assert (type(t) == str), 'variable names are not strings' d = {} for i in range(len(var)): d[info[i]] = var[i] with fu.fopen(pickle_file_name, 'wb') as f: cPickle.dump(d, f)
def get_meta_data(self, file_name, data_dir=None): if data_dir is None: data_dir = self.get_data_dir() full_file_name = os.path.join(data_dir, 'meta', file_name) assert(fu.exists(full_file_name)), \ '{:s} does not exist'.format(full_file_name) ext = os.path.splitext(full_file_name)[1] if ext == '.txt': ls = [] with fu.fopen(full_file_name, 'r') as f: for l in f: ls.append(l.rstrip()) elif ext == '.pkl': ls = utils.load_variables(full_file_name) return ls
def mkdir_if_missing(output_dir): if not fu.exists(output_dir): fu.makedirs(output_dir)
def plot_trajectory_first_person(dt, orig_maps, out_dir): out_dir = os.path.join(out_dir, FLAGS.config_name+_get_suffix_str(), FLAGS.imset) fu.makedirs(out_dir) # Load the model so that we can render. plt.set_cmap('gray') samples_per_action = 8; wait_at_action = 0; Writer = animation.writers['mencoder'] writer = Writer(fps=3*(samples_per_action+wait_at_action), metadata=dict(artist='anonymous'), bitrate=1800) args = sna.get_args_for_config(FLAGS.config_name + '+bench_'+FLAGS.imset) args.navtask.logdir = None navtask_ = copy.deepcopy(args.navtask) navtask_.camera_param.modalities = ['rgb'] navtask_.task_params.modalities = ['rgb'] sz = 512 navtask_.camera_param.height = sz navtask_.camera_param.width = sz navtask_.task_params.img_height = sz navtask_.task_params.img_width = sz R = lambda: nav_env.get_multiplexer_class(navtask_, 0) R = R() b = R.buildings[0] f = [0 for _ in range(wait_at_action)] + \ [float(_)/samples_per_action for _ in range(samples_per_action)]; # Generate things for it to render. inds_to_do = [] inds_to_do += [1, 4, 10] #1291, 1268, 1273, 1289, 1302, 1426, 1413, 1449, 1399, 1390] for i in inds_to_do: fig = plt.figure(figsize=(10,8)) gs = GridSpec(3,4) gs.update(wspace=0.05, hspace=0.05, left=0.0, top=0.97, right=1.0, bottom=0.) ax = fig.add_subplot(gs[:,:-1]) ax1 = fig.add_subplot(gs[0,-1]) ax2 = fig.add_subplot(gs[1,-1]) ax3 = fig.add_subplot(gs[2,-1]) axes = [ax, ax1, ax2, ax3] # ax = fig.add_subplot(gs[:,:]) # axes = [ax] for ax in axes: ax.set_axis_off() node_ids = dt['all_node_ids'][i, :, 0]*1 # Prune so that last node is not repeated more than 3 times? if np.all(node_ids[-4:] == node_ids[-1]): while node_ids[-4] == node_ids[-1]: node_ids = node_ids[:-1] num_steps = np.minimum(FLAGS.num_steps, len(node_ids)) xyt = b.to_actual_xyt_vec(b.task.nodes[node_ids]) xyt_diff = xyt[1:,:] - xyt[:-1:,:] xyt_diff[:,2] = np.mod(xyt_diff[:,2], 4) ind = np.where(xyt_diff[:,2] == 3)[0] xyt_diff[ind, 2] = -1 xyt_diff = np.expand_dims(xyt_diff, axis=1) to_cat = [xyt_diff*_ for _ in f] perturbs_all = np.concatenate(to_cat, axis=1) perturbs_all = np.concatenate([perturbs_all, np.zeros_like(perturbs_all[:,:,:1])], axis=2) node_ids_all = np.expand_dims(node_ids, axis=1)*1 node_ids_all = np.concatenate([node_ids_all for _ in f], axis=1) node_ids_all = np.reshape(node_ids_all[:-1,:], -1) perturbs_all = np.reshape(perturbs_all, [-1, 4]) imgs = b.render_nodes(b.task.nodes[node_ids_all,:], perturb=perturbs_all) # Get action at each node. actions = [] _, action_to_nodes = b.get_feasible_actions(node_ids) for j in range(num_steps-1): action_to_node = action_to_nodes[j] node_to_action = dict(zip(action_to_node.values(), action_to_node.keys())) actions.append(node_to_action[node_ids[j+1]]) def init_fn(): return fig, gt_dist_to_goal = [] # Render trajectories. def worker(j): # Plot the image. step_number = j/(samples_per_action + wait_at_action) img = imgs[j]; ax = axes[0]; ax.clear(); ax.set_axis_off(); img = img.astype(np.uint8); ax.imshow(img); tt = ax.set_title( "First Person View\n" + "Top corners show diagnostics (distance, agents' action) not input to agent.", fontsize=12) plt.setp(tt, color='white') # Distance to goal. t = 'Dist to Goal:\n{:2d} steps'.format(int(dt['all_d_at_t'][i, step_number])) t = ax.text(0.01, 0.99, t, horizontalalignment='left', verticalalignment='top', fontsize=20, color='red', transform=ax.transAxes, alpha=1.0) t.set_bbox(dict(color='white', alpha=0.85, pad=-0.1)) # Action to take. action_latex = ['$\odot$ ', '$\curvearrowright$ ', '$\curvearrowleft$ ', r'$\Uparrow$ '] t = ax.text(0.99, 0.99, action_latex[actions[step_number]], horizontalalignment='right', verticalalignment='top', fontsize=40, color='green', transform=ax.transAxes, alpha=1.0) t.set_bbox(dict(color='white', alpha=0.85, pad=-0.1)) # Plot the map top view. ax = axes[-1] if j == 0: # Plot the map locs = dt['all_locs'][i,:num_steps,:] goal_loc = dt['all_goal_locs'][i,:,:] xymin = np.minimum(np.min(goal_loc, axis=0), np.min(locs, axis=0)) xymax = np.maximum(np.max(goal_loc, axis=0), np.max(locs, axis=0)) xy1 = (xymax+xymin)/2. - 0.7*np.maximum(np.max(xymax-xymin), 24) xy2 = (xymax+xymin)/2. + 0.7*np.maximum(np.max(xymax-xymin), 24) ax.set_axis_on() ax.patch.set_facecolor((0.333, 0.333, 0.333)) ax.set_xticks([]); ax.set_yticks([]); ax.imshow(orig_maps, origin='lower', vmin=-1.0, vmax=2.0) ax.plot(goal_loc[:,0], goal_loc[:,1], 'g*', markersize=12) locs = dt['all_locs'][i,:1,:] ax.plot(locs[:,0], locs[:,1], 'b.', markersize=12) ax.set_xlim([xy1[0], xy2[0]]) ax.set_ylim([xy1[1], xy2[1]]) locs = dt['all_locs'][i,step_number,:] locs = np.expand_dims(locs, axis=0) ax.plot(locs[:,0], locs[:,1], 'r.', alpha=1.0, linewidth=0, markersize=4) tt = ax.set_title('Trajectory in topview', fontsize=14) plt.setp(tt, color='white') return fig, line_ani = animation.FuncAnimation(fig, worker, (num_steps-1)*(wait_at_action+samples_per_action), interval=500, blit=True, init_func=init_fn) tmp_file_name = 'tmp.mp4' line_ani.save(tmp_file_name, writer=writer, savefig_kwargs={'facecolor':'black'}) out_file_name = os.path.join(out_dir, 'vis_{:04d}.mp4'.format(i)) print(out_file_name) if fu.exists(out_file_name): gfile.Remove(out_file_name) gfile.Copy(tmp_file_name, out_file_name) gfile.Remove(tmp_file_name) plt.close(fig)