图片验证码部分代码整理

X = tf.placeholder(tf.float32,[None, IMAGE_HEIGHT * IMAGE_WIDTH])
Y = tf.placeholder(tf.int32,[None, char_len_max])
keep_prob = tf.placeholder(tf.float32)  # dropout
w_alpha = 0.1
b_alpha = 0.1
lr = 0.0006  # 学习率

batch_size=64

def add_layer(input=None, w_shape=None, b_shape=None, conv2d=False, active_func=None, name=None):
    # 前两维是patch的大小,第三维时输入通道的数目,最后一维是输出通道的数目。我们对每个输出通道加上了偏置(bias)
    with tf.name_scope(name):
        with tf.name_scope('weights'):
            w = tf.Variable(w_alpha * tf.random_normal(w_shape))
            #tf.summary.histogram(name+'/weights',w)
        with tf.name_scope('biases'):
            b = tf.Variable(b_alpha * tf.random_normal(b_shape))
            #tf.summary.histogram(name+'/biases',b)
        # 卷基层与池化层
        if conv2d and active_func:
            conv = active_func(tf.nn.bias_add(tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding='SAME'), b))
            conv = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
            conv = tf.nn.dropout(conv, keep_prob=keep_prob)
            #tf.summary.histogram(name + '/conv', conv)
            return conv
        # 全连接层
        elif active_func:
            dense = tf.reshape(input, [-1, w_shape[0]])
            dense = active_func(tf.add(tf.matmul(dense, w), b))
            dense = tf.nn.dropout(dense, keep_prob=keep_prob)
            #tf.summary.histogram(name+'/dense',dense)
            return dense
        # 输出层
        else:
            out = tf.add(tf.matmul(input, w), b)
            #tf.summary.histogram(name+'/output',out)
            return out


# 定义模型
def model():
    # 为了使得图片与计算层匹配,我们首先reshape输入图像x为4维的tensor,第2、3维对应图片的宽和高,最后一维对应颜色通道的数目。
    x = tf.reshape(X, [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])

    # w_shape前两维是patch的大小,第三维时输入通道的数目,最后一维是输出通道的数目。我们对每个输出通道加上了偏置(bias)
    # 第一层
    layer1 = add_layer(input=x, w_shape=[3, 3, 1, 32], b_shape=[32], conv2d=True, active_func=tf.nn.relu, name='layer1')
    # 第二层
    layer2 = add_layer(input=layer1, w_shape=[3, 3, 32, 64], b_shape=[64], conv2d=True, active_func=tf.nn.relu,
                       name='layer2')
    # 第三层
    layer3 = add_layer(input=layer2, w_shape=[3, 3, 64, 64], b_shape=[64], conv2d=True, active_func=tf.nn.relu,
                       name='layer3')
    # 全连接层
    layer_full = add_layer(input=layer3, w_shape=[2 * 40 * 64, 1024], b_shape=[1024], active_func=tf.nn.relu,
                           name='layer_full')
    # 输出层
    layer_out = add_layer(input=layer_full, w_shape=[1024, char_total_num*char_len_max],
                          b_shape=[char_total_num*char_len_max], name='layer_out')
    return layer_out


# 训练
def train_model():
    #tf.summary.scalar('accuracy',accuracy)
    pred = model()
    # 损失函数
    pred=tf.reshape(pred,[-1,char_total_num])
    target = tf.reshape(Y, [-1])
    loss = sequence_loss_by_example([pred], [target], [tf.ones_like(target, dtype=tf.float32)])
    cost = tf.reduce_sum(loss) / batch_size
    train_op = tf.train.AdamOptimizer(lr).minimize(cost)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值