Create NATS
This commit is contained in:
		| @@ -16,7 +16,7 @@ matplotlib.use('agg') | ||||
| import matplotlib.pyplot as plt | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
| from nas_201_api import NASBench201API, NASBench301API | ||||
| from nas_201_api import NASBench201API | ||||
| from log_utils import time_string | ||||
| from models import get_cell_based_tiny_net | ||||
| from utils import weight_watcher | ||||
|   | ||||
| @@ -3,9 +3,6 @@ | ||||
| ########################################################################################################################################################### | ||||
| # Before run these commands, the files must be properly put. | ||||
| # | ||||
| # python exps/experimental/test-ww-bench.py --base_path $HOME/.torch/NAS-Bench-201-v1_0-e61699 | ||||
| # python exps/experimental/test-ww-bench.py --base_path $HOME/.torch/NAS-Bench-201-v1_1-096897 --dataset cifar10-valid --use_12 1 --use_valid 1 | ||||
| # CUDA_VISIBLE_DEVICES='' OMP_NUM_THREADS=4 python exps/experimental/test-ww-bench.py --base_path $HOME/.torch/NAS-Bench-201-v1_1-096897 --dataset cifar10 | ||||
| # CUDA_VISIBLE_DEVICES='' OMP_NUM_THREADS=4 python exps/experimental/test-ww-bench.py --search_space sss --base_path $HOME/.torch/NAS-Bench-301-v1_0 --dataset cifar10 | ||||
| # CUDA_VISIBLE_DEVICES='' OMP_NUM_THREADS=4 python exps/experimental/test-ww-bench.py --search_space sss --base_path $HOME/.torch/NAS-Bench-301-v1_0 --dataset cifar100 | ||||
| # CUDA_VISIBLE_DEVICES='' OMP_NUM_THREADS=4 python exps/experimental/test-ww-bench.py --search_space sss --base_path $HOME/.torch/NAS-Bench-301-v1_0 --dataset ImageNet16-120 | ||||
| @@ -22,8 +19,8 @@ matplotlib.use('agg') | ||||
| import matplotlib.pyplot as plt | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
| from nas_201_api import NASBench201API, NASBench301API | ||||
| from log_utils import time_string | ||||
| from nats_bench import create | ||||
| from models import get_cell_based_tiny_net | ||||
| from utils import weight_watcher | ||||
|  | ||||
| @@ -52,8 +49,8 @@ def evaluate(api, weight_dir, data: str): | ||||
|     # compute the weight watcher results | ||||
|     config = api.get_net_config(arch_index, data) | ||||
|     net = get_cell_based_tiny_net(config) | ||||
|     meta_info = api.query_meta_info_by_index(arch_index, hp='200' if isinstance(api, NASBench201API) else '90') | ||||
|     params = meta_info.get_net_param(data, 888 if isinstance(api, NASBench201API) else 777) | ||||
|     meta_info = api.query_meta_info_by_index(arch_index, hp='200' if api.search_space_name == 'topology' else '90') | ||||
|     params = meta_info.get_net_param(data, 888 if api.search_space_name == 'topology' else 777) | ||||
|     with torch.no_grad(): | ||||
|       net.load_state_dict(params) | ||||
|       _, summary = weight_watcher.analyze(net, alphas=False) | ||||
| @@ -70,7 +67,7 @@ def evaluate(api, weight_dir, data: str): | ||||
|       ok += 1 | ||||
|       norms.append(cur_norm) | ||||
|     # query the accuracy | ||||
|     info = meta_info.get_metrics(data, 'ori-test', iepoch=None, is_random=888 if isinstance(api, NASBench201API) else 777) | ||||
|     info = meta_info.get_metrics(data, 'ori-test', iepoch=None, is_random=888 if api.search_space_name == 'topology' else 777) | ||||
|     accuracies.append(info['accuracy']) | ||||
|     del net, meta_info | ||||
|     # print the information | ||||
| @@ -81,9 +78,8 @@ def evaluate(api, weight_dir, data: str): | ||||
|  | ||||
|  | ||||
| def main(search_space, meta_file: str, weight_dir, save_dir, xdata): | ||||
|   API = NASBench201API if search_space == 'tss' else NASBench301API | ||||
|   save_dir.mkdir(parents=True, exist_ok=True) | ||||
|   api = API(meta_file, verbose=False) | ||||
|   api = create(meta_file, search_space, verbose=False) | ||||
|   datasets = ['cifar10-valid', 'cifar10', 'cifar100', 'ImageNet16-120'] | ||||
|   print(time_string() + ' ' + '='*50) | ||||
|   for data in datasets: | ||||
|   | ||||
| @@ -3,8 +3,8 @@ | ||||
| ############################################################### | ||||
| # Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2020.06           # | ||||
| ############################################################### | ||||
| # Usage: python exps/experimental/vis-bench-algos.py --search_space tss | ||||
| # Usage: python exps/experimental/vis-bench-algos.py --search_space sss | ||||
| # Usage: python exps/experimental/vis-nats-bench-algos.py --search_space tss | ||||
| # Usage: python exps/experimental/vis-nats-bench-algos.py --search_space sss | ||||
| ############################################################### | ||||
| import os, gc, sys, time, torch, argparse | ||||
| import numpy as np | ||||
| @@ -22,7 +22,7 @@ import matplotlib.ticker as ticker | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
| from config_utils import dict2config, load_config | ||||
| from nas_201_api import NASBench201API, NASBench301API | ||||
| from nats_bench import create | ||||
| from log_utils import time_string | ||||
| 
 | ||||
