tensorflow已经出到2.0.0版本,之前的环境搭建也是为2.0.0版本api服务的。
之前demo里举的captcha的验证码识别模型的例子却是1.*版本api,所以要在导入步骤时:
import tensorflow as tf => import tensorflow.compat.v1 as tf
今天抽空熟悉一下2.0版本中keras的api,试着搭建了一下,发现2.0版本api搭建模型真的好方便!
原来的3个文件减为2个,具体代码如下:
模型并不一一对应,只是个模式举例。
第一个文件没变化:capthca_model.py
#!/usr/bin/python
# -*- coding: utf-8 -*
import tensorflow.compat.v1 as tf
import math
class captchaModel():
def __init__(self,
width = 160,
height = 60,
char_num = 4,
classes = 62):
self.width = width
self.height = height
self.char_num = char_num
self.classes = classes
def conv2d(self,x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(self,x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(self,shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(self,shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def create_model(self,x_images,keep_prob):
#first layer
w_conv1 = self.weight_variable([5, 5, 1, 32])
b_conv1 = self.bias_variable([32])
h_conv1 = tf.nn.relu(tf.nn.bias_add(self.conv2d(x_images, w_conv1), b_conv1))
h_pool1 = self.max_pool_2x2(h_conv1)
h_dropout1 = tf.nn.dropout(h_pool1,keep_prob)
conv_width = math.ceil(self.width/2)
conv_height = math.ceil(self.height/2)
#second layer
w_conv2 = self.weight_variable([5, 5, 32, 64])
b_conv2 = self.bias_variable([64])
h_conv2 = tf.nn.relu(tf.nn.bias_add(self.conv2d(h_dropout1, w_conv2), b_conv2))
h_pool2 = self.max_pool_2x2(h_conv2)
h_dropout2 = tf.nn.dropout(h_pool2,keep_prob)
conv_width = math.ceil(conv_width/2)
conv_height = math.ceil(conv_height/2)
#third layer
w_conv3 = self.weight_variable([5, 5, 64, 64])
b_conv3 = self.bias_variable([64])
h_conv3 = tf.nn.relu(tf.nn.bias_add(self.conv2d(h_dropout2, w_conv3), b_conv3))
h_pool3 = self.max_pool_2x2(h_conv3)
h_dropout3 = tf.nn.dropout(h_pool3,keep_prob)
conv_width = math.ceil(conv_width/2)
conv_height = math.ceil(conv_height/2)
#first fully layer
conv_width = int(conv_width)
conv_height = int(conv_height)
w_fc1 = self.weight_variable([64*conv_width*conv_height,1024])
b_fc1 = self.bias_variable([1024])
h_dropout3_flat = tf.reshape(h_dropout3,[-1,64*conv_width*conv_height])
h_fc1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(h_dropout3_flat, w_fc1), b_fc1))
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#second fully layer
w_fc2 = self.weight_variable([1024,self.char_num*self.classes])
b_fc2 = self.bias_variable([self.char_num*self.classes])
y_conv = tf.add(tf.matmul(h_fc1_drop, w_fc2), b_fc2)
return y_conv
另一个文件文件:train.py
# coding:utf8
import os
import string
import numpy as np
import tensorflow as tf
from captcha_tensorflow_apiv2 import gen_captcha
def crack_captcha_cnn(MAX_CAPTCHA, CHAR_SET_LEN):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3, 3)))
model.add(tf.keras.layers.PReLU())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool2D((2, 2), strides=2))
model.add(tf.keras.layers.Conv2D(64, (5, 5)))
model.add(tf.keras.layers.PReLU())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool2D((2, 2), strides=2))
model.add(tf.keras.layers.Conv2D(128, (5, 5)))
model.add(tf.keras.layers.PReLU())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool2D((2, 2), strides=2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(MAX_CAPTCHA * CHAR_SET_LEN))
model.add(tf.keras.layers.Reshape([MAX_CAPTCHA, CHAR_SET_LEN]))
model.add(tf.keras.layers.Softmax())
return model
if __name__ == '__main__':
captcha = gen_captcha.generateCaptcha()
characters = string.digits + string.ascii_uppercase
width, height, char_num, characters, classes = captcha.get_parameter()
SAVE_PATH = os.path.abspath("./")
try:
model = tf.keras.models.load_model(SAVE_PATH + 'model')
except Exception as e:
print('#######Exception', e)
model = crack_captcha_cnn(4,62)
model.compile(optimizer='Adam',
metrics=['accuracy'],
loss='categorical_crossentropy')
for times in range(500000):
batch_x, batch_y = next(captcha.gen_captcha(batch_size=512))
print('times=', times, ' batch_x.shape=', batch_x.shape, ' batch_y.shape=', batch_y.shape)
import os
print(os.path.abspath('./'))
model.fit(batch_x, batch_y, epochs=4)
print("y预测=\n", np.argmax(model.predict(batch_x), axis=2))
print("y实际=\n", np.argmax(batch_y, axis=2))
if 0 == times % 10:
print("save model at times=", times)
model.save(SAVE_PATH + 'model')
主要代码是这里:
def crack_captcha_cnn(MAX_CAPTCHA, CHAR_SET_LEN):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3, 3)))
model.add(tf.keras.layers.PReLU())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool2D((2, 2), strides=2))
model.add(tf.keras.layers.Conv2D(64, (5, 5)))
model.add(tf.keras.layers.PReLU())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool2D((2, 2), strides=2))
model.add(tf.keras.layers.Conv2D(128, (5, 5)))
model.add(tf.keras.layers.PReLU())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.MaxPool2D((2, 2), strides=2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(MAX_CAPTCHA * CHAR_SET_LEN))
model.add(tf.keras.layers.Reshape([MAX_CAPTCHA, CHAR_SET_LEN]))
model.add(tf.keras.layers.Softmax())
return model