Example #1
0
def predict(model, imgdata):
    img_width, img_height = 223, 50
    img_channels = 3
    max_nb_cha = 6  # 文本最大长度
    len_set = range(1, max_nb_cha + 1)  # 文本可能的长度
    cha_set = list("0123456789qwertyuioplkjhgfdsazxcvbnm")  # 文本字符集
    nb_classes = 36

    X_test = load_data(imgdata, img_width, img_height, img_channels)
    # print X_test
    predictions = model.predict({'input': X_test})
    pred_chas = [
        one_hot_decoder(predictions['output%d' % j], cha_set)
        for j in range(1, max_nb_cha + 1)
    ]
    pred_nbs = one_hot_decoder(predictions['output_nb'], len_set)
    length = min(max_nb_cha, pred_nbs[0])
    ans = []
    for i in range(length):
        ans.append(pred_chas[i][0])
    ans = ''.join(ans)
    # print ans
    # print predictions['output1']
    res = {'valid': True, 'answer': ans, 'expr': ans}

    return json.dumps(res)
Example #2
0
def test_multi(model, len_set, cha_set, max_nb_cha, X_test, Y_test_nb, Y_test):
    # 开始预测并验证准确率,需要先把预测结果从概率转到对应的标签
    predictions = model.predict({'input': X_test})
    # print predictions
    pred_nbs = one_hot_decoder(predictions['output_nb'], len_set)
    pred_chas = [
        one_hot_decoder(predictions['output%d' % j], cha_set)
        for j in range(1, max_nb_cha + 1)
    ]
    Y_test_nb = one_hot_decoder(Y_test_nb, len_set)
    Y_test = [one_hot_decoder(i, cha_set) for i in Y_test]

    correct = 0
    len_correct = 0
    nb_sample = X_test.shape[0]
    for i in range(nb_sample):
        pred_nb = pred_nbs[i]
        true_nb = Y_test_nb[i]
        # print 'len(pred, true):', pred_nb, true_nb
        allright = (pred_nb == true_nb)
        if allright:
            len_correct += 1
        for j in range(true_nb):
            # print pred_chas[j][i], Y_test[j][i]
            allright = allright and (pred_chas[j][i] == Y_test[j][i])
        if allright:
            correct += 1
        else:
            print 'id:' + str(i + 1), pred_chas[0][i], Y_test[0][i]
    print 'Length accuracy:', float(len_correct) / nb_sample
    print 'Accuracy:', float(correct) / nb_sample
Example #3
0
def predict(model, post_vals):
    img_width, img_height = 223, 50
    img_channels = 3
    max_nb_cha = 6  # 文本最大长度
    len_set = range(1, max_nb_cha + 1)  # 文本可能的长度
    cha_set = list("0123456789qwertyuioplkjhgfdsazxcvbnm")  # 文本字符集
    nb_classes = 36

    keys = post_vals.keys()
    img_vals = post_vals.values()
    X_test = load_data(img_vals, img_width, img_height, img_channels)
    predictions = model.predict({'input': X_test})
    pred_chas = [
        one_hot_decoder(predictions['output%d' % j], cha_set)
        for j in range(1, max_nb_cha + 1)
    ]
    pred_nbs = one_hot_decoder(predictions['output_nb'], len_set)
    res = {}
    for i, img_val in enumerate(img_vals):
        length = min(max_nb_cha, pred_nbs[i])
        ans = []
        for j in range(length):
            ans.append(pred_chas[j][i])
        ans = ''.join(ans)

        valid = True
        if sys.getsizeof(img_val.stream) > 3000:  # Atten tion!
            valid = False
        form = {'valid': valid, 'answer': ans, 'expr': ans}
        res[keys[i]] = form

    print res, len(res)
    return json.dumps(res)
Example #4
0
def predict(model, post_vals):
    img_width, img_height = 223, 50
    img_channels = 3 
    max_nb_cha = 6 # 文本最大长度
    len_set = range(1, max_nb_cha+1) # 文本可能的长度
    cha_set = list("0123456789qwertyuioplkjhgfdsazxcvbnm")# 文本字符集
    nb_classes = 36

    keys = post_vals.keys()
    img_vals = post_vals.values()
    X_test = load_data(img_vals, img_width, img_height, img_channels)
    predictions = model.predict({'input':X_test})
    pred_chas = [one_hot_decoder(predictions['output%d' % j], cha_set) for j in range(1, max_nb_cha+1)]
    pred_nbs = one_hot_decoder(predictions['output_nb'], len_set)
    res = {}
    for i, img_val in enumerate(img_vals):
        length = min(max_nb_cha, pred_nbs[i])
        ans = []
        for j in range(length):
            ans.append(pred_chas[j][i])
        ans = ''.join(ans)

        valid = True
        if sys.getsizeof(img_val.stream) > 3000: # Atten tion!
            valid = False
        form = {'valid':valid, 'answer':ans, 'expr':ans}
        res[keys[i]] = form
    
    print res, len(res)
    return json.dumps(res)
