教练过程中生成数据,生成式对搞网络包含一个变迁模型(generative

if __name__ ==’__main__’:
#概念超参数
nb_epochs = 50
batch_size = 100
latent_size = 100
#优化器学习率
adam_lr = 0.0002
adam_beta_l = 0.5
#构建判别网络
discriminator = build_discriminator()
discriminator.compile(optimizer=adam(lr=adam_lr,
beta_l=adam_beta_l), loss=’binary_crossentropy’)
latent = Input(shape=(lastent_size, ))
image_class = Input(shape-(1, ), dtype=’int32′)
#变化组合模型
discriminator.trainable = False
fake, aux = discriminator(fake)
combined = Model(input=[latent, image_class], output=[fake, aux])
combined.compile(optimizer=Adam(lr=adam_lr, beta_l=adam_beta_1),
loss=[‘binary_crossentropy’, ‘sparse_categorical_crossentropy’])
#将mnist数据转发为(…,1,28,28)维度,取值范围为[-1,1]
(X_train,y_train),(X_test,y_test) = mnist.load_data()
X_train = (X_train.astype(np.float32) – 127.5) / 127.5
X_train = np.expand_dims(X_train, axis=1)
X_test = (X_test.astype(np.float32) – 127.5) / 127.5
X_test = np.expand_dims(X_test, axis=1)
num_train, num_test = X_train.shape[0], X_test.shape[0]
train_history = defaultdict(list)
test_history = defaultdict(list)
for epoch in range(epochs):
print(‘Epoch {} of {}’.format(epoch + 1, epochs))
num_batches = int(X_train.shape[0] / batch_size)
progress_bar = Progbar(target=num_batches)
epoch_gen_loss = []
epoch_disc_loss = []
for index in range(num_batches):
progress_bar.update(index)
#发出一个批次的噪声数据
noise = np.random.uniform(-1, 1, (batch_size, latent_size))
# 获取一个批次的实在数据
image_batch = X_train[index * batch_size:(index + 1) *
batch_size]
label_batch = y_train[index * batch_size:(index + 1) *
batch_size]
# 生成一些噪音标记
sampled_labels = np.random.randint(0, 10, batch_size)
# 发生一个批次的虚伪图片
generated_images = generator.predict(
[noise, sampled_labels.reshape((-1, 1))], verbose=0)
X = np.concatenate((image_batch, generated_images))
y = np.array([1] * batch_size + [0] * batch_size)
aux_y = np.concatenate((label_batch, sampled_labels), axis=0)
epoch_disc_loss.append(discriminator.train_on_batch(X, [y,
aux_y]))
# 暴发五个批次噪声和符号
noise = np.random.uniform(-1, 1, (2 * batch_size, latent_size))
sampled_labels = np.random.randint(0, 10, 2 * batch_size)
# 练习转变模型来掩人耳目判别模型,输出真/假都设为真
trick = np.ones(2 * batch_size)
epoch_gen_loss.append(combined.train_on_batch(
[noise, sampled_labels.reshape((-1, 1))],
[trick, sampled_labels]))
print(‘\nTesting for epoch {}:’.format(epoch + 1))
# 评估测试集,暴发一个新批次噪声数据
noise = np.random.uniform(-1, 1, (num_test, latent_size))
sampled_labels = np.random.randint(0, 10, num_test)
generated_images = generator.predict(
[noise, sampled_labels.reshape((-1, 1))], verbose=False)
X = np.concatenate((X_test, generated_images))
y = np.array([1] * num_test + [0] * num_test)
aux_y = np.concatenate((y_test, sampled_labels), axis=0)
# 判别模型是否能鉴别
discriminator_test_loss = discriminator.evaluate(
X, [y, aux_y], verbose=False)
discriminator_train_loss = np.mean(np.array(epoch_disc_loss),
axis=0)
# 创设多少个批次新噪声数据
noise = np.random.uniform(-1, 1, (2 * num_test, latent_size))
sampled_labels = np.random.randint(0, 10, 2 * num_test)
trick = np.ones(2 * num_test)
generator_test_loss = combined.evaluate(
[noise, sampled_labels.reshape((-1, 1))],
[trick, sampled_labels], verbose=False)
generator_train_loss = np.mean(np.array(epoch_gen_loss), axis=0)
# 损失值等性能目的记录下来,并出口
train_history[‘generator’].append(generator_train_loss)
train_history[‘discriminator’].append(discriminator_train_loss)
test_history[‘generator’].append(generator_test_loss)
test_history[‘discriminator’].append(discriminator_test_loss)
print(‘{0:<22s} | {1:4s} | {2:15s} | {3:5s}’.format(
‘component’, *discriminator.metrics_names))
print(‘-‘ * 65)
ROW_FMT = ‘{0:<22s} | {1:<4.2f} | {2:<15.2f} | {3:<5.2f}’
print(ROW_FMT.format(‘generator (train)’,
*train_history[‘generator’][-1]))
print(ROW_FMT.format(‘generator (test)’,
*test_history[‘generator’][-1]))
print(ROW_FMT.format(‘discriminator (train)’,
*train_history[‘discriminator’][-1]))
print(ROW_FMT.format(‘discriminator (test)’,
*test_history[‘discriminator’][-1]))
# 每个epoch保存一回权重
generator.save_weights(
‘params_generator_epoch_{0:03d}.hdf5’.format(epoch), True)
discriminator.save_weights(
‘params_discriminator_epoch_{0:03d}.hdf5’.format(epoch), True)
# 生成一些可视化虚假数字看衍变过程
noise = np.random.uniform(-1, 1, (100, latent_size))
sampled_labels = np.array([
[i] * 10 for i in range(10)
]).reshape(-1, 1)
generated_images = generator.predict(
[noise, sampled_labels], verbose=0)
# 整理到一个方格
img = (np.concatenate([r.reshape(-1, 28)
for r in np.split(generated_images, 10)
], axis=-1) * 127.5 + 127.5).astype(np.uint8)
Image.fromarray(img).save(
‘plot_epoch_{0:03d}_generated.png’.format(epoch))
pickle.dump({‘train’: train_history, ‘test’: test_history},
open(‘acgan-history.pkl’, ‘wb’))

if __name__ ==’__main__’:
#概念超参数
nb_epochs = 50
batch_size = 100
latent_size = 100
#优化器学习率
adam_lr = 0.0002
adam_beta_l = 0.5
#构建判别网络
discriminator = build_discriminator()
discriminator.compile(optimizer=adam(lr=adam_lr,
beta_l=adam_beta_l), loss=’binary_crossentropy’)
latent = Input(shape=(lastent_size, ))
image_class = Input(shape-(1, ), dtype=’int32′)
#变迁组合模型
discriminator.trainable = False
fake, aux = discriminator(fake)
combined = Model(input=[latent, image_class], output=[fake, aux])
combined.compile(optimizer=Adam(lr=adam_lr, beta_l=adam_beta_1),
loss=[‘binary_crossentropy’, ‘sparse_categorical_crossentropy’])
#将mnist数据转发为(…,1,28,28)维度,取值范围为[-1,1]
(X_train,y_train),(X_test,y_test) = mnist.load_data()
X_train = (X_train.astype(np.float32) – 127.5) / 127.5
X_train = np.expand_dims(X_train, axis=1)
X_test = (X_test.astype(np.float32) – 127.5) / 127.5
X_test = np.expand_dims(X_test, axis=1)
num_train, num_test = X_train.shape[0], X_test.shape[0]
train_history = defaultdict(list)
test_history = defaultdict(list)
for epoch in range(epochs):
print(‘Epoch {} of {}’.format(epoch + 1, epochs))
num_batches = int(X_train.shape[0] / batch_size)
progress_bar = Progbar(target=num_batches)
epoch_gen_loss = []
epoch_disc_loss = []
for index in range(num_batches):
progress_bar.update(index)
#暴发一个批次的噪声数据
noise = np.random.uniform(-1, 1, (batch_size, latent_size))
# 获取一个批次的实事求是数据
image_batch = X_train[index * batch_size:(index + 1) *
batch_size]
label_batch = y_train[index * batch_size:(index + 1) *
batch_size]
# 生成一些噪音标记
sampled_labels = np.random.randint(0, 10, batch_size)
# 暴发一个批次的仿真图片
generated_images = generator.predict(
[noise, sampled_labels.reshape((-1, 1))], verbose=0)
X = np.concatenate((image_batch, generated_images))
y = np.array([1] * batch_size + [0] * batch_size)
aux_y = np.concatenate((label_batch, sampled_labels), axis=0)
epoch_disc_loss.append(discriminator.train_on_batch(X, [y,
aux_y]))
# 暴发六个批次噪声和标志
noise = np.random.uniform(-1, 1, (2 * batch_size, latent_size))
sampled_labels = np.random.randint(0, 10, 2 * batch_size)
# 操练转变模型来欺骗判别模型,输出真/假都设为真
trick = np.ones(2 * batch_size)
epoch_gen_loss.append(combined.train_on_batch(
[noise, sampled_labels.reshape((-1, 1))],
[trick, sampled_labels]))
print(‘\nTesting for epoch {}:’.format(epoch + 1))
# 评估测试集,暴发一个新批次噪声数据
noise = np.random.uniform(-1, 1, (num_test, latent_size))
sampled_labels = np.random.randint(0, 10, num_test)
generated_images = generator.predict(
[noise, sampled_labels.reshape((-1, 1))], verbose=False)
X = np.concatenate((X_test, generated_images))
y = np.array([1] * num_test + [0] * num_test)
aux_y = np.concatenate((y_test, sampled_labels), axis=0)
# 判别模型是否能识别
discriminator_test_loss = discriminator.evaluate(
X, [y, aux_y], verbose=False)
discriminator_train_loss = np.mean(np.array(epoch_disc_loss),
axis=0)
# 创造多少个批次新噪声数据
noise = np.random.uniform(-1, 1, (2 * num_test, latent_size))
sampled_labels = np.random.randint(0, 10, 2 * num_test)
trick = np.ones(2 * num_test)
generator_test_loss = combined.evaluate(
[noise, sampled_labels.reshape((-1, 1))],
[trick, sampled_labels], verbose=False)
generator_train_loss = np.mean(np.array(epoch_gen_loss), axis=0)
# 损失值等性能目标记录下来,并出口
train_history[‘generator’].append(generator_train_loss)
train_history[‘discriminator’].append(discriminator_train_loss)
test_history[‘generator’].append(generator_test_loss)
test_history[‘discriminator’].append(discriminator_test_loss)
print(‘{0:<22s} | {1:4s} | {2:15s} | {3:5s}’.format(
‘component’, *discriminator.metrics_names))
print(‘-‘ * 65)
ROW_FMT = ‘{0:<22s} | {1:<4.2f} | {2:<15.2f} | {3:<5.2f}’
print(ROW_FMT.format(‘generator (train)’,
*train_history[‘generator’][-1]))
print(ROW_FMT.format(‘generator (test)’,
*test_history[‘generator’][-1]))
print(ROW_FMT.format(‘discriminator (train)’,
*train_history[‘discriminator’][-1]))
print(ROW_FMT.format(‘discriminator (test)’,
*test_history[‘discriminator’][-1]))
# 每个epoch保存一遍权重
generator.save_weights(
‘params_generator_epoch_{0:03d}.hdf5’.format(epoch), True)
discriminator.save_weights(
‘params_discriminator_epoch_{0:03d}.hdf5’.format(epoch), True)
# 生成一些可视化虚假数字看衍生和变化过程
noise = np.random.uniform(-1, 1, (100, latent_size))
sampled_labels = np.array([
[i] * 10 for i in range(10)
]).reshape(-1, 1)
generated_images = generator.predict(
[noise, sampled_labels], verbose=0)
# 整理到一个方格
img = (np.concatenate([r.reshape(-1, 28)
for r in np.split(generated_images, 10)
], axis=-1) * 127.5 + 127.5).astype(np.uint8)
Image.fromarray(img).save(
‘plot_epoch_{0:03d}_generated.png’.format(epoch))
pickle.dump({‘train’: train_history, ‘test’: test_history},
open(‘acgan-history.pkl’, ‘wb’))

3.2.3举行结果

step:75173,loss:0.010555

step:75174,loss:0.009410

step:75175,loss:0.009978

step:75176,loss:0.008089

step:75177,loss:0.009949

step:75178,loss:0.010126

step:75179,loss:0.009584

step:75180,loss:0.012272

step:75181,loss:0.010157

step:75182,loss:0.009529

step:75183,loss:0.007636

step:75184,loss:0.009058

step:75185,loss:0.010061

step:75186,loss:0.009941

step:75187,loss:0.009339

step:75188,loss:0.009685

step:75189,loss:0.009879

step:75190,loss:0.007799

step:75191,loss:0.010866

step:75192,loss:0.009838

step:75193,loss:0.010931

step:75194,loss:0.012859

step:75195,loss:0.008747

step:75196,loss:0.009147

step:75197,loss:0.009351

step:75198,loss:0.009746

step:75199,loss:0.010014

step:75200,loss:0.009024

###############################################step:75200,accuracy:0.992500

原理。
生成式对搞网络包含一个变通模型(generative
model,G)和一个鉴别模型(discriminative model,D)。伊恩 J.Goodfellow、姬恩(Jean)Pouget-Abadie、Mehdi Mirza、Bing Xu、David 沃德(Ward)e-Farley、Sherjil
Ozair、亚伦(Aaron) Courville、Yoshua Bengio小说,《Generative Adversarial
Network》,https://arxiv.org/abs/1406.2661
生成式对抗网络布局:
噪音数据->生成模型->假图片—|
|->判别模型->真/假
打乱操练多少->磨练集->真图片-|
生成式对抗网络根本解决哪些从操练样本中学习出新样本。生成模型负责锻炼出样本的遍布,假若锻炼样本是图形就转变相似的图片,若是锻炼样本是作品名子就成形相似的稿子名子。判别模型是一个二分类器,用来判断输入样本是开诚相见数据仍然教练转变的范本。
生成式对抗网络优化,是一个二元极小极大博弈(minimax two-player
game)问题。使生成模型输出在输入给判别模型时,判断模型秀难判断是真性数据或者虚似数据。训练好的浮动模型,能把一个噪声向量转化成和教练集类似的样本。Argustus
Odena、Christopher Olah、Jonathon Shlens杂文《Coditional Image Synthesis
with Auxiliary Classifier GANs》。
援救分类器生成式对抗网络(auxiliary classifier GAN,AC-GAN)实现。

生成式对抗网络立异。生成式对抗网络(generative adversarial
network,GAN)在无监控学习不行有效。常规生成式对抗网络判别器使用Sigmoid交叉熵损失函数,学习过程梯度消失。Wasserstein生成式对抗网络(Wasserstein
generative adversarial
network,WGAN),使用Wasserstein距离度量,而不是Jensen-Shannon散度(Jensen-Shannon
divergence,JSD)。使用最小二乘生成式对抗网络(least squares generative
adversarial network,LSGAN),判别模型用小小平方损失小函数(least squares
loss function)。Sebastian Nowozin、Botond Cseke、Ryota
汤姆(Tom)ioka散文《f-GAN: Training Generative Neural 山姆plers using
Variational Divergence Minimization》。

3.2.2 然后实施

cd /home/ubuntu;

python train_captcha.py

def build_generator(latent_size):
cnn = Sequential()
cnn.add(Dense(1024, input_dim=latent_size, activation=’relu’))
cnn.add(Dense(128 * 7 * 7, activation=’relu’))
cnn.add(Reshape((128, 7, 7)))
#上采样,图你尺寸变为 14X14
cnn.add(UpSampling2D(size=(2,2)))
cnn.add(Convolution2D(256, 5, 5, border_mode=’same’, activation=’relu’,
init=’glorot_normal’))
#上采样,图像尺寸变为28X28
cnn.add(UpSampling2D(size=(2,2)))
cnn.add(Convolution2D(128, 5, 5, border_mode=’same’, activation=’relu’,
init=’glorot_normal’))
#规约到1个通道
cnn.add(Convolution2D(1, 2, 2, border_mode=’same’, activation=’tanh’,
init=’glorot_normal’))
#变更模型输入层,特征向量
latent = Input(shape=(latent_size, ))
#变迁模型输入层,标记
image_class = Input(shape=(1,), dtype=’int32′)
cls = Flatten()(Embedding(10, latent_size,
init=’glorot_normal’)(image_class))
h = merge([latent, cls], mode=’mul’)
fake_image = cnn(h) #输出虚假图片
return Model(input=[latent, image_class], output=fake_image)
概念判别模型,输入(1,28,28)图片,输出五个值,一个是识别模型认为这张图纸是否是虚假图片,另一个是可辨模型认为这第图片所属分类。

迎接付费咨询(150元每刻钟),我的微信:qingxingfengzi

3.3.1示范代码

现在在 /home/ubuntu 目录下成立源文件 predict_captcha.py,内容可参看:

predict_captcha.py

#!/usr/bin/python

from PIL import Image, ImageFilter

import tensorflow as tf

import numpy as np

import string

import sys

import generate_captcha

import captcha_model

if __name__ == ‘__main__’:

    captcha = generate_captcha.generateCaptcha()

    width, height, char_num, characters, classes =
captcha.get_parameter()

    gray_image = Image.open(sys.argv[1]).convert(‘L’)

    img = np.array(gray_image.getdata())

    test_x = np.reshape(img,[height,width,1])/255.0

    x = tf.placeholder(tf.float32, [None,height,width,1])

    keep_prob = tf.placeholder(tf.float32)

    model =
captcha_model.captchaModel(width,height,char_num,classes)

    y_conv = model.create_model(x,keep_prob)

    predict = tf.argmax(tf.reshape(y_conv,[-1,char_num,
classes]),2)

    init_op = tf.global_variables_initializer()

    saver = tf.train.Saver()

    gpu_options =
tf.GPUOptions(per_process_gpu_memory_fraction=0.95)

   
withtf.Session(config=tf.ConfigProto(log_device_placement=False,gpu_options=gpu_options))as
sess:

        sess.run(init_op)

        saver.restore(sess,”capcha_model.ckpt”)

        pre_list = sess.run(predict,feed_dict={x:
[test_x],keep_prob: 1})

        for i in pre_list:

            s = ”

            for j in i:

                s += characters[j]

                print s

生成式对抗网络利用。生成数字,生成人脸图像。

参考资料:
《TensorFlow技术解析与实战》

2 生成验证码训练多少

生成式对抗网络(gennerative adversarial
network,GAN),Google2014年提议网络模型。灵感自二人博弈的零和博弈,目前最火的非监督深度学习。GAN之父,伊恩(Ian)J.Goodfellow,公认人工智能一级专家。

def build_discriminator();
#使用激活函数Leaky ReLU来替换标准的卷积神经网络中的激活函数
cnn = Wequential()
cnn.add(Convolution2D(32, 3, 3, border_mode=’same’, subsample=(2, 2),
input_shape=(1, 28, 28)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Convolution2D(64, 3, 3, border_mode=’same’, subsample=(1,
1)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Convolution2D(128, 3, 3, border_mode=’same’, subsample=(1,
1)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Convolution2D(256, 3, 3, border_mode=’same’, subsample=(1,
1)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Flatten())
image = Input(shape=(1, 28, 28))
features = cnn(image)
#有六个出口
#输出真假值,范围在0~1
fake = Dense(1, activation=’sigmoid’,name=’generation’)(features)
#扶植分类器,输出图片分类
aux = Dense(10, activation=’softmax’, name=’auxiliary’)(features)
return Model(input=image, output=[fake, aux])
教练过程,50轮(epoch),把权重保存,每轮把假冒伪劣数据生成图处保存,观看虚假数据演变过程。

2.1.1 示例代码

现在在 /home/ubuntu 目录下创造源文件
generate_captcha.py,内容可参考:

generate_captcha.py

#!/usr/bin/python

# -*- coding: utf-8 -*

from captcha.image import ImageCaptcha

from PIL import Image

import numpy as np

import random

import string

class generateCaptcha():

    def __init__(self,

        width = 160,#验证码图片的宽

        height = 60,#验证码图片的高

        char_num = 4,#验证码字符个数

characters = string.digits +string.ascii_uppercase +
string.ascii_lowercase):

#验证码组成,数字+大写字母+小写字母

        self.width = width

        self.height = height

        self.char_num = char_num

        self.characters = characters

        self.classes = len(characters)

    def gen_captcha(self,batch_size = 50):

        X =np.zeros([batch_size,self.height,self.width,1])

        img =np.zeros((self.height,self.width),dtype=np.uint8)

        Y =np.zeros([batch_size,self.char_num,self.classes])

        image = ImageCaptcha(width =self.width,height = self.height)

        while True:

            for i in range(batch_size):

            captcha_str
=”.join(random.sample(self.characters,self.char_num))

            img =image.generate_image(captcha_str).convert(‘L’)

            img = np.array(img.getdata())

            X[i] =np.reshape(img,[self.height,self.width,1])/255.0

            for j,ch inenumerate(captcha_str):

                Y[i,j,self.characters.find(ch)] = 1

        Y =np.reshape(Y,(batch_size,self.char_num*self.classes))

yield X,Y

    def decode_captcha(self,y):

        y =np.reshape(y,(len(y),self.char_num,self.classes))

        return ”.join(self.characters[x] for xin np.argmax(y,axis =
2)[0,:])

    def get_parameter(self):

        return self.width, self.height, self.char_num,
self.characters, self.classes

    def gen_test_captcha(self):

        image = ImageCaptcha(width =self.width,height = self.height)

        captcha_str
=”.join(random.sample(self.characters,self.char_num))

        img = image.generate_image(captcha_str)

        img.save(captcha_str + ‘.jpg’)

迎接付费咨询(150元每时辰),我的微信:qingxingfengzi

生成式对抗网络(gennerative adversarial
network,GAN),Google2014年提出网络模型。灵感自二人博弈的零和博弈,如今最火的非监督深度学习。GAN之父,伊恩J.Goodfellow,公认人工智能超级专家。

4 完成

以上。

参考资料:
《TensorFlow技术解析与实战》

def build_generator(latent_size):
cnn = Sequential()
cnn.add(Dense(1024, input_dim=latent_size, activation=’relu’))
cnn.add(Dense(128 * 7 * 7, activation=’relu’))
cnn.add(Reshape((128, 7, 7)))
#上采样,图你尺寸变为 14X14
cnn.add(UpSampling2D(size=(2,2)))
cnn.add(Convolution2D(256, 5, 5, border_mode=’same’, activation=’relu’,
init=’glorot_normal’))
#上采样,图像尺寸变为28X28
cnn.add(UpSampling2D(size=(2,2)))
cnn.add(Convolution2D(128, 5, 5, border_mode=’same’, activation=’relu’,
init=’glorot_normal’))
#规约到1个通道
cnn.add(Convolution2D(1, 2, 2, border_mode=’same’, activation=’tanh’,
init=’glorot_normal’))
#变迁模型输入层,特征向量
latent = Input(shape=(latent_size, ))
#扭转模型输入层,标记
image_class = Input(shape=(1,), dtype=’int32′)
cls = Flatten()(Embedding(10, latent_size,
init=’glorot_normal’)(image_class))
h = merge([latent, cls], mode=’mul’)
fake_image = cnn(h) #输出虚假图片
return Model(input=[latent, image_class], output=fake_image)
概念判别模型,输入(1,28,28)图片,输出五个值,一个是识别模型认为这张图片是否是虚假图片,另一个是甄别模型认为这第图片所属分类。

3.1 cnn 验证码识别模型

3 层隐藏层、2
层全连接层,对每层都进展dropout。input——>conv——>pool——>dropout——>conv——>pool——>dropout——>conv——>pool——>dropout——>fullyconnected
layer——>dropout——>fully connected layer——>output

生成式对抗网络实现。https://github.com/fchollet/keras/blob/master/examples/mnist\_acgan.py

Augustus Odena、Chistopher Olah和Jonathon Shlens 论文《Conditional Image
Synthesis With Auxiliary Classifier GANs》。
透过噪声,让变化模型G生成虚假数据,和实际数据一起送到判别模型D,判别模型一方面输出数据真/假,一方面输出图片分类。
先是定义生成模型,目标是生成一对(z,L)数据,z是噪声向量,L是(1,28,28)的图像空间。

生成式对抗网络实现。https://github.com/fchollet/keras/blob/master/examples/mnist\_acgan.py

Augustus Odena、Chistopher Olah和Jonathon Shlens 论文《Conditional Image
Synthesis With Auxiliary Classifier GANs》。
透过噪声,让变化模型G生成虚假数据,和真实性数据一起送到判别模型D,判别模型一方面输出数据真/假,一方面输出图片分类。
先是定义生成模型,目标是生成一对(z,L)数据,z是噪声向量,L是(1,28,28)的图像空间。

3.2 磨炼cnn 验证码识别模型

每批次接纳 64 个训练样本,每 100 次循环使用 100
个测试样本检查识别准确度,当准确度大于 99% 时,锻练停止,采取 GPU 需要
5-6个钟头左右,CPU 大概需要 20 个时辰左右。

注:作为实验,你能够经过调整train_captcha.py文件中if acc >
0.99:
代码行的准确度节省锻练时间(比如将0.99 为
0.01);同时,我们曾经经过长日子的教练取得了一个磨练集,可以透过如下命令将训练集下载到本地。

wget http://tensorflow-1253902462.cosgz.myqcloud.com/captcha/capcha\_model.zip

unzip capcha_model.zip

生成式对抗网络立异。生成式对抗网络(generative adversarial
network,GAN)在无监督学习十分实惠。常规生成式对抗网络判别器使用Sigmoid交叉熵损失函数,学习过程梯度消失。Wasserstein生成式对抗网络(Wasserstein
generative adversarial
network,WGAN),使用Wasserstein距离度量,而不是Jensen-Shannon散度(Jensen-Shannon
divergence,JSD)。使用最小二乘生成式对抗网络(least squares generative
adversarial network,LSGAN),判别模型用小小平方损失小函数(least squares
loss function)。Sebastian Nowozin、Botond Cseke、Ryota
汤姆ioka故事集《f-GAN: Training Generative Neural Samplers using
Variational Divergence Minimization》。

教练停止,创设3类文件。params_discriminator_epoch_{{epoch_number}}.hdf5,判别模型权重参数。params_generator_epoch_{{epoch_number}}.hdf5,生成模型权重参数。plot_epoch_{{epoch_number}}_generated.png

2.1.3 执行结果

在 /home/ubuntu 目录下查看生成的验证码,jpg 格式的图形可以点击查阅。

如:

图片 1

Gxdl

图片 2

KrO7

def build_discriminator();
#应用激活函数Leaky ReLU来替换标准的卷积神经网络中的激活函数
cnn = Wequential()
cnn.add(Convolution2D(32, 3, 3, border_mode=’same’, subsample=(2, 2),
input_shape=(1, 28, 28)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Convolution2D(64, 3, 3, border_mode=’same’, subsample=(1,
1)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Convolution2D(128, 3, 3, border_mode=’same’, subsample=(1,
1)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Convolution2D(256, 3, 3, border_mode=’same’, subsample=(1,
1)))
cnn.add(LeakyReLU())
cnn.add(Dropout(0.3))
cnn.add(Flatten())
image = Input(shape=(1, 28, 28))
features = cnn(image)
#有六个出口
#出口真假值,范围在0~1
fake = Dense(1, activation=’sigmoid’,name=’generation’)(features)
#扶植分类器,输出图片分类
aux = Dense(10, activation=’softmax’, name=’auxiliary’)(features)
return Model(input=image, output=[fake, aux])
操练过程,50轮(epoch),把权重保存,每轮把假冒伪劣数据生成图处保存,阅览虚假数据演变过程。

生成式对抗网络接纳。生成数字,生成人脸图像。

3.3.3 执行结果:

Gxdl

注:因时光的限定,大家恐怕调整了准确度导致执行结果不相符预期,那属于常规境况。

在教练时间丰裕长的状态下,能够使用验证码生成器生成测试数据,cnn磨炼出来的验证码辨识模型依旧很有力的,大小写的
z 都可以区分,甚至有时人都没法儿区分,该模型也足以正确的甄别。

磨练停止,创设3类文件。params_discriminator_epoch_{{epoch_number}}.hdf5,判别模型权重参数。params_generator_epoch_{{epoch_number}}.hdf5,生成模型权重参数。plot_epoch_{{epoch_number}}_generated.png

原理。
生成式对搞网络包含一个扭转模型(generative
model,G)和一个识别模型(discriminative model,D)。伊恩(Ian) J.Goodfellow、JeanPouget-Abadie、Mehdi Mirza、Bing Xu、戴维(David) Warde-Farley、Sherjil
Ozair、亚伦(Aaron) Courville、Yoshua Bengio杂谈,《Generative Adversarial
Network》,https://arxiv.org/abs/1406.2661
生成式对抗网络布局:
噪音数据->生成模型->假图片—|
|->判别模型->真/假
打乱磨练多少->锻练集->真图片-|
生成式对抗网络根本解决哪些从磨炼样本中学习出新样本。生成模型负责磨炼出样本的分布,假使磨练样本是图形就成形相似的图纸,假设磨练样本是小说名子就变化相似的篇章名子。判别模型是一个二分类器,用来判定输入样本是实际数据或者锻炼转变的范本。
生成式对抗网络优化,是一个二元极小极大博弈(minimax two-player
game)问题。使生成模型输出在输入给判别模型时,判断模型秀难判断是真实数据仍然虚似数据。练习好的更动模型,能把一个噪声向量转化成和操练集类似的样本。Argustus
Odena、克里斯(Rhys)托(Stowe)弗(Christopher) Olah、Jonathon Shlens小说《Coditional Image Synthesis
with Auxiliary Classifier GANs》。
扶持分类器生成式对抗网络(auxiliary classifier GAN,AC-GAN)实现。

2.1 验证码生成器

利用 python
中生成器形式来生成我们的教练多少,这样的利益是,不需要提前转移大量的数额,磨炼过程中生成数据,并且可以无限生成数据。

3.3 测试

cnn 验证码识别模型

3.3.2 然后举行

cd /home/ubuntu;

python predict_captcha.py Gxdl.jpg

3 验证码识别模型

将验证码识别问题转化为分类问题,总共62^4 种档次,采取 4 个 one-hot
编码分别表示 4 个字符取值。

1 安装captcha库

sudo pip install captcha

环境:Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-105-generic x86_64)

3.2.1 示例代码

现在在 /home/ubuntu 目录下创造源文件 train_captcha.py,内容可参照:

train_captcha.py

#!/usr/bin/python

import tensorflow as tf

import numpy as np

import string

import generate_captcha

import captcha_model

if __name__ == ‘__main__’:

    captcha =generate_captcha.generateCaptcha()

    width,height,char_num,characters,classes =
captcha.get_parameter()

    x = tf.placeholder(tf.float32, [None,height,width,1])

    y_ = tf.placeholder(tf.float32, [None,char_num*classes])

    keep_prob = tf.placeholder(tf.float32)

    model =
captcha_model.captchaModel(width,height,char_num,classes)

    y_conv = model.create_model(x,keep_prob)

    cross_entropy =
tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_,logits=y_conv))

    train_step =
tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

    predict = tf.reshape(y_conv, [-1,char_num,classes])

    real = tf.reshape(y_,[-1,char_num,classes])

    correct_prediction = tf.equal(tf.argmax(predict,2),
tf.argmax(real,2))

    correct_prediction = tf.cast(correct_prediction, tf.float32)

    accuracy = tf.reduce_mean(correct_prediction)

    saver = tf.train.Saver()

    with tf.Session() as sess:

        sess.run(tf.global_variables_initializer())

        step = 0

        while True:

            batch_x,batch_y = next(captcha.gen_captcha(64))

            _,loss =
sess.run([train_step,cross_entropy],feed_dict ={x: batch_x, y_:
batch_y,keep_prob: 0.75})

            print (‘step:%d,loss:%f’ %(step,loss))

            if step % 100 == 0:

                batch_x_test,batch_y_test =
next(captcha.gen_captcha(100))

                acc = sess.run(accuracy,feed_dict={x: batch_x_test,
y_: batch_y_test, keep_prob: 1.})

               
print(‘###############################################step:%d,accuracy:%f’
%(step,acc))

if acc > 0.99:

saver.save(sess,”capcha_model.ckpt”)

break

step += 1

2.1.2 然后进行

cd /home/ubuntu;

python

import generate_captcha

g = generate_captcha.generateCaptcha()

g.gen_test_captcha()

3.1.1 示例代码

现在在 /home/ubuntu 目录下创制源文件 captcha_model.py,内容可参看:

captcha_model.py

#!/usr/bin/python

# -*- coding: utf-8 -*

import tensorflow 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

相关文章