深度学习项目示例 使用自编码器进行模糊图像修复
图像模糊是由相机或拍摄对象移动、对焦不准确或使用光圈配置不当导致的图像不清晰。为了获得更清晰的照片,我们可以使用相机镜头的首选焦点重新拍摄同一张照片,或者使用深度学习知识重现模糊的图像。由于我的专长不是摄影,只能选择使用深度学习技术对图像进行去模糊处理!

在开始这个项目之前,本文假定读者应该了解深度学习的基本概念,例如神经网络、CNN。还要稍微熟悉一下 Keras、Tensorflow 和 OpenCV。
有各种类型的模糊——运动模糊、高斯模糊、平均模糊等。但我们将专注于高斯模糊图像。在这种模糊类型中,像素权重是不相等的。模糊在中心处较高,在边缘处按照钟形曲线减少。

数据集
在开始使用代码之前,首先需要的是一个由 2 组图像组成的数据集——模糊图像和干净图像。目前可能没有现成的数据集可以使用,但是就像我们上面所说的,如果你有opencv的基础这个对于我们来说是非常个简单的,只要我们有原始图像,使用opencv就可以自己生成训练需要的数据集。
这里我的数据集大小约为 50 张图像(50 张干净图像和 50 张模糊图像),因为只是演示目的所以只选择了少量图像。
编写代码
已经准备好数据集,可以开始编写代码了。
依赖项
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt%matplotlib inlineimport randomimport cv2import osimport tensorflow as tffrom tqdm import tqdm
这里导入了 tqdm 库来帮助我创建进度条,这样可以知道运行代码需要多长时间。
导入数据
good_frames = '/content/drive/MyDrive/mini_clean'bad_frames = '/content/drive/MyDrive/mini_blur'
现在创建了2 个列表。我们将使用 keras 预处理库读取“.jpg”、“jpeg”或“.png”类型的图像,并转换为数组。这里图像尺寸为 128x128。
clean_frames = []for file in tqdm(sorted(os.listdir(good_frames))):if any(extension in file for extension in ['.jpg', 'jpeg', '.png']):image = tf.keras.preprocessing.image.load_img(good_frames + '/' + file, target_size=(128,128))image = tf.keras.preprocessing.image.img_to_array(image).astype('float32') / 255
完整文章:
https://www.overfit.cn/post/d9b6d1a979a444f39c34edc47c647be6
clean_frames.append(image)clean_frames = np.array(clean_frames)blurry_frames = []for file in tqdm(sorted(os.listdir(bad_frames))):if any(extension in file for extension in ['.jpg', 'jpeg', '.png']):image = tf.keras.preprocessing.image.load_img(bad_frames + '/' + file, target_size=(128,128))image = tf.keras.preprocessing.image.img_to_array(image).astype('float32') / 255blurry_frames.append(image)blurry_frames = np.array(blurry_frames)
导入模型库
from keras.layers import Dense, Inputfrom keras.layers import Conv2D, Flattenfrom keras.layers import Reshape, Conv2DTransposefrom keras.models import Modelfrom keras.callbacks import ReduceLROnPlateau, ModelCheckpointfrom keras.utils.vis_utils import plot_modelfrom keras import backend as Krandom.seed = 21np.random.seed = seed
将数据集拆分为训练集和测试集
现在我们按 80:20 的比例将数据集分成训练和测试集。
完整文章:
https://www.overfit.cn/post/d9b6d1a979a444f39c34edc47c647be6

浙公网安备 33010602011771号