Open zhaohaao111 opened 6 years ago
同求,需要吧 tf.constant(input_images) 改成 tf.var... 但是不会呀
已经处理好了, captcha_model.py 中增加方法
def test(images, keep_prob): images = tf.reshape(images, [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])
with tf.variable_scope('conv1') as scope:
kernel = _weight_variable('weights', shape=[3, 3, 1, 64])
biases = _bias_variable('biases', [64])
pre_activation = tf.nn.bias_add(_conv2d(images, kernel), biases)
conv1 = tf.nn.relu(pre_activation, name=scope.name)
pool1 = _max_pool_2x2(conv1, name='pool1')
with tf.variable_scope('conv2') as scope:
kernel = _weight_variable('weights', shape=[3, 3, 64, 64])
biases = _bias_variable('biases', [64])
pre_activation = tf.nn.bias_add(_conv2d(pool1, kernel), biases)
conv2 = tf.nn.relu(pre_activation, name=scope.name)
pool2 = _max_pool_2x2(conv2, name='pool2')
with tf.variable_scope('conv3') as scope:
kernel = _weight_variable('weights', shape=[3, 3, 64, 64])
biases = _bias_variable('biases', [64])
pre_activation = tf.nn.bias_add(_conv2d(pool2, kernel), biases)
conv3 = tf.nn.relu(pre_activation, name=scope.name)
pool3 = _max_pool_2x2(conv3, name='pool3')
with tf.variable_scope('conv4') as scope:
kernel = _weight_variable('weights', shape=[3, 3, 64, 64])
biases = _bias_variable('biases', [64])
pre_activation = tf.nn.bias_add(_conv2d(pool3, kernel), biases)
conv4 = tf.nn.relu(pre_activation, name=scope.name)
pool4 = _max_pool_2x2(conv4, name='pool4')
with tf.variable_scope('local1') as scope:
batch_size = 1 # images.get_shape()[0].value
reshape = tf.reshape(pool4, [batch_size, -1])
# dim = reshape.get_shape()[1].value
# dim = 512 # for 26x60
dim = 2048 # for 52x120
# dim = 3200 # for 70x160
weights = _weight_variable('weights', shape=[dim, 1024])
biases = _bias_variable('biases', [1024])
local1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name)
local1_drop = tf.nn.dropout(local1, keep_prob)
with tf.variable_scope('softmax_linear') as scope:
weights = _weight_variable('weights', shape=[1024, CHARS_NUM * CLASSES_NUM])
biases = _bias_variable('biases', [CHARS_NUM * CLASSES_NUM])
softmax_linear = tf.add(tf.matmul(local1_drop, weights), biases, name=scope.name)
return tf.reshape(softmax_linear, [-1, CHARS_NUM, CLASSES_NUM])
调用模型
def input_image(image_path, image_height, image_width): image = Image.open(image_path) image_gray = image.convert('L') image_resize = image_gray.resize(size=(image_width, image_height)) image.close() input_img = np.array(image_resize, dtype='float32') input_img = np.multiply(input_img.flatten(), 1. / 255) - 0.5 return np.reshape(input_img, (image_height, image_width, 1))
tf.placeholder(tf.float32, [None, image_height, image_width, 1]) # 特征向量 logits = captcha.test(self.X, keep_prob=1) ... image = input_image(img, self.image_height, self.image_width) ... predict_result = sess.run(self.predict, feed_dict={self.X: [image]})
我在用你的程序做验证码识别,每次识别都会重新加载模型,有什么办法只让模型加载一次吗?