ココアのお勉強ブログ

技術職の一般人です。趣味でコード書いたりソフト触ったり。

SinGAN: Learning a Generative Model from a Single Natural Image 論文まとめ

SinGANという論文を軽く読んだのでメモ。

この記事は自分用にまとめたもので間違っている可能性があります。

もし間違い等あればご指摘して頂けると助かります。

どんなもの?

1枚の学習画像の内部統計をキャプチャする無条件の(つまり、ノイズから生成する)生成モデル。

つまりSinGANは画像「1枚だけ」から、それと似たような画像を生成出来る。

推論時に任意の解像度に変更可能で, 下層の入力画像をいじることで, ペイントから画像への変換、画像の編集、画像の調和、超解像度およびアニメーションを、アーキテクチャの変更やさらなるチューニングなしで実行するために使用できる。

f:id:hotcocoastudy:20191030011214p:plain

Figure 1: Image generation learned from a single training image. We propose SinGAN–a new unconditional generative model trained on a single natural image. Our model learns the image’s patch statistics across multiple scales, using a dedicated multi-scale adversarial training scheme; it can then be used to generate new realistic image samples that preserve the original patch distribution while creating new object configurations and structures.

先行研究と比べてどこがすごい?

f:id:hotcocoastudy:20191030011520p:plain

Figure 2: Image manipulation. SinGAN can be used in various image manipulation tasks, including: transforming a paint (clipart) into a realistic photo, rearranging and editing objects in the image, harmonizing a new object into an image, image super-resolution and creating an animation from a single input. In all these cases, our model observes only the training image (first row) and is trained in the same manner for all applications, with no architectural changes or further tuning (see Sec. 4).

1枚の自然画像に対してGANベースのモデルを提案したものはあるが、画像を画像にマッピングする(ある画像を入れると必ず決まった画像が出力される)ので、ランダムサンプルの生成には向いていない。

というか1枚の画像で生成するGANは今までテクスチャを生成するものがほとんどだった(らしい)。
その証拠に従来の1枚の画像で生成できるGANを使うとFigure 3のようになる。

f:id:hotcocoastudy:20191030011600p:plain

Figure 3: SinGAN vs. Single Image Texture Generation. Single image models for texture generation [3, 16] are not designed to deal with natural images. Our model can produce realistic image samples that consist of complex textures and non-reptititve global structures.

非テクスチャ画像だと意味のあるサンプルを生成していない。一方SinGANはPSGAN、Deep Texture Synthesisよりうまく描画できている。SinGANではテクスチャ画像に限定されずに一般的な自然画像(非テクスチャ画像)でも1枚の画像から生成が可能になった(Figure 1)。

技術や手法のキモはどこ?

軽量なFCN (Fully Convolutional Network)のGANを何層かに積み重ねたようなGANピラミッドによって構成され、それぞれが異なるスケールでパッチの分布をキャプチャする。学習が完了すると、SinGANはさまざまな高品質の画像サンプル(任意のサイズ)を生成できる。

以下のFigure 4はSinGANの全体図である。各スケールに対してPatchGANを行うイメージ。

f:id:hotcocoastudy:20191030011753p:plain

Figure 4: SinGAN’s multi-scale pipeline. Our model consists of a pyramid of GANs, where both training and inference are done in a coarse-to-fine fashion. At each scale, Gn learns to generate image samples in which all the overlapping patches cannot be distinguished from the patches in the down-sampled training image, xn, by the discriminator Dn; the effective patch size decreases as we go up the pyramid (marked in yellow on the original image for illustration). The input to Gn is a random noise image zn, and the generated image from the previous scale x˜n, upsampled to the current resolution (except for the coarsest level which is purely generative). The generation process at level n involves all generators {GN . . . Gn} and all noise maps {zN , . . . , zn} up to this level. See more details at Sec. 2.

ピラミッド形式のGANで学習と推論の両方が「荒い→精巧」のような流れで行われる。

各スケールで Generator G_n は、すべてのオーバーラップパッチを Discriminator D_n がダウンサンプリングされた学習画像 x_n のパッチと騙されるような画像サンプルを生成するように学習する。

