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

test_nn.py运行结果错误 #19

Open
a790399215 opened this issue Jul 18, 2019 · 4 comments
Open

test_nn.py运行结果错误 #19

a790399215 opened this issue Jul 18, 2019 · 4 comments

Comments

@a790399215
Copy link

想请教一下,我运行create_traindata生成了5000张图片,batch_size是256训练了20轮,但是在运行test_nn.py文件输出的结果却是完全错误的,想请教一下大家有遇到这种情况吗

@a790399215
Copy link
Author

例如:藏UPXSGW车牌被识别成:藏PPPPPP

@a790399215
Copy link
Author

后面六个字符的识别结果基本是一样的

@a790399215
Copy link
Author

test_nn.py代码如下:
`import numpy as np
import pickle
from keras.layers import Dense, Input, BatchNormalization, Conv2D, Flatten, MaxPooling2D, Activation, Reshape, Layer
from keras.models import Model, load_model, model_from_json
from keras.callbacks import TensorBoard, ModelCheckpoint, LearningRateScheduler
from keras.optimizers import RMSprop
from keras import backend as K
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

chars = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
"琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z"]

class NormLayer(Layer):

def __init__(self, **kwargs):
    super(NormLayer, self).__init__(**kwargs)

def build(self, input_shape):
    self.kernal = self.add_weight(name='NormLayer', shape=(
        1, 13), initializer='ones', trainable=True)
    super(NormLayer, self).build(input_shape)

def call(self, inputs):
    # out = self.kernal * inputs
    out = K.dot(self.kernal, inputs)
    out = K.permute_dimensions(out, (1, 0, 2))
    return out[:, 0, :]
''' because this NormLayer do not change the input_shape,
    so the compute_output_shape need not to implement (maybe)
'''

def compute_output_shape(self, input_shape):
    return (input_shape[0], 65)

def get_config(self):
    # config = {}
    base_config = super(NormLayer, self).get_config()
    return dict(list(base_config.items()))
    # return dict(list(base_config.items()) + list(config.items()))

e2e_model = load_model('e2e_model.h5', custom_objects={'NormLayer': NormLayer})
label_path = 'label.txt'
tem_label = np.loadtxt(label_path)
row, col = tem_label.shape
label1 = np.zeros([row, 31])
label2 = np.zeros([row, 34])
label3 = np.zeros([row, 34])
label4 = np.zeros([row, 34])
label5 = np.zeros([row, 34])
label6 = np.zeros([row, 34])
label7 = np.zeros([row, 34])
for i in range(row):
label1[i, int(tem_label[i, 0])] = 1
label2[i, int(tem_label[i, 1]) - 31] = 1
label3[i, int(tem_label[i, 2]) - 31] = 1
label4[i, int(tem_label[i, 3]) - 31] = 1
label5[i, int(tem_label[i, 4]) - 31] = 1
label6[i, int(tem_label[i, 5]) - 31] = 1
label7[i, int(tem_label[i, 6]) - 31] = 1

img_path = './train_data.pkl'
img_data = np.load(img_path,allow_pickle=True)
img_data = img_data.transpose(0, 2, 1, 3)

e2e_predict = e2e_model.predict(img_data[0:5, :, :, :])

def print_trueLabel(num):
print(np.array([np.argmax(label1[num, :]), np.argmax(label2[num, :]) + 31, np.argmax(label3[num, :]) + 31,
np.argmax(label4[num, :]) + 31, np.argmax(label5[num, :]) + 31, np.argmax(label6[num, :]) + 31,
np.argmax(label7[num, :]) + 31]))
print(chars[np.argmax(label1[num, :])] + chars[np.argmax(label2[num, :]) + 31] + chars[np.argmax(label3[num, :]) + 31] +
chars[np.argmax(label4[num, :]) + 31] + chars[np.argmax(label5[num, :]) + 31] + chars[np.argmax(label6[num, :]) + 31] +
chars[np.argmax(label7[num, :]) + 31])

def print_predictLabel(x):
num = x[0].shape[0]
sort = np.zeros([num, len(x)])
for i in range(len(x)):
temp = x[i]
sort[:,i] = np.argmax(temp, 1)
for i in range(num):
print(np.array([int(sort[i, 0]), int(sort[i, 1] + 31), int(sort[i, 2] + 31), int(sort[i, 3] + 31), int(sort[i, 4] + 31), int(sort[i, 5] + 31), int(sort[i, 6] + 31)]))
print(chars[int(sort[i, 0])] + chars[int(sort[i, 1] + 31)] + chars[int(sort[i, 2] + 31)] +
chars[int(sort[i, 3] + 31)] + chars[int(sort[i, 4] + 31)] + chars[int(sort[i, 5] + 31)] +
chars[int(sort[i, 6] + 31)])

for i in range(5):
print_trueLabel(i)
print_predictLabel(e2e_predict)`

@caisan
Copy link

caisan commented Oct 31, 2019

test_nn.py是哪里的?

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