Beispiel #1
0
def _set_startup(args):
    if args.startup in ['benchmark', 'b']:
        cfg = Config(args.config_file)
        config = deepcopy(cfg)
        if 'benchmark' in cfg.keys():
            benchmark_config = cfg.pop('benchmark')
            config = update_dict(benchmark_config, cfg)
    else:
        config = Config(args.config_file)
    return config
Beispiel #2
0
 def _init_hps(self, hps=None):
     """Load hps from file."""
     if hps is not None:
         self.hps = hps
     elif self.config.hps_file is not None:
         desc_file = self.config.hps_file.replace("{local_base_path}", self.local_base_path)
         self.hps = Config(desc_file)
     elif self.config.hps_folder is not None:
         folder = self.config.hps_folder.replace("{local_base_path}", self.local_base_path)
         pattern = FileOps.join_path(folder, "desc_*.json")
         desc_file = glob.glob(pattern)[0]
         self.hps = Config(desc_file)
     if self.hps and self.hps.get('trainer'):
         self.config.from_json(self.hps.get('trainer'))
         self.epochs = self.config.epochs
Beispiel #3
0
def _set_config(args, step_name, step_type):
    """Fully train."""
    # general
    General.step_name = step_name
    if hasattr(args, "general"):
        General.from_json(args.general)
    # pipeline
    PipelineConfig.steps = [step_name]
    # pipestep
    PipeStepConfig.type = step_type
    # model
    if hasattr(args, "model"):
        if hasattr(args.model, "model_desc"):
            args.model.model_desc = Config(args.model.model_desc)
        PipeStepConfig.model.from_json(args.model)
    # dataset
    if hasattr(args, "dataset"):
        PipeStepConfig.dataset.from_json(args.dataset)
    # trainer
    if hasattr(args, "trainer"):
        TrainerConfig.from_json(args.trainer)
    # evaluator
    if hasattr(args, "evaluator"):
        # PipeStepConfig.evaluator._type_name = args.evaluator
        if "GpuEvaluator" in args.evaluator:
            PipeStepConfig.evaluator_enable = True
            PipeStepConfig.evaluator.gpu_evaluator_enable = True
        if "DavinciMobileEvaluator" in args.evaluator:
            PipeStepConfig.evaluator_enable = True
            PipeStepConfig.evaluator.davinci_mobile_evaluator_enable = True
Beispiel #4
0
def _parse_args(sections, desc):
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("-backend", "--general.backend", default="pytorch", type=str,
                        help="pytorch|tensorflow|mindspore")
    if "cluster" in sections:
        parser.add_argument("-devices_per_trainer", "--general.worker.devices_per_trainer", default=None, type=int)
        parser.add_argument("-master_ip", "--general.cluster.master_ip", default=None, type=str)
        parser.add_argument("-listen_port", "--general.cluster.listen_port", default=8000, type=int)
        parser.add_argument("-slaves", "--general.cluster.slaves", default=[],
                            action='store', dest='general.cluster.slaves', type=str, nargs='*',
                            help="slave IP list")
    parser.add_argument("-dataset", "--dataset.type", required=True, type=str, help="dataset name.")
    parser.add_argument("-data_path", "--dataset.common.data_path", type=str, help="dataset path.")
    parser.add_argument("-batch_size", "--dataset.common.batch_size", default=256, type=int)
    if "model" in sections:
        parser.add_argument("-model_desc", "--model.model_desc", type=str)
        parser.add_argument("-model_file", "--model.pretrained_model_file", type=str)
    if "trainer" in sections:
        parser.add_argument("-epochs", "--trainer.epochs", type=int)
    if "fine_tune" in sections:
        parser.add_argument("-task_type", "--task_type", default="classification", type=str,
                            help="classification|detection|segmentation|super_resolution")
        parser.add_argument("-num_classes", "--trainer.num_classes", type=int)
    parser.add_argument("-evaluator", "--evaluator", default=[],
                        action='store', dest='evaluator', type=str, nargs='*',
                        help="evaluator list, eg. -evaluator GpuEvaluator DavinciMobileEvaluator")
    args = vars(parser.parse_args())
    args = {key: value for key, value in args.items() if args[key]}
    tree = Config(build_tree(args))
    return tree
