-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha_test.py
45 lines (39 loc) · 1.7 KB
/
captcha_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- 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 main():
cnn = CNN()
cnn.eval()
cnn.load_state_dict(torch.load('model.pkl'))
print("load cnn net.")
test_dataloader = my_dataset.get_test_data_loader()
correct = 0
total = 0
for i, (images, labels) in enumerate(test_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' % (c0)
true_label = one_hot_encoding.decode(labels.numpy()[0])
#print(predict_label,true_label)
total += labels.size(0)
if(predict_label == true_label):
correct += 1
else:
print('\033[31m%s --> %s'%(true_label,predict_label ))
if(total%20==0):
print('\033[0m%s --> %s'%(true_label,predict_label ))
if(total%200==0):
break
print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
if __name__ == '__main__':
main()