在Keras上实现GAN:构建消除图片模糊的应用

简介: 2014 年,Ian Goodfellow 提出了生成对抗网络(GAN),今天,GAN 已经成为深度学习最热门的方向之一。本文将重点介绍如何利用 Keras 将 GAN 应用于图像去模糊(image deblurring)任务当中。

2014 年,Ian Goodfellow 提出了生成对抗网络(GAN),今天,GAN 已经成为深度学习最热门的方向之一。本文将重点介绍如何利用 Keras 将 GAN 应用于图像去模糊(image deblurring)任务当中。


Keras 代码地址:https://github.com/RaphaelMeudec/deblur-gan


此外,请查阅 DeblurGAN 的原始论文(https://arxiv.org/pdf/1711.07064.pdf)及其 Pytorch 版本实现:https://github.com/KupynOrest/DeblurGAN/。


生成对抗网络简介


在生成对抗网络中,有两个网络互相进行训练。生成器通过生成逼真的虚假输入来误导判别器,而判别器会分辨输入是真实的还是人造的。


GAN 训练流程


训练过程中有三个关键步骤:


  • 使用生成器根据噪声创造虚假输入;

  • 利用真实输入和虚假输入训练判别器;

  • 训练整个模型:该模型是判别器和生成器连接所构建的。


请注意,判别器的权重在第三步中被冻结。


对两个网络进行连接的原因是不存在单独对生成器输出的反馈。我们唯一的衡量标准是判别器是否能接受生成的样本。


以上,我们简要介绍了 GAN 的架构。如果你觉得不够详尽,可以参考这篇优秀的介绍:生成对抗网络初学入门:一文读懂 GAN 的基本原理(附资源)。


数据


Ian Goodfellow 首先应用 GAN 模型生成 MNIST 数据。而在本教程中,我们将生成对抗网络应用于图像去模糊。因此,生成器的输入不是噪声,而是模糊的图像。


我们采用的数据集是 GOPRO 数据集。该数据集包含来自多个街景的人工模糊图像。根据场景的不同,该数据集在不同子文件夹中分类。


你可以下载简单版:https://drive.google.com/file/d/1H0PIXvJH4c40pk7ou6nAwoxuR4Qh_Sa2/view

或完整版:https://drive.google.com/file/d/1SlURvdQsokgsoyTosAaELc4zRjQz9T2U/view


我们首先将图像分配到两个文件夹 A(模糊)B(清晰)中。这种 A&B 的架构对应于原始的 pix2pix 论文。为此我创建了一个自定义的脚本在 github 中执行这个任务,请按照 README 的说明去使用它:

https://github.com/RaphaelMeudec/deblur-gan/blob/master/organize_gopro_dataset.py


模型


训练过程保持不变。首先,让我们看看神经网络的架构吧!


生成器


该生成器旨在重现清晰的图像。该网络基于 ResNet 模块,它不断地追踪关于原始模糊图像的演变。本文同样使用了一个基于 UNet 的版本,但我还没有实现这个版本。这两种模块应该都适合图像去模糊。


DeblurGAN 生成器网络架构,源论文《DeblurGAN: Blind Motion Deblurring Using Conditional Adversarial Networks》。


其核心是应用于原始图像上采样的 9 个 ResNet 模块。让我们来看看 Keras 上的代码实现!


from keras.layers import Input, Conv2D, Activation, BatchNormalization
from keras.layers.merge import Add
from keras.layers.core import Dropout

def res_block(input, filters, kernel_size=(3,3), strides=(1,1), use_dropout=False):
   """
   Instanciate a Keras Resnet Block using sequential API.
   :param input: Input tensor
   :param filters: Number of filters to use
   :param kernel_size: Shape of the kernel for the convolution
   :param strides: Shape of the strides for the convolution
   :param use_dropout: Boolean value to determine the use of dropout
   :return: Keras Model
   """

   x = ReflectionPadding2D((1,1))(input)
   x = Conv2D(filters=filters,
              kernel_size=kernel_size,
              strides=strides,)(x)
   x = BatchNormalization()(x)
   x = Activation('relu')(x)

   if use_dropout:
       x = Dropout(0.5)(x)

   x = ReflectionPadding2D((1,1))(x)
   x = Conv2D(filters=filters,
               kernel_size=kernel_size,
               strides=strides,)(x)
   x = BatchNormalization()(x)

   # Two convolution layers followed by a direct connection between input and output
   merged = Add()([input, x])
   return merged


该 ResNet 层基本是卷积层,其输入和输出都被添加以形成最终的输出。


from keras.layers import Input, Activation, Add
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.core import Lambda
from keras.layers.normalization import BatchNormalization
from keras.models import Model

from layer_utils import ReflectionPadding2D, res_block

ngf = 64
input_nc = 3
output_nc = 3
input_shape_generator = (256, 256, input_nc)
n_blocks_gen = 9


def generator_model():
   """Build generator architecture."""
   # Current version : ResNet block
   inputs = Input(shape=image_shape)

   x = ReflectionPadding2D((3, 3))(inputs)
   x = Conv2D(filters=ngf, kernel_size=(7,7), padding='valid')(x)
   x = BatchNormalization()(x)
   x = Activation('relu')(x)

   # Increase filter number
   n_downsampling = 2
   for i in range(n_downsampling):
       mult = 2**i
       x = Conv2D(filters=ngf*mult*2, kernel_size=(3,3), strides=2, padding='same')(x)
       x = BatchNormalization()(x)
       x = Activation('relu')(x)

   # Apply 9 ResNet blocks
   mult = 2**n_downsampling
   for i in range(n_blocks_gen):
       x = res_block(x, ngf*mult, use_dropout=True)

   # Decrease filter number to 3 (RGB)
   for i in range(n_downsampling):
       mult = 2**(n_downsampling - i)
       x = Conv2DTranspose(filters=int(ngf * mult / 2), kernel_size=(3,3), strides=2, padding='same')(x)
       x = BatchNormalization()(x)
       x = Activation('relu')(x)

   x = ReflectionPadding2D((3,3))(x)
   x = Conv2D(filters=output_nc, kernel_size=(7,7), padding='valid')(x)
   x = Activation('tanh')(x)

   # Add direct connection from input to output and recenter to [-1, 1]
   outputs = Add()([x, inputs])
   outputs = Lambda(lambda z: z/2)(outputs)

   model = Model(inputs=inputs, outputs=outputs, name='Generator')
   return model

生成器架构的 Keras 实现


按照计划,9 个 ResNet 模块会应用于输入的上采样版本。我们在其中添加了从输入到输出的连接,并对结果除以 2 以保持标准化输出。


这就是生成器的架构!让我们继续看看判别器怎么做吧。


判别器


判别器的目标是判断输入图像是否是人造的。因此,判别器的体系结构是卷积以及输出单一值。


from keras.layers import Input
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Conv2D
from keras.layers.core import Dense, Flatten
from keras.layers.normalization import BatchNormalization
from keras.models import Model

ndf = 64
output_nc = 3
input_shape_discriminator = (256, 256, output_nc)


def discriminator_model():
   """Build discriminator architecture."""
   n_layers, use_sigmoid = 3, False
   inputs = Input(shape=input_shape_discriminator)

   x = Conv2D(filters=ndf, kernel_size=(4,4), strides=2, padding='same')(inputs)
   x = LeakyReLU(0.2)(x)

   nf_mult, nf_mult_prev = 1, 1
   for n in range(n_layers):
       nf_mult_prev, nf_mult = nf_mult, min(2**n, 8)
       x = Conv2D(filters=ndf*nf_mult, kernel_size=(4,4), strides=2, padding='same')(x)
       x = BatchNormalization()(x)
       x = LeakyReLU(0.2)(x)

   nf_mult_prev, nf_mult = nf_mult, min(2**n_layers, 8)
   x = Conv2D(filters=ndf*nf_mult, kernel_size=(4,4), strides=1, padding='same')(x)
   x = BatchNormalization()(x)
   x = LeakyReLU(0.2)(x)

   x = Conv2D(filters=1, kernel_size=(4,4), strides=1, padding='same')(x)
   if use_sigmoid:
       x = Activation('sigmoid')(x)

   x = Flatten()(x)
   x = Dense(1024, activation='tanh')(x)
   x = Dense(1, activation='sigmoid')(x)

   model = Model(inputs=inputs, outputs=x, name='Discriminator')
   return model

判别器架构的 Keras 实现


最后一步是构建完整的模型。本文中这个生成对抗网络的特殊性在于:其输入是实际图像而非噪声。因此,对于生成器的输出,我们能得到直接的反馈。


from keras.layers import Input
from keras.models import Model

def generator_containing_discriminator_multiple_outputs(generator, discriminator):
   inputs = Input(shape=image_shape)
   generated_images = generator(inputs)
   outputs = discriminator(generated_images)
   model = Model(inputs=inputs, outputs=[generated_images, outputs])
   return model


让我们一起看看,如何利用两个损失函数来充分利用这种特殊性。


训练过程


损失函数


我们在两个级别提取损失函数:生成器的末尾和整个模型的末尾。


前者是一种知觉损失(perceptual loss),它直接根据生成器的输出计算而来。这种损失函数确保了 GAN 模型面向一个去模糊任务。它比较了 VGG 第一批卷积的输出值。


import keras.backend as K
from keras.applications.vgg16 import VGG16
from keras.models import Model

image_shape = (256, 256, 3)

def perceptual_loss(y_true, y_pred):
   vgg = VGG16(include_top=False, weights='imagenet', input_shape=image_shape)
   loss_model = Model(inputs=vgg.input, outputs=vgg.get_layer('block3_conv3').output)
   loss_model.trainable = False
   return K.mean(K.square(loss_model(y_true) - loss_model(y_pred)))


而后者是对整个模型的输出执行的 Wasserstein 损失,它取的是两个图像差异的均值。这种损失函数可以改善生成对抗网络的收敛性。


import keras.backend as K

def wasserstein_loss(y_true, y_pred):
   return K.mean(y_true*y_pred)


训练过程


第一步是加载数据并初始化所有模型。我们使用我们的自定义函数加载数据集,同时在我们的模型中添加 Adam 优化器。我们通过设置 Keras 的可训练选项防止判别器进行训练。


# Load dataset
data = load_images('./images/train', n_images)
y_train, x_train = data['B'], data['A']

# Initialize models
g = generator_model()
d = discriminator_model()
d_on_g = generator_containing_discriminator_multiple_outputs(g, d)

# Initialize optimizers
g_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
d_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
d_on_g_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)

