Ai
1 Star 0 Fork 1

Barneys/pytorch_captcha_recognition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
captcha_predict.py 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
Barneys 提交于 2021-12-11 13:49 +08:00 . 20211211
# -*- coding: UTF-8 -*-
import numpy as np
import torch
from torch.autograd import Variable
import captcha_setting
import my_dataset
from captcha_cnn_model import CNN
import one_hot_encoding
def validate():
cnn = CNN()
cnn.eval()
cnn.load_state_dict(torch.load('model.pkl'))
print("load cnn net.")
validate_dataloader = my_dataset.get_predict_data_loader()
#print(len(list(enumerate(validate_dataloader))))
correct = 0
total = len(list(enumerate(validate_dataloader)))
for i, (images, labels) in enumerate(validate_dataloader):
image = images
vimage = Variable(image)
predict_label = cnn(vimage)
c0 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())]
c1 = captcha_setting.ALL_CHAR_SET[np.argmax(
predict_label[0, captcha_setting.ALL_CHAR_SET_LEN:2 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())]
c2 = captcha_setting.ALL_CHAR_SET[np.argmax(
predict_label[0, 2 * captcha_setting.ALL_CHAR_SET_LEN:3 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())]
c3 = captcha_setting.ALL_CHAR_SET[np.argmax(
predict_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())]
predict_label = '%s%s%s%s' % (c0, c1, c2, c3)
true_label = one_hot_encoding.decode(labels.numpy()[0])
#print(true_label, "---------")
print(predict_label,"//",true_label)
if (predict_label == true_label):
correct += 1
# if(total%200==0):
# print('Train Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
print('Train Accuracy of the model on the %d validate images: %f %%' % (total, 100 * correct / total))
return 100 * correct / total
if __name__ == '__main__':
validate()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/Barneys/pytorch_captcha_recognition.git
git@gitee.com:Barneys/pytorch_captcha_recognition.git
Barneys
pytorch_captcha_recognition
pytorch_captcha_recognition
master

搜索帮助