Beispiel #5
0
    def __init__(self, **cfg):
        """Initialize method."""
        cfg = Config(cfg)
        self.use_cuda = cfg.use_cuda
        self.use_distributed = cfg.use_distributed
        self.SR_lr = cfg.SR_lr
        self.cyc_lr = cfg.cyc_lr
        super(CycleSRModel, self).__init__(cfg)
        self.max_norm = cfg.grad_clip
        self.loss_names.append("G_SR")
        self.loss_names.append("SR")
        self.loss_SR = 0
        self.loss_G_SR = 0
        self.SR_lam = cfg.SR_lam
        self.cycleSR_lam = cfg.cycleSR_lam
        logging.info("Now we are using CycleGan with SR")

        self.G_SR = None
        self.HR = None
        self.LR = None
        self.SR = None
        # add model names
        self.model_names.append("SR")
        self.netSR = define_SR(cfg.VDSR, self.use_cuda, self.use_distributed)
        self.criterionSR = torch.nn.MSELoss().cuda()
        # initialize optimizers
        self.optimizer_SR = torch.optim.Adam(self.netSR.parameters(), lr=cfg.SR_lr, betas=(0.5, 0.999))
Beispiel #6
0
 def __init__(self):
     self.name = ''
     self.data_format = General.data_format
     self._modules = Config()
     self._parameters = OrderedDict()
     self._weights_buffer = OrderedDict()
     self._init_configs()
Beispiel #7
0
 def __init__(self, hps=None, mode='train', **kwargs):
     """Construct method."""
     super(Dataset, self).__init__()
     self.args = dict()
     self.mode = mode
     if mode == "val" and not hasattr(self.config, "val") and not hasattr(self.config.common,
                                                                          "train_portion"):
         self.mode = "test"
     # modify config from kwargs, `Cifar10(mode='test', data_path='/cache/datasets')`
     if kwargs:
         self.args = Config(kwargs)
     if hasattr(self, 'config'):
         config = getattr(self.config, self.mode)
         config.from_dict(self.args)
         self.args = config().to_dict()
     self._init_hps(hps)
     self.train = self.mode in ["train", "val"]
     transforms_list = self._init_transforms()
     self._transforms = Transforms(transforms_list)
     # if "transforms" in kwargs.keys():
     #     self._transforms.__transform__ = kwargs["transforms"]
     self.dataset_init()
     self.world_size = 1
     self.rank = 0
     self.collate_fn = None
Beispiel #8
0
 def __init__(self):
     self.parent_scope_name = ''
     self._scope_name = ''
     self._modules = Config()
     self._training = True
     self.enable_scope_name = enable_scope_name
     self.data_format = General.data_format
Beispiel #9
0
 def __init__(self, search_space=None, **kwargs):
     super(SpNasCodec, self).__init__(search_space, **kwargs)
     config_template_file = search_space.config_template_file
     assert config_template_file is not None
     self.config_template = Config(config_template_file)
     if 'epoch' in search_space.keys():
         self.config_template['total_epochs'] = search_space.epoch
Beispiel #10
0
 def __init__(self):
     self.parent_scope_name = ''
     self._scope_name = ''
     self._modules = Config()
     self._training = True
     self.enable_scope_name = enable_scope_name
     self.data_format = General.data_format
     self.pretrained_model_file = None
     self._is_load_pretrained = False
     self.load_pretrained_type = None
     self._trainable = True
     self.pretrained_prefix = None
Beispiel #11
0
 def __init__(self, config=None):
     """Initialize."""
     self.is_multi_opt = False
     if config is not None:
         self.config = Config(config)
     raw_config = self.config.to_dict()
     raw_config.type = self.config.type
     map_dict = OptimMappingDict
     self.map_config = ConfigBackendMapping(
         map_dict.type_mapping_dict,
         map_dict.params_mapping_dict).backend_mapping(raw_config)
     self.optim_cls = ClassFactory.get_cls(ClassType.OPTIMIZER,
                                           self.map_config.type)