# Compile models
d.trainable = True
d.compile(optimizer=d_opt, loss=wasserstein_loss)
d.trainable = False
loss = [perceptual_loss, wasserstein_loss]
loss_weights = [100, 1]
d_on_g.compile(optimizer=d_on_g_opt, loss=loss, loss_weights=loss_weights)
d.trainable = True


然后,我们启动 epoch 并将数据集分成不同批量。


for epoch in range(epoch_num):
 print('epoch: {}/{}'.format(epoch, epoch_num))
 print('batches: {}'.format(x_train.shape[0] / batch_size))

 # Randomize images into batches
 permutated_indexes = np.random.permutation(x_train.shape[0])

 for index in range(int(x_train.shape[0] / batch_size)):
     batch_indexes = permutated_indexes[index*batch_size:(index+1)*batch_size]
     image_blur_batch = x_train[batch_indexes]
     image_full_batch = y_train[batch_indexes]


最后,根据两种损失,我们先后训练判别器和生成器。我们用生成器产生虚假输入,然后训练判别器来区分虚假输入和真实输入,并训练整个模型。


for epoch in range(epoch_num):
 for index in range(batches):
   # [Batch Preparation]

   # Generate fake inputs
   generated_images = g.predict(x=image_blur_batch, batch_size=batch_size)

   # Train multiple times discriminator on real and fake inputs
   for _ in range(critic_updates):
       d_loss_real = d.train_on_batch(image_full_batch, output_true_batch)
       d_loss_fake = d.train_on_batch(generated_images, output_false_batch)
       d_loss = 0.5 * np.add(d_loss_fake, d_loss_real)

   d.trainable = False
   # Train generator only on discriminator's decision and generated images
   d_on_g_loss = d_on_g.train_on_batch(image_blur_batch, [image_full_batch, output_true_batch])

   d.trainable = True


