def test_net(network, data_path, ckpt): """define the evaluation method""" print("============== Starting Testing ==============") #load the saved model for evaluation load_checkpoint(ckpt, net=network) #load testing dataset ds_eval = create_dataset(False, data_path) # config = GPTConfig(batch_size=4, # seq_length=1024, # vocab_size=50257, # embedding_size=1024, # num_layers=24, # num_heads=16, # expand_ratio=4, # post_layernorm_residual=False, # dropout_rate=0.1, # compute_dtype=mstype.float16, # use_past=False) # loss = CrossEntropyLoss(config) net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') model = Model(resnet, net_loss, metrics={"Accuracy": Accuracy()}) # model = Model(resnet, net_loss, metrics={"Accuracy": Accuracy()}, amp_level="O3") acc = model.eval(ds_eval, dataset_sink_mode=False) print("============== Accuracy:{} ==============".format(acc))
def output_file_formats(ckpt_path, net_work, batch_size, output_file_name, output_format): load_checkpoint(ckpt_path, net=net_work) input_data = np.random.uniform(0.0, 1.0, size=batch_size).astype(np.float32) export(net_work, Tensor(input_data), file_name=output_file_name, file_format=output_format)
def main(): network = LeNet5() # load the parameter into net load_checkpoint(args.ckpt_path, net=network) input_x = np.random.uniform(0.0, 1.0, size=[1, 1, 32, 32]).astype(np.float32) export(network, Tensor(input_x), file_name=args.file_name, file_format=args.file_format)
def __init__(self, batch_size, encoder_ck_file, downstream_ck_file): """init function""" super(Reranker, self).__init__(auto_prefix=False) self.encoder = Albert(batch_size) param_dict = load_checkpoint(encoder_ck_file) not_load_params_1 = load_param_into_net(self.encoder, param_dict) print(f"re-ranker albert not loaded params: {not_load_params_1}") self.no_answer_mlp = Rerank_Downstream() param_dict = load_checkpoint(downstream_ck_file) not_load_params_2 = load_param_into_net(self.no_answer_mlp, param_dict) print(f"re-ranker downstream not loaded params: {not_load_params_2}")
def __init__(self, batch_size, encoder_ck_file, downstream_ck_file): """init function""" super(Reader, self).__init__(auto_prefix=False) self.encoder = Reader_Albert(batch_size) param_dict = load_checkpoint(encoder_ck_file) not_load_params = load_param_into_net(self.encoder, param_dict) print(f"not loaded: {not_load_params}") self.downstream = Reader_Downstream() param_dict = load_checkpoint(downstream_ck_file) not_load_params = load_param_into_net(self.downstream, param_dict) print(f"not loaded: {not_load_params}") self.bmm = BatchMatMul()
def __eval(self): # import from mindspore import Model, load_param_into_net, load_checkpoint from mindspore.nn.metrics import Accuracy # load params if self.__ckpt_path: load_param_into_net(self.__network, load_checkpoint(self.__ckpt_path)) else: print( "Warning: `ckpt_path` is None, Please call func: `set_ckpt_path($ckpt_path)`." ) return # loss_fn & optimizer & metrics model = Model(self.__network, loss_fn=self.__loss_fn, optimizer=self.__optimizer, metrics={"Accuracy": Accuracy()} if self.__metrics is None else self.__metrics) # eval print(">>>>>>>>>>>>>>>>>>>>> eval start ... <<<<<<<<<<<<<<<<<<<<<<") result = model.eval(self.__dataset) print( ">>>>>>>>>>>>>>>>>>>>> eval success ~ <<<<<<<<<<<<<<<<<<<<<<: result=", result)
def __init__(self, config, network): super(OneHopBert, self).__init__(auto_prefix=False) self.network = network self.mlp = Model() param_dict = load_checkpoint(config.onehop_mlp_path) load_param_into_net(self.mlp, param_dict) self.cast = P.Cast()
def __init__(self, requires_grad=False): super(Vgg19, self).__init__() ##load vgg16 vgg = Vgg(cfg['19'], phase="test", args=imagenet_cfg) # model = os.path.join(opt.data_url, 'vgg19_ImageNet.ckpt') model = os.path.join('./', 'vgg19_ImageNet.ckpt') print(model) param_dict = load_checkpoint(model) # print(param_dict) load_param_into_net(vgg, param_dict) vgg.set_train(False) vgg_pretrained_features = vgg.layers self.slice1 = nn.SequentialCell() self.slice2 = nn.SequentialCell() self.slice3 = nn.SequentialCell() self.slice4 = nn.SequentialCell() self.slice5 = nn.SequentialCell() for x in range(2): self.slice1.append(vgg_pretrained_features[x]) for x in range(2, 7): self.slice2.append(vgg_pretrained_features[x]) for x in range(7, 12): self.slice3.append(vgg_pretrained_features[x]) for x in range(12, 21): self.slice4.append(vgg_pretrained_features[x]) for x in range(21, 30): self.slice5.append(vgg_pretrained_features[x]) if not requires_grad: for param in self.get_parameters(): param.requires_grad = False
def fix_ckpt(self, ckpt_path, new_ckpt_path): """Fix checkpoint file.""" param_dict = load_checkpoint(ckpt_path) main_module_name = self._fixed_mapper['main_module_name'] fixed_variable_dict = self._fixed_mapper['fixed_variable_mapper'] fixed_module_dict = self._fixed_mapper['fixed_module_mapper'] save_obj = list() for weight_name, weight_value in param_dict.items(): weight_name_scopes = weight_name.split('.') weight_name_scopes.insert(0, main_module_name) for idx, w in enumerate(weight_name_scopes[:-1]): for fixed_variable_module, fixed_variable_name_mapper in fixed_variable_dict.items( ): if re.match( fixed_variable_module, fixed_module_dict.get('_'.join(w.split('_')[:-1]), w)): weight_name = weight_name.replace( weight_name_scopes[idx + 1], fixed_variable_name_mapper.get( weight_name_scopes[idx + 1], weight_name_scopes[idx + 1])) obj = {'name': weight_name, 'data': Tensor(weight_value)} save_obj.append(obj) save_checkpoint(save_obj, new_ckpt_path) logger_console.info(f'Saved new checkpoint file to {new_ckpt_path}.')
def epoch_end(self, run_context): if self.step_eval: return None flag = '' cb_params = run_context.original_args() cur_epoch = cb_params.cur_epoch_num if cur_epoch > 0 and cur_epoch % self.eval_epoch == 0: acc = self.eval() if acc > self.best_acc: flag = '↑' self.best_acc = acc save_checkpoint(self.model._network, self.best_ckpt) self.logger.update_acc_ckpt(acc, self.best_ckpt) else: if acc > self.threshold: self.patience_count += 1 if self.patience_count > self.patience: param_dict = load_checkpoint( ckpt_file_name=self.best_ckpt, net=self.model._network) load_param_into_net(net=self.model._network, parameter_dict=param_dict) self.patience_count = 0 print( f'* acc for epoch: {cur_epoch} is {acc * 100}%{flag}, best acc is {self.best_acc * 100}%' )
def eval_quant(): context.set_context(mode=context.GRAPH_MODE, device_target=device_target) cfg = quant_cfg ds_eval = create_dataset(os.path.join(data_path, "test"), cfg.batch_size, 1) ckpt_path = './ckpt_lenet_quant-10_937.ckpt' # define fusion network network = LeNet5Fusion(cfg.num_classes) # convert fusion network to quantization aware network quantizer = QuantizationAwareTraining(quant_delay=0, bn_fold=False, freeze_bn=10000, per_channel=[True, False], symmetric=[True, False]) network = quantizer.quantize(network) # define loss net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") # define network optimization net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum) # call back and monitor model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) # load quantization aware network checkpoint param_dict = load_checkpoint(ckpt_path) not_load_param = load_param_into_net(network, param_dict) if not_load_param: raise ValueError("Load param into net fail!") print("============== Starting Testing ==============") acc = model.eval(ds_eval, dataset_sink_mode=True) print("============== {} ==============".format(acc)) assert acc['Accuracy'] > 0.98
def __train(self): # import from mindspore import Model, load_param_into_net, load_checkpoint # load params if self.__ckpt_path and os.path.isfile( self.__ckpt_path) and os.path.exists(self.__ckpt_path): load_param_into_net(self.__network, load_checkpoint(self.__ckpt_path)) # loss_fn & optimizer & metrics model = Model(self.__network, loss_fn=self.__loss_fn, optimizer=self.__optimizer, metrics=self.__metrics) # train print(">>>>>>>>>>>>>>>>>>>>> train start ... <<<<<<<<<<<<<<<<<<<<<<") self.__update_cbs() model.train(self.__epoch_size, self.__dataset, callbacks=self.__callbacks, dataset_sink_mode=self.__do_sink, sink_size=self.__sink_size) print(">>>>>>>>>>>>>>>>>>>>> train success ~ <<<<<<<<<<<<<<<<<<<<<<")
def test_net(network, model, mnist_path): """Define the evaluation method""" print("==================== Starting Testing ===============") param_dict = load_checkpoint( "./model/ckpt/mindspore_quick_start/checkpoint_lenet-1_1874.ckpt") load_param_into_net(network, param_dict) ds_eval = create_dataset(os.path.join(mnist_path, "test")) acc = model.eval(ds_eval, dataset_sink_mode=False) print("==================== Accuracy:{} ===============".format(acc))
def train_lenet_quant(optim_option="QAT"): context.set_context(mode=context.GRAPH_MODE, device_target=device_target) cfg = quant_cfg ckpt_path = './ckpt_lenet_noquant-10_1875.ckpt' ds_train = create_dataset(os.path.join(data_path, "train"), cfg.batch_size, 1) step_size = ds_train.get_dataset_size() # define fusion network network = LeNet5Fusion(cfg.num_classes) # load quantization aware network checkpoint param_dict = load_checkpoint(ckpt_path) load_nonquant_param_into_quant_net(network, param_dict) # convert fusion network to quantization aware network if optim_option == "LEARNED_SCALE": quant_optim_otions = OptimizeOption.LEARNED_SCALE quantizer = QuantizationAwareTraining( bn_fold=False, per_channel=[True, False], symmetric=[True, True], narrow_range=[True, True], freeze_bn=0, quant_delay=0, one_conv_fold=True, optimize_option=quant_optim_otions) else: quantizer = QuantizationAwareTraining(quant_delay=900, bn_fold=False, per_channel=[True, False], symmetric=[True, False]) network = quantizer.quantize(network) # define network loss net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") # define network optimization net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum) # call back and monitor config_ckpt = CheckpointConfig(save_checkpoint_steps=cfg.epoch_size * step_size, keep_checkpoint_max=cfg.keep_checkpoint_max) ckpt_callback = ModelCheckpoint(prefix="ckpt_lenet_quant" + optim_option, config=config_ckpt) # define model model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) print("============== Starting Training ==============") model.train(cfg['epoch_size'], ds_train, callbacks=[ckpt_callback, LossMonitor()], dataset_sink_mode=True) print("============== End Training ==============")
def source_checker(py_path, ckpt_path): """Check source model script and source checkpoint file.""" sys.path.append(os.path.dirname(py_path)) model = getattr( import_module(os.path.basename(py_path).replace('.py', '')), 'Model')() try: param_dict = load_checkpoint(ckpt_path) not_load_name = load_param_into_net(model, param_dict) return not not_load_name except (ValueError, TypeError, RuntimeError): return False
def test_net(network, network_model, data_path): """Define the evaluation method.""" print("============== Starting Testing ==============") # load the saved model for evaluation param_dict = load_checkpoint("checkpoint_lenet-1_1875.ckpt") # load parameter to the network load_param_into_net(network, param_dict) # load testing dataset ds_eval = create_dataset(os.path.join(data_path, "test")) acc = network_model.eval(ds_eval, dataset_sink_mode=False) print("============== Accuracy:{} ==============".format(acc))
def test_eval(model_ckpt_path): '''eval model''' target = args_opt.device_target if target == "Ascend": devid = int(os.getenv('DEVICE_ID')) context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=devid) bert_net_cfg.batch_size = 1 poetrymodel = BertPoetryModel(bert_net_cfg, False, 3191, dropout_prob=0.0) poetrymodel.set_train(False) param_dict = load_checkpoint(model_ckpt_path) load_param_into_net(poetrymodel, param_dict) # random generation/continue start_time = time.time() output = generate_random_poetry(poetrymodel, s='') end_to_end_delay = (time.time() - start_time) * 1000 a = re.findall(r'[\u4e00-\u9fa5]*[\uff0c\u3002]', output) print("\n**********************************") print("随机生成: \n") for poem in a: print(poem) print("\ncost time: {:.1f} ms".format(end_to_end_delay)) print("\n") start = "天下为公" start_time = time.time() output = generate_random_poetry(poetrymodel, s=start) end_to_end_delay = (time.time() - start_time) * 1000 a = re.findall(r'[\u4e00-\u9fa5]*[\uff0c\u3002]', output) print("\n**********************************") print("续写 【{}】: \n".format(start)) for poem in a: print(poem) print("\ncost time: {:.1f} ms".format(end_to_end_delay)) print("\n") # hidden poetry s = "人工智能" start_time = time.time() output = generate_hidden(poetrymodel, head=s) end_to_end_delay = (time.time() - start_time) * 1000 a = re.findall(r'[\u4e00-\u9fa5]*[\uff0c\u3002]', output) print("\n**********************************") print("藏头诗 【{}】: \n".format(s)) for poem in a: print(poem) print("\ncost time: {:.1f} ms".format(end_to_end_delay)) print("\n")
def export_net(model_ckpt_path): bert_net_cfg.batch_size = 1 poetrymodel = BertPoetryModel(bert_net_cfg, False, 3191, dropout_prob=0.0) poetrymodel.set_train(False) param_dict = load_checkpoint(model_ckpt_path) load_param_into_net(poetrymodel, param_dict) input_id = np.ones(shape=(1, 128)) token_type_id = np.ones(shape=(1, 128)) pad_mask = np.ones(shape=(1, 128)) export(poetrymodel, Tensor(input_id, mstype.int32),\ Tensor(token_type_id, mstype.int32),\ Tensor(pad_mask, mstype.float32),\ file_name='poetry.pb', file_format='MINDIR')
def run_export(args): """ export """ device_id = int(os.getenv('DEVICE_ID', '0')) context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=device_id) net = RCAN(arg) param_dict = load_checkpoint(args.ckpt_path) load_param_into_net(net, param_dict) net.set_train(False) print('load mindspore net and checkpoint successfully.') inputs = Tensor(np.zeros([args.batch_size, 3, 678, 1020], np.float32)) export(net, inputs, file_name=args.file_name, file_format=args.file_format) print('export successfully!')
def test_net(network, network_model, data_path): """Define the evaluation method.""" print("============== Starting Testing ==============") # load the saved model for evaluation latest_file = newest(data_path) print(latest_file) param_dict = load_checkpoint(latest_file) # load parameter to the network load_param_into_net(network, param_dict) # load testing dataset ds_eval = dm.create_dataset(os.path.join( data_path, "./MindSpore_train_images_dataset/test"), do_train=False) acc = network_model.eval(ds_eval, dataset_sink_mode=False) print("============== Accuracy:{} ==============".format(acc))
def load_model(self, filename=None): """Loading pre-trained model weights from a file.""" model_name = Config().trainer.model_name model_dir = Config().params['pretrained_model_dir'] if filename is not None: model_path = f'{model_dir}{filename}' else: model_path = f'{model_dir}{model_name}.ckpt' if self.client_id == 0: logging.info("[Server #%d] Loading a model from %s.", os.getpid(), model_path) else: logging.info("[Client #%d] Loading a model from %s.", self.client_id, model_path) param_dict = mindspore.load_checkpoint(model_path) mindspore.load_param_into_net(self.model, param_dict)
def test_net(network, model, data_path): """Define the evaluation method.""" print("============== Starting Testing ==============") # load the saved model for evaluation checkpoint = "./101.ckpt" _t = time.time() param_dict = load_checkpoint(checkpoint) # load parameter to the network load_param_into_net(network, param_dict) print(f"Load params to model cost: {time.time() - _t} s") # inference print("============== Infer Testing ==============") input_data = Tensor(np.random.randint(0, 255, [1, 3, 224, 224]), mindspore.float32) infer_time = 0 while infer_time < 21: _t = time.time() model.predict(input_data) print(f"Model infer cost: {time.time() - _t} s") infer_time += 1
def eval_lenet(): context.set_context(mode=context.GRAPH_MODE, device_target=device_target) cfg = nonquant_cfg ds_eval = create_dataset(os.path.join(data_path, "test"), cfg.batch_size, 1) ckpt_path = './ckpt_lenet_noquant-10_1875.ckpt' # define fusion network network = LeNet5(cfg.num_classes) net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum) # call back and monitor model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}) # load quantization aware network checkpoint param_dict = load_checkpoint(ckpt_path) not_load_param = load_param_into_net(network, param_dict) if not_load_param: raise ValueError("Load param into net fail!") print("============== Starting Testing ==============") acc = model.eval(ds_eval, dataset_sink_mode=True) print("============== {} ==============".format(acc)) assert acc['Accuracy'] > 0.98
required=True, help='inceptionv3 ckpt file.') parser.add_argument('--output_file', type=str, default='inceptionv3.air', help='inceptionv3 output air name.') parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format') parser.add_argument('--width', type=int, default=299, help='input width') parser.add_argument('--height', type=int, default=299, help='input height') args = parser.parse_args() if __name__ == '__main__': net = InceptionV3(num_classes=cfg.num_classes, is_training=False) param_dict = load_checkpoint(args.ckpt_file) load_param_into_net(net, param_dict) input_arr = Tensor( np.random.uniform(0.0, 1.0, size=[cfg.batch_size, 3, args.width, args.height]), ms.float32) export(net, input_arr, file_name=args.output_file, file_format=args.file_format)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") parser = argparse.ArgumentParser(description='CNNCTC_export') parser.add_argument('--ckpt_file', type=str, default='./ckpts/cnn_ctc.ckpt', help='CNN&CTC ckpt file.') parser.add_argument('--output_file', type=str, default='cnn_ctc', help='CNN&CTC output air name.') args_opt = parser.parse_args() if __name__ == '__main__': cfg = Config_CNNCTC() ckpt_path = cfg.CKPT_PATH if args_opt.ckpt_file != "": ckpt_path = args_opt.ckpt_file net = CNNCTC_Model(cfg.NUM_CLASS, cfg.HIDDEN_SIZE, cfg.FINAL_FEATURE_WIDTH) load_checkpoint(ckpt_path, net=net) bs = cfg.TEST_BATCH_SIZE input_data = Tensor(np.zeros([bs, 3, cfg.IMG_H, cfg.IMG_W]), mstype.float32) export(net, input_data, file_name=args_opt.output_file, file_format="AIR")
from src.lenet import LeNet5 parser = argparse.ArgumentParser(description='MindSpore MNIST Example') parser.add_argument( '--device_target', type=str, default="Ascend", choices=['Ascend', 'GPU'], help='device where the code will be implemented (default: Ascend)') parser.add_argument( '--ckpt_path', type=str, default="", help='if mode is test, must provide path where the trained ckpt file') args = parser.parse_args() if __name__ == "__main__": context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target) # define fusion network network = LeNet5(cfg.num_classes) # load network checkpoint param_dict = load_checkpoint(args.ckpt_path) load_param_into_net(network, param_dict) # export network inputs = Tensor(np.ones([1, 1, cfg.image_height, cfg.image_width]), mindspore.float32) export(network, inputs, file_name=cfg.air_name, file_format='AIR')
"""export""" import argparse import numpy as np from mindspore import context, Tensor, load_checkpoint, load_param_into_net, export from src.resnet_thor import resnet50 as resnet from src.config import config parser = argparse.ArgumentParser(description='checkpoint export') parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path') args_opt = parser.parse_args() if __name__ == '__main__': context.set_context(mode=context.GRAPH_MODE, save_graphs=False) # define net net = resnet(class_num=config.class_num) net.add_flags_recursive(thor=False) # load checkpoint param_dict = load_checkpoint(args_opt.checkpoint_path) keys = list(param_dict.keys()) for key in keys: if "damping" in key: param_dict.pop(key) load_param_into_net(net, param_dict) inputs = np.random.uniform(0.0, 1.0, size=[1, 3, 224, 224]).astype(np.float32) export(net, Tensor(inputs), file_name='resnet-42_5004.air', file_format='AIR')
def evaluation(d_id): """evaluation""" context.set_context(mode=context.GRAPH_MODE, device_target='Ascend', device_id=d_id, save_graphs=False) print('********************** loading corpus ********************** ') s_lc = time.time() data_generator = DataGen(config) queries = read_query(config, d_id) print("loading corpus time (h):", (time.time() - s_lc) / 3600) print('********************** loading model ********************** ') s_lm = time.time() model_onehop_bert = ModelOneHop(256) param_dict = load_checkpoint(config.onehop_bert_path) load_param_into_net(model_onehop_bert, param_dict) model_twohop_bert = ModelOneHop(448) param_dict2 = load_checkpoint(config.twohop_bert_path) load_param_into_net(model_twohop_bert, param_dict2) onehop = OneHopBert(config, model_onehop_bert) twohop = TwoHopBert(config, model_twohop_bert) print("loading model time (h):", (time.time() - s_lm) / 3600) print('********************** evaluation ********************** ') f_dev = open(config.dev_path, 'rb') dev_data = json.load(f_dev) f_dev.close() q_gold = {} q_2id = {} for onedata in dev_data: if onedata["question"] not in q_gold: q_gold[onedata["question"]] = [ get_new_title(get_raw_title(item)) for item in onedata['path'] ] q_2id[onedata["question"]] = onedata['_id'] val, true_count, count, step = 0, 0, 0, 0 batch_queries = split_queries(config, queries) output_path = [] for _, batch in enumerate(batch_queries): print("###step###: ", str(step) + "_" + str(d_id)) query = batch[0] temp_dict = {} temp_dict['q_id'] = q_2id[query] temp_dict['question'] = query gold_path = q_gold[query] input_ids_1, token_type_ids_1, input_mask_1 = data_generator.convert_onehop_to_features( batch) start = 0 TOTAL = len(input_ids_1) split_chunk = 8 while start < TOTAL: end = min(start + split_chunk - 1, TOTAL - 1) chunk_len = end - start + 1 input_ids_1_ = input_ids_1[start:start + chunk_len] input_ids_1_ = Tensor(input_ids_1_, mstype.int32) token_type_ids_1_ = token_type_ids_1[start:start + chunk_len] token_type_ids_1_ = Tensor(token_type_ids_1_, mstype.int32) input_mask_1_ = input_mask_1[start:start + chunk_len] input_mask_1_ = Tensor(input_mask_1_, mstype.int32) cls_out = onehop(input_ids_1_, token_type_ids_1_, input_mask_1_) if start == 0: out = cls_out else: out = P.Concat(0)((out, cls_out)) start = end + 1 out = P.Squeeze(1)(out) onehop_prob, onehop_index = P.TopK(sorted=True)(out, config.topk) onehop_prob = P.Softmax()(onehop_prob) sample, path_raw, last_out = data_generator.get_samples( query, onehop_index, onehop_prob) input_ids_2, token_type_ids_2, input_mask_2 = data_generator.convert_twohop_to_features( sample) start_2 = 0 TOTAL_2 = len(input_ids_2) split_chunk = 8 while start_2 < TOTAL_2: end_2 = min(start_2 + split_chunk - 1, TOTAL_2 - 1) chunk_len = end_2 - start_2 + 1 input_ids_2_ = input_ids_2[start_2:start_2 + chunk_len] input_ids_2_ = Tensor(input_ids_2_, mstype.int32) token_type_ids_2_ = token_type_ids_2[start_2:start_2 + chunk_len] token_type_ids_2_ = Tensor(token_type_ids_2_, mstype.int32) input_mask_2_ = input_mask_2[start_2:start_2 + chunk_len] input_mask_2_ = Tensor(input_mask_2_, mstype.int32) cls_out = twohop(input_ids_2_, token_type_ids_2_, input_mask_2_) if start_2 == 0: out_2 = cls_out else: out_2 = P.Concat(0)((out_2, cls_out)) start_2 = end_2 + 1 out_2 = P.Softmax()(out_2) last_out = Tensor(last_out, mstype.float32) out_2 = P.Mul()(out_2, last_out) val, true_count, topk_titles = eval_output(out_2, last_out, path_raw, gold_path, val, true_count) temp_dict['topk_titles'] = topk_titles output_path.append(temp_dict) step += 1 count += 1 return { 'val': val, 'count': count, 'true_count': true_count, 'path': output_path }
def _load_weights(self, model_path: str = None) -> None: param_dict = load_checkpoint(model_path) load_param_into_net(self, param_dict)
num_labels=number_labels, use_crf=True, tag_to_index=tag_to_index) else: net = BertNERModel(bert_net_cfg, False, number_labels, use_crf=(args.use_crf.lower() == "true")) elif args.downstream_task == "CLS": net = BertCLSModel(bert_net_cfg, False, num_labels=number_labels) elif args.downstream_task == "SQUAD": net = BertSquadModel(bert_net_cfg, False) else: raise ValueError("unsupported downstream task") load_checkpoint(args.ckpt_file, net=net) net.set_train(False) input_ids = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32) input_mask = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32) token_type_id = Tensor( np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32) label_ids = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32) if args.downstream_task == "NER" and args.use_crf.lower() == "true": input_data = [input_ids, input_mask, token_type_id, label_ids] else: input_data = [input_ids, input_mask, token_type_id]