Example #5
0
def pred(model, X, char_set, label_set, post_correction):
	pred_res = model.predict(X)
	pred_res = [one_hot_decoder(i, char_set) for i in pred_res]
	pred_res = [list2str(i) for i in pred_res]
	# post correction
	if post_correction:
		pred_res = correction(pred_res, label_set)
	return pred_res
Example #6
0
def pred(model, X, char_set, label_set, post_correction):
    pred_res = model.predict(X)
    pred_res = [one_hot_decoder(i, char_set) for i in pred_res]
    pred_res = [list2str(i) for i in pred_res]
    # post correction
    if post_correction:
        pred_res = correction(pred_res, label_set)
    return pred_res
Example #7
0
 def pred(self, X):
     pred_res = self.model.predict(X, batch_size=256)
     self.pred_probs = pred_res
     pred_res = [one_hot_decoder(i, self.char_set) for i in pred_res]
     pred_res = [list2str(i) for i in pred_res]
     # post correction
     if self.post_correction:
         pred_res = correction(pred_res, self.label_set)
     return pred_res
Example #8
0
 def pred(self, X):
     pred_res = self.model.predict(X, batch_size=256)
     self.pred_probs = pred_res
     pred_res = [one_hot_decoder(i, self.char_set) for i in pred_res]
     pred_res = [list2str(i) for i in pred_res]
     # post correction
     if self.post_correction:
         pred_res = correction(pred_res, self.label_set)
     return pred_res
Example #9
0
def test_single(model, len_set, cha_set, max_nb_cha, X_test, Y_test_nb,
                Y_test):
    # 开始预测并验证准确率,需要先把预测结果从概率转到对应的标签
    predictions = model.predict(X_test)
    print model.predict_proba(X_test)[86]
    # print predictions
    pred_chas = one_hot_decoder(predictions, cha_set)
    Y_test = one_hot_decoder(Y_test[0], cha_set)

    correct = 0
    nb_sample = X_test.shape[0]
    for i in range(nb_sample):
        allright = pred_chas[i] == Y_test[i]
        if allright:
            correct += 1
        else:
            print 'id:' + str(i + 1), pred_chas[i], Y_test[i]
            pass
    print 'Accuracy:', float(correct) / nb_sample
Example #10
0
def test(model, test_data, char_set, post_correction):
	test_X, test_y = test_data[0], test_data[1]
	test_y = [one_hot_decoder(i, char_set) for i in test_y]
	test_y = [list2str(i) for i in test_y]
	pred_res = pred(model, test_X, char_set, post_correction)

	nb_correct = sum(pred_res[i]==test_y[i] for i in range(len(pred_res)))
	for i in range(len(pred_res)):
		if test_y[i] != pred_res[i]:
			print ('test:', test_y[i])
			print ('pred:', pred_res[i])
			pass
	print ('nb_correct: ', nb_correct)
	print ('Acurracy: ', float(nb_correct) / len(pred_res))
Example #11
0
def predict(model, imgdata):
    img_width, img_height = 223, 50
    img_channels = 3 
    max_nb_cha = 6 # 文本最大长度
    len_set = range(1, max_nb_cha+1) # 文本可能的长度
    cha_set = list("0123456789qwertyuioplkjhgfdsazxcvbnm")# 文本字符集
    nb_classes = 36

    X_test = load_data(imgdata, img_width, img_height, img_channels)
    # print X_test
    predictions = model.predict({'input':X_test})
    pred_chas = [one_hot_decoder(predictions['output%d' % j], cha_set) for j in range(1, max_nb_cha+1)]
    pred_nbs = one_hot_decoder(predictions['output_nb'], len_set)
    length = min(max_nb_cha, pred_nbs[0])
    ans = []
    for i in range(length):
        ans.append(pred_chas[i][0])
    ans = ''.join(ans)
    # print ans
    # print predictions['output1']
    res = {'valid':True, 'answer':ans, 'expr':ans}
    
    return json.dumps(res)
Example #12
0
def test(model, test_data, char_set, label_set, post_correction):
	test_X, test_y = test_data[0], test_data[1]
	test_y = [one_hot_decoder(i, char_set) for i in test_y]
	test_y = [list2str(i) for i in test_y]
	pred_res = pred(model, test_X, char_set, label_set, post_correction)
	# for i in pred_res:
	# 	print i
	nb_correct = sum(pred_res[i]==test_y[i] for i in range(len(pred_res)))
	for i in range(len(pred_res)):
		if test_y[i] != pred_res[i]:
			print 'test:', test_y[i]
			print 'pred:', pred_res[i]
			pass
	print 'nb_correct: ', nb_correct
	print 'Acurracy: ', float(nb_correct) / len(pred_res)