你可以参考如下 Github 地址查看完整的循环:

https://www.github.com/raphaelmeudec/deblur-gan


材料


我使用了 Deep Learning AMI(3.0 版本)中的 AWS 实例(p2.xlarge)。它在 GOPRO 数据集上的训练时间约为 5 小时(50 个 epoch)。


图像去模糊结果


从左到右:原始图像、模糊图像、GAN 输出。


上面的输出是我们 Keras Deblur GAN 的输出结果。即使是在模糊不清的情况下,网络也能够产生更令人信服的图像。车灯和树枝都会更清晰。


左图:GOPRO 测试图片;右图:GAN 输出。


其中的一个限制是图像顶部的噪点图案,这可能是由于使用 VGG 作为损失函数引起的。


左图:GOPRO 测试图片;右图:GAN 输出。


希望你在这篇「基于生成对抗网络进行图像去模糊」的文章中度过了一段愉快的阅读时光!


左图:GOPRO 测试图片;右图:GAN 输出。


论文:DeblurGAN: Blind Motion Deblurring Using Conditional Adversarial Networks 



论文地址:https://arxiv.org/pdf/1711.07064.pdf


摘要:我们提出了一种基于有条件 GAN 和内容损失函数的运动去模糊的端到端学习方法——DeblurGAN。在结构相似性测量和视觉外观方面,DeblurGAN 达到了业内最先进的技术水平。去模糊模型的质量也以一种新颖的方式在现实问题中考量——即对(去)模糊图像的对象检测。该方法比目前最佳的竞争对手速度提升了 5 倍。另外,我们提出了一种从清晰图像合成运动模糊图像的新方法,它可以实现真实数据集的增强。


