def load_pretrained_weights(self, pretrained_path=''): model_dict = self.state_dict() if not os.path.exists(pretrained_path): print(f'\nFile "{pretrained_path}" does not exist.') print('You need to specify the correct path to the pre-trained weights.\n' 'You can download the weights for HRNet from the repository:\n' 'https://github.com/HRNet/HRNet-Image-Classification') exit(1) pretrained_dict = paddle.load(pretrained_path) pretrained_dict = {k.replace('last_layer', 'aux_head').replace('model.', ''): v for k, v in pretrained_dict.items()} pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict.keys()} model_dict.update(pretrained_dict) self.set_state_dict(model_dict)
def __init__(self): super(Photo2Cartoon, self).__init__() # 加载人脸关键点检测模型 self.face_detector = hub.Module(name="face_landmark_localization") # 加载人脸分割模型 self.seg = hub.Module(name='FCN_HRNet_W18_Face_Seg') # 加载人脸动漫化模型 self.net = ResnetGenerator(ngf=32, img_size=256, light=True) # 加载人脸动漫化模型参数 state_dict = paddle.load( os.path.join(self.directory, 'photo2cartoon_weights.pdparams')) self.net.set_state_dict(state_dict['genA2B']) # 将人脸动漫化模型设为评估模式 self.net.eval()
def init_params(self, params_path: str): if params_path.endswith('.npz'): logger.info('Load as AlphaFold pre-trained model') with open(params_path, 'rb') as f: params = np.load(io.BytesIO(f.read()), allow_pickle=False) params = dict(params) pd_params = utils.jax_params_to_paddle(params) pd_params = {k[len('alphafold.'):]: v for k, v in pd_params.items()} elif params_path.endswith('.pd'): logger.info('Load as Paddle model') pd_params = paddle.load(params_path) else: raise ValueError('Unsupported params file type') self.alphafold.set_state_dict(pd_params)
def animate(image_path, output_path): genA2B, faceseg = ResnetUGATITP2CGenerator(), FaceSeg() genA2B.set_state_dict(load(__file__.replace('py', 'bin'))) genA2B.eval() face_image = align_crop(open(image_path)) face_mask = res(faceseg(face_image), (256, 256))[:, :, newaxis] / 255. face = to_tensor( transpose(((resize(face_image, (256, 256), interpolation=3) * face_mask + (1 - face_mask) * 255) / 127.5 - 1)[newaxis, :, :, :], (0, 3, 1, 2)).astype(float32)) with no_grad(): cartoon = genA2B(face)[0][0] imwrite( output_path, cvtColor(((transpose(cartoon.numpy(), (1, 2, 0)) + 1) * 127.5 * face_mask + (1 - face_mask) * 255).astype(uint8), 4))
def load_checkpoints(self, config, checkpoint_path): generator = OcclusionAwareGenerator( **config['model_params']['generator_params'], **config['model_params']['common_params']) kp_detector = KPDetector(**config['model_params']['kp_detector_params'], **config['model_params']['common_params']) checkpoint = paddle.load(self.weight_path) generator.set_state_dict(checkpoint['generator']) kp_detector.set_state_dict(checkpoint['kp_detector']) generator.eval() kp_detector.eval() return generator, kp_detector
def _construct_model(self, model): """ Construct the inference model for the predictor. """ model_instance = BiAffineParser( encoding_model=self.encoding_model, n_rels=len(self.rel_vocab), n_words=len(self.word_vocab), pad_index=self.word_pad_index, bos_index=self.word_bos_index, eos_index=self.word_eos_index, ) # Load the model parameter for the predict state_dict = paddle.load( os.path.join(self._task_path, self.model, "model.pdparams")) model_instance.set_dict(state_dict) model_instance.eval() self._model = model_instance
def __init__( self, max_length=256, max_out_len=256, ): super(STTransformer, self).__init__() bpe_codes_fpath = os.path.join(MODULE_HOME, "transformer_nist_wait_all", "assets", "2M.zh2en.dict4bpe.zh") src_vocab_fpath = os.path.join(MODULE_HOME, "transformer_nist_wait_all", "assets", "nist.20k.zh.vocab") trg_vocab_fpath = os.path.join(MODULE_HOME, "transformer_nist_wait_all", "assets", "nist.10k.en.vocab") params_fpath = os.path.join(MODULE_HOME, "transformer_nist_wait_all", "assets", "transformer.pdparams") self.max_length = max_length self.max_out_len = max_out_len self.tokenizer = STACLTokenizer( bpe_codes_fpath, src_vocab_fpath, trg_vocab_fpath, ) src_vocab_size = self.tokenizer.src_vocab_size trg_vocab_size = self.tokenizer.trg_vocab_size self.transformer = SimultaneousTransformer( src_vocab_size, trg_vocab_size, max_length=self.max_length, n_layer=self.model_config['n_layer'], n_head=self.model_config['n_head'], d_model=self.model_config['d_model'], ) model_dict = paddle.load(params_fpath) # To avoid a longer length than training, reset the size of position # encoding to max_length model_dict[ "src_pos_embedding.pos_encoder.weight"] = position_encoding_init( self.max_length + 1, self.model_config['d_model']) model_dict[ "trg_pos_embedding.pos_encoder.weight"] = position_encoding_init( self.max_length + 1, self.model_config['d_model']) self.transformer.load_dict(model_dict)
def load_pretrain_weight(model, pretrain_weight, load_static_weights=False, weight_type='pretrain'): assert weight_type in ['pretrain', 'finetune'] if is_url(pretrain_weight): pretrain_weight = get_weights_path_dist(pretrain_weight) path = _strip_postfix(pretrain_weight) if not (os.path.isdir(path) or os.path.isfile(path) or os.path.exists(path + '.pdparams')): raise ValueError("Model pretrain path {} does not " "exists.".format(path)) model_dict = model.state_dict() if load_static_weights: pre_state_dict = paddle.static.load_program_state(path) param_state_dict = {} for key in model_dict.keys(): weight_name = model_dict[key].name if weight_name in pre_state_dict.keys(): logger.info('Load weight: {}, shape: {}'.format( weight_name, pre_state_dict[weight_name].shape)) param_state_dict[key] = pre_state_dict[weight_name] else: if 'backbone' in key: logger.info('Lack weight: {}, structure name: {}'.format( weight_name, key)) param_state_dict[key] = model_dict[key] model.set_dict(param_state_dict) return param_state_dict = paddle.load(path + '.pdparams') if weight_type == 'pretrain': model.backbone.set_dict(param_state_dict) else: ignore_set = set() for name, weight in model_dict.items(): if name in param_state_dict: if weight.shape != param_state_dict[name].shape: param_state_dict.pop(name, None) model.set_dict(param_state_dict) return
def ptq(self): start_time = time.time() self.set_vars() params_path = self.download_model(self.lenet_url, self.lenet_md5, "lenet") params_path += "/lenet_pretrained/lenet.pdparams" with fluid.dygraph.guard(): model = ImperativeLenet() model_state_dict = paddle.load(params_path) model.set_state_dict(model_state_dict) _logger.info("Test fp32 model") fp32_acc_top1 = self.model_test(model, self.test_batch_num, self.test_batch_size) self.qat.quantize(model) use_amp = True self.model_train(model, self.train_batch_num, self.train_batch_size, use_amp) _logger.info("Test int8 model") int8_acc_top1 = self.model_test(model, self.test_batch_num, self.test_batch_size, use_amp) _logger.info('fp32_acc_top1: %f, int8_acc_top1: %f' % (fp32_acc_top1, int8_acc_top1)) self.assertTrue(int8_acc_top1 > fp32_acc_top1 - 0.01, msg='fp32_acc_top1: %f, int8_acc_top1: %f' % (fp32_acc_top1, int8_acc_top1)) input_spec = [ paddle.static.InputSpec(shape=[None, 1, 28, 28], dtype='float32') ] paddle.jit.save(layer=model, path=self.save_path, input_spec=input_spec) print('Quantized model saved in {%s}' % self.save_path) end_time = time.time() print("total time: %ss" % (end_time - start_time))
def __init__(self, max_length: int = 256, max_out_len: int = 256, beam_size: int = 5): super(MTTransformer, self).__init__() bpe_codes_file = os.path.join(MODULE_HOME, 'transformer_en_de', 'assets', 'bpe.33708') vocab_file = os.path.join(MODULE_HOME, 'transformer_en_de', 'assets', 'vocab_all.bpe.33708') checkpoint = os.path.join(MODULE_HOME, 'transformer_en_de', 'assets', 'transformer.pdparams') self.max_length = max_length self.beam_size = beam_size self.tokenizer = MTTokenizer(bpe_codes_file=bpe_codes_file, lang_src=self.lang_config['source'], lang_trg=self.lang_config['target']) self.vocab = Vocab.load_vocabulary( filepath=vocab_file, unk_token=self.vocab_config['unk_token'], bos_token=self.vocab_config['bos_token'], eos_token=self.vocab_config['eos_token']) self.vocab_size = (len(self.vocab) + self.vocab_config['pad_factor'] - 1) \ // self.vocab_config['pad_factor'] * self.vocab_config['pad_factor'] self.transformer = InferTransformerModel( src_vocab_size=self.vocab_size, trg_vocab_size=self.vocab_size, bos_id=self.vocab_config['bos_id'], eos_id=self.vocab_config['eos_id'], max_length=self.max_length + 1, max_out_len=max_out_len, beam_size=self.beam_size, **self.model_config) state_dict = paddle.load(checkpoint) # To avoid a longer length than training, reset the size of position # encoding to max_length state_dict["encoder.pos_encoder.weight"] = position_encoding_init( self.max_length + 1, self.model_config['d_model']) state_dict["decoder.pos_encoder.weight"] = position_encoding_init( self.max_length + 1, self.model_config['d_model']) self.transformer.set_state_dict(state_dict)
def load_pretrain_weight(model, pretrain_weight): if is_url(pretrain_weight): pretrain_weight = get_weights_path_dist(pretrain_weight) path = _strip_postfix(pretrain_weight) if not (os.path.isdir(path) or os.path.isfile(path) or os.path.exists(path + '.pdparams')): raise ValueError("Model pretrain path `{}` does not exists. " "If you don't want to load pretrain model, " "please delete `pretrain_weights` field in " "config file.".format(path)) model_dict = model.state_dict() weights_path = path + '.pdparams' param_state_dict = paddle.load(weights_path) ignore_weights = set() # hack: fit for faster rcnn. Pretrain weights contain prefix of 'backbone' # while res5 module is located in bbox_head.head. Replace the prefix of # res5 with 'bbox_head.head' to load pretrain weights correctly. for k in list(param_state_dict.keys()): if 'backbone.res5' in k: new_k = k.replace('backbone', 'bbox_head.head') if new_k in model_dict.keys(): value = param_state_dict.pop(k) param_state_dict[new_k] = value for name, weight in param_state_dict.items(): if name in model_dict.keys(): if list(weight.shape) != list(model_dict[name].shape): logger.info( '{} not used, shape {} unmatched with {} in model.'.format( name, weight.shape, list(model_dict[name].shape))) ignore_weights.add(name) else: logger.info('Redundant weight {} and ignore it.'.format(name)) ignore_weights.add(name) for weight in ignore_weights: param_state_dict.pop(weight, None) model.set_dict(param_state_dict) logger.info('Finish loading model weights: {}'.format(weights_path))
def from_pretrained(cls, pretrain_dir_or_url, force_download=False, **kwargs): if not Path(pretrain_dir_or_url).exists( ) and pretrain_dir_or_url in cls.resource_map: url = cls.resource_map[pretrain_dir_or_url] log.info('get pretrain dir from %s' % url) pretrain_dir = _fetch_from_remote(url, force_download) else: log.info('pretrain dir %s not in %s, read from local' % (pretrain_dir_or_url, repr(cls.resource_map))) pretrain_dir = Path(pretrain_dir_or_url) if not pretrain_dir.exists(): raise ValueError('pretrain dir not found: %s' % pretrain_dir) state_dict_path = pretrain_dir / 'saved_weights.pdparams' config_path = pretrain_dir / 'ernie_config.json' if not config_path.exists(): raise ValueError('config path not found: %s' % config_path) name_prefix = kwargs.pop('name', None) cfg_dict = dict(json.loads(config_path.open().read()), **kwargs) model = cls(cfg_dict, name=name_prefix) log.info('loading pretrained model from %s' % pretrain_dir) #param_path = pretrain_dir / 'params' #if os.path.exists(param_path): # raise NotImplementedError() # log.debug('load pretrained weight from program state') # F.io.load_program_state(param_path) #buggy in dygraph.gurad, push paddle to fix if state_dict_path.exists(): m = P.load(state_dict_path) for k, v in model.state_dict().items(): if k not in m: log.warn('param:%s not set in pretrained model, skip' % k) m[k] = v # FIXME: no need to do this in the future model.set_state_dict(m) else: raise ValueError('weight file not found in pretrain dir: %s' % pretrain_dir) return model
def do_train(): paddle.set_device(args.device) rank = paddle.distributed.get_rank() if paddle.distributed.get_world_size() > 1: paddle.distributed.init_parallel_env() set_seed(args.seed) dev_ds = load_dataset(read_test, src_path=args.test_file, lazy=False) print(dev_ds[0]) pretrained_model = ppnlp.transformers.ErnieGramModel.from_pretrained( 'ernie-gram-zh') tokenizer = ppnlp.transformers.ErnieGramTokenizer.from_pretrained( 'ernie-gram-zh') trans_func_eval = partial( convert_example, tokenizer=tokenizer, max_seq_length=args.max_seq_length, phase="eval") batchify_fn_eval = lambda samples, fn=Tuple( Pad(axis=0, pad_val=tokenizer.pad_token_id, dtype="int64"), # pair_input Pad(axis=0, pad_val=tokenizer.pad_token_type_id, dtype="int64"), # pair_segment Stack(dtype="int64") # label ): [data for data in fn(samples)] dev_data_loader = create_dataloader( dev_ds, mode='dev', batch_size=args.batch_size, batchify_fn=batchify_fn_eval, trans_fn=trans_func_eval) model = PairwiseMatching(pretrained_model, margin=args.margin) if args.init_from_ckpt and os.path.isfile(args.init_from_ckpt): state_dict = paddle.load(args.init_from_ckpt) model.set_dict(state_dict) metric = paddle.metric.Auc() evaluate(model, metric, dev_data_loader, "dev")
def detr_resnet101_dc5(pretrained=False, num_classes=91, return_postprocessor=False): """ DETR-DC5 R101 with 6 encoder and 6 decoder layers. The last block of ResNet-101 has dilation to increase output resolution. Achieves 44.9/64.7 AP/AP50 on COCO val5k. """ model = _make_detr('resnet101', dilation=True, num_classes=num_classes) if pretrained: checkpoint = paddle.load( 'https://dl.fbaipublicfiles.com/detr/detr-r101-dc5-a2e86def.pdiparams' ) model.load_state_dict(checkpoint['model']) if return_postprocessor: return model, PostProcess() return model
def do_predict(): paddle.set_device(args.device) # Reads label_map. label_map_path = os.path.join(args.data_path, "predicate2id.json") if not (os.path.exists(label_map_path) and os.path.isfile(label_map_path)): sys.exit("{} dose not exists or is not a file.".format(label_map_path)) with open(label_map_path, 'r', encoding='utf8') as fp: label_map = json.load(fp) num_classes = (len(label_map.keys()) - 2) * 2 + 2 # Loads pretrained model ERNIE model = ErnieForTokenClassification.from_pretrained( "ernie-1.0", num_classes=num_classes) tokenizer = ErnieTokenizer.from_pretrained("ernie-1.0") criterion = BCELossForDuIE() # Loads dataset. test_dataset = DuIEDataset.from_file(args.predict_data_file, tokenizer, args.max_seq_length, True) collator = DataCollator() test_batch_sampler = paddle.io.BatchSampler(test_dataset, batch_size=args.batch_size, shuffle=False, drop_last=True) test_data_loader = DataLoader(dataset=test_dataset, batch_sampler=test_batch_sampler, collate_fn=collator, return_list=True) # Loads model parameters. if not (os.path.exists(args.init_checkpoint) and os.path.isfile(args.init_checkpoint)): sys.exit("wrong directory: init checkpoints {} not exist".format( args.init_checkpoint)) state_dict = paddle.load(args.init_checkpoint) model.set_dict(state_dict) # Does predictions. print("\n=====start predicting=====") evaluate(model, criterion, test_data_loader, args.predict_data_file, "predict") print("=====predicting complete=====")
def __init__( self, task: str = None, load_checkpoint: str = None, label_map: Dict = None, num_classes: int = 2, **kwargs, ): super(Bert, self).__init__() if label_map: self.label_map = label_map self.num_classes = len(label_map) else: self.num_classes = num_classes if task == 'sequence_classification': task = 'seq-cls' logger.warning( "current task name 'sequence_classification' was renamed to 'seq-cls', " "'sequence_classification' has been deprecated and will be removed in the future.", ) if task == 'seq-cls': self.model = BertForSequenceClassification.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', num_classes=self.num_classes, **kwargs) self.criterion = paddle.nn.loss.CrossEntropyLoss() self.metric = paddle.metric.Accuracy() elif task == 'token-cls': self.model = BertForTokenClassification.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', num_classes=self.num_classes, **kwargs) self.criterion = paddle.nn.loss.CrossEntropyLoss() self.metric = ChunkEvaluator( label_list=[self.label_map[i] for i in sorted(self.label_map.keys())] ) elif task is None: self.model = BertModel.from_pretrained(pretrained_model_name_or_path='bert-large-uncased', **kwargs) else: raise RuntimeError("Unknown task {}, task should be one in {}".format( task, self._tasks_supported)) self.task = task if load_checkpoint is not None and os.path.isfile(load_checkpoint): state_dict = paddle.load(load_checkpoint) self.set_state_dict(state_dict) logger.info('Loaded parameters from %s' % os.path.abspath(load_checkpoint))
def main(args): if not args.use_cuda: paddle.set_device("cpu") if paddle.distributed.get_world_size() > 1: paddle.distributed.init_parallel_env() dataset = load(args.dataset) graph = dataset.graph model = Model(graph.num_nodes, args.embed_size, dataset.num_groups) model = paddle.DataParallel(model) batch_size = len(dataset.train_index) train_steps = int(len(dataset.train_index) / batch_size) * args.epoch scheduler = paddle.optimizer.lr.PolynomialDecay( learning_rate=args.multiclass_learning_rate, decay_steps=train_steps, end_lr=0.0001) optim = Adam(learning_rate=scheduler, parameters=model.parameters()) if args.load_from_static: model.set_state_dict(load_from_files("./model")) else: model.set_state_dict(paddle.load("model.pdparams")) train_data_loader = node_classify_generator(graph, dataset.train_index, batch_size=batch_size, epoch=1) test_data_loader = node_classify_generator(graph, dataset.test_index, batch_size=batch_size, epoch=1) best_test_macro_f1 = -1 for epoch in tqdm.tqdm(range(args.epoch)): train_loss = train(model, train_data_loader(), optim) test_loss, test_macro_f1, test_micro_f1 = test(model, test_data_loader()) best_test_macro_f1 = max(best_test_macro_f1, test_macro_f1) log.info("Best test macro f1 is %s." % best_test_macro_f1)
def main(args): os.environ['PADDLESEG_EXPORT_STAGE'] = 'True' cfg = Config(args.cfg) net = cfg.model if args.model_path: para_state_dict = paddle.load(args.model_path) net.set_dict(para_state_dict) logger.info('Loaded trained params of model successfully.') if args.input_shape is None: shape = [None, 3, None, None] else: shape = args.input_shape if not args.without_argmax or args.with_softmax: new_net = SavedSegmentationNet(net, args.without_argmax, args.with_softmax) else: new_net = net new_net.eval() new_net = paddle.jit.to_static( new_net, input_spec=[paddle.static.InputSpec(shape=shape, dtype='float32')]) save_path = os.path.join(args.save_dir, 'model') paddle.jit.save(new_net, save_path) yml_file = os.path.join(args.save_dir, 'deploy.yaml') with open(yml_file, 'w') as file: transforms = cfg.export_config.get('transforms', [{ 'type': 'Normalize' }]) data = { 'Deploy': { 'transforms': transforms, 'model': 'model.pdmodel', 'params': 'model.pdiparams' } } yaml.dump(data, file) logger.info(f'Model is saved in {args.save_dir}.')
def main(args): os.environ['PADDLESEG_EXPORT_STAGE'] = 'True' cfg = Config(args.cfg) net = cfg.model net.eval() if args.model_path: para_state_dict = paddle.load(args.model_path) net.set_dict(para_state_dict) logger.info('Loaded trained params of model successfully.') if args.input_shape is None: shape = [None, 3, None, None] else: shape = args.input_shape input_spec = [{"img": paddle.static.InputSpec(shape=shape, name='img')}] if args.trimap: shape[1] = 1 input_spec[0]['trimap'] = paddle.static.InputSpec(shape=shape, name='trimap') net = paddle.jit.to_static(net, input_spec=input_spec) save_path = os.path.join(args.save_dir, 'model') paddle.jit.save(net, save_path) yml_file = os.path.join(args.save_dir, 'deploy.yaml') with open(yml_file, 'w') as file: transforms = cfg.val_dataset_config.get('transforms', [{ 'type': 'Normalize' }]) data = { 'Deploy': { 'transforms': transforms, 'model': 'model.pdmodel', 'params': 'model.pdiparams' } } yaml.dump(data, file) logger.info(f'Model is saved in {args.save_dir}.')
def test_model(model, dataset, cfg, weight, world_size): """Test model entry Args: model (paddle.nn.Layer): The model to be tested. dataset (paddle.dataset): Train dataaset. """ #NOTE: add a new field : test_batch_size ? batch_size = cfg.DATASET.get("test_batch_size", 2) places = paddle.set_device('gpu') dataloader_setting = dict(batch_size=batch_size, num_workers=cfg.DATASET.get("num_workers", 0), places=places, drop_last=False, shuffle=False) data_loader = build_dataloader(dataset, **dataloader_setting) model.eval() state_dicts = paddle.load(weight) model.set_state_dict(state_dicts) parallel = world_size != 1 if parallel: model = paddle.DataParallel(model) # add params to metrics cfg.METRIC.data_size = len(dataset) cfg.METRIC.batch_size = batch_size cfg.METRIC.world_size = world_size Metric = build_metric(cfg.METRIC) for batch_id, data in enumerate(data_loader): if parallel: outputs = model._layers.test_step(data) else: outputs = model.test_step(data) Metric.update(batch_id, data, outputs) Metric.accumulate()
def do_train(args): paddle.set_device("gpu" if args.n_gpu else "cpu") args.model_type = args.model_type.lower() model_class, tokenizer_class = MODEL_CLASSES[args.model_type] config_path = os.path.join(args.model_name_or_path, 'model_config.json') cfg_dict = dict(json.loads(open(config_path).read())) num_labels = cfg_dict['num_classes'] model = model_class.from_pretrained(args.model_name_or_path, num_classes=num_labels) origin_model = model_class.from_pretrained(args.model_name_or_path, num_classes=num_labels) sp_config = supernet(expand_ratio=[1.0, args.width_mult]) model = Convert(sp_config).convert(model) ofa_model = OFA(model) sd = paddle.load( os.path.join(args.model_name_or_path, 'model_state.pdparams')) ofa_model.model.set_state_dict(sd) best_config = utils.dynabert_config(ofa_model, args.width_mult) ofa_model.export(best_config, input_shapes=[[1, args.max_seq_length], [1, args.max_seq_length]], input_dtypes=['int64', 'int64'], origin_model=origin_model) for name, sublayer in origin_model.named_sublayers(): if isinstance(sublayer, paddle.nn.MultiHeadAttention): sublayer.num_heads = int(args.width_mult * sublayer.num_heads) output_dir = os.path.join(args.sub_model_output_dir, "model_width_%.5f" % args.width_mult) if not os.path.exists(output_dir): os.makedirs(output_dir) model_to_save = origin_model model_to_save.save_pretrained(output_dir) if args.static_sub_model != None: export_static_model(origin_model, args.static_sub_model, args.max_seq_length)
def __init__(self, line=4, word=7): """ initialize with the necessary elements """ if line not in [4, 8]: raise ValueError("The line could only be 4 or 8.") if word not in [5, 7]: raise ValueError("The word could only be 5 or 7.") self.line = line assets_path = os.path.join(self.directory, "assets") gen_checkpoint_path = os.path.join(assets_path, "ernie_gen_acrostic_poetry_L%sW%s.pdparams" % (line, word)) self.model = ErnieForGeneration.from_pretrained("ernie-1.0") model_state = paddle.load(gen_checkpoint_path) self.model.set_dict(model_state) self.tokenizer = ErnieTokenizer.from_pretrained("ernie-1.0") self.rev_dict = self.tokenizer.vocab.idx_to_token self.rev_dict[self.tokenizer.vocab['[PAD]']] = '' # replace [PAD] self.rev_dict[self.tokenizer.vocab['[UNK]']] = '' # replace [PAD] self.rev_lookup = np.vectorize(lambda i: self.rev_dict[i])
def __init__(self, config_name, pretrained=True): super().__init__() assert config_name in CONFIG_NAMES, f'input config {config_name} incorrect, available configs: {CONFIG_NAMES}' config_url = URL_BASE + f'config/{config_name}.json' config_path = download.get_weights_path_from_url(config_url) config = PretrainedConfig.from_pretrained(config_path) self.wav2vec2 = Wav2Vec2Model(config) self.dropout = nn.Dropout(config.final_dropout) self.lm_head = nn.Linear(config.hidden_size, config.vocab_size) self.config = config if pretrained: weight_url = URL_BASE + f'weights/{config_name}.pdparam' weight_path = download.get_weights_path_from_url(weight_url) state_dict = paddle.load(weight_path) self.load_dict(state_dict) self.eval()
def test_save_load(self): layer, opt = self.build_and_train_model() # save layer_save_path = "test_paddle_save_load.linear.pdparams" opt_save_path = "test_paddle_save_load.linear.pdopt" layer_state_dict = layer.state_dict() opt_state_dict = opt.state_dict() paddle.save(layer_state_dict, layer_save_path) paddle.save(opt_state_dict, opt_save_path) # load load_layer_state_dict = paddle.load(layer_save_path) load_opt_state_dict = paddle.load(opt_save_path) self.check_load_state_dict(layer_state_dict, load_layer_state_dict) self.check_load_state_dict(opt_state_dict, load_opt_state_dict) # test save load in static mode paddle.enable_static() static_save_path = "static_mode_test/test_paddle_save_load.linear.pdparams" paddle.save(layer_state_dict, static_save_path) load_static_state_dict = paddle.load(static_save_path) self.check_load_state_dict(layer_state_dict, load_static_state_dict) # error test cases, some tests relay base test above # 1. test save obj not dict error test_list = [1, 2, 3] with self.assertRaises(NotImplementedError): paddle.save(test_list, "not_dict_error_path") # 2. test save path format error with self.assertRaises(ValueError): paddle.save(layer_state_dict, "test_paddle_save_load.linear.model/") # 3. test load path not exist error with self.assertRaises(ValueError): paddle.load("test_paddle_save_load.linear.params") # 4. test load old save path error with self.assertRaises(ValueError): paddle.load("test_paddle_save_load.linear")
def load_model(config, weights_fpath: Path): """ Loads the model in memory. If this function is not explicitely called, it will be run on the first call to embed_frames() with the default weights file. :param weights_fpath: the path to saved model weights. :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The model will be loaded and will run on this device. Outputs will however always be on the cpu. If None, will default to your GPU if it"s available, otherwise your CPU. """ # TODO: I think the slow loading of the encoder might have something to do with the device it # was saved on. Worth investigating. global _model _model = SpeakerEncoder(config.data.n_mels, config.model.num_layers, config.model.hidden_size, config.model.embedding_size) model_state_dict = paddle.load(weights_fpath + ".pdparams") _model.set_state_dict(model_state_dict) _model.eval() print(f"Loaded encoder {weights_fpath}")
def __init__(self, output='output', weight_path=None, artistic=False, render_factor=32): self.output = os.path.join(output, 'DeOldify') if not os.path.exists(self.output): os.makedirs(self.output) self.render_factor = render_factor self.model = build_model( model_type='artistic' if artistic else 'stable') if weight_path is None: if artistic: weight_path = get_path_from_url(DEOLDIFY_ART_WEIGHT_URL) else: weight_path = get_path_from_url(DEOLDIFY_STABLE_WEIGHT_URL) state_dict = paddle.load(weight_path) self.model.load_dict(state_dict) self.model.eval()
def validation(): model = Regressor() # 参数为保存模型参数的文件地址 model_dict = paddle.load('LR_model.pdparams') model.load_dict(model_dict) model.eval() # 参数为数据集的文件地址 one_data, label = load_one_example() # 将数据转为动态图的variable格式 one_data = paddle.to_tensor(one_data) predict = model(one_data) # 对结果做反归一化处理 predict = predict * (max_values[-1] - min_values[-1]) + avg_values[-1] # 对label数据做反归一化处理 label = label * (max_values[-1] - min_values[-1]) + avg_values[-1] print("Inference result is {}, the corresponding label is {}".format( predict.numpy(), label))
def main(args): """ main function """ model_config = json.load(open(args.model_config, 'r')) paddle.set_device("gpu") strategy = fleet.DistributedStrategy() fleet.init(is_collective=True, strategy=strategy) eval_loader = create_dataloader(data_dir=args.eval_data, model_config=model_config) encoder_model = ProteinEncoderModel(model_config, name='protein') model = ProteinModel(encoder_model, model_config) model = fleet.distributed_model(model) model.load_dict(paddle.load(args.eval_model)) criterion = ProteinCriterion(model_config) metric = get_metric(model_config['task']) eval_cur_loss = eval(model, eval_loader, criterion, metric)
def load_pretrain_weight(model, pretrain_weight): if is_url(pretrain_weight): pretrain_weight = get_weights_path(pretrain_weight) path = _strip_postfix(pretrain_weight) if not (os.path.isdir(path) or os.path.isfile(path) or os.path.exists(path + '.pdparams')): raise ValueError("Model pretrain path `{}` does not exists. " "If you don't want to load pretrain model, " "please delete `pretrain_weights` field in " "config file.".format(path)) model_dict = model.state_dict() weights_path = path + '.pdparams' param_state_dict = paddle.load(weights_path) param_state_dict = match_state_dict(model_dict, param_state_dict) model.set_dict(param_state_dict) logger.info('Finish loading model weights: {}'.format(weights_path))
def main(): word_vocab = load_vocab(os.path.join(args.data_dir, 'word.dic')) label_vocab = load_vocab(os.path.join(args.data_dir, 'tag.dic')) model = BiGruCrf(args.emb_dim, args.hidden_size, len(word_vocab), len(label_vocab)) state_dict = paddle.load(args.params_path) model.set_dict(state_dict) model.eval() model = paddle.jit.to_static( model, input_spec=[ InputSpec( shape=[None, None], dtype="int64", name='token_ids'), InputSpec( shape=[None], dtype="int64", name='length') ]) # Save in static graph model. paddle.jit.save(model, args.output_path)