예제 #1
0
    def get_model(cls, network_desc=None, pretrained=False):
        """Get model from model zoo.

        :param network_name: the name of network, eg. ResNetVariant.
        :type network_name: str or None.
        :param network_desc: the description of network.
        :type network_desc: str or None.
        :param pretrained: pre-trained model or not.
        :type pretrained: bool.
        :return: model.
        :rtype: model.

        """
        try:
            network = NetworkDesc(network_desc)
            model = network.to_model()
        except Exception as e:
            logging.error(
                "Failed to get model, network_desc={}, msg={}".format(
                    network_desc, str(e)))
            raise e
        logging.info("Model was created, model_desc={}".format(network_desc))
        if pretrained is True:
            logging.info("Load pretrained model.")
            model = cls._load_pretrained_model(network, model)
            logging.info("Pretrained model was loaded.")
        return model
예제 #2
0
파일: trainer.py 프로젝트: zhwzhong/vega
 def _init_model(self, model=None):
     """Load model desc from save path and parse to model."""
     if model is not None:
         return model
     model_cfg = ClassFactory.__configs__.get('model')
     if 'model_desc_file' in model_cfg and model_cfg.model_desc_file is not None:
         desc_file = model_cfg.model_desc_file.replace(
             "{model_zoo}", self.model_zoo_path)
         desc_file = desc_file.replace("{local_base_path}",
                                       self.local_base_path)
         if ":" not in desc_file:
             desc_file = os.path.abspath(desc_file)
         if ":" in desc_file:
             local_desc_file = FileOps.join_path(
                 self.local_output_path, os.path.basename(desc_file))
             FileOps.copy_file(desc_file, local_desc_file)
             desc_file = local_desc_file
         if self.horovod:
             hvd.join()
         model_desc = Config(desc_file)
         logging.info("net_desc:{}".format(model_desc))
     elif 'model_desc' in model_cfg and model_cfg.model_desc is not None:
         model_desc = model_cfg.model_desc
     else:
         return None
     if model_desc is not None:
         self.model_desc = model_desc
         net_desc = NetworkDesc(model_desc)
         model = net_desc.to_model()
         return model
     else:
         return None
예제 #3
0
파일: model_zoo.py 프로젝트: zeyefkey/vega
    def get_model(cls, model_desc=None, model_checkpoint=None):
        """Get model from model zoo.

        :param network_name: the name of network, eg. ResNetVariant.
        :type network_name: str or None.
        :param network_desc: the description of network.
        :type network_desc: str or None.
        :param model_checkpoint: path of model.
        :type model_checkpoint: str.
        :return: model.
        :rtype: model.

        """
        try:
            network = NetworkDesc(model_desc)
            model = network.to_model()
        except Exception as e:
            logging.error("Failed to get model, model_desc={}, msg={}".format(
                model_desc, str(e)))
            raise e
        logging.info("Model was created.")
        logging.debug("model_desc={}".format(model_desc))
        if model_checkpoint is not None:
            logging.info("Load model with weight.")
            model = cls._load_pretrained_model(network, model, model_checkpoint)
            logging.info("Model was loaded.")
        return model
예제 #4
0
 def new_model(self):
     """Build new model."""
     net_desc = NetworkDesc(self.search_space)
     model_new = net_desc.to_model().cuda()
     for x, y in zip(model_new.arch_parameters(),
                     self.model.arch_parameters()):
         x.detach().copy_(y.detach())
     return model_new
예제 #5
0
    def _init_model(self):
        """Initialize the model architecture for full train step.

        :return: train model
        :rtype: class
        """
        search_space = Config({"search_space": self.model_desc})
        self.codec = Codec(self.cfg.codec, search_space)
        self.get_selected_arch()
        indiv_cfg = self.codec.decode(self.elitism)
        self.trainer.model_desc = self.elitism.active_net_list()
        # self.output_model_desc()
        net_desc = NetworkDesc(indiv_cfg)
        model = net_desc.to_model()
        return model
예제 #6
0
    def _init_model(self):
        """Initialize the model architecture for full train step.

        :return: train model
        :rtype: class
        """
        logging.info('Initializing model')
        if 'model_desc' in self.cfg and self.cfg.model_desc is not None:
            if self.horovod:
                hvd.join()
            model_desc = self.cfg.model_desc
            self.model_desc = self.cfg.model_desc
            net_desc = NetworkDesc(model_desc)
            model = net_desc.to_model()
            return model
        else:
            return None
예제 #7
0
    def _init_model(self):
        """Initialize the model architecture for full train step.

        :return: train model
        :rtype: class
        """
        model_cfg = ClassFactory.__configs__.get('model')
        if 'model_desc' in model_cfg and model_cfg.model_desc is not None:
            model_desc = model_cfg.model_desc
        else:
            raise ValueError('Model_desc is None for evaluator')
        search_space = Config({"search_space": model_desc})
        self.codec = Codec(self.cfg.codec, search_space)
        self._get_selected_arch()
        indiv_cfg = self.codec.decode(self.elitism)
        logger.info('Model arch:{}'.format(self.elitism.active_net_list()))
        self.model_desc = self.elitism.active_net_list()
        net_desc = NetworkDesc(indiv_cfg)
        model = net_desc.to_model()
        return model
예제 #8
0
    def _init_model(self):
        """Initialize the model architecture for full train step.

        :return: train model
        :rtype: class
        """
        logging.info('Initializing model')
        if self.cfg.model_desc:
            logging.debug("model_desc: {}".format(self.cfg.model_desc))
            _file = FileOps.join_path(
                self.worker_path, "model_desc_{}.json".format(self._worker_id))
            with open(_file, "w") as f:
                json.dump(self.cfg.model_desc, f)
            if self.cfg.distributed:
                hvd.join()
            model_desc = self.cfg.model_desc
            net_desc = NetworkDesc(model_desc)
            model = net_desc.to_model()
            return model
        else:
            return None
예제 #9
0
 def _init_model(self, model=None):
     """Load model desc from save path and parse to model."""
     if model is not None:
         if vega.is_torch_backend() and self.use_cuda:
             model = model.cuda()
         return model
     model_cfg = Config(ClassFactory.__configs__.get('model'))
     if "model_desc_file" in model_cfg and model_cfg.model_desc_file is not None:
         desc_file = model_cfg.model_desc_file
         desc_file = desc_file.replace("{local_base_path}",
                                       self.local_base_path)
         if ":" not in desc_file:
             desc_file = os.path.abspath(desc_file)
         if ":" in desc_file:
             local_desc_file = FileOps.join_path(
                 self.local_output_path, os.path.basename(desc_file))
             FileOps.copy_file(desc_file, local_desc_file)
             desc_file = local_desc_file
         model_desc = Config(desc_file)
         logging.info("net_desc:{}".format(model_desc))
     elif "model_desc" in model_cfg and model_cfg.model_desc is not None:
         model_desc = model_cfg.model_desc
     elif "models_folder" in model_cfg and model_cfg.models_folder is not None:
         folder = model_cfg.models_folder.replace("{local_base_path}",
                                                  self.local_base_path)
         pattern = FileOps.join_path(folder, "desc_*.json")
         desc_file = glob.glob(pattern)[0]
         model_desc = Config(desc_file)
     else:
         return None
     if model_desc is not None:
         self.model_desc = model_desc
         net_desc = NetworkDesc(model_desc)
         model = net_desc.to_model()
         if vega.is_torch_backend() and self.use_cuda:
             model = model.cuda()
         return model
     else:
         return None