github repo: github.com/ximeiorg/ochw

image/png python 推理代码:

def get_labels():
    labels = []
    with open("data/label.txt", "r", encoding="utf-8") as f:
        for line in f:
            # line: !	0
            line = line.strip()
            label = line.split("\t")[0]
            labels.append(label)
    return labels

if __name__ == "__main__":
    model = HandwritingTrainer.load_from_checkpoint("checkpoint-epoch=32-val_loss=0.156.ckpt")
    model.eval()
    model = model.to("cuda")
    img = Image.open("./testdata/hui.png")
    img = img.convert("RGB")
    img = img.resize((96,96))
    rans = transforms.Compose([
        transforms.Resize((96, 96)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.95], std=[0.2])
    ])
    img = trans(img)
    img = img.unsqueeze(0)
    img = img.to("cuda")
    labels = get_labels()
    with torch.no_grad():
        output = model(img)
        output = torch.nn.functional.softmax(output,dim=1)
        # 获取top5的预测结果
        top5_prob, top5_idx = torch.topk(output, 5)
        top5_prob = top5_prob.cpu().numpy()
        top5_idx = top5_idx.cpu().numpy()
        for i in range(5):
            idx = top5_idx[0][i]
            print(f"Top {i+1} 预测标签: {labels[idx]}, 概率: {top5_prob[0][i]:.4f}")
        

得到的结果如下:

Top 1 预测标签: 知, 概率: 0.9505
Top 2 预测标签: 勉, 概率: 0.0095
Top 3 预测标签: 贮, 概率: 0.0025
Top 4 预测标签: 处, 概率: 0.0025
Top 5 预测标签: ‰, 概率: 0.0025
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support