深度學習專案示例 使用自編碼器進行模糊影象修復
影象模糊是由相機或拍攝物件移動、對焦不準確或使用光圈配置不當導致的影象不清晰。為了獲得更清晰的照片,我們可以使用相機鏡頭的首選焦點重新拍攝同一張照片,或者使用深度學習知識重現模糊的影象。由於我的專長不是攝影,只能選擇使用深度學習技術對影象進行去模糊處理!
在開始這個專案之前,本文假定讀者應該瞭解深度學習的基本概念,例如神經網路、CNN。還要稍微熟悉一下 Keras、Tensorflow 和 OpenCV。
有各種型別的模糊——運動模糊、高斯模糊、平均模糊等。但我們將專注於高斯模糊影象。在這種模糊型別中,畫素權重是不相等的。模糊在中心處較高,在邊緣處按照鐘形曲線減少。
資料集
在開始使用程式碼之前,首先需要的是一個由 2 組影象組成的資料集——模糊影象和乾淨影象。目前可能沒有現成的資料集可以使用,但是就像我們上面所說的,如果你有opencv的基礎這個對於我們來說是非常個簡單的,只要我們有原始影象,使用opencv就可以自己生成訓練需要的資料集。
這裡我的資料集大小約為 50 張影象(50 張乾淨影象和 50 張模糊影象),因為只是演示目的所以只選擇了少量影象。
編寫程式碼
已經準備好資料集,可以開始編寫程式碼了。
依賴項
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import random
import cv2
import os
import tensorflow as tf
from 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') / 255
blurry_frames.append(image)
blurry_frames = np.array(blurry_frames)
匯入模型庫
from keras.layers import Dense, Input
from keras.layers import Conv2D, Flatten
from keras.layers import Reshape, Conv2DTranspose
from keras.models import Model
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from keras.utils.vis_utils import plot_model
from keras import backend as K
random.seed = 21
np.random.seed = seed
將資料集拆分為訓練集和測試集
現在我們按 80:20 的比例將資料集分成訓練和測試集。
完整文章:
https://www.overfit.cn/post/d9b6d1a979a444f39c34edc47c647be6