大雑把な流れは以下の図のようになる。

f:id:hotcocoastudy:20191104171753p:plain

 

f:id:hotcocoastudy:20191031121738p:plain

 

パッチサイズとは?

patchGANというGANで用いられたものです。パッチサイズごとにDiscriminatorがRealかFakeを判定している。

「patch GAN」の画像検索結果"

https://blog.shikoan.com/pytorch_pix2pix_colorization/より

f:id:hotcocoastudy:20191030020031p:plain

patchGANは決められたパッチサイズごとにRealかFakeかを判別する

https://blog.paperspace.com/unpaired-image-to-image-translation-with-cyclegan/より

(追記:なおSinGANでは識別器だけでなく生成器にもpatchGANを適用してあるとのことです。言葉足らずで申し訳ございませんでした。 参考https://github.com/Yagami360/MachineLearning-Papers_Survey/issues/40

Generator

画像サンプルの生成は、最も粗いスケール N (Figure 4の1番下側)から開始し、すべてのジェネレーターに最も細かいスケール 0 (Figure 4の1番上側)まで順次渡していく。

以下の図は Generator の様子。

Generatorは5つの畳み込みブロック(「Conv3x3」-「BatchNorm」-「LeakyReLU)」)を持つFCN (Fully Convolutional Network)である(つまり畳み込み層のみのネットワーク)。

f:id:hotcocoastudy:20191030012519p:plain

ブロックごとに、最も粗いスケールで32個のカーネル(畳み込みフィルターのこと)から開始し、4スケールごとに2倍のカーネル数にする。 

D_nG_nと同じアーキテクチャで、パッチサイズは11×11。

なおノイズマップの次元を変更することにより、テスト時に任意のサイズとアスペクト比の画像を生成できる。

Discriminator

各Discriminator D_nはマルコフ識別器(PatchGAN)であり、 x_nの各N×NパッチがRealかFakeか(つまり、 x_nのパッチijがRealかFakeか)を判別する。

 

学習

GANを粗いスケールから細かいスケールまで順番に学習される。

各GANが学習されると、固定される。

Loss

f:id:hotcocoastudy:20191030020848p:plain

n番目のGANのトレーニング損失は敵対項と再構成項で構成。

敵対損失 L_{adv}は、 x_nのパッチの分布と生成されたサンプル\tilde{x}_nのパッチの分布の間の距離に対してペナルティを科す。

論文では敵対的損失に対してWGAN-GP損失(最終的な識別損失=パッチ識別マップの平均)を用いているよう。

再構成損失 L_{rec}は、 特定のノイズマップセットからx_nを生成できることを保証するために

f:id:hotcocoastudy:20191031120828p:plain

を設定する。なお z^∗は固定されたノイズマップ(学習中にサンプリングされたノイズで学習中は固定されたまま)。

以上を踏まえて再構成損失 L_{rec}を以下のように定義する。

f:id:hotcocoastudy:20191030022737p:plain

f:id:hotcocoastudy:20191030022805p:plain

\tilde{x}^{rec}_nG_nからの再構成画像で、各スケールでのノイズ z_n標準偏差 σ_nの決定に使用されるらしい。

σ_nRMSE (\tilde{x}^{rec}_n)↑^r, x_n に比例し、そのスケールで追加する必要がある詳細の量を示すらしい。

どうやって有効だと検証した?

 以下はランダム画像サンプル生成結果。

1つの画像でSinGANをトレーニングした後、モデルは新しい構造とオブジェクト構成を表すリアルなランダム画像サンプルを生成できる。

また学習画像のパッチ分布は保持される。

f:id:hotcocoastudy:20191030030655p:plain

Figure 6: Random image samples. After training SinGAN on a single image, our model can generate realistic random image samples that depict new structures and object configurations, yet preserve the patch distribution of the training image. Because our model is fully convolutional, the generated images may have arbitrary sizes and aspect ratios. Note that our goal is not image retargeting – our image samples are random and optimized to maintain the patch statistics, rather than preserving salient objects. See SM for more results and qualitative comparison to image retargeting methods.

 以下は高解像度の生成結果。

