complete maml and remove vis_compare_algo
This commit is contained in:
@@ -221,76 +221,7 @@ def visualize_env(save_dir, version):
|
||||
os.system("{:} {xdir}/env-{ver}.webm".format(base_cmd, xdir=save_dir, ver=version))
|
||||
|
||||
|
||||
def compare_algs(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
save_dir = Path(str(save_dir))
|
||||
save_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
dpi, width, height = 30, 1800, 1400
|
||||
figsize = width / float(dpi), height / float(dpi)
|
||||
LabelSize, LegendFontsize, font_gap = 80, 80, 5
|
||||
|
||||
cache_path = Path(alg_dir) / "env-info.pth"
|
||||
assert cache_path.exists(), "{:} does not exist".format(cache_path)
|
||||
env_info = torch.load(cache_path)
|
||||
|
||||
alg_name2dir = OrderedDict()
|
||||
alg_name2dir["Optimal"] = "use-same-timestamp"
|
||||
alg_name2dir["History SL"] = "use-all-past-data"
|
||||
colors = ["r", "g"]
|
||||
|
||||
dynamic_env = env_info["dynamic_env"]
|
||||
min_t, max_t = dynamic_env.min_timestamp, dynamic_env.max_timestamp
|
||||
|
||||
linewidths = 10
|
||||
for idx, (timestamp, (ori_allx, ori_ally)) in enumerate(
|
||||
tqdm(dynamic_env, ncols=50)
|
||||
):
|
||||
if idx == 0:
|
||||
continue
|
||||
fig = plt.figure(figsize=figsize)
|
||||
cur_ax = fig.add_subplot(1, 1, 1)
|
||||
|
||||
# the data
|
||||
allx, ally = ori_allx[:, 0].numpy(), ori_ally[:, 0].numpy()
|
||||
plot_scatter(cur_ax, allx, ally, "k", 0.99, linewidths, "Raw Data")
|
||||
|
||||
for idx_alg, (alg, xdir) in enumerate(alg_name2dir.items()):
|
||||
ckp_path = (
|
||||
Path(alg_dir)
|
||||
/ xdir
|
||||
/ "{:04d}-{:04d}.pth".format(idx, env_info["total"])
|
||||
)
|
||||
assert ckp_path.exists()
|
||||
ckp_data = torch.load(ckp_path)
|
||||
with torch.no_grad():
|
||||
predicts = ckp_data["model"](ori_allx)
|
||||
predicts = predicts.cpu().view(-1).numpy()
|
||||
plot_scatter(cur_ax, allx, predicts, colors[idx_alg], 0.99, linewidths, alg)
|
||||
|
||||
cur_ax.set_xlabel("X", fontsize=LabelSize)
|
||||
cur_ax.set_ylabel("Y", rotation=0, fontsize=LabelSize)
|
||||
for tick in cur_ax.xaxis.get_major_ticks():
|
||||
tick.label.set_fontsize(LabelSize - font_gap)
|
||||
tick.label.set_rotation(10)
|
||||
for tick in cur_ax.yaxis.get_major_ticks():
|
||||
tick.label.set_fontsize(LabelSize - font_gap)
|
||||
cur_ax.set_xlim(-10, 10)
|
||||
cur_ax.set_ylim(-60, 60)
|
||||
cur_ax.legend(loc=1, fontsize=LegendFontsize)
|
||||
|
||||
save_path = save_dir / "{:05d}".format(idx)
|
||||
fig.savefig(str(save_path) + ".pdf", dpi=dpi, bbox_inches="tight", format="pdf")
|
||||
fig.savefig(str(save_path) + ".png", dpi=dpi, bbox_inches="tight", format="png")
|
||||
plt.close("all")
|
||||
save_dir = save_dir.resolve()
|
||||
base_cmd = "ffmpeg -y -i {xdir}/%05d.png -vf scale={w}:{h} -pix_fmt yuv420p -vb 5000k".format(
|
||||
xdir=save_dir, w=width, h=height
|
||||
)
|
||||
os.system("{:} {xdir}/compare-alg.mp4".format(base_cmd, xdir=save_dir))
|
||||
os.system("{:} {xdir}/compare-alg.webm".format(base_cmd, xdir=save_dir))
|
||||
|
||||
|
||||
def compare_algs_v2(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
def compare_algs(save_dir, version, alg_dir="./outputs/lfna-synthetic"):
|
||||
save_dir = Path(str(save_dir))
|
||||
save_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -298,16 +229,21 @@ def compare_algs_v2(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
figsize = width / float(dpi), height / float(dpi)
|
||||
LabelSize, LegendFontsize, font_gap = 80, 80, 5
|
||||
|
||||
cache_path = Path(alg_dir) / "env-info.pth"
|
||||
cache_path = Path(alg_dir) / "env-{:}-info.pth".format(version)
|
||||
assert cache_path.exists(), "{:} does not exist".format(cache_path)
|
||||
env_info = torch.load(cache_path)
|
||||
|
||||
alg_name2dir = OrderedDict()
|
||||
alg_name2dir["Optimal"] = "use-same-timestamp"
|
||||
# alg_name2dir["Supervised Learning (History Data)"] = "use-all-past-data"
|
||||
alg_name2dir["Supervised Learning (History Data)"] = "use-all-past-data"
|
||||
alg_name2dir["MAML"] = "use-maml-s1"
|
||||
alg_name2all_containers = OrderedDict()
|
||||
if version == "v1":
|
||||
poststr = "v1-d16"
|
||||
else:
|
||||
raise ValueError("Invalid version: {:}".format(version))
|
||||
for idx_alg, (alg, xdir) in enumerate(alg_name2dir.items()):
|
||||
ckp_path = Path(alg_dir) / xdir / "final-ckp.pth"
|
||||
ckp_path = Path(alg_dir) / "{:}-{:}".format(xdir, poststr) / "final-ckp.pth"
|
||||
xdata = torch.load(ckp_path)
|
||||
alg_name2all_containers[alg] = xdata["w_container_per_epoch"]
|
||||
# load the basic model
|
||||
@@ -320,7 +256,7 @@ def compare_algs_v2(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
)
|
||||
|
||||
alg2xs, alg2ys = defaultdict(list), defaultdict(list)
|
||||
colors = ["r", "g"]
|
||||
colors = ["r", "g", "b"]
|
||||
|
||||
dynamic_env = env_info["dynamic_env"]
|
||||
min_t, max_t = dynamic_env.min_timestamp, dynamic_env.max_timestamp
|
||||
@@ -339,15 +275,6 @@ def compare_algs_v2(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
plot_scatter(cur_ax, allx, ally, "k", 0.99, linewidths, "Raw Data")
|
||||
|
||||
for idx_alg, (alg, xdir) in enumerate(alg_name2dir.items()):
|
||||
"""
|
||||
ckp_path = (
|
||||
Path(alg_dir)
|
||||
/ xdir
|
||||
/ "{:04d}-{:04d}.pth".format(idx, env_info["total"])
|
||||
)
|
||||
assert ckp_path.exists()
|
||||
ckp_data = torch.load(ckp_path)
|
||||
"""
|
||||
with torch.no_grad():
|
||||
# predicts = ckp_data["model"](ori_allx)
|
||||
predicts = model.forward_with_container(
|
||||
@@ -369,8 +296,12 @@ def compare_algs_v2(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
tick.label.set_rotation(10)
|
||||
for tick in cur_ax.yaxis.get_major_ticks():
|
||||
tick.label.set_fontsize(LabelSize - font_gap)
|
||||
cur_ax.set_xlim(-10, 10)
|
||||
cur_ax.set_ylim(-60, 60)
|
||||
if version == "v1":
|
||||
cur_ax.set_xlim(-2, 2)
|
||||
cur_ax.set_ylim(-8, 8)
|
||||
elif version == "v2":
|
||||
cur_ax.set_xlim(-10, 10)
|
||||
cur_ax.set_ylim(-60, 60)
|
||||
cur_ax.legend(loc=1, fontsize=LegendFontsize)
|
||||
|
||||
# the trajectory data
|
||||
@@ -398,16 +329,20 @@ def compare_algs_v2(save_dir, alg_dir="./outputs/lfna-synthetic"):
|
||||
cur_ax.set_ylim(0, 10)
|
||||
cur_ax.legend(loc=1, fontsize=LegendFontsize)
|
||||
|
||||
save_path = save_dir / "{:05d}".format(idx)
|
||||
save_path = save_dir / "v{:}-{:05d}".format(version, idx)
|
||||
fig.savefig(str(save_path) + ".pdf", dpi=dpi, bbox_inches="tight", format="pdf")
|
||||
fig.savefig(str(save_path) + ".png", dpi=dpi, bbox_inches="tight", format="png")
|
||||
plt.close("all")
|
||||
save_dir = save_dir.resolve()
|
||||
base_cmd = "ffmpeg -y -i {xdir}/%05d.png -vf scale={w}:{h} -pix_fmt yuv420p -vb 5000k".format(
|
||||
xdir=save_dir, w=width, h=height
|
||||
base_cmd = "ffmpeg -y -i {xdir}/v{ver}-%05d.png -vf scale={w}:{h} -pix_fmt yuv420p -vb 5000k".format(
|
||||
xdir=save_dir, w=width, h=height, ver=version
|
||||
)
|
||||
os.system(
|
||||
"{:} {xdir}/com-alg-{ver}.mp4".format(base_cmd, xdir=save_dir, ver=version)
|
||||
)
|
||||
os.system(
|
||||
"{:} {xdir}/com-alg-{ver}.webm".format(base_cmd, xdir=save_dir, ver=version)
|
||||
)
|
||||
os.system("{:} {xdir}/compare-alg.mp4".format(base_cmd, xdir=save_dir))
|
||||
os.system("{:} {xdir}/compare-alg.webm".format(base_cmd, xdir=save_dir))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -421,8 +356,7 @@ if __name__ == "__main__":
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
visualize_env(os.path.join(args.save_dir, "vis-env"), "v1")
|
||||
visualize_env(os.path.join(args.save_dir, "vis-env"), "v2")
|
||||
# compare_algs_v2(os.path.join(args.save_dir, "compare-alg-v2"))
|
||||
# visualize_env(os.path.join(args.save_dir, "vis-env"), "v1")
|
||||
# visualize_env(os.path.join(args.save_dir, "vis-env"), "v2")
|
||||
compare_algs(os.path.join(args.save_dir, "compare-alg-v2"), "v1")
|
||||
# compare_cl(os.path.join(args.save_dir, "compare-cl"))
|
||||
# compare_algs(os.path.join(args.save_dir, "compare-alg"))
|
||||
|
Reference in New Issue
Block a user