生成式对搞互连网蕴含二个扭转模型(generative,何况能够非常生成数据

生成式对抗互连网(gennerative adversarial
network,GAN),谷歌(Google)二〇一六年提议网络模型。灵感自肆个人博弈的零和博艺,近日最火的非监督深度学习。GAN之父,伊恩J.Goodfellow,公众以为人工智能顶尖专家。

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

原理。
生成式对搞互连网包括叁个变型模型(generative
model,G)和一个识别模型(discriminative model,D)。伊恩 J.Goodfellow、姬恩Pouget-Abadie、Mehdi Mirza、Bing Xu、David Warde-Farley、Sherjil
Ozair、亚伦 Courville、Yoshua Bengio随想,《Generative Adversarial
Network》,https://arxiv.org/abs/1406.2661
生成式对抗互连网布局:
噪音数据->生成模型->假图片—|
|->判断模型->真/假
打乱磨炼多少->陶冶集->真图片-|
生成式对抗网络重大消除什么从磨炼样本中学习出新样本。生成模型担当磨练出样本的布满,假诺练习样本是图形就变化相似的图样,假使磨练样本是作品名子就调换相似的稿子名子。判断模型是七个二分类器,用来推断输入样本是动真格的数据依然磨炼调换的范本。
生成式对抗互联网优化,是一个二元相当小十分大博弈(minimax two-player
game)难点。使生成模型输出在输入给剖断模型时,决断模型秀难判别是动真格的数据依然虚似数据。磨炼好的生成模型,能把一个噪音向量转化成和教练集类似的样本。Argustus
Odena、克Rees多夫 Olah、Jonathon Shlens杂文《Coditional Image Synthesis
with Auxiliary Classifier GANs》。
援助分类器生成式对抗互联网(auxiliary classifier GAN,AC-GAN)完成。

1 安装captcha库

sudo pip install captcha

生成式对抗网络采取。生成数字,生成年人脸图像。

2 生成验证码磨练多少

生成式对抗互联网完成。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)的图像空间。

2.1 验证码生成器

选择 python
中生成器情势来生成大家的演练多少,那样的裨益是,不须求超前转移多量的多少,演练进度中变化数据,并且能够无限生成数据。

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)图片,输出多少个值,一个是可辨模型以为那张图纸是不是是虚假图片,另多个是甄别模型以为那第图片所属分类。

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’)

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.2 然后实行

cd /home/ubuntu;

python

import generate_captcha

g = generate_captcha.generateCaptcha()

g.gen_test_captcha()

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_体育365网址,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’))

2.1.3 推行结果

在 /home/ubuntu 目录下查看生成的验证码,jpg 格式的图纸能够点击查看。

如:

体育365网址 1

Gxdl

体育365网址 2

KrO7

教练甘休,创造3类文件。params_discriminator_epoch_{{epoch_number}}.hdf5,剖断模型权重参数。params_generator_epoch_{{epoch_number}}.hdf5,生成模型权重参数。plot_epoch_{{epoch_number}}_generated.png

3 验证码识别模型

将验证码识别难点转化为分类难题,总共62^4 种档案的次序,采纳 4 个 one-hot
编码分别代表 4 个字符取值。

生成式对抗互连网创新。生成式对抗互联网(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)。塞Bath蒂恩 Nowozin、Botond Cseke、Ryota
汤姆ioka杂谈《f-GAN: Training Generative Neural Samplers using
Variational Divergence 迷你mization》。

3.1 cnn 验证码识别模型

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

参照他事他说加以考察资料:
《TensorFlow手艺剖判与实战》

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

应接付费咨询(150元每时辰),作者的微信:qingxingfengzi

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

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

3.2.2 然后实践

cd /home/ubuntu;

python train_captcha.py

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

3.3 测试

cnn 验证码识别模型

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

3.3.2 然后施行

cd /home/ubuntu;

python predict_captcha.py Gxdl.jpg

3.3.3 实施结果:

Gxdl

注:因时间的限量,大家大概调节了准确度导致试行结果不适合预期,那属夏梅常景况。

在教练时间丰盛长的地方下,能够使用验证码生成器生成测验数据,cnn磨炼出来的验证码辨识模型照旧很庞大的,大小写的
z 都得以区分,乃至有时人都没有办法儿区分,该模型也能够精确的辨别。

4 完成

以上。

相关文章