Beispiel #12
0
 def __init__(self, config=None):
     """Initialize."""
     # register pytorch optim as default
     if config:
         self.config = Config(config)
         raw_config = deepcopy(self.config)
     else:
         self.config = LrScheduler.config
         raw_config = self.config.to_dict()
     raw_config.type = self.config.type
     map_dict = LrSchedulerMappingDict()
     self.map_config = ConfigBackendMapping(
         map_dict.type_mapping_dict, map_dict.params_mapping_dict).backend_mapping(raw_config)
     self._cls = ClassFactory.get_cls(ClassType.LR_SCHEDULER, self.map_config.type)
Beispiel #13
0
 def __init__(self, **desc):
     """Initialize."""
     super(SimpleCnn, self).__init__()
     desc = Config(**desc)
     self.num_class = desc.num_class
     self.fp16 = desc.get('fp16', False)
     self.channels = desc.channels
     self.conv1 = ops.Conv2d(3, 32, padding=1, kernel_size=3)
     self.pool1 = ops.MaxPool2d(2, stride=2)
     self.blocks = self._blocks(self.channels, desc.blocks)
     self.pool2 = ops.MaxPool2d(2, stride=2)
     self.conv2 = ops.Conv2d(self.channels, 64, padding=1, kernel_size=3)
     self.global_conv = ops.Conv2d(64, 64, kernel_size=8)
     self.view = ops.View()
     self.fc = ops.Linear(64, self.num_class)
Beispiel #14
0
 def _init_hps(self, hps=None):
     """Load hps from file."""
     # load config
     if hps is not None:
         pass
     elif self.config.hps_file is not None:
         desc_file = self.config.hps_file.replace("{local_base_path}",
                                                  self.local_base_path)
         hps = Config(desc_file)
         if "trainer" in hps:
             if "epochs" in hps["trainer"]:
                 hps["trainer"].pop("epochs")
             if "checkpoint_path" in hps["trainer"]:
                 hps["trainer"].pop("checkpoint_path")
     elif self.config.hps_folder is not None:
         folder = self.config.hps_folder.replace("{local_base_path}",
                                                 self.local_base_path)
         pattern = FileOps.join_path(folder, "hps_*.json")
         desc_file = glob.glob(pattern)[0]
         hps = Config(desc_file)
         if "trainer" in hps:
             if "epochs" in hps["trainer"]:
                 hps["trainer"].pop("epochs")
             if "checkpoint_path" in hps["trainer"]:
                 hps["trainer"].pop("checkpoint_path")
     # merge config
     if not self.hps:
         self.hps = hps
     elif hps:
         hps.from_dict(self.hps)
         self.hps = hps
     # set config
     if self.hps and self.hps.get('trainer'):
         self.config.from_dict(self.hps.get('trainer'))
         self.load_checkpoint = self.config.load_checkpoint
     self.epochs = self.config.epochs
Beispiel #15
0
 def _create_examples(self, lines, set_type):
     """Create examples for the training, dev and test sets."""
     examples = []
     for (i, line) in enumerate(lines):
         if i == 0:
             continue
         guid = "%s-%s" % (set_type, i)
         text_a = line[3]
         text_b = line[4]
         label = None if set_type == "test" else line[0]
         examples.append(
             Config(
                 dict(guid=guid, text_a=text_a, text_b=text_b,
                      label=label)))
     return examples
Beispiel #16
0
 def __new__(cls, *args, **kwargs):
     """Record params."""
     desc = {}
     params_sig = sig(cls.__init__).parameters
     param_names = list(params_sig.keys())
     if len(param_names) > len(args):
         # not dynamic parameter for connections
         for idx, arg in enumerate(args):
             arg_name = param_names[idx + 1]
             desc[arg_name] = arg
     if kwargs:
         desc.update(kwargs)
     instance = super(Serializable, cls).__new__(cls)
     instance._deep_level = 0
     instance._target_level = None
     instance.desc = Config(desc)
     return instance
    def decode(self, desc):
        """Decode hps: `trainer.optim.lr : 0.1` to dict format.

        And convert to `zeus.common.config import Config` object
        This Config will be override in Trainer or Datasets class
        The override priority is: input hps > user configuration >  default configuration
        :param hps: hyper params
        :return: dict
        """
        hps_dict = {}
        if desc is None:
            return None
        if isinstance(desc, tuple):
            return desc
        for hp_name, value in desc.items():
            hp_dict = {}
            for key in list(reversed(hp_name.split('.'))):
                if hp_dict:
                    hp_dict = {key: hp_dict}
                else:
                    hp_dict = {key: value}
            # update cfg with hps
            hps_dict = update_dict(hps_dict, hp_dict, [])
        return Config(hps_dict)
