ビジュアルAIガイド
VQGAN とコードブック画像合成
VQGANは、画像を学習済みのコードブック(再利用可能な視覚パーツの辞書)からのエントリの小さなグリッドに圧縮し、そのグリッドを鮮明な画像にデコードするニューラルネットワークです。
このガイドを聴くAIによるナレーション · ~9 min
このページでは10分で読めます
概要
2020年に導入され、高解像度でトランスフォーマーによる画像生成を実用化し、トークナイザーのアイデアは多くのテキストから画像へのモデルやマルチモーダルモデルの基盤となっています
主なポイント
- VQGANは画像を学習済みのコードブックからのインデックスの小さなグリッドに変換するため、画像生成はトークン予測問題となります。
- Esser、Rombach、Ommerによる2020年のTaming Transformersの論文では、256x256画像に対してデフォルトで16x16のトークングリッドを使用するVQGANが導入されました。
- 知覚損失および敵対的損失を用いることで、VQGANはVQ-VAEよりもはるかに高い圧縮率を実現しつつ、鮮明な再構成を維持することができます。
- VQGAN-CLIP、MaskGIT、ViT-VQGAN、Partiはすべて2021年から2022年にかけてVQGANトークナイザーのアイデアを直接採用しました
- コードブック崩壊、遅い順次サンプリング、細部の欠落はコードブック画像合成の主な弱点です
ディープダイブ
VQGAN, short for Vector Quantized Generative Adversarial Network, is an image autoencoder with a twist. An autoencoder is a pair of networks: an encoder that squeezes an image into a compact representation and a decoder that rebuilds the image from it. In VQGAN the compact representation is not a grid of arbitrary numbers but a grid of indices into a codebook, a learned table of vectors that act like a vocabulary of visual parts such as a patch of fur, a stretch of sky or the corner of a window. Once an image is a grid of codebook indices, it looks like a sentence made of tokens, and any model that is good at predicting the next token in text can be trained to generate images.
The method was introduced by Patrick Esser, Robin Rombach and Bjorn Ommer of Heidelberg University in the paper Taming Transformers for High-Resolution Image Synthesis, first posted to arXiv on 17 December 2020 and revised in June 2021. Their motivation was cost. Transformers, the attention-based networks behind modern language models, compare every element of a sequence with every other element, so the cost grows with the square of the sequence length. A 256 by 256 image has 65,536 pixels, far too many to treat as a sequence. Convolutional neural networks, which process images with small sliding filters, are efficient at local texture but weaker at long-range structure. The paper's answer was to let a convolutional VQGAN learn the vocabulary of local parts and let a transformer learn how those parts are arranged.
The mechanism works in two stages. In stage one, the encoder downsamples the image by a factor f; the paper's default is f = 16, so a 256 by 256 image becomes a 16 by 16 grid of feature vectors. Each vector is replaced by its nearest neighbor in the codebook, a step called vector quantization. The default codebook holds 1,024 entries, with larger codebooks of 4,096 to 16,384 entries used for some datasets. The decoder then reconstructs the image from the quantized grid. Because choosing the nearest entry is not differentiable, gradients are copied straight through the quantizer during training, a trick called the straight-through estimator. In stage two, the encoder and codebook are frozen, every training image is converted to its 256 indices, and a transformer is trained to predict each index from the ones before it. To generate, the transformer samples indices one by one and the decoder turns the finished grid into pixels.
What separates VQGAN from its predecessor is the training loss. VQ-VAE, introduced by Aaron van den Oord, Oriol Vinyals and Koray Kavukcuoglu at DeepMind in November 2017, established the codebook idea and showed that discrete latents avoid posterior collapse, a failure where a powerful decoder learns to ignore the latent code. But VQ-VAE was trained mainly to reproduce pixels, which pushes reconstructions toward blur when the code is heavily compressed. VQGAN adds two terms. A perceptual loss compares images in the feature space of a pretrained network rather than pixel by pixel, and a patch-based discriminator, the adversarial half of a generative adversarial network (GAN), penalizes local patches that look fake. The weight on the adversarial term is set automatically from the ratio of gradient sizes at the decoder's last layer. The result is that VQGAN can compress much harder while staying sharp. The paper reports that its ImageNet VQGAN with 16,384 codes on a 16 by 16 grid reaches a reconstruction FID of 4.98, against 32.01 for the DALL-E tokenizer with 8,192 codes on a 32 by 32 grid, while using a sequence one quarter as long. FID, or Frechet Inception Distance, measures how far the statistics of generated images are from real ones; lower is better.
The generation results were strong for their time. With a GPT-2-medium-sized transformer of about 307 million parameters, the paper reports FIDs of 9.6 on the FFHQ face dataset and 10.2 on CelebA-HQ. On class-conditional ImageNet at 256 by 256 pixels it reports an FID near 16 without rejection sampling and 5.20 with it, compared with about 31 for VQ-VAE-2 without rejection, using roughly a tenth of VQ-VAE-2's estimated parameter count. For images larger than the training size, the transformer attends only to a sliding window of nearby tokens, which allowed the authors to produce megapixel images guided by segmentation or depth maps.
VQGAN quickly became a building block. The VQGAN-CLIP method, written up by Katherine Crowson, Stella Biderman and colleagues for ECCV 2022, paired a pretrained VQGAN with OpenAI's CLIP model and optimized the latent grid until CLIP judged the decoded image to match a text prompt, with no task-specific training. Google's ViT-VQGAN (October 2021) replaced the convolutional encoder and decoder with Vision Transformers and improved codebook learning, raising Inception Score on ImageNet from 70.6 for vanilla VQGAN to 175.1 and lowering FID from 17.04 to 4.17. MaskGIT (February 2022) kept VQ tokens but generated them in parallel by iteratively filling in masked positions. Parti (June 2022) scaled an autoregressive Transformer on ViT-VQGAN tokens to 20 billion parameters. The same Heidelberg group's Latent Diffusion paper (December 2021) reused the idea of working in the compressed space of a pretrained autoencoder, but ran a diffusion model there instead of a token predictor.
Compared with alternatives, codebook synthesis has clear trade-offs. Against pixel-space GANs, it separates compression from generation, so the generator can be a large, stable, likelihood-trained transformer rather than an adversarial network that is prone to training collapse. Against pixel-space diffusion, it is far cheaper because the model works on a few hundred tokens rather than tens of thousands of pixels. Against latent diffusion with continuous latents, discrete tokens fit naturally into the same sequence as text tokens, which is why tokenizers of this family appear in models that mix text and images. The cost is that quantization throws information away, so fine detail such as small text or faces in a crowd can be lost before generation even begins.
The main failure modes are well documented. Codebook collapse, where only a small fraction of entries is ever selected, wastes capacity; the Taming Transformers authors note that their ImageNet model effectively used 973 of its 16,384 entries. Autoregressive sampling is slow because a 256-token image needs 256 sequential steps, which motivated MaskGIT's parallel decoding. Raster-order generation, row by row, can produce inconsistent global layouts, and outputs depend heavily on sampling settings such as top-k. Finally, a 16-fold downsampling limits how much detail any downstream model can express.
In practice the requirements are modest by current standards. The paper reports that most of its transformers were sized to train with batch size 2 on a 12 GB GPU, typically using two to four GPUs, and that an updated ImageNet model trained for 45.8 days on a single A100. Training a new VQGAN still needs a large image dataset matched to the target domain, careful tuning of the adversarial weight and a warm-up period before the discriminator switches on. The authors released code and pretrained models, so encoding and decoding with an existing VQGAN needs far less compute than training one.
04作業例
VQGANパイプラインで1枚の画像を追跡する
256×256ピクセルのRGB写真から始めます。これには65,536個のピクセル位置が含まれています。
ダウンサンプリング係数16でエンコーダーに通し、256個の特徴ベクトルからなる16×16のグリッドを取得します。
各ベクトルについて、1,024個のコードブックエントリから最も近いものを探し、そのインデックスを記録します。これにより、0から1,023の間の256個の整数が得られます。
256個のインデックスをデコーダーに入力し、再構築された画像と元の画像を、目視および知覚メトリクスで比較します。
グリッドの下半分をマスクし、学習済みのTransformerを使用して、top-kサンプリングにより欠落した128個のインデックスを1つずつサンプリングします。
完成したグリッドをデコードし、新しい下半分が上半分のテクスチャやレイアウトをどのように継続しているかを確認します。
それが示すもの
学習の結果、上半分の入力が再構築され、下半分が生成された画像が得られます。鮮明な再構築はコードブックが画像をうまく捉えていることを裏付け、一貫性のある補完はTransformerがコードブックのパーツがどのように組み合わさるかを学習したことを裏付けています。
現実世界の実装
元の Taming Transformers 論文では、VQGAN と GPT-2 スタイルのトランスフォーマーを組み合わせ、セマンティックセグメンテーションマップからメガピクセルの風景を生成し、スライディングアテンションウィンドウで画像をパッチごとに描画しました。
VQGAN-CLIP は Katherine Crowson らによって発表され、ECCV 2022 で受理されました。事前学習済み VQGAN を CLIP のテキスト‑画像モデルで制御し、追加の学習なしで自由文テキストプロンプトから画像の生成・編集を行います。
GoogleのMaskGIT(2022)は、1つずつではなく並列でマスクされたVQ画像トークンを予測し、著者らはオートレグレッシブ生成と比べてデコード速度が最大64倍速くなると報告しています。
GoogleのPartiテキスト・ツー・イメージモデル(2022)は、改良されたViT-VQGANトークナイザーと最大20億パラメータのTransformerを使用し、MS-COCOでゼロショットFID7.23を報告しています。
Taming Transformers論文のクラス条件付きImageNet生成は、拒否サンプリングと組み合わせることで256×256ピクセルでFID5.20を達成し、当時のオートレグレッシブモデルとしては強力なスコアでした。
VQGAN とコードブック画像合成の未来
Research since 2022 has focused on the tokenizer as the bottleneck. ViT-VQGAN (2021) showed that better codebook learning raises downstream image quality, and Finite Scalar Quantization (September 2023) argued that the learned codebook can be replaced with simple rounding while avoiding codebook collapse and the extra losses VQ needs. Parallel decoding methods such as MaskGIT (2022) addressed the speed of sequential sampling, and Parti (2022) showed autoregressive models on VQ tokens can scale to 20 billion parameters. Open questions remain about how much compression is too much: higher downsampling makes sequences shorter and cheaper but loses fine detail, and there is no settled rule for choosing the factor per domain. A second open question is whether discrete tokens or continuous latents are the better interface for models that handle text and images together, since latent diffusion (2021) showed that continuous latents also cut compute sharply, while discrete tokens slot directly into the same sequence as text. Evaluation also lags, because FID and Inception Score capture distribution statistics rather than prompt faithfulness or the legibility of small details.
実践する
なぜそれが重要なのか
- 速度とスケール. Visual AI は、検査、検出、タグ付けタスクを大規模に自動化できます。
- ビルドの選択. クリエイティブ チームは、手動での修正を減らし、より迅速にコンセプトのプロトタイプを作成できます。
- チームとワークフロー. 以前は処理が困難であった画像信号やビデオ信号を操作に使用できるようになります。
気をつけてください
- 出所が不明瞭な場合、肖像権と同意が法的リスクとなる可能性があります。
- モデルのパフォーマンスは、照明、人口統計、環境によって異なる場合があります。
- 信頼度のしきい値が監視されない限り、誤検知は気付かれない可能性があります。
次のステップ
- 精度、再現率、エラーコストの許容基準を定義します。
- 実際の生産条件に一致するデータを使用してテストします。
- 信頼性の低い予測や影響の大きい予測については、人間によるレビューを追加します。
- モデルのドリフトを追跡し、カメラまたはデータセットの変更後に再検証します。
出典とさらなる参考文献
- arXiv (Esser, Rombach, Ommer)高解像度画像合成のためのTaming Transformers
- arXiv (van den Oord, Vinyals, Kavukcuoglu)ニューラル離散表現学習
- arXiv (Google Research)改良されたVQGANを用いたベクトル量子化画像モデリング
- arXiv / ECCV 2022VQGAN-CLIP:自然言語ガイダンスによるオープンドメイン画像生成と編集
- arXiv (Google Research)MaskGIT:マスク付き生成画像トランスフォーマー
- arXiv (Google Research)コンテンツリッチなテキスト・ツー・イメージ生成のためのオートレグレッシブモデルのスケーリング
- arXiv / CVPR 2022潜在拡散モデルによる高解像度画像合成
- arXiv (Google DeepMind)有限スカラー量子化:VQ-VAEをシンプルに
探検を続けましょう
よくある質問
VQGANとコードブック画像合成とは?
VQGANは、学習済みコードブック(再利用可能な視覚パーツの辞書)から画像を小さなグリッドに圧縮し、そのグリッドを鋭い画像にデコードするニューラルネットワークです。2020年に導入され、高解像度でのトランスフォーマー画像生成を実用化し、そのトークナイザーのアイデアは多くのテキスト・ツー・イメージおよびマルチモーダルモデルの基盤となっています。
VQGAN は画像を個別のトークンとして表現するために何を使用しますか?
VQGAN は、エンコーダの特徴を学習されたコードブック内の最も近いエントリに量子化し、画像を離散コード インデックスのシーケンスに変換します。
元のVQGAN論文は、敵対的および知覚的トレーニングから何を得たと報告していますか?
元の論文では、敵対的および知覚的損失が評価設定で圧縮率が上がった際に知覚品質を維持するのに役立ったと報告しています。これはすべてのVQVAEやすべてのトークングリッドサイズに対する普遍的な主張ではありません。
画像が一連のトークンになったら、どのモデルが新しい画像を生成するのでしょうか?
画像は離散トークン シーケンスになるため、トランスフォーマーはテキストを生成するのと同じように自己回帰的に画像をモデル化できます。
微分不可能な量子化ステップで勾配を流すにはどのような手法がありますか?
ベクトル量子化は微分不可能であるため、VQGAN はストレートスルー勾配推定器を使用してエンコーダーをトレーニングします。
VQGAN が 2021 年の創造に貢献した有名な AI アート トレンドは何ですか?
VQGAN と CLIP ガイダンスを組み合わせることで、広く共有される「VQGAN+CLIP」アートが生み出され、テキスト駆動の画像生成が普及しました。