f:id:hotcocoastudy:20191030031124p:plain

Figure 7: High resolution image generation. A random sample produced by our model, trained on the 243 × 1024 image (upper right corner); new global structures as well as fine details are realistically generated. See 4Mpix examples in SM.

 以下はさまざまなスケールからの生成(推論時)。

より細かいスケールnから生成するために、ダウンサンプリングされた元の画像 x_nをそのスケールへの入力とする。

これにより、生成された構造のスケールを制御することができる。

例えば、Zebraの形状とポーズを保持し n = N -1から生成を開始することでストライプテクスチャのみを変更できる。

f:id:hotcocoastudy:20191030031302p:plain

Figure 8: Generation from different scales (at inference). We show the effect of starting our hierarchical generation from a given level n. For our full generation scheme (n = N), the input at the coarsest level is random noise. For generation from a finer scale n, we plug in the downsampled original image, xn, as input to that scale. This allows us to control the scale of the generated structures, e.g., we can preserve the shape and pose of the Zebra and only change its stripe texture by starting the generation from n = N −1.

以下は異なる数のスケールでの学習の効果。

SinGANのスケールの数は、結果に大きく影響する。

スケールの数が少ないモデルは、テクスチャのみをキャプチャする。

スケールの数が増えると、SinGANは画像内物体のより大きな構造をキャプチャすることができるということを示している。

f:id:hotcocoastudy:20191030031915p:plain

Figure 9: The effect of training with a different number of scales. The number of scales in SinGAN’s architecture strongly influences the results. A model with a small number of scales only captures textures. As the number of scales increases, SinGAN manages to capture larger structures as well as the global arrangement of objects in the scene.

以下は超解像の結果。

f:id:hotcocoastudy:20191030032320p:plain

Figure 10: Super-Resolution. When SinGAN is trained on a low resolution image, we are able to super resolve. This is done by iteratively upsampling the image and feeding it to SinGAN’s finest scale generator. As can be seen, SinGAN’s visual quality is better than the SOTA internal methods ZSSR [46] and DIP [51]. It is also better than EDSR [32] and comparable to SRGAN [30], external methods trained on large collections. Corresponding PSNR and NIQE [40] are shown in parentheses.

以下はペイント→写真変換の結果。

f:id:hotcocoastudy:20191030032401p:plain

Figure 11: Paint-to-Image. We train SinGAN on a target image and inject a downsampled version of the paint into one of the coarse levels at test time. Our generated images preserve the layout and general structure of the clipart while generating realistic texture and fine details that match the training image. Well-known style transfer methods [17, 38] fail in this task.

以下は画像編集の結果。

f:id:hotcocoastudy:20191030032453p:plain

Figure 12: Editing. We copy and paste a few patches from the original image (a), and input a downsampled version of the edited image (b) to an intermediate level of our model (pretrained on (a)). In the generated image (d), these local edits are translated into coherent and photo-realistic structures. (c) comparison to Photoshop content aware move.

以下は画像調和の結果。

f:id:hotcocoastudy:20191030032551p:plain

Figure 13: Harmonization. Our model is able to preserve the structure of the pasted object, while adjusting its appearance and texture. The dedicated harmonization

議論はある? 

 色々なものに応用できる点が本当にすごいしクオリティも高い。

Twitterでは「SinGANがbest paperかぁ...」みたいな微妙な反応がちらほらあったのが気になりました。

次に読むべき論文は?

 InGAN : http://openaccess.thecvf.com/content_ICCV_2019/html/Shocher_InGAN_Capturing_and_Retargeting_the_DNA_of_a_Natural_Image_ICCV_2019_paper.html

参考

 http://peluigi.hatenablog.com/entry/2019/10/29/190927

 https://github.com/Yagami360/MachineLearning-Papers_Survey/issues/40

 

著者実装(Pytorch)

 https://github.com/tamarott/SinGAN