模型、训练代码和数据集都可以在以下地址获得:https://github.com/KupynOrest/DeblurGAN。


原文链接:https://blog.sicara.com/keras-generative-adversarial-networks-image-deblurring-45e3ab6977b5



from:https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650740005&idx=3&sn=947d138f9edf1421a134b901e884383b&chksm=871ad15bb06d584dd5d23029567fd197b1793034c41e9567b49cf30ff5b664b31dc9c4192c96&mpshare=1&scene=23&srcid=0329OkiiirFXZ6pebIwoy69c%23rd
目录
相关文章
|
人工智能 搜索推荐 数据可视化
10个适合产品经理使用的AI生成PPT工具推荐!
AIGC时代,有哪些好用的AI生成PPT工具呢?这篇文章来给各位介绍适合产品经理使用的10款AI PPT软件!
|
人工智能 自然语言处理 算法
经典大模型提示词工程技术路线概述
本文概述几种经典提示词工程方法,总结关键信息,分析其优势和局限,并分享笔者的一点思考。
1214 105
经典大模型提示词工程技术路线概述
|
11月前
|
人工智能 IDE 开发工具
2025 智能研发工具链选型:多模型架构与全栈环境的融合之路
在云原生与 AI 深度融合的 2025 年,开发者工具(Developer Tools)正在经历一场从“单一辅助”到“智能底座”的架构重构。本文将从 DevOps 视角出发,深度解析 GitHub Copilot、Baidu Comate 等主流工具在 多模型路由、异构 IDE 兼容 及 RAG 私有化 方面的技术演进,为企业级选型提供参考。
|
消息中间件 缓存 负载均衡
php怎么解决高并发的问题
在实际应用中,应根据具体需求和应用场景,选择合适的优化方案,并进行持续监控和优化,确保系统的高效稳定运行。
696 6
|
算法 程序员 Python
用伪代码表示算法
在算法设计和编程中,伪代码是一种非常重要的工具。它允许我们以一种既非特定编程语言又足够详细的方式来描述算法。伪代码的目标是提供一个清晰、简洁的算法表示,而不必拘泥于特定的编程语法或规则。本文将探讨伪代码的优势,并提供一个用伪代码表示算法的例子。
1545 1
|
人工智能 文字识别 异构计算
NVIDIA-Ingest:英伟达开源智能文档提取及结构化工具,支持 GPU 加速和并行处理
NVIDIA-Ingest 是英伟达开源的智能文档提取工具,支持 PDF、Word、PPT 等多种格式,提供并行处理和 GPU 加速,适用于企业内容管理和生成式应用。
1044 18
NVIDIA-Ingest:英伟达开源智能文档提取及结构化工具,支持 GPU 加速和并行处理
|
机器学习/深度学习 监控 算法
深度学习之图像去噪与去模糊
基于深度学习的图像去噪和去模糊是计算机视觉中的重要任务,旨在提升图像质量,去除噪声和模糊。
1456 3
|
网络协议 应用服务中间件 nginx
Nginx配置server_name讲解
Nginx配置server_name讲解
2226 0
|
存储 Shell 区块链
怎么把Python脚本打包成可执行程序?
该文档介绍了如何将Python脚本及其运行环境打包成EXE可执行文件,以便在不具备Python环境的计算机上运行。首先确保Python脚本能够正常运行,然后通过安装PyInstaller并使用`--onefile`参数将脚本打包成独立的EXE文件。此外,还提供了去除命令行窗口和指定可执行文件图标的详细方法。这些步骤帮助用户轻松地将Python程序分发给最终用户。
734 3
怎么把Python脚本打包成可执行程序?
|
人工智能 自然语言处理 前端开发
从文案到设计,我用通义版Artifacts生成了365张灵感日历
本文介绍了如何利用通义AI的“代码模式”功能,轻松制作个性化日历。作者通过实例展示了从设计日历样式、推荐每日生活小事到赋予小事新解的过程,强调了AI在创意实现上的强大助力。此外,还探讨了AI代码生成技术对未来创造力的影响,以及通义AI代码模式如何降低创作门槛,提高效率,让每个人都能成为应用开发者。