Upgrade API of NAS-Bench-201
This commit is contained in:
		| @@ -4,4 +4,5 @@ | ||||
| from .api import NASBench201API | ||||
| from .api import ArchResults, ResultsCount | ||||
|  | ||||
| NAS_BENCH_201_API_VERSION="v1.1"  # [2020.02.25] | ||||
| # NAS_BENCH_201_API_VERSION="v1.1"  # [2020.02.25] | ||||
| NAS_BENCH_201_API_VERSION="v1.2"  # [2020.03.09] | ||||
|   | ||||
| @@ -8,7 +8,7 @@ | ||||
| # | ||||
| # | ||||
| import os, copy, random, torch, numpy as np | ||||
| from typing import List, Text, Union, Dict, Any | ||||
| from typing import List, Text, Union, Dict | ||||
| from collections import OrderedDict, defaultdict | ||||
|  | ||||
|  | ||||
| @@ -19,8 +19,7 @@ def print_information(information, extra_info=None, show=False): | ||||
|     return 'loss = {:.3f}, top1 = {:.2f}%'.format(loss, acc) | ||||
|  | ||||
|   for ida, dataset in enumerate(dataset_names): | ||||
|     #flop, param, latency = information.get_comput_costs(dataset) | ||||
|     metric = information.get_comput_costs(dataset) | ||||
|     metric = information.get_compute_costs(dataset) | ||||
|     flop, param, latency = metric['flops'], metric['params'], metric['latency'] | ||||
|     str1 = '{:14s} FLOP={:6.2f} M, Params={:.3f} MB, latency={:} ms.'.format(dataset, flop, param, '{:.2f}'.format(latency*1000) if latency is not None and latency > 0 else None) | ||||
|     train_info = information.get_metrics(dataset, 'train') | ||||
| @@ -80,6 +79,7 @@ class NASBench201API(object): | ||||
|     return ('{name}({num}/{total} architectures)'.format(name=self.__class__.__name__, num=len(self.evaluated_indexes), total=len(self.meta_archs))) | ||||
|  | ||||
|   def random(self): | ||||
|     """Return a random index of all architectures.""" | ||||
|     return random.randint(0, len(self.meta_archs)-1) | ||||
|  | ||||
|   # This function is used to query the index of an architecture in the search space. | ||||
| @@ -166,7 +166,7 @@ class NASBench201API(object): | ||||
|     else                  : basestr, arch2infos = '200epochs', self.arch2infos_full | ||||
|     best_index, highest_accuracy = -1, None | ||||
|     for i, idx in enumerate(self.evaluated_indexes): | ||||
|       info = arch2infos[idx].get_comput_costs(dataset) | ||||
|       info = arch2infos[idx].get_compute_costs(dataset) | ||||
|       flop, param, latency = info['flops'], info['params'], info['latency'] | ||||
|       if FLOP_max  is not None and flop  > FLOP_max : continue | ||||
|       if Param_max is not None and param > Param_max: continue | ||||
| @@ -178,38 +178,40 @@ class NASBench201API(object): | ||||
|         best_index, highest_accuracy = idx, accuracy | ||||
|     return best_index, highest_accuracy | ||||
|  | ||||
|   # return the topology structure of the `index`-th architecture | ||||
|  | ||||
|   def arch(self, index: int): | ||||
|     """Return the topology structure of the `index`-th architecture.""" | ||||
|     assert 0 <= index < len(self.meta_archs), 'invalid index : {:} vs. {:}.'.format(index, len(self.meta_archs)) | ||||
|     return copy.deepcopy(self.meta_archs[index]) | ||||
|  | ||||
|   """ | ||||
|   This function is used to obtain the trained weights of the `index`-th architecture on `dataset` with the seed of `seed` | ||||
|   Args [seed]: | ||||
|     -- None : return a dict containing the trained weights of all trials, where each key is a seed and its corresponding value is the weights. | ||||
|     -- a interger : return the weights of a specific trial, whose seed is this interger. | ||||
|   Args [use_12epochs_result]: | ||||
|     -- True : train the model by 12 epochs | ||||
|     -- False : train the model by 200 epochs | ||||
|   """ | ||||
|   def get_net_param(self, index, dataset, seed, use_12epochs_result=False): | ||||
|     if use_12epochs_result: basestr, arch2infos = '12epochs' , self.arch2infos_less | ||||
|     else                  : basestr, arch2infos = '200epochs', self.arch2infos_full | ||||
|     archresult = arch2infos[index] | ||||
|     return archresult.get_net_param(dataset, seed) | ||||
|     """ | ||||
|       This function is used to obtain the trained weights of the `index`-th architecture on `dataset` with the seed of `seed` | ||||
|       Args [seed]: | ||||
|         -- None : return a dict containing the trained weights of all trials, where each key is a seed and its corresponding value is the weights. | ||||
|         -- a interger : return the weights of a specific trial, whose seed is this interger. | ||||
|       Args [use_12epochs_result]: | ||||
|         -- True : train the model by 12 epochs | ||||
|         -- False : train the model by 200 epochs | ||||
|     """ | ||||
|     if use_12epochs_result: arch2infos = self.arch2infos_less | ||||
|     else: arch2infos = self.arch2infos_full | ||||
|     arch_result = arch2infos[index] | ||||
|     return arch_result.get_net_param(dataset, seed) | ||||
|    | ||||
|   """ | ||||
|   This function is used to obtain the configuration for the `index`-th architecture on `dataset`. | ||||
|   Args [dataset] (4 possible options): | ||||
|     -- cifar10-valid : training the model on the CIFAR-10 training set. | ||||
|     -- cifar10 : training the model on the CIFAR-10 training + validation set. | ||||
|     -- cifar100 : training the model on the CIFAR-100 training set. | ||||
|     -- ImageNet16-120 : training the model on the ImageNet16-120 training set. | ||||
|   This function will return a dict. | ||||
|   ========= Some examlpes for using this function: | ||||
|   config = api.get_net_config(128, 'cifar10') | ||||
|   """ | ||||
|   def get_net_config(self, index, dataset): | ||||
|  | ||||
|   def get_net_config(self, index: int, dataset: Text): | ||||
|     """ | ||||
|       This function is used to obtain the configuration for the `index`-th architecture on `dataset`. | ||||
|       Args [dataset] (4 possible options): | ||||
|         -- cifar10-valid : training the model on the CIFAR-10 training set. | ||||
|         -- cifar10 : training the model on the CIFAR-10 training + validation set. | ||||
|         -- cifar100 : training the model on the CIFAR-100 training set. | ||||
|         -- ImageNet16-120 : training the model on the ImageNet16-120 training set. | ||||
|       This function will return a dict. | ||||
|       ========= Some examlpes for using this function: | ||||
|       config = api.get_net_config(128, 'cifar10') | ||||
|     """ | ||||
|     archresult = self.arch2infos_full[index] | ||||
|     all_results = archresult.query(dataset, None) | ||||
|     if len(all_results) == 0: raise ValueError('can not find one valid trial for the {:}-th architecture on {:}'.format(index, dataset)) | ||||
| @@ -218,12 +220,25 @@ class NASBench201API(object): | ||||
|       #print ('SEED [{:}] : {:}'.format(seed, result)) | ||||
|     raise ValueError('Impossible to reach here!') | ||||
|  | ||||
|   # obtain the cost metric for the `index`-th architecture on a dataset | ||||
|   def get_cost_info(self, index, dataset, use_12epochs_result=False): | ||||
|     if use_12epochs_result: basestr, arch2infos = '12epochs' , self.arch2infos_less | ||||
|     else                  : basestr, arch2infos = '200epochs', self.arch2infos_full | ||||
|     archresult = arch2infos[index] | ||||
|     return archresult.get_comput_costs(dataset) | ||||
|  | ||||
|   def get_cost_info(self, index: int, dataset: Text, use_12epochs_result: bool = False) -> Dict[Text, float]: | ||||
|     """To obtain the cost metric for the `index`-th architecture on a dataset.""" | ||||
|     if use_12epochs_result: arch2infos = self.arch2infos_less | ||||
|     else: arch2infos = self.arch2infos_full | ||||
|     arch_result = arch2infos[index] | ||||
|     return arch_result.get_compute_costs(dataset) | ||||
|  | ||||
|  | ||||
|   def get_latency(self, index: int, dataset: Text, use_12epochs_result: bool = False) -> float: | ||||
|     """ | ||||
|     To obtain the latency of the network (by default it will return the latency with the batch size of 256). | ||||
|     :param index: the index of the target architecture | ||||
|     :param dataset: the dataset name (cifar10-valid, cifar10, cifar100, ImageNet16-120) | ||||
|     :return: return a float value in seconds | ||||
|     """ | ||||
|     cost_dict = self.get_cost_info(index, dataset, use_12epochs_result) | ||||
|     return cost_dict['latency'] | ||||
|  | ||||
|  | ||||
|   # obtain the metric for the `index`-th architecture | ||||
|   # `dataset` indicates the dataset: | ||||
| @@ -298,12 +313,15 @@ class NASBench201API(object): | ||||
|         xifo['est-valid-accuracy'] = est_valid_info['accuracy'] | ||||
|       return xifo | ||||
|  | ||||
|   """ | ||||
|   This function will print the information of a specific (or all) architecture(s). | ||||
|   If the index < 0: it will loop for all architectures and print their information one by one. | ||||
|   else: it will print the information of the 'index'-th archiitecture. | ||||
|   """ | ||||
|  | ||||
|   def show(self, index: int = -1) -> None: | ||||
|     """ | ||||
|     This function will print the information of a specific (or all) architecture(s). | ||||
|  | ||||
|     :param index: If the index < 0: it will loop for all architectures and print their information one by one. | ||||
|                   else: it will print the information of the 'index'-th archiitecture. | ||||
|     :return: nothing | ||||
|     """ | ||||
|     if index < 0: # show all architectures | ||||
|       print(self) | ||||
|       for i, idx in enumerate(self.evaluated_indexes): | ||||
| @@ -330,19 +348,27 @@ class NASBench201API(object): | ||||
|       else: | ||||
|         print('This index ({:}) is out of range (0~{:}).'.format(index, len(self.meta_archs))) | ||||
|  | ||||
|   # This func shows how to read the string-based architecture encoding | ||||
|   #   the same as the `str2structure` func in `AutoDL-Projects/lib/models/cell_searchs/genotypes.py` | ||||
|   # Usage: | ||||
|   #   arch = api.str2lists( '|nor_conv_1x1~0|+|none~0|none~1|+|none~0|none~1|skip_connect~2|' ) | ||||
|   #   print ('there are {:} nodes in this arch'.format(len(arch)+1)) # arch is a list | ||||
|   #   for i, node in enumerate(arch): | ||||
|   #     print('the {:}-th node is the sum of these {:} nodes with op: {:}'.format(i+1, len(node), node)) | ||||
|  | ||||
|   @staticmethod | ||||
|   def str2lists(xstr: Text) -> List[Any]: | ||||
|     # assert isinstance(xstr, str), 'must take string (not {:}) as input'.format(type(xstr)) | ||||
|     nodestrs = xstr.split('+') | ||||
|   def str2lists(arch_str: Text) -> List[tuple]: | ||||
|     """ | ||||
|     This function shows how to read the string-based architecture encoding. | ||||
|       It is the same as the `str2structure` func in `AutoDL-Projects/lib/models/cell_searchs/genotypes.py` | ||||
|  | ||||
|     :param | ||||
|       arch_str: the input is a string indicates the architecture topology, such as | ||||
|                     |nor_conv_1x1~0|+|none~0|none~1|+|none~0|none~1|skip_connect~2| | ||||
|     :return: a list of tuple, contains multiple (op, input_node_index) pairs. | ||||
|  | ||||
|     :usage | ||||
|       arch = api.str2lists( '|nor_conv_1x1~0|+|none~0|none~1|+|none~0|none~1|skip_connect~2|' ) | ||||
|       print ('there are {:} nodes in this arch'.format(len(arch)+1)) # arch is a list | ||||
|       for i, node in enumerate(arch): | ||||
|         print('the {:}-th node is the sum of these {:} nodes with op: {:}'.format(i+1, len(node), node)) | ||||
|     """ | ||||
|     node_strs = arch_str.split('+') | ||||
|     genotypes = [] | ||||
|     for i, node_str in enumerate(nodestrs): | ||||
|     for i, node_str in enumerate(node_strs): | ||||
|       inputs = list(filter(lambda x: x != '', node_str.split('|'))) | ||||
|       for xinput in inputs: assert len(xinput.split('~')) == 2, 'invalid input length : {:}'.format(xinput) | ||||
|       inputs = ( xi.split('~') for xi in inputs ) | ||||
| @@ -350,40 +376,47 @@ class NASBench201API(object): | ||||
|       genotypes.append( input_infos ) | ||||
|     return genotypes | ||||
|  | ||||
|   # This func shows how to convert the string-based architecture encoding to the encoding strategy in NAS-Bench-101 | ||||
|   # Usage: | ||||
|   #   # this will return a numpy matrix (2-D np.array) | ||||
|   #   matrix = api.str2matrix( '|nor_conv_1x1~0|+|none~0|none~1|+|none~0|none~1|skip_connect~2|' ) | ||||
|   #   # This matrix is 4-by-4 matrix representing a cell with 4 nodes (only the lower left triangle is useful). | ||||
|   #      [ [0, 0, 0, 0],  # the first line represents the input (0-th) node | ||||
|   #        [2, 0, 0, 0],  # the second line represents the 1-st node, is calculated by 2-th-op( 0-th-node ) | ||||
|   #        [0, 0, 0, 0],  # the third line represents the 2-nd node, is calculated by 0-th-op( 0-th-node ) + 0-th-op( 1-th-node ) | ||||
|   #        [0, 0, 1, 0] ] # the fourth line represents the 3-rd node, is calculated by 0-th-op( 0-th-node ) + 0-th-op( 1-th-node ) + 1-th-op( 2-th-node ) | ||||
|   #   In NAS-Bench-201 search space, 0-th-op is 'none', 1-th-op is 'skip_connect' | ||||
|   #      2-th-op is 'nor_conv_1x1', 3-th-op is 'nor_conv_3x3', 4-th-op is 'avg_pool_3x3'. | ||||
|  | ||||
|   @staticmethod | ||||
|   def str2matrix(xstr): | ||||
|     assert isinstance(xstr, str), 'must take string (not {:}) as input'.format(type(xstr)) | ||||
|     # this only support NAS-Bench-201 search space | ||||
|     # this defination will be consistant with this line https://github.com/D-X-Y/AutoDL-Projects/blob/master/lib/models/cell_operations.py#L24 | ||||
|     # If a node has two input-edges from the same node, this function does not work. One edge will be overleaped. | ||||
|     NAS_BENCH_201         = ['none', 'skip_connect', 'nor_conv_1x1', 'nor_conv_3x3', 'avg_pool_3x3'] | ||||
|     nodestrs = xstr.split('+') | ||||
|     num_nodes = len(nodestrs) + 1 | ||||
|     matrix = np.zeros((num_nodes,num_nodes)) | ||||
|     for i, node_str in enumerate(nodestrs): | ||||
|   def str2matrix(arch_str: Text, | ||||
|                  search_space: List[Text] = ['none', 'skip_connect', 'nor_conv_1x1', 'nor_conv_3x3', 'avg_pool_3x3']) -> np.ndarray: | ||||
|     """ | ||||
|     This func shows how to convert the string-based architecture encoding to the encoding strategy in NAS-Bench-101. | ||||
|  | ||||
|     :param | ||||
|       arch_str: the input is a string indicates the architecture topology, such as | ||||
|                     |nor_conv_1x1~0|+|none~0|none~1|+|none~0|none~1|skip_connect~2| | ||||
|       search_space: a list of operation string, the default list is the search space for NAS-Bench-201 | ||||
|         the default value should be be consistent with this line https://github.com/D-X-Y/AutoDL-Projects/blob/master/lib/models/cell_operations.py#L24 | ||||
|     :return | ||||
|       the numpy matrix (2-D np.ndarray) representing the DAG of this architecture topology | ||||
|     :usage | ||||
|       matrix = api.str2matrix( '|nor_conv_1x1~0|+|none~0|none~1|+|none~0|none~1|skip_connect~2|' ) | ||||
|       This matrix is 4-by-4 matrix representing a cell with 4 nodes (only the lower left triangle is useful). | ||||
|          [ [0, 0, 0, 0],  # the first line represents the input (0-th) node | ||||
|            [2, 0, 0, 0],  # the second line represents the 1-st node, is calculated by 2-th-op( 0-th-node ) | ||||
|            [0, 0, 0, 0],  # the third line represents the 2-nd node, is calculated by 0-th-op( 0-th-node ) + 0-th-op( 1-th-node ) | ||||
|            [0, 0, 1, 0] ] # the fourth line represents the 3-rd node, is calculated by 0-th-op( 0-th-node ) + 0-th-op( 1-th-node ) + 1-th-op( 2-th-node ) | ||||
|       In NAS-Bench-201 search space, 0-th-op is 'none', 1-th-op is 'skip_connect', | ||||
|          2-th-op is 'nor_conv_1x1', 3-th-op is 'nor_conv_3x3', 4-th-op is 'avg_pool_3x3'. | ||||
|     :(NOTE) | ||||
|       If a node has two input-edges from the same node, this function does not work. One edge will be overlapped. | ||||
|     """ | ||||
|     node_strs = arch_str.split('+') | ||||
|     num_nodes = len(node_strs) + 1 | ||||
|     matrix = np.zeros((num_nodes, num_nodes)) | ||||
|     for i, node_str in enumerate(node_strs): | ||||
|       inputs = list(filter(lambda x: x != '', node_str.split('|'))) | ||||
|       for xinput in inputs: assert len(xinput.split('~')) == 2, 'invalid input length : {:}'.format(xinput) | ||||
|       for xi in inputs: | ||||
|         op, idx = xi.split('~') | ||||
|         if op not in NAS_BENCH_201: raise ValueError('this op ({:}) is not in {:}'.format(op, NAS_BENCH_201)) | ||||
|         op_idx, node_idx = NAS_BENCH_201.index(op), int(idx) | ||||
|         if op not in search_space: raise ValueError('this op ({:}) is not in {:}'.format(op, search_space)) | ||||
|         op_idx, node_idx = search_space.index(op), int(idx) | ||||
|         matrix[i+1, node_idx] = op_idx | ||||
|     return matrix | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| class ArchResults(object): | ||||
|  | ||||
|   def __init__(self, arch_index, arch_str): | ||||
| @@ -393,15 +426,15 @@ class ArchResults(object): | ||||
|     self.dataset_seed = dict() | ||||
|     self.clear_net_done = False | ||||
|  | ||||
|   def get_comput_costs(self, dataset): | ||||
|   def get_compute_costs(self, dataset): | ||||
|     x_seeds = self.dataset_seed[dataset] | ||||
|     results = [self.all_results[ (dataset, seed) ] for seed in x_seeds] | ||||
|  | ||||
|     flops      = [result.flop for result in results] | ||||
|     params     = [result.params for result in results] | ||||
|     lantencies = [result.get_latency() for result in results] | ||||
|     lantencies = [x for x in lantencies if x > 0] | ||||
|     mean_latency = np.mean(lantencies) if len(lantencies) > 0 else None | ||||
|     flops     = [result.flop for result in results] | ||||
|     params    = [result.params for result in results] | ||||
|     latencies = [result.get_latency() for result in results] | ||||
|     latencies = [x for x in latencies if x > 0] | ||||
|     mean_latency = np.mean(latencies) if len(latencies) > 0 else None | ||||
|     time_infos = defaultdict(list) | ||||
|     for result in results: | ||||
|       time_info = result.get_times() | ||||
| @@ -416,38 +449,38 @@ class ArchResults(object): | ||||
|       else: info[key] = None | ||||
|     return info | ||||
|  | ||||
|   """ | ||||
|   This `get_metrics` function is used to obtain obtain the loss, accuracy, etc information on a specific dataset. | ||||
|   If not specify, each set refer to the proposed split in NAS-Bench-201 paper. | ||||
|   If some args return None or raise error, then it is not avaliable. | ||||
|   ======================================== | ||||
|   Args [dataset] (4 possible options): | ||||
|     -- cifar10-valid : training the model on the CIFAR-10 training set. | ||||
|     -- cifar10 : training the model on the CIFAR-10 training + validation set. | ||||
|     -- cifar100 : training the model on the CIFAR-100 training set. | ||||
|     -- ImageNet16-120 : training the model on the ImageNet16-120 training set. | ||||
|   Args [setname] (each dataset has different setnames): | ||||
|     -- When dataset = cifar10-valid, you can use 'train', 'x-valid', 'ori-test' | ||||
|     ------ 'train' : the metric on the training set. | ||||
|     ------ 'x-valid' : the metric on the validation set. | ||||
|     ------ 'ori-test' : the metric on the test set. | ||||
|     -- When dataset = cifar10, you can use 'train', 'ori-test'. | ||||
|     ------ 'train' : the metric on the training + validation set. | ||||
|     ------ 'ori-test' : the metric on the test set. | ||||
|     -- When dataset = cifar100 or ImageNet16-120, you can use 'train', 'ori-test', 'x-valid', 'x-test' | ||||
|     ------ 'train' : the metric on the training set. | ||||
|     ------ 'x-valid' : the metric on the validation set. | ||||
|     ------ 'x-test' : the metric on the test set. | ||||
|     ------ 'ori-test' : the metric on the validation + test set. | ||||
|   Args [iepoch] (None or an integer in [0, the-number-of-total-training-epochs) | ||||
|     ------ None : return the metric after the last training epoch. | ||||
|     ------ an integer i : return the metric after the i-th training epoch. | ||||
|   Args [is_random]: | ||||
|     ------ True : return the metric of a randomly selected trial. | ||||
|     ------ False : return the averaged metric of all avaliable trials. | ||||
|     ------ an integer indicating the 'seed' value : return the metric of a specific trial (whose random seed is 'is_random'). | ||||
|   """ | ||||
|   def get_metrics(self, dataset, setname, iepoch=None, is_random=False): | ||||
|     """ | ||||
|       This `get_metrics` function is used to obtain obtain the loss, accuracy, etc information on a specific dataset. | ||||
|       If not specify, each set refer to the proposed split in NAS-Bench-201 paper. | ||||
|       If some args return None or raise error, then it is not avaliable. | ||||
|       ======================================== | ||||
|       Args [dataset] (4 possible options): | ||||
|         -- cifar10-valid : training the model on the CIFAR-10 training set. | ||||
|         -- cifar10 : training the model on the CIFAR-10 training + validation set. | ||||
|         -- cifar100 : training the model on the CIFAR-100 training set. | ||||
|         -- ImageNet16-120 : training the model on the ImageNet16-120 training set. | ||||
|       Args [setname] (each dataset has different setnames): | ||||
|         -- When dataset = cifar10-valid, you can use 'train', 'x-valid', 'ori-test' | ||||
|         ------ 'train' : the metric on the training set. | ||||
|         ------ 'x-valid' : the metric on the validation set. | ||||
|         ------ 'ori-test' : the metric on the test set. | ||||
|         -- When dataset = cifar10, you can use 'train', 'ori-test'. | ||||
|         ------ 'train' : the metric on the training + validation set. | ||||
|         ------ 'ori-test' : the metric on the test set. | ||||
|         -- When dataset = cifar100 or ImageNet16-120, you can use 'train', 'ori-test', 'x-valid', 'x-test' | ||||
|         ------ 'train' : the metric on the training set. | ||||
|         ------ 'x-valid' : the metric on the validation set. | ||||
|         ------ 'x-test' : the metric on the test set. | ||||
|         ------ 'ori-test' : the metric on the validation + test set. | ||||
|       Args [iepoch] (None or an integer in [0, the-number-of-total-training-epochs) | ||||
|         ------ None : return the metric after the last training epoch. | ||||
|         ------ an integer i : return the metric after the i-th training epoch. | ||||
|       Args [is_random]: | ||||
|         ------ True : return the metric of a randomly selected trial. | ||||
|         ------ False : return the averaged metric of all avaliable trials. | ||||
|         ------ an integer indicating the 'seed' value : return the metric of a specific trial (whose random seed is 'is_random'). | ||||
|     """ | ||||
|     x_seeds = self.dataset_seed[dataset] | ||||
|     results = [self.all_results[ (dataset, seed) ] for seed in x_seeds] | ||||
|     infos   = defaultdict(list) | ||||
| @@ -483,20 +516,55 @@ class ArchResults(object): | ||||
|   def get_dataset_seeds(self, dataset): | ||||
|     return copy.deepcopy( self.dataset_seed[dataset] ) | ||||
|  | ||||
|   """ | ||||
|   This function will return the trained network's weights on the 'dataset'. | ||||
|   When the 'seed' is None, it will return the weights for every run trial in the form of a dict. | ||||
|   When the  | ||||
|   """ | ||||
|   def get_net_param(self, dataset, seed=None): | ||||
|   def get_net_param(self, dataset: Text, seed: Union[None, int] =None): | ||||
|     """ | ||||
|     This function will return the trained network's weights on the 'dataset'. | ||||
|     :arg | ||||
|       dataset: one of 'cifar10-valid', 'cifar10', 'cifar100', and 'ImageNet16-120'. | ||||
|       seed: an integer indicates the seed value or None that indicates returing all trials. | ||||
|     """ | ||||
|     if seed is None: | ||||
|       x_seeds = self.dataset_seed[dataset] | ||||
|       return {seed: self.all_results[(dataset, seed)].get_net_param() for seed in x_seeds} | ||||
|     else: | ||||
|       return self.all_results[(dataset, seed)].get_net_param() | ||||
|  | ||||
|   # get the total number of training epochs | ||||
|   def reset_latency(self, dataset: Text, seed: Union[None, Text], latency: float) -> None: | ||||
|     """This function is used to reset the latency in all corresponding ResultsCount(s).""" | ||||
|     if seed is None: | ||||
|       for seed in self.dataset_seed[dataset]: | ||||
|         self.all_results[(dataset, seed)].update_latency([latency]) | ||||
|     else: | ||||
|       self.all_results[(dataset, seed)].update_latency([latency]) | ||||
|  | ||||
|   def reset_pseudo_train_times(self, dataset: Text, seed: Union[None, Text], estimated_per_epoch_time: float) -> None: | ||||
|     """This function is used to reset the train-times in all corresponding ResultsCount(s).""" | ||||
|     if seed is None: | ||||
|       for seed in self.dataset_seed[dataset]: | ||||
|         self.all_results[(dataset, seed)].reset_pseudo_train_times(estimated_per_epoch_time) | ||||
|     else: | ||||
|       self.all_results[(dataset, seed)].reset_pseudo_train_times(estimated_per_epoch_time) | ||||
|  | ||||
|   def reset_pseudo_eval_times(self, dataset: Text, seed: Union[None, Text], eval_name: Text, estimated_per_epoch_time: float) -> None: | ||||
|     """This function is used to reset the eval-times in all corresponding ResultsCount(s).""" | ||||
|     if seed is None: | ||||
|       for seed in self.dataset_seed[dataset]: | ||||
|         self.all_results[(dataset, seed)].reset_pseudo_eval_times(eval_name, estimated_per_epoch_time) | ||||
|     else: | ||||
|       self.all_results[(dataset, seed)].reset_pseudo_eval_times(eval_name, estimated_per_epoch_time) | ||||
|  | ||||
|   def get_latency(self, dataset: Text) -> float: | ||||
|     """Get the latency of a model on the target dataset. [Timestamp: 2020.03.09]""" | ||||
|     latencies = [] | ||||
|     for seed in self.dataset_seed[dataset]: | ||||
|       latency = self.all_results[(dataset, seed)].get_latency() | ||||
|       if not isinstance(latency, float) or latency <= 0: | ||||
|         raise ValueError('invalid latency of {:} for {:} with {:}'.format(dataset)) | ||||
|       latencies.append(latency) | ||||
|     return sum(latencies) / len(latencies) | ||||
|  | ||||
|   def get_total_epoch(self, dataset=None): | ||||
|     """Return the total number of training epochs.""" | ||||
|     if dataset is None: | ||||
|       epochss = [] | ||||
|       for xdata, x_seeds in self.dataset_seed.items(): | ||||
| @@ -509,13 +577,13 @@ class ArchResults(object): | ||||
|     if len(set(epochss)) > 1: raise ValueError('Each trial mush have the same number of training epochs : {:}'.format(epochss)) | ||||
|     return epochss[-1] | ||||
|  | ||||
|   # return the ResultsCount object (containing all information of a single trial) for 'dataset' and 'seed' | ||||
|   def query(self, dataset, seed=None): | ||||
|     """Return the ResultsCount object (containing all information of a single trial) for 'dataset' and 'seed'""" | ||||
|     if seed is None: | ||||
|       x_seeds = self.dataset_seed[dataset] | ||||
|       return {seed: self.all_results[ (dataset, seed) ] for seed in x_seeds} | ||||
|       return {seed: self.all_results[(dataset, seed)] for seed in x_seeds} | ||||
|     else: | ||||
|       return self.all_results[ (dataset, seed) ] | ||||
|       return self.all_results[(dataset, seed)] | ||||
|  | ||||
|   def arch_idx_str(self): | ||||
|     return '{:06d}'.format(self.arch_index) | ||||
| @@ -573,7 +641,18 @@ class ArchResults(object): | ||||
|   def clear_params(self): | ||||
|     for key, result in self.all_results.items(): | ||||
|       result.net_state_dict = None | ||||
|     self.clear_net_done = True  | ||||
|     self.clear_net_done = True | ||||
|  | ||||
|   def debug_test(self): | ||||
|     """This function is used for me to debug and test, which will call most methods.""" | ||||
|     all_dataset = ['cifar10-valid', 'cifar10', 'cifar100', 'ImageNet16-120'] | ||||
|     for dataset in all_dataset: | ||||
|       print('---->>>> {:}'.format(dataset)) | ||||
|       print('The latency on {:} is {:} s'.format(dataset, self.get_latency(dataset))) | ||||
|       for seed in self.dataset_seed[dataset]: | ||||
|         result = self.all_results[(dataset, seed)] | ||||
|         print('  ==>> result = {:}'.format(result)) | ||||
|         print('  ==>> cost = {:}'.format(result.get_times())) | ||||
|  | ||||
|   def __repr__(self): | ||||
|     return ('{name}(arch-index={index}, arch={arch}, {num} runs, clear={clear})'.format(name=self.__class__.__name__, index=self.arch_index, arch=self.arch_str, num=len(self.all_results), clear=self.clear_net_done)) | ||||
| @@ -603,12 +682,25 @@ class ResultsCount(object): | ||||
|     # evaluation results | ||||
|     self.reset_eval() | ||||
|  | ||||
|   def update_train_info(self, train_acc1es, train_acc5es, train_losses, train_times): | ||||
|   def update_train_info(self, train_acc1es, train_acc5es, train_losses, train_times) -> None: | ||||
|     self.train_acc1es = train_acc1es | ||||
|     self.train_acc5es = train_acc5es | ||||
|     self.train_losses = train_losses | ||||
|     self.train_times  = train_times | ||||
|  | ||||
|   def reset_pseudo_train_times(self, estimated_per_epoch_time: float) -> None: | ||||
|     """Assign the training times.""" | ||||
|     train_times = OrderedDict() | ||||
|     for i in range(self.epochs): | ||||
|       train_times[i] = estimated_per_epoch_time | ||||
|     self.train_times = train_times | ||||
|  | ||||
|   def reset_pseudo_eval_times(self, eval_name: Text, estimated_per_epoch_time: float) -> None: | ||||
|     """Assign the evaluation times.""" | ||||
|     if eval_name not in self.eval_names: raise ValueError('invalid eval name : {:}'.format(eval_name)) | ||||
|     for i in range(self.epochs): | ||||
|       self.eval_times['{:}@{:}'.format(eval_name,i)] = estimated_per_epoch_time | ||||
|  | ||||
|   def reset_eval(self): | ||||
|     self.eval_names  = [] | ||||
|     self.eval_acc1es = {} | ||||
| @@ -618,6 +710,11 @@ class ResultsCount(object): | ||||
|   def update_latency(self, latency): | ||||
|     self.latency = copy.deepcopy( latency ) | ||||
|  | ||||
|   def get_latency(self) -> float: | ||||
|     """Return the latency value in seconds. -1 represents not avaliable ; otherwise it should be a float value""" | ||||
|     if self.latency is None: return -1.0 | ||||
|     else: return sum(self.latency) / len(self.latency) | ||||
|  | ||||
|   def update_eval(self, accs, losses, times):  # new version | ||||
|     data_names = set([x.split('@')[0] for x in accs.keys()]) | ||||
|     for data_name in data_names: | ||||
| @@ -642,28 +739,22 @@ class ResultsCount(object): | ||||
|     set_name = '[' + ', '.join(self.eval_names) + ']' | ||||
|     return ('{name}({xname}, arch={arch}, FLOP={flop:.2f}M, Param={param:.3f}MB, seed={seed}, {num_eval} eval-sets: {set_name})'.format(name=self.__class__.__name__, xname=self.name, arch=self.arch_config['arch_str'], flop=self.flop, param=self.params, seed=self.seed, num_eval=num_eval, set_name=set_name)) | ||||
|  | ||||
|   # get the total number of training epochs | ||||
|   def get_total_epoch(self): | ||||
|     return copy.deepcopy(self.epochs) | ||||
|    | ||||
|   # get the latency | ||||
|   # -1 represents not avaliable ; otherwise it should be a float value | ||||
|   def get_latency(self): | ||||
|     if self.latency is None: return -1 | ||||
|     else: return sum(self.latency) / len(self.latency) | ||||
|  | ||||
|   # get the information regarding time | ||||
|   def get_times(self): | ||||
|     """Obtain the information regarding both training and evaluation time.""" | ||||
|     if self.train_times is not None and isinstance(self.train_times, dict): | ||||
|       train_times = list( self.train_times.values() ) | ||||
|       time_info = {'T-train@epoch': np.mean(train_times), 'T-train@total': np.sum(train_times)} | ||||
|       for name in self.eval_names: | ||||
|     else: | ||||
|       time_info = {'T-train@epoch':                 None, 'T-train@total':               None } | ||||
|     for name in self.eval_names: | ||||
|       try: | ||||
|         xtimes = [self.eval_times['{:}@{:}'.format(name,i)] for i in range(self.epochs)] | ||||
|         time_info['T-{:}@epoch'.format(name)] = np.mean(xtimes) | ||||
|         time_info['T-{:}@total'.format(name)] = np.sum(xtimes) | ||||
|     else: | ||||
|       time_info = {'T-train@epoch':                 None, 'T-train@total':               None } | ||||
|       for name in self.eval_names: | ||||
|       except: | ||||
|         time_info['T-{:}@epoch'.format(name)] = None | ||||
|         time_info['T-{:}@total'.format(name)] = None | ||||
|     return time_info | ||||
| @@ -699,18 +790,19 @@ class ResultsCount(object): | ||||
|             'cur_time': xtime, | ||||
|             'all_time': atime} | ||||
|  | ||||
|   def get_net_param(self): | ||||
|     return self.net_state_dict | ||||
|   def get_net_param(self, clone=False): | ||||
|     if clone: return copy.deepcopy(self.net_state_dict) | ||||
|     else: return self.net_state_dict | ||||
|  | ||||
|   # This function is used to obtain the config dict for this architecture. | ||||
|   def get_config(self, str2structure): | ||||
|     if str2structure is None: | ||||
|       return {'name': 'infer.tiny', 'C': self.arch_config['channel'], \ | ||||
|               'N'   : self.arch_config['num_cells'], \ | ||||
|       return {'name': 'infer.tiny', 'C': self.arch_config['channel'], | ||||
|               'N'   : self.arch_config['num_cells'], | ||||
|               'arch_str': self.arch_config['arch_str'], 'num_classes': self.arch_config['class_num']} | ||||
|     else: | ||||
|       return {'name': 'infer.tiny', 'C': self.arch_config['channel'], \ | ||||
|               'N'   : self.arch_config['num_cells'], \ | ||||
|       return {'name': 'infer.tiny', 'C': self.arch_config['channel'], | ||||
|               'N'   : self.arch_config['num_cells'], | ||||
|               'genotype': str2structure(self.arch_config['arch_str']), 'num_classes': self.arch_config['class_num']} | ||||
|  | ||||
|   def state_dict(self): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user