2020-06-30 11:05:38 +02:00
|
|
|
###############################################################
|
|
|
|
# NAS-Bench-201, ICLR 2020 (https://arxiv.org/abs/2001.00326) #
|
|
|
|
###############################################################
|
2020-07-30 15:07:11 +02:00
|
|
|
# NATS-Bench: Benchmarking NAS algorithms for Architecture Topology and Size
|
|
|
|
###############################################################
|
2020-06-30 11:05:38 +02:00
|
|
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2020.06 #
|
|
|
|
###############################################################
|
2020-07-30 15:07:11 +02:00
|
|
|
# Usage: python exps/NAS-Bench-201/test-nas-api.py #
|
2020-06-30 11:05:38 +02:00
|
|
|
###############################################################
|
|
|
|
import os, sys, time, torch, argparse
|
|
|
|
import numpy as np
|
|
|
|
from typing import List, Text, Dict, Any
|
|
|
|
from shutil import copyfile
|
|
|
|
from collections import defaultdict
|
|
|
|
from copy import deepcopy
|
|
|
|
from pathlib import Path
|
|
|
|
import matplotlib
|
|
|
|
import seaborn as sns
|
|
|
|
matplotlib.use('agg')
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
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
|
2020-07-30 15:07:11 +02:00
|
|
|
from nats_bench import create
|
2020-06-30 11:05:38 +02:00
|
|
|
from log_utils import time_string
|
2020-07-08 07:08:55 +02:00
|
|
|
from models import get_cell_based_tiny_net, CellStructure
|
2020-06-30 11:05:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_api(api, is_301=True):
|
|
|
|
print('{:} start testing the api : {:}'.format(time_string(), api))
|
|
|
|
api.clear_params(12)
|
|
|
|
api.reload(index=12)
|
|
|
|
|
|
|
|
# Query the informations of 1113-th architecture
|
|
|
|
info_strs = api.query_info_str_by_arch(1113)
|
|
|
|
print(info_strs)
|
|
|
|
info = api.query_by_index(113)
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
info = api.query_by_index(113, 'cifar100')
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
|
|
|
|
info = api.query_meta_info_by_index(115, '90' if is_301 else '200')
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
|
|
|
|
for dataset in ['cifar10', 'cifar100', 'ImageNet16-120']:
|
|
|
|
for xset in ['train', 'test', 'valid']:
|
|
|
|
best_index, highest_accuracy = api.find_best(dataset, xset)
|
|
|
|
print('')
|
|
|
|
params = api.get_net_param(12, 'cifar10', None)
|
|
|
|
|
2020-07-01 14:29:46 +02:00
|
|
|
# Obtain the config and create the network
|
2020-06-30 11:05:38 +02:00
|
|
|
config = api.get_net_config(12, 'cifar10')
|
|
|
|
print('{:}\n'.format(config))
|
|
|
|
network = get_cell_based_tiny_net(config)
|
|
|
|
network.load_state_dict(next(iter(params.values())))
|
|
|
|
|
2020-07-01 14:29:46 +02:00
|
|
|
# Obtain the cost information
|
2020-06-30 11:05:38 +02:00
|
|
|
info = api.get_cost_info(12, 'cifar10')
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
info = api.get_latency(12, 'cifar10')
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
|
2020-07-01 14:29:46 +02:00
|
|
|
# Count the number of architectures
|
2020-06-30 11:05:38 +02:00
|
|
|
info = api.statistics('cifar100', '12')
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
|
2020-07-01 14:29:46 +02:00
|
|
|
# Show the information of the 123-th architecture
|
2020-06-30 11:05:38 +02:00
|
|
|
api.show(123)
|
|
|
|
|
2020-07-01 14:29:46 +02:00
|
|
|
# Obtain both cost and performance information
|
2020-06-30 11:05:38 +02:00
|
|
|
info = api.get_more_info(1234, 'cifar10')
|
|
|
|
print('{:}\n'.format(info))
|
|
|
|
print('{:} finish testing the api : {:}'.format(time_string(), api))
|
|
|
|
|
2020-07-13 12:04:52 +02:00
|
|
|
if not is_301:
|
|
|
|
arch_str = '|nor_conv_3x3~0|+|nor_conv_3x3~0|avg_pool_3x3~1|+|skip_connect~0|nor_conv_3x3~1|skip_connect~2|'
|
|
|
|
matrix = api.str2matrix(arch_str)
|
|
|
|
print('Compute the adjacency matrix of {:}'.format(arch_str))
|
|
|
|
print(matrix)
|
|
|
|
info = api.simulate_train_eval(123, 'cifar10')
|
|
|
|
print('simulate_train_eval : {:}'.format(info))
|
|
|
|
|
2020-06-30 11:05:38 +02:00
|
|
|
|
|
|
|
def test_issue_81_82(api):
|
2020-07-01 14:29:46 +02:00
|
|
|
results = api.query_by_index(0, 'cifar10-valid', hp='12')
|
2020-06-30 11:05:38 +02:00
|
|
|
results = api.query_by_index(0, 'cifar10-valid', hp='200')
|
2020-07-01 14:29:46 +02:00
|
|
|
print(list(results.keys()))
|
|
|
|
print(results[888].get_eval('valid'))
|
2020-06-30 11:05:38 +02:00
|
|
|
print(results[888].get_eval('x-valid'))
|
|
|
|
result_dict = api.get_more_info(index=0, dataset='cifar10-valid', iepoch=11, hp='200', is_random=False)
|
2020-07-08 07:08:55 +02:00
|
|
|
info = api.query_by_arch('|nor_conv_3x3~0|+|skip_connect~0|nor_conv_3x3~1|+|skip_connect~0|none~1|nor_conv_3x3~2|', '200')
|
|
|
|
print(info)
|
|
|
|
structure = CellStructure.str2structure('|nor_conv_3x3~0|+|skip_connect~0|nor_conv_3x3~1|+|skip_connect~0|none~1|nor_conv_3x3~2|')
|
|
|
|
info = api.query_by_arch(structure, '200')
|
|
|
|
print(info)
|
2020-06-30 11:05:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2020-07-30 15:07:11 +02:00
|
|
|
api201 = create(os.path.join(os.environ['TORCH_HOME'], 'NAS-Bench-201-v1_0-e61699.pth'), 'topology', True)
|
2020-06-30 11:05:38 +02:00
|
|
|
test_issue_81_82(api201)
|
2020-07-01 14:29:46 +02:00
|
|
|
print ('Test {:} done'.format(api201))
|
|
|
|
|
2020-07-30 15:07:11 +02:00
|
|
|
api201 = create(None, 'topology', True) # use the default file path
|
2020-06-30 11:05:38 +02:00
|
|
|
test_issue_81_82(api201)
|
|
|
|
test_api(api201, False)
|
2020-07-01 14:29:46 +02:00
|
|
|
print ('Test {:} done'.format(api201))
|
2020-06-30 11:05:38 +02:00
|
|
|
|
2020-07-30 15:07:11 +02:00
|
|
|
api301 = create(None, 'size', True)
|
|
|
|
test_api(api301, True)
|