| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | ############################################################### | 
					
						
							| 
									
										
										
										
											2021-01-25 21:48:14 +08:00
										 |  |  | # NATS-Bench (arxiv.org/pdf/2009.00437.pdf), IEEE TPAMI 2021  # | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | # The code to draw Figure 6 in our paper.                     # | 
					
						
							|  |  |  | ############################################################### | 
					
						
							|  |  |  | # Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2020.06           # | 
					
						
							|  |  |  | ############################################################### | 
					
						
							|  |  |  | # Usage: python exps/NATS-Bench/draw-fig8.py                  # | 
					
						
							|  |  |  | ############################################################### | 
					
						
							|  |  |  | import os, gc, sys, time, torch, argparse | 
					
						
							|  |  |  | import numpy as np | 
					
						
							|  |  |  | from typing import List, Text, Dict, Any | 
					
						
							|  |  |  | from shutil import copyfile | 
					
						
							|  |  |  | from collections import defaultdict, OrderedDict | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  | from copy import deepcopy | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | from pathlib import Path | 
					
						
							|  |  |  | import matplotlib | 
					
						
							|  |  |  | import seaborn as sns | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | matplotlib.use("agg") | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | import matplotlib.pyplot as plt | 
					
						
							|  |  |  | import matplotlib.ticker as ticker | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  | lib_dir = (Path(__file__).parent / ".." / ".." / "lib").resolve() | 
					
						
							|  |  |  | if str(lib_dir) not in sys.path: | 
					
						
							|  |  |  |     sys.path.insert(0, str(lib_dir)) | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | from config_utils import dict2config, load_config | 
					
						
							|  |  |  | from nats_bench import create | 
					
						
							|  |  |  | from log_utils import time_string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 21:43:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  | plt.rcParams.update( | 
					
						
							|  |  |  |     {"text.usetex": True, "font.family": "sans-serif", "font.sans-serif": ["Helvetica"]} | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | ## for Palatino and other serif fonts use: | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  | plt.rcParams.update( | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         "text.usetex": True, | 
					
						
							|  |  |  |         "font.family": "serif", | 
					
						
							|  |  |  |         "font.serif": ["Palatino"], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fetch_data(root_dir="./output/search", search_space="tss", dataset=None): | 
					
						
							|  |  |  |     ss_dir = "{:}-{:}".format(root_dir, search_space) | 
					
						
							|  |  |  |     alg2all = OrderedDict() | 
					
						
							|  |  |  |     # alg2name['REINFORCE'] = 'REINFORCE-0.01' | 
					
						
							|  |  |  |     # alg2name['RANDOM'] = 'RANDOM' | 
					
						
							|  |  |  |     # alg2name['BOHB'] = 'BOHB' | 
					
						
							|  |  |  |     if search_space == "tss": | 
					
						
							|  |  |  |         hp = "$\mathcal{H}^{1}$" | 
					
						
							|  |  |  |         if dataset == "cifar10": | 
					
						
							|  |  |  |             suffixes = ["-T1200000", "-T1200000-FULL"] | 
					
						
							|  |  |  |     elif search_space == "sss": | 
					
						
							|  |  |  |         hp = "$\mathcal{H}^{2}$" | 
					
						
							|  |  |  |         if dataset == "cifar10": | 
					
						
							|  |  |  |             suffixes = ["-T200000", "-T200000-FULL"] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise ValueError("Unkonwn search space: {:}".format(search_space)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     alg2all[r"REA ($\mathcal{H}^{0}$)"] = dict( | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |         path=os.path.join(ss_dir, dataset + suffixes[0], "R-EA-SS3", "results.pth"), | 
					
						
							|  |  |  |         color="b", | 
					
						
							|  |  |  |         linestyle="-", | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  |     alg2all[r"REA ({:})".format(hp)] = dict( | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |         path=os.path.join(ss_dir, dataset + suffixes[1], "R-EA-SS3", "results.pth"), | 
					
						
							|  |  |  |         color="b", | 
					
						
							|  |  |  |         linestyle="--", | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for alg, xdata in alg2all.items(): | 
					
						
							|  |  |  |         data = torch.load(xdata["path"]) | 
					
						
							|  |  |  |         for index, info in data.items(): | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |             info["time_w_arch"] = [ | 
					
						
							|  |  |  |                 (x, y) for x, y in zip(info["all_total_times"], info["all_archs"]) | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |             for j, arch in enumerate(info["all_archs"]): | 
					
						
							|  |  |  |                 assert arch != -1, "invalid arch from {:} {:} {:} ({:}, {:})".format( | 
					
						
							|  |  |  |                     alg, search_space, dataset, index, j | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |         xdata["data"] = data | 
					
						
							|  |  |  |     return alg2all | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def query_performance(api, data, dataset, ticket): | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |     results, is_size_space = [], api.search_space_name == "size" | 
					
						
							|  |  |  |     for i, info in data.items(): | 
					
						
							|  |  |  |         time_w_arch = sorted(info["time_w_arch"], key=lambda x: abs(x[0] - ticket)) | 
					
						
							|  |  |  |         time_a, arch_a = time_w_arch[0] | 
					
						
							|  |  |  |         time_b, arch_b = time_w_arch[1] | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |         info_a = api.get_more_info( | 
					
						
							|  |  |  |             arch_a, dataset=dataset, hp=90 if is_size_space else 200, is_random=False | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         info_b = api.get_more_info( | 
					
						
							|  |  |  |             arch_b, dataset=dataset, hp=90 if is_size_space else 200, is_random=False | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |         accuracy_a, accuracy_b = info_a["test-accuracy"], info_b["test-accuracy"] | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |         interplate = (time_b - ticket) / (time_b - time_a) * accuracy_a + ( | 
					
						
							|  |  |  |             ticket - time_a | 
					
						
							|  |  |  |         ) / (time_b - time_a) * accuracy_b | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |         results.append(interplate) | 
					
						
							|  |  |  |     # return sum(results) / len(results) | 
					
						
							|  |  |  |     return np.mean(results), np.std(results) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | y_min_s = { | 
					
						
							|  |  |  |     ("cifar10", "tss"): 91, | 
					
						
							|  |  |  |     ("cifar10", "sss"): 91, | 
					
						
							|  |  |  |     ("cifar100", "tss"): 65, | 
					
						
							|  |  |  |     ("cifar100", "sss"): 65, | 
					
						
							|  |  |  |     ("ImageNet16-120", "tss"): 36, | 
					
						
							|  |  |  |     ("ImageNet16-120", "sss"): 40, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | y_max_s = { | 
					
						
							|  |  |  |     ("cifar10", "tss"): 94.5, | 
					
						
							|  |  |  |     ("cifar10", "sss"): 93.5, | 
					
						
							|  |  |  |     ("cifar100", "tss"): 72.5, | 
					
						
							|  |  |  |     ("cifar100", "sss"): 70.5, | 
					
						
							|  |  |  |     ("ImageNet16-120", "tss"): 46, | 
					
						
							|  |  |  |     ("ImageNet16-120", "sss"): 46, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | x_axis_s = { | 
					
						
							|  |  |  |     ("cifar10", "tss"): 1200000, | 
					
						
							|  |  |  |     ("cifar10", "sss"): 200000, | 
					
						
							|  |  |  |     ("cifar100", "tss"): 400, | 
					
						
							|  |  |  |     ("cifar100", "sss"): 400, | 
					
						
							|  |  |  |     ("ImageNet16-120", "tss"): 1200, | 
					
						
							|  |  |  |     ("ImageNet16-120", "sss"): 600, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  | name2label = { | 
					
						
							|  |  |  |     "cifar10": "CIFAR-10", | 
					
						
							|  |  |  |     "cifar100": "CIFAR-100", | 
					
						
							|  |  |  |     "ImageNet16-120": "ImageNet-16-120", | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | spaces2latex = { | 
					
						
							|  |  |  |     "tss": r"$\mathcal{S}_{t}$", | 
					
						
							|  |  |  |     "sss": r"$\mathcal{S}_{s}$", | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 21:43:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # FuncFormatter can be used as a decorator | 
					
						
							|  |  |  | @ticker.FuncFormatter | 
					
						
							|  |  |  | def major_formatter(x, pos): | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |     if x == 0: | 
					
						
							|  |  |  |         return "0" | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return "{:.2f}e5".format(x / 1e5) | 
					
						
							| 
									
										
										
										
											2020-12-02 21:43:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 22:25:23 +08:00
										 |  |  | def visualize_curve(api_dict, vis_save_dir): | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |     vis_save_dir = vis_save_dir.resolve() | 
					
						
							|  |  |  |     vis_save_dir.mkdir(parents=True, exist_ok=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dpi, width, height = 250, 5000, 2000 | 
					
						
							|  |  |  |     figsize = width / float(dpi), height / float(dpi) | 
					
						
							|  |  |  |     LabelSize, LegendFontsize = 28, 28 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def sub_plot_fn(ax, search_space, dataset): | 
					
						
							|  |  |  |         max_time = x_axis_s[(dataset, search_space)] | 
					
						
							|  |  |  |         alg2data = fetch_data(search_space=search_space, dataset=dataset) | 
					
						
							|  |  |  |         alg2accuracies = OrderedDict() | 
					
						
							|  |  |  |         total_tickets = 200 | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |         time_tickets = [ | 
					
						
							|  |  |  |             float(i) / total_tickets * int(max_time) for i in range(total_tickets) | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |         ax.set_xlim(0, x_axis_s[(dataset, search_space)]) | 
					
						
							|  |  |  |         ax.set_ylim(y_min_s[(dataset, search_space)], y_max_s[(dataset, search_space)]) | 
					
						
							|  |  |  |         for tick in ax.get_xticklabels(): | 
					
						
							|  |  |  |             tick.set_rotation(25) | 
					
						
							|  |  |  |             tick.set_fontsize(LabelSize - 6) | 
					
						
							|  |  |  |         for tick in ax.get_yticklabels(): | 
					
						
							|  |  |  |             tick.set_fontsize(LabelSize - 6) | 
					
						
							|  |  |  |         ax.xaxis.set_major_formatter(major_formatter) | 
					
						
							|  |  |  |         for idx, (alg, xdata) in enumerate(alg2data.items()): | 
					
						
							|  |  |  |             accuracies = [] | 
					
						
							|  |  |  |             for ticket in time_tickets: | 
					
						
							|  |  |  |                 # import pdb; pdb.set_trace() | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |                 accuracy, accuracy_std = query_performance( | 
					
						
							|  |  |  |                     api_dict[search_space], xdata["data"], dataset, ticket | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |                 accuracies.append(accuracy) | 
					
						
							|  |  |  |             # print('{:} plot alg : {:10s}, final accuracy = {:.2f}$\pm${:.2f}'.format(time_string(), alg, accuracy, accuracy_std)) | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |             print( | 
					
						
							|  |  |  |                 "{:} plot alg : {:10s} on {:}".format(time_string(), alg, search_space) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |             alg2accuracies[alg] = accuracies | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |             ax.plot( | 
					
						
							|  |  |  |                 time_tickets, | 
					
						
							|  |  |  |                 accuracies, | 
					
						
							|  |  |  |                 c=xdata["color"], | 
					
						
							|  |  |  |                 linestyle=xdata["linestyle"], | 
					
						
							|  |  |  |                 label="{:}".format(alg), | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |             ax.set_xlabel("Estimated wall-clock time", fontsize=LabelSize) | 
					
						
							|  |  |  |             ax.set_ylabel("Test accuracy", fontsize=LabelSize) | 
					
						
							|  |  |  |             ax.set_title( | 
					
						
							| 
									
										
										
										
											2021-03-18 16:02:55 +08:00
										 |  |  |                 r"Results on {:} over {:}".format( | 
					
						
							|  |  |  |                     name2label[dataset], spaces2latex[search_space] | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 fontsize=LabelSize, | 
					
						
							| 
									
										
										
										
											2021-03-17 09:25:58 +00:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ax.legend(loc=4, fontsize=LegendFontsize) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fig, axs = plt.subplots(1, 2, figsize=figsize) | 
					
						
							|  |  |  |     sub_plot_fn(axs[0], "tss", "cifar10") | 
					
						
							|  |  |  |     sub_plot_fn(axs[1], "sss", "cifar10") | 
					
						
							|  |  |  |     save_path = (vis_save_dir / "full-curve.png").resolve() | 
					
						
							|  |  |  |     fig.savefig(save_path, dpi=dpi, bbox_inches="tight", format="png") | 
					
						
							|  |  |  |     print("{:} save into {:}".format(time_string(), save_path)) | 
					
						
							|  |  |  |     plt.close("all") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser( | 
					
						
							|  |  |  |         description="NATS-Bench: Benchmarking NAS Algorithms for Architecture Topology and Size", | 
					
						
							|  |  |  |         formatter_class=argparse.ArgumentDefaultsHelpFormatter, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     parser.add_argument( | 
					
						
							|  |  |  |         "--save_dir", | 
					
						
							|  |  |  |         type=str, | 
					
						
							|  |  |  |         default="output/vis-nas-bench/nas-algos-vs-h", | 
					
						
							|  |  |  |         help="Folder to save checkpoints and log.", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     save_dir = Path(args.save_dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     api_tss = create(None, "tss", fast_mode=True, verbose=False) | 
					
						
							|  |  |  |     api_sss = create(None, "sss", fast_mode=True, verbose=False) | 
					
						
							|  |  |  |     visualize_curve(dict(tss=api_tss, sss=api_sss), save_dir) |