From 4ad089cfdecc639b13f3a8342195ecd684ecd28e Mon Sep 17 00:00:00 2001 From: hzyjerry Date: Mon, 13 Nov 2017 22:59:43 -0800 Subject: [PATCH] Plot functions --- .gitignore | 3 +- examples/scripts/plot_reward_local.py | 76 +++++++-- examples/scripts/plot_reward_openai.py | 224 ++++++++++++++++++------- 3 files changed, 226 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index 40e2df00a..2e80e1392 100644 --- a/.gitignore +++ b/.gitignore @@ -46,4 +46,5 @@ checkpoint examples/train/models/ -log_*/ \ No newline at end of file +log_*/ +monitor_* \ No newline at end of file diff --git a/examples/scripts/plot_reward_local.py b/examples/scripts/plot_reward_local.py index 3def939d3..e5cca8e73 100644 --- a/examples/scripts/plot_reward_local.py +++ b/examples/scripts/plot_reward_local.py @@ -14,29 +14,31 @@ all_log_dirs = [os.path.join(root_dir, d) for d in all_log_dirs if "openai" in d filename = "0.monitor.csv" -local_logs = [os.path.join(all_log_dirs[-1], filename)] +local_logs = [os.path.join(all_log_dirs[-1], filename), os.path.join(all_log_dirs[-2], filename)] log_dir = [all_log_dirs[-1]] +print(log_dir) for i in range(len(log_dir)): local_logs.append(str(i) + filename) -def smooth_median(rewards, factor=10): + +def smooth_median(rewards): rew_median = [] i = 0 - while i < len(rewards): - cur_list = rewards[i: min(i + factor, len(rewards))] - while len(cur_list) < factor: - cur_list.append(rewards[-1]) + while i < len(rewards) - MEDIAN_FC: + cur_list = rewards[i: min(i + MEDIAN_FC, len(rewards))] + while len(cur_list) < MEDIAN_FC: + cur_list.append(rewards[-1]) i = i + 1 rew_median.append(np.median(cur_list)) return rew_median -def smooth_max(rewards, factor=10): +def smooth_max(rewards): rew_median = [] i = 0 - while i < len(rewards): - cur_list = rewards[i: min(i + factor, len(rewards))] - while len(cur_list) < factor: + while i < len(rewards) - MEDIAN_FC: + cur_list = rewards[i: min(i + MEDIAN_FC, len(rewards))] + while len(cur_list) < MEDIAN_FC: cur_list.append(rewards[-1]) i = i + 1 rew_median.append(np.max(cur_list)) @@ -62,9 +64,59 @@ def plot(index, smooth_fc=smooth_median): all_times = num_rows[row_index, 1].tolist() t_range = np.arange(0, len(all_rewards), 1) print("Total number of steps:", sum(all_times)) + print('Average time length', np.mean(np.array(all_times))) plt.plot(t_range, all_rewards, '.', t_range, smooth_fc(all_rewards, args.smooth), '-') +def plot2(index1, index2, smooth_fc=smooth_median): + with open(local_logs[index1], 'r') as csvfile: + csv_rows = csv.reader(csvfile, delimiter=' ', quotechar='|') + str_rows = [] + num_rows = [] + for row in csv_rows: + str_rows.append(row[0]) + for row in str_rows: + try: + num_rows.append([float(num) for num in row.split(',')]) + except: + continue + num_rows = np.array(num_rows) + + row_index = num_rows[:, 0] > -200 ## Avoid plotting too low points + + num_rows[row_index, 0] = num_rows[row_index, 0] + 1.2 ## change of initial coordinate + + all_rewards_1 = num_rows[row_index, 0].tolist() + all_times_1 = num_rows[row_index, 1].tolist() + #print("Total number of steps:", sum(all_times)) + #print('Average time length', np.mean(np.array(all_times))) + sm_rewards_1 = smooth_fc(all_rewards_1) + t_range_1 = np.arange(0, len(sm_rewards_1), 1) + plt.plot(t_range_1, sm_rewards_1, '-') + + with open(local_logs[index2], 'r') as csvfile: + csv_rows = csv.reader(csvfile, delimiter=' ', quotechar='|') + str_rows = [] + num_rows = [] + for row in csv_rows: + str_rows.append(row[0]) + for row in str_rows: + try: + num_rows.append([float(num) for num in row.split(',')]) + except: + continue + num_rows = np.array(num_rows) + + row_index = num_rows[:, 0] > -200 ## Avoid plotting too low points + + all_rewards_2 = num_rows[row_index, 0].tolist() + all_times_2 = num_rows[row_index, 1].tolist() + sm_rewards_2 = smooth_fc(all_rewards_2) + t_range_2 = np.arange(0, len(sm_rewards_2), 1) + + plt.plot(t_range_2, sm_rewards_2, '-') + + def smooth(rewards, factor=10): smoothed_rew = [] for i in range(0, len(rewards) - factor + 1): @@ -72,8 +124,9 @@ def smooth(rewards, factor=10): return smoothed_rew def main(): - plot(0, smooth_median) + #plot(0, smooth_median) #plot(0, smooth_max) + plot2(0, 1, smooth_median) plt.show() if __name__ == '__main__': @@ -81,4 +134,5 @@ if __name__ == '__main__': parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--smooth', type=int, default=50) args = parser.parse_args() + MEDIAN_FC = args.smooth main() diff --git a/examples/scripts/plot_reward_openai.py b/examples/scripts/plot_reward_openai.py index 810b8fa9b..dd314b4da 100644 --- a/examples/scripts/plot_reward_openai.py +++ b/examples/scripts/plot_reward_openai.py @@ -2,124 +2,133 @@ import os import matplotlib.pyplot as plt import csv import numpy as np +import subprocess +import sys -## sudo /usr/bin/X :0 + +#sudo service lightdm stop +#sudo pkill XOrg +#sudo /usr/bin/X :0 & ## DISPLAY=:0 python examples/train/train_ant_climb_ppo1.py --mode RGBD ## DISPLAY=:0 python examples/train/train_ant_climb_ppo1.py --mode DEPTH ## DISPLAY=:0 python examples/train/train_ant_climb_ppo1.py --mode SENSOR ## scp -i /home/jerry/Dropbox/CVGL/universe.pem ubuntu@ec2-34-215-160-202.us-west-2.compute.amazonaws.com:/tmp/openai-2017-11-12-13-16-28-113773/0.monitor.csv -cmd = "scp -i /home/jerry/Dropbox/CVGL/universe.pem ubuntu@{}:/tmp/{}/{} {}" +## ssh -i /home/jerry/Dropbox/CVGL/universe.pem ubuntu@ec2-34-215-160-202.us-west-2.compute.amazonaws.com ls -l /tmp/ + aws_addr = [] -log_dir = [] +aws_names = [] ''' Universe ant climb 1 ssh -i universe.pem ubuntu@ec2-34-215-160-202.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-34-215-160-202.us-west-2.compute.amazonaws.com") +aws_names.append("RGB & Sensor, small init random, small lr") + ''' Universe ant climb 2 ssh -i universe.pem ubuntu@ec2-52-24-76-187.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-52-24-76-187.us-west-2.compute.amazonaws.com") +aws_names.append("RGBD & Sensor, small init random, large lr") ''' Universe ant climb 3 ec2-52-42-249-133.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-52-42-249-133.us-west-2.compute.amazonaws.com") +aws_names.append("Depth & Sensor, small init random, small lr") ''' Universe ant climb 4 ec2-52-38-25-41.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-52-38-25-41.us-west-2.compute.amazonaws.com") +aws_names.append("Depth & Sensor, small init random, large lr") + ''' Universe ant climb 5 ec2-34-214-189-116.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-34-214-189-116.us-west-2.compute.amazonaws.com") +aws_names.append("RGBD & Sensor, large init random, small lr") + ''' Universe ant climb 6 ec2-34-216-35-8.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-34-216-35-8.us-west-2.compute.amazonaws.com") +aws_names.append("RGBD & Sensor, large init random, large lr") ''' Universe ant climb 7 ec2-34-213-149-40.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-34-213-149-40.us-west-2.compute.amazonaws.com") +aws_names.append("Depth & Sensor, large init random, small lr") + ''' Universe ant climb 8 ec2-52-89-201-217.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-52-89-201-217.us-west-2.compute.amazonaws.com") +aws_names.append("Depth & Sensor, large init random, large lr") + ''' Universe ant climb 9 ec2-52-38-59-13.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-52-38-59-13.us-west-2.compute.amazonaws.com") +aws_names.append("Sensor only, small lr") + ''' Universe ant climb 10 ec2-34-212-248-24.us-west-2.compute.amazonaws.com ''' +aws_addr.append("ec2-34-212-248-24.us-west-2.compute.amazonaws.com") +aws_names.append("Sensor only, large lr") +aws_dirs = [] +file_names = [] +remote_filename = "0.monitor.csv" +name_template = "monitor_{}_{}.csv" - - -aws_addr.append("ec2-34-215-160-202.us-west-2.compute.amazonaws.com") -log_dir.append("openai-2017-11-12-13-43-46-948548") - - -''' -Universe husky navigate 2 -RGB + NO SENSOR + COLLISION - timesteps_per_actorbatch=1024, - clip_param=0.2, entcoeff=0.001, - optim_epochs=10, optim_stepsize=3e-4, optim_batchsize=64, - gamma=0.995, lam=0.95, -''' -aws_addr.append("ec2-52-24-76-187.us-west-2.compute.amazonaws.com") -log_dir.append("openai-2017-11-12-14-31-31-175329") - - -''' -Universe husky navigate 3 -RGB + NO SENSOR + COLLISION - timesteps_per_actorbatch=1024, - clip_param=0.2, entcoeff=0.0001, - optim_epochs=10, optim_stepsize=3e-6, optim_batchsize=64, - gamma=0.995, lam=0.95, -''' -aws_addr.append("ec2-52-42-249-133.us-west-2.compute.amazonaws.com") -log_dir.append("openai-2017-11-12-14-27-49-793285") - - -''' -Universe husky navigate 4 -RGB + SENSOR + NO COLLISION -''' -aws_addr.append("ec2-52-38-25-41.us-west-2.compute.amazonaws.com") -log_dir.append("openai-2017-11-12-14-03-35-785424") - - - -filename = "0.monitor.csv" -local_logs = [] -for i in range(len(log_dir)): - local_logs.append(str(i) + filename) +scp_cmd = "scp -i /home/jerry/Dropbox/CVGL/universe.pem ubuntu@{}:/tmp/{}/{} {}" def download(index): - full_cmd = cmd.format(aws_addr[index], log_dir[index], filename, local_logs[index]) + full_cmd = scp_cmd.format(aws_addr[index], aws_dirs[index], remote_filename, file_names[index]) print(full_cmd) os.system(full_cmd) -def plot(index, axis, smooth_fc=6): - with open(local_logs[index], 'r') as csvfile: +def get_latest_openai_dir(host): + ## ssh -i /home/jerry/Dropbox/CVGL/universe.pem ubuntu@ec2-34-215-160-202.us-west-2.compute.amazonaws.com ls -l /tmp/ + # ls_cmd = "ssh -i /home/jerry/Dropbox/CVGL/universe.pem ubuntu@{} ls -l /tmp/" + COMMAND="ls -l /tmp/" + ssh = subprocess.Popen(["ssh", "-i", "/home/jerry/Dropbox/CVGL/universe.pem", "ubuntu@%s" % host, COMMAND], + shell=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + results = ssh.stdout.readlines() + results = [str(r,'utf-8') for r in results] + results = [r.split()[-1].strip() for r in results if "openai" in r] + results = sorted(results) + return results[-1] + + +def plot(index, axis): + with open(file_names[index], 'r') as csvfile: csv_rows = csv.reader(csvfile, delimiter=' ', quotechar='|') str_rows = [] num_rows = [] @@ -133,39 +142,124 @@ def plot(index, axis, smooth_fc=6): num_rows = np.array(num_rows) all_rewards = num_rows[:, 0].tolist() - all_times = num_rows[:, 2].tolist() + all_steps = num_rows[:, 1].tolist() t_range = np.arange(0, len(all_rewards), 1) - if smooth_fc: - axis[index].plot(t_range, all_rewards, '.', t_range, smooth_median(all_rewards, smooth_fc), '-') + step_length = sum(all_steps) + total_time = num_rows[-1, 2] + time_length = sum(all_times) + if SMOOTH: + axis[index].plot(t_range, all_rewards, '.', t_range, smooth_median(all_rewards, SMOOTH_FC), '-') + axis[index].text(.5, .9, aws_names[index] + " ts: {}".format(step_length), horizontalalignment='center', + transform=axis[index].transAxes) + axis[index].text(.5, .1, "Time ts: {}".format(time_length), horizontalalignment='center', + transform=axis[index].transAxes) else: axis[index].plot(t_range, all_rewards, '.') + axis[index].text(.5, .9, aws_names[index] + " ts: {}".format(step_length), horizontalalignment='center', + transform=axis[index].transAxes) + axis[index].text(.5, .1, "Time ts: {}".format(time_length), horizontalalignment='center', + transform=axis[index].transAxes) -def smooth_median(rewards, factor=10): +def get_smooth(index): + with open(file_names[index], 'r') as csvfile: + csv_rows = csv.reader(csvfile, delimiter=' ', quotechar='|') + str_rows = [] + num_rows = [] + for row in csv_rows: + str_rows.append(row[0]) + for row in str_rows: + try: + num_rows.append([float(num) for num in row.split(',')]) + except: + continue + num_rows = np.array(num_rows) + + all_rewards = num_rows[:, 0].tolist() + total_time = num_rows[-1, 2] + all_steps = num_rows[:, 1].tolist() + print("Total time", total_time, "averge step", np.mean(all_steps)) + return smooth_median(all_rewards) + + + +def smooth_median(rewards): rew_median = [] i = 0 - while i < len(rewards): - cur_list = rewards[i: min(i + factor, len(rewards))] - while len(cur_list) < factor: + while i < len(rewards) - SMOOTH_FC: + cur_list = rewards[i: min(i + SMOOTH_FC, len(rewards))] + while len(cur_list) < SMOOTH_FC: cur_list.append(rewards[-1]) i = i + 1 rew_median.append(np.median(cur_list)) return rew_median -def smooth(rewards, factor=30): +def smooth_mean(rewards): + rew_mean = [] + i = 0 + while i < len(rewards) - SMOOTH_FC: + cur_list = rewards[i: min(i + SMOOTH_FC, len(rewards))] + while len(cur_list) < SMOOTH_FC: + cur_list.append(rewards[-1]) + i = i + 1 + rew_mean.append(np.mean(cur_list)) + return rew_mean + +def smooth(rewards): smoothed_rew = [] - for i in range(0, len(rewards) - factor + 1): - smoothed_rew.append(sum(rewards[i:i + factor]) / factor) + for i in range(0, len(rewards) - SMOOTH_FC + 1): + smoothed_rew.append(sum(rewards[i:i + SMOOTH_FC]) / SMOOTH_FC) return smoothed_rew def main(): - for i in range(len(log_dir)): - download(i) - - f, axarr = plt.subplots(len(log_dir), sharex=True) - for i in range(len(log_dir)): + f, axarr = plt.subplots(len(file_names), sharex=True) + for i in range(len(file_names)): plot(i, axarr, 10) plt.show() +def main2(): + REW_MEDIAN_FACTOR = 10 + MED_MEDIAN_FACTOR = 10 + LENGTH_CAP = 700 + + smoothed = [] + lengths = [] + smoothed_names = [] + for i in range(len(file_names)): + smooth_i = get_smooth(i) + #smooth_i = smooth_mean(smooth_i) + if len(smooth_i) < LENGTH_CAP: + smoothed.append(smooth_i) + lengths.append(len(smooth_i)) + smoothed_names.append(aws_names[i]) + max_time_length = max([len(series) for series in smoothed]) + + legends = [] + for i in range(len(smoothed)): + patch, = plt.plot(np.arange(0, lengths[i], 1), smoothed[i], label=smoothed_names[i]) + legends.append(patch) + plt.legend(handles=legends) + plt.show() + + + if __name__ == '__main__': - main() + LOCAL = True + SMOOTH = True + SMOOTH_FC = 10 + + if LOCAL: + for i in range(len(aws_addr)): + host = aws_addr[i] + remote_dir = get_latest_openai_dir(host) + file_names.append(name_template.format(host, remote_dir)) + else: + for i in range(len(aws_addr)): + host = aws_addr[i] + remote_dir = get_latest_openai_dir(host) + print(host, remote_dir) + aws_dirs.append(remote_dir) + file_names.append(name_template.format(host, remote_dir)) + download(i) + file_names = file_names[:] + main2()