Beispiel #18
0
 def __init__(self):
     super(Module, self).__init__()
     self.children_ms = []
     self._modules = Config()
Beispiel #19
0
 def _init_hps(self, hps):
     """Convert trainer values in hps to cfg."""
     if hps is not None:
         self.args = Config(update_dict(hps, self.args))
Beispiel #20
0
 def __init__(self, config=None):
     """Initialize."""
     self.is_multi_opt = True
     if config is not None:
         self.config = Config(config)
     self._opts = OrderedDict()
Beispiel #21
0
    def convert_examples_to_features(self, examples, label_list,
                                     max_seq_length, tokenizer):
        """Load a data file into a list of `InputBatch`s."""
        label_map = {label: i for i, label in enumerate(label_list)}
        features = []
        for (ex_index, example) in enumerate(examples):
            tokens_a = tokenizer.tokenize(example.text_a)

            tokens_b = None
            if example.text_b:
                tokens_b = tokenizer.tokenize(example.text_b)
                # Modifies `tokens_a` and `tokens_b` in place so that the total
                # length is less than the specified length.
                # Account for [CLS], [SEP], [SEP] with "- 3"
                _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)
            else:
                # Account for [CLS] and [SEP] with "- 2"
                if len(tokens_a) > max_seq_length - 2:
                    tokens_a = tokens_a[:(max_seq_length - 2)]

            # The convention in BERT is:
            # (a) For sequence pairs:
            #  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
            #  type_ids: 0   0  0    0    0     0       0 0    1  1  1  1   1 1
            # (b) For single sequences:
            #  tokens:   [CLS] the dog is hairy . [SEP]
            #  type_ids: 0   0   0   0  0     0 0
            #
            # Where "type_ids" are used to indicate whether this is the first
            # sequence or the second sequence. The embedding vectors for `type=0` and
            # `type=1` were learned during pre-training and are added to the wordpiece
            # embedding vector (and position vector). This is not *strictly* necessary
            # since the [SEP] token unambigiously separates the sequences, but it makes
            # it easier for the model to learn the concept of sequences.
            #
            # For classification tasks, the first vector (corresponding to [CLS]) is
            # used as as the "sentence vector". Note that this only makes sense because
            # the entire model is fine-tuned.
            tokens = ["[CLS]"] + tokens_a + ["[SEP]"]
            segment_ids = [0] * len(tokens)

            if tokens_b:
                tokens += tokens_b + ["[SEP]"]
                segment_ids += [1] * (len(tokens_b) + 1)

            input_ids = tokenizer.convert_tokens_to_ids(tokens)

            # The mask has 1 for real tokens and 0 for padding tokens. Only real
            # tokens are attended to.
            input_mask = [1] * len(input_ids)

            # Zero-pad up to the sequence length.
            padding = [0] * (max_seq_length - len(input_ids))
            input_ids += padding
            input_mask += padding
            segment_ids += padding

            assert len(input_ids) == max_seq_length
            assert len(input_mask) == max_seq_length
            assert len(segment_ids) == max_seq_length

            label_id = label_map[example.label]
            if ex_index < 5:
                logging.info("*** Example ***")
                logging.info("guid: %s" % (example.guid))
                logging.info("tokens: %s" % " ".join([str(x) for x in tokens]))
                logging.info("input_ids: %s" %
                             " ".join([str(x) for x in input_ids]))
                logging.info("input_mask: %s" %
                             " ".join([str(x) for x in input_mask]))
                logging.info("segment_ids: %s" %
                             " ".join([str(x) for x in segment_ids]))
                logging.info("label: %s (id = %d)" % (example.label, label_id))

            features.append(
                Config(
                    dict(input_ids=input_ids,
                         input_mask=input_mask,
                         segment_ids=segment_ids,
                         label_id=label_id)))
        return features