| 
 | ||||
| @@ -48,18 +48,19 @@ def fetch_data(root_dir='./output/search', search_space='tss', dataset=None): | ||||
| 
 | ||||
| 
 | ||||
| def query_performance(api, data, dataset, ticket): | ||||
|   results, is_301 = [], isinstance(api, NASBench301API) | ||||
|   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] | ||||
|     info_a = api.get_more_info(arch_a, dataset=dataset, hp=90 if is_301 else 200, is_random=False) | ||||
|     info_b = api.get_more_info(arch_b, dataset=dataset, hp=90 if is_301 else 200, is_random=False) | ||||
|     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) | ||||
|     accuracy_a, accuracy_b = info_a['test-accuracy'], info_b['test-accuracy'] | ||||
|     interplate = (time_b-ticket) / (time_b-time_a) * accuracy_a + (ticket-time_a) / (time_b-time_a) * accuracy_b | ||||
|     results.append(interplate) | ||||
|   return sum(results) / len(results) | ||||
| 
 | ||||
| 
 | ||||
| y_min_s = {('cifar10', 'tss'): 90, | ||||
|            ('cifar10', 'sss'): 92, | ||||
|            ('cifar100', 'tss'): 65, | ||||
| @@ -74,6 +75,10 @@ y_max_s = {('cifar10', 'tss'): 94.5, | ||||
|            ('ImageNet16-120', 'tss'): 44, | ||||
|            ('ImageNet16-120', 'sss'): 46} | ||||
| 
 | ||||
| name2label = {'cifar10': 'CIFAR-10', | ||||
|               'cifar100': 'CIFAR-100', | ||||
|               'ImageNet16-120': 'ImageNet-16-120'} | ||||
| 
 | ||||
| def visualize_curve(api, vis_save_dir, search_space, max_time): | ||||
|   vis_save_dir = vis_save_dir.resolve() | ||||
|   vis_save_dir.mkdir(parents=True, exist_ok=True) | ||||
| @@ -99,8 +104,8 @@ def visualize_curve(api, vis_save_dir, search_space, max_time): | ||||
|       alg2accuracies[alg] = accuracies | ||||
|       ax.plot([x/100 for x in time_tickets], accuracies, c=colors[idx], label='{:}'.format(alg)) | ||||
|       ax.set_xlabel('Estimated wall-clock time (1e2 seconds)', fontsize=LabelSize) | ||||
|       ax.set_ylabel('Test accuracy on {:}'.format(dataset), fontsize=LabelSize) | ||||
|       ax.set_title('Searching results on {:}'.format(dataset), fontsize=LabelSize+4) | ||||
|       ax.set_ylabel('Test accuracy on {:}'.format(name2label[dataset]), fontsize=LabelSize) | ||||
|       ax.set_title('Searching results on {:}'.format(name2label[dataset]), fontsize=LabelSize+4) | ||||
|     ax.legend(loc=4, fontsize=LegendFontsize) | ||||
| 
 | ||||
|   fig, axs = plt.subplots(1, 3, figsize=figsize) | ||||
| @@ -123,10 +128,5 @@ if __name__ == '__main__': | ||||
| 
 | ||||
|   save_dir = Path(args.save_dir) | ||||
| 
 | ||||
|   if args.search_space == 'tss': | ||||
|     api = NASBench201API(verbose=False) | ||||
|   elif args.search_space == 'sss': | ||||
|     api = NASBench301API(verbose=False) | ||||
|   else: | ||||
|     raise ValueError('Invalid search space : {:}'.format(args.search_space)) | ||||
|   api = create(None, args.search_space, verbose=False) | ||||
|   visualize_curve(api, save_dir, args.search_space, args.max_time) | ||||
| @@ -3,8 +3,8 @@ | ||||
| ############################################################### | ||||
| # Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2020.06           # | ||||
| ############################################################### | ||||
| # Usage: python exps/experimental/vis-bench-ws.py --search_space tss | ||||
| # Usage: python exps/experimental/vis-bench-ws.py --search_space sss | ||||
| # Usage: python exps/experimental/vis-nats-bench-ws.py --search_space tss | ||||
| # Usage: python exps/experimental/vis-nats-bench-ws.py --search_space sss | ||||
| ############################################################### | ||||
| import os, gc, sys, time, torch, argparse | ||||
| import numpy as np | ||||
| @@ -22,15 +22,16 @@ import matplotlib.ticker as ticker | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
| from config_utils import dict2config, load_config | ||||
| from nas_201_api import NASBench201API, NASBench301API | ||||
| from nats_bench import create | ||||
| from log_utils import time_string | ||||
| 
 | ||||
| 
 | ||||
| def fetch_data(root_dir='./output/search', search_space='tss', dataset=None): | ||||
|   ss_dir = '{:}-{:}'.format(root_dir, search_space) | ||||
|   alg2name, alg2path = OrderedDict(), OrderedDict() | ||||
|   seeds = [777, 888, 999] | ||||
|   print('\n[fetch data] from {:} on {:}'.format(search_space, dataset)) | ||||
|   if search_space == 'tss': | ||||
|     seeds = [777] | ||||
|     alg2name['GDAS'] = 'gdas-affine0_BN0-None' | ||||
|     alg2name['RSPS'] = 'random-affine0_BN0-None' | ||||
|     alg2name['DARTS (1st)'] = 'darts-v1-affine0_BN0-None' | ||||
| @@ -38,7 +39,6 @@ def fetch_data(root_dir='./output/search', search_space='tss', dataset=None): | ||||
|     alg2name['ENAS'] = 'enas-affine0_BN0-None' | ||||
|     alg2name['SETN'] = 'setn-affine0_BN0-None' | ||||
|   else: | ||||
|     seeds = [777, 888, 999] | ||||
|     alg2name['TAS'] = 'tas-affine0_BN0' | ||||
|     alg2name['FBNetV2'] = 'fbv2-affine0_BN0' | ||||
|     alg2name['TuNAS'] = 'tunas-affine0_BN0' | ||||
| @@ -46,13 +46,19 @@ def fetch_data(root_dir='./output/search', search_space='tss', dataset=None): | ||||
|     alg2path[alg] = os.path.join(ss_dir, dataset, name, 'seed-{:}-last-info.pth') | ||||
|   alg2data = OrderedDict() | ||||
|   for alg, path in alg2path.items(): | ||||
|     alg2data[alg] = [] | ||||
|     alg2data[alg], ok_num = [], 0 | ||||
|     for seed in seeds: | ||||
|       xpath = path.format(seed) | ||||
|       assert os.path.isfile(xpath), 'invalid path : {:}'.format(xpath) | ||||
|       if os.path.isfile(xpath): | ||||
|         ok_num += 1 | ||||
|       else: | ||||
|         print('This is an invalid path : {:}'.format(xpath)) | ||||
|         continue | ||||
|       data = torch.load(xpath, map_location=torch.device('cpu')) | ||||
|       data = torch.load(data['last_checkpoint'], map_location=torch.device('cpu')) | ||||
|       alg2data[alg].append(data['genotypes']) | ||||
|     print('This algorithm : {:} has {:} valid ckps.'.format(alg, ok_num)) | ||||
|     assert ok_num > 0, 'Must have at least 1 valid ckps.' | ||||
|   return alg2data | ||||
| 
 | ||||
| 
 | ||||
| @@ -95,7 +101,7 @@ def visualize_curve(api, vis_save_dir, search_space): | ||||
|       for iepoch in range(epochs+1): | ||||
|         structures, accs = [_[iepoch-1] for _ in data], [] | ||||
|         for structure in structures: | ||||
|           info = api.get_more_info(structure, dataset=dataset, hp=90 if isinstance(api, NASBench301API) else 200, is_random=False) | ||||
|           info = api.get_more_info(structure, dataset=dataset, hp=90 if api.search_space_name == 'size' else 200, is_random=False) | ||||
|           accs.append(info['test-accuracy']) | ||||
|         accuracies.append(sum(accs)/len(accs)) | ||||
|         xs.append(iepoch) | ||||
| @@ -124,12 +130,6 @@ if __name__ == '__main__': | ||||
|   args = parser.parse_args() | ||||
| 
 | ||||
|   save_dir = Path(args.save_dir) | ||||
|   alg2data = fetch_data(search_space='tss', dataset='cifar10') | ||||
| 
 | ||||
|   if args.search_space == 'tss': | ||||
|     api = NASBench201API(verbose=False) | ||||
|   elif args.search_space == 'sss': | ||||
|     api = NASBench301API(verbose=False) | ||||
|   else: | ||||
|     raise ValueError('Invalid search space : {:}'.format(args.search_space)) | ||||
|   api = create(None, args.search_space, verbose=False) | ||||
|   visualize_curve(api, save_dir, args.search_space) | ||||
| @@ -21,9 +21,9 @@ import matplotlib.ticker as ticker | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
| from config_utils import dict2config, load_config | ||||
| from nas_201_api import NASBench201API, NASBench301API | ||||
| from log_utils import time_string | ||||
| from models import get_cell_based_tiny_net | ||||
| from nats_bench import create | ||||
|  | ||||
|  | ||||
| def visualize_info(api, vis_save_dir, indicator): | ||||
| @@ -391,11 +391,11 @@ if __name__ == '__main__': | ||||
|   to_save_dir = Path(args.save_dir) | ||||
|  | ||||
|   datasets = ['cifar10', 'cifar100', 'ImageNet16-120'] | ||||
|   api201 = NASBench201API(None, verbose=True) | ||||
|   api201 = create(None, 'tss', verbose=True) | ||||
|   for xdata in datasets: | ||||
|     visualize_tss_info(api201, xdata, to_save_dir) | ||||
|  | ||||
|   api301 = NASBench301API(None, verbose=True) | ||||
|   api301 = create(None, 'size', verbose=True) | ||||
|   for xdata in datasets: | ||||
|     visualize_sss_info(api301, xdata, to_save_dir) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user