Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predict_g2p_bert.py报错 #23

Open
anaivebird opened this issue Feb 26, 2024 · 1 comment
Open

predict_g2p_bert.py报错 #23

anaivebird opened this issue Feb 26, 2024 · 1 comment

Comments

@anaivebird
Copy link

predict.sh:
python scripts/predict_g2p_bert.py
--config saved_models/CPP_BERT_M_DescWS-Sec-cLin-B_POSw01/config.py
--checkpoint saved_models/CPP_BERT_M_DescWS-Sec-cLin-B_POSw01/best_accuracy.pth
--sent_path cpp_dataset/test2.sent
--output_path output_pred.txt

(chatchat) root@a100:/home/data/tts/wetts/shtel/g2pW# sh predict.sh
Some weights of G2PW were not initialized from the model checkpoint at /home/nas/modelhub/llm/bert-base-chinese and are newly initialized: ['char_descriptor.weight', 'classifier.bias', 'pos_classifier.bias', 'second_order_descriptor.weight', 'classifier.weight', 'pos_classifier.weight', 'descriptor_bias.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
Traceback (most recent call last):
File "/home/data/tts/wetts/shtel/g2pW/scripts/predict_g2p_bert.py", line 86, in
main(config, opt.checkpoint, opt.sent_path, output_path=opt.output_path)
File "/home/data/tts/wetts/shtel/g2pW/scripts/predict_g2p_bert.py", line 58, in main
preds, confidences = predict(model, dataloader, device, labels)
File "/home/data/tts/wetts/shtel/g2pW/./g2pw/api.py", line 29, in predict
probs = onnx_session.run(
File "/opt/conda/envs/chatchat/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1695, in getattr
raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")
AttributeError: 'G2PW' object has no attribute 'run'

看起来predict函数的第一个参数应该是一个onnx-session,但是predict_g2p_bert.py里传入predict第一个参数的是model = G2PW.from_pretrained

@GitYCC
Copy link
Owner

GitYCC commented Mar 2, 2024

Can you try to revise the function to:

def predict(model, dataloader, device, labels, turnoff_tqdm=False):
    model.eval()
    all_preds = []
    all_confidences = []
    with torch.no_grad():
        generator = dataloader if turnoff_tqdm else tqdm(dataloader, desc='predict')
        for data in generator:
            input_ids, token_type_ids, attention_mask, phoneme_mask, char_ids, position_ids = \
                [data[name].to(device) for name in ('input_ids', 'token_type_ids', 'attention_mask', 'phoneme_mask', 'char_ids', 'position_ids')]
            probs = model(
                input_ids=input_ids,
                token_type_ids=token_type_ids,
                attention_mask=attention_mask,
                phoneme_mask=phoneme_mask,
                char_ids=char_ids,
                position_ids=position_ids
            )
            max_probs, preds = map(lambda x: x.cpu().tolist(), probs.max(dim=-1))
            all_preds += [labels[pred] for pred in preds]
            all_confidences += max_probs
    return all_preds, all_confidences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants