project website:http://www.harryyang.org/inpainting/
code:https://github.com/leehomyc/Faster-High-Res-Neural-Inpainting
參考部落格:
https://www.cnblogs.com/wenshinlee/p/12444785.html
https://blog.csdn.net/weixin_42360095/article/details/89525787
模型整體架構
整體架構如上,圖像修複部分被限制了,為固定的中心區域。如輸入為128 x 128,則待修複區域為中心的 64 x 64
網絡分2個子產品:
Content Network
x 0 x_0 x0:去除了中心部分的輸入(中心部分填補均值)
f ( x 0 ) f(x_0) f(x0):Content Network輸出,大小為 64 x 64 表示待修補區域
h ( x , R ) h(x,R) h(x,R):Ground Truth,即原圖的中心區域
即一個Encode Decode結構,由網絡自動學習得到中心區域。
Texture Network
x x x:即 x 0 + f ( x 0 ) x_0 + f(x_0) x0+f(x0),由content Network生成的結果補全輸入後,整體做為Texture Network的輸入。
其網絡為基于ImageNet訓練好的VGG網絡,VGG隻用來計算Loss用于訓練Content Network。其思想為利用訓練好的VGG的提取特征能力,是以隻提取其中間層的特征,無需整個VGG網絡。其作用看其loss就明白了。
Loss
x ~ i + 1 = arg min x E c ( h ( x , R ) , h ( x i , R ) ) + α E t ( ϕ t ( x ) , R ϕ ) + β Υ ( x ) (1) \begin{aligned} \tilde{x}_{i+1}=& \underset{x}{\arg \min } E_{c}\left(h(x, R), h\left(x_{i}, R\right)\right) \\ &+\alpha E_{t}\left(\phi_{t}(x), R^{\phi}\right)+\beta \Upsilon(x) \end{aligned}\tag 1 x~i+1=xargminEc(h(x,R),h(xi,R))+αEt(ϕt(x),Rϕ)+βΥ(x)(1)
其中 i = 1 , 2 , . . . , N i = 1,2,... , N i=1,2,...,N為多尺度修複。針對大圖(512 x 512),下采樣修複後在逐層上采樣refine。(這個多尺度形式總覺得怪怪的)
Content Network Loss
predict 和 GT 的 L2 Loss
E c ( h ( x , R ) , h ( x i , R ) ) = ∥ h ( x , R ) − h ( x i , R ) ∥ 2 2 (2) E_{c}\left(h(x, R), h\left(x_{i}, R\right)\right)=\left\|h(x, R)-h\left(x_{i}, R\right)\right\|_{2}^{2} \tag 2 Ec(h(x,R),h(xi,R))=∥h(x,R)−h(xi,R)∥22(2)
還可參考GAN的做法,加一個判别器 D ,去增強Content Network
L a d v ( x , x g , R ) = max D E x ∈ X [ log ( D ( h ( x g , R ) ) ) ) ] + log ( 1 − D ( f ( x ) ) ) ] \begin{aligned} L_{a d v}\left(x, x_{g}, R\right)=\max _{D} E_{x \in \mathcal{X}}\left[\log \left(D\left(h\left(x_{g}, R\right)\right)\right)\right.&)] \\ +\log (1-D(f(x)))] \end{aligned} Ladv(x,xg,R)=DmaxEx∈X[log(D(h(xg,R)))+log(1−D(f(x)))])]
最終Content Network Loss為2個Loss的權重組合
Texture Network Loss
為了讓缺失部分的細節與已知部分的細節相似(進一步促使網絡從已知部分的結構去生成缺失部分,如棋盤這種重複結構的圖像修補應該有挺大幫助)。
以小視窗(如3x3)滑動取生成區域的特征圖,去和已知區域部分的特征圖比對。選取和生成區域的特征圖最相似的已知區域特征。用L2 Loss限制這2部分。
E t ( ϕ t ( x ) , R ) = 1 ∣ R ϕ ∣ ∑ i ∈ R ϕ ∥ h ( ϕ t ( x ) , P i ) − h ( ϕ t ( x ) , P n n ( i ) ) ∥ 2 2 (3) \begin{array}{l} E_{t}\left(\phi_{t}(x), R\right)= \\ \frac{1}{\left|R^{\phi}\right|} \sum_{i \in R^{\phi}}\left\|h\left(\phi_{t}(x), P_{i}\right)-h\left(\phi_{t}(x), P_{n n(i)}\right)\right\|_{2}^{2} \end{array} \tag 3 Et(ϕt(x),R)=∣Rϕ∣1∑i∈Rϕ∥∥h(ϕt(x),Pi)−h(ϕt(x),Pnn(i))∥∥22(3)
n n ( i ) = arg min j ∈ N ( i ) ∧ j ∉ R ϕ ∥ h ( ϕ t ( x ) , P i ) − h ( ϕ t ( x ) , P j ) ∥ 2 2 (4) n n(i)=\underset{j \in \mathcal{N}(i) \wedge j \notin R^{\phi}}{\arg \min }\left\|h\left(\phi_{t}(x), P_{i}\right)-h\left(\phi_{t}(x), P_{j}\right)\right\|_{2}^{2} \tag 4 nn(i)=j∈N(i)∧j∈/Rϕargmin∥h(ϕt(x),Pi)−h(ϕt(x),Pj)∥22(4)
∣ R ϕ ∣ |R^{\phi}| ∣Rϕ∣:用于求均值
ϕ t ( x ) \phi_{t}(x) ϕt(x):VGG網絡輸入 x x x提取的特征
P i P_i Pi:以 i 為中心的一小塊區域(如以坐标 i 為中心的一個 3x3區域)
h ( ϕ t ( x ) , P i ) h\left(\phi_{t}(x), P_{i}\right) h(ϕt(x),Pi):提取 ϕ t ( x ) \phi_{t}(x) ϕt(x)中 P i P_i Pi區域的特征
n n ( i ) nn(i) nn(i):即已知區域裡的特征,和 P i P_i Pi最相似的部分。
TV Loss
平滑 loss
Υ ( x ) = ∑ i , j ( ( x i , j + 1 − x i , j ) 2 + ( x i + 1 , j − x i , j ) 2 ) \Upsilon(x)=\sum_{i, j}\left(\left(x_{i, j+1}-x_{i, j}\right)^{2}+\left(x_{i+1, j}-x_{i, j}\right)^{2}\right) Υ(x)=i,j∑((xi,j+1−xi,j)2+(xi+1,j−xi,j)2)
結果
小結
- Content Network 思想直接,直接輸出未知區域(周圍區域 + 大量資料訓練後模型的想象力)
- Texture Network 進一步比對相似的已知區域,比較直覺(人腦也是由已知去估計未知)
- 限制了隻能生成圖像中央的正方形區域。而且中心這麼一大塊區域,本身的恢複難度也很高。