PixelCNN

On this page 8

PixelCNN is an autoregressive generative model for images that predicts each pixel from the pixels generated before it, using masked convolutional layers so that no pixel can see its own value or any later pixel. Introduced in 2016 alongside PixelRNN, it became one of the reference architectures of generative AI for images because it assigns an exact, tractable likelihood to every image it models.

Within the wider field of artificial intelligence, PixelCNN matters less as a production image generator today and more as a foundational idea: the same factorization of a joint distribution into conditional steps underlies VQ-VAE priors, autoregressive image transformers and the token-by-token generation of language models.

How PixelCNN factorizes an image

PixelCNN models an image as a product of conditional distributions. The pixels are placed in a fixed raster-scan order, left to right and top to bottom, and the probability of the whole image equals the product of the probability of each pixel given all previous pixels. For color images, the red, green and blue channels of one pixel are also ordered, so green is conditioned on red and blue on both.

The network enforces this order with masked convolutions. Each convolution kernel has its weights zeroed for positions to the right of the center in the current row and for every row below. The first layer uses mask type A, which also hides the center pixel, while all later layers use mask type B, which allows the center position because it already carries only information from earlier pixels.

Each pixel value is predicted as a 256-way softmax over intensities 0 to 255 in the original design. Treating intensity as a categorical variable lets the model express multimodal distributions, for example a pixel that is either very dark or very bright, which a single Gaussian output cannot represent.

The following table summarizes the core components of the architecture and the role each one plays.

ComponentFunctionEffect on the model
Mask type AHides the current pixel in the first layerPrevents the model from copying its target
Mask type BAllows the center position in deeper layersPasses earlier context through the stack
Residual blocksStack many masked layersExtends the receptive field without vanishing gradients
Softmax outputGives a distribution over 256 valuesCaptures multimodal pixel statistics
Channel orderingOrders R, G and B within a pixelKeeps color dependencies consistent
Core components of a PixelCNN

Training and sampling behavior

Training is fully parallel because every target pixel is already known. One forward pass computes all conditional distributions at once, and the loss is the negative log-likelihood of the true pixels, usually reported in bits per dimension. This makes PixelCNN far faster to train than PixelRNN, whose recurrent layers process rows sequentially.

Sampling is the opposite. Generation must proceed one pixel and one channel at a time, because each new value depends on the ones just produced. A 32 by 32 color image therefore needs 3,072 sequential network evaluations, and a 256 by 256 image needs nearly 200,000. Caching intermediate activations reduces the cost, but slow sampling remains the defining weakness of the family.

The contrast between the two phases is shown below.

PropertyTrainingSampling
ParallelismAll pixels at onceStrictly sequential
Network passes per imageOneOne per pixel and channel
Typical speedComparable to a standard CNNSeconds to minutes per image
Main bottleneckMemory for deep stacksSequential dependency chain
Training versus sampling in PixelCNN

Variants that fixed the original weaknesses

The original design had a blind spot: stacked masked kernels never see a triangular region of pixels to the upper right of the current position. Gated PixelCNN solved this by splitting the network into a vertical stack and a horizontal stack, and it replaced ReLU activations with gated tanh-sigmoid units that improved likelihood scores.

Later versions focused on output modeling and efficiency. The table below lists the best-known variants and what each one changed.

VariantKey changeMain benefit
Gated PixelCNNVertical and horizontal stacks, gated unitsRemoves the blind spot, supports conditioning
PixelCNN++Discretized logistic mixture outputSmoother gradients and fewer parameters
Conditional PixelCNNClass labels or embeddings as extra inputClass-specific or guided generation
PixelSNAILAdds causal self-attentionCaptures long-range structure
VQ-VAE priorModels discrete latent codes, not pixelsMuch shorter sequences and faster sampling
Notable PixelCNN variants

PixelCNN++ is especially influential because its discretized logistic mixture likelihood treats neighboring intensities as similar, unlike the 256-way softmax, which treats value 127 as unrelated to 128.

How PixelCNN compares with other generative models

PixelCNN occupies the exact-likelihood corner of the generative model landscape. A GAN trains a generator against a discriminator and produces sharp samples quickly but offers no likelihood. A variational autoencoder optimizes a lower bound on likelihood and samples fast but often looks blurry. A diffusion model reaches excellent quality through many denoising steps, and a normalizing flow gives exact likelihood with invertible transforms.

Model familyLikelihoodSampling speedTypical sample quality
PixelCNNExactVery slowGood local detail, weak global coherence
GANNoneVery fastSharp, risk of mode collapse
Variational autoencoderLower boundFastOften blurry
Diffusion modelBound or estimateSlow to moderateState of the art
Normalizing flowExactFastModerate
PixelCNN among major generative model families

Because its likelihood is exact, PixelCNN long served as a benchmark for density estimation on datasets such as CIFAR-10 and ImageNet at low resolution, where results near 3 bits per dimension were typical for strong models.

Where PixelCNN is still used

Pure pixel-level PixelCNN is rarely used to generate high-resolution images today. Its main role is as a learned prior over discrete codes: in a VQ-VAE pipeline, an encoder compresses an image into a small grid of codebook indices, and a PixelCNN or PixelSNAIL models that grid. Sampling a 32 by 32 code grid is far cheaper than sampling every pixel.

  • Density estimation and lossless compression research, where exact likelihood is required.
  • Out-of-distribution detection, with the caveat that raw likelihoods can favor simple images.
  • Image completion from a known top portion, since the raster order naturally fits that task.
  • Teaching autoregressive modeling, because the masking idea transfers directly to causal transformers.

Limitations and common misconceptions

The most cited limitation is sequential sampling cost, but quality issues matter as well. Samples tend to show convincing textures with weak global structure, because the model optimizes local prediction and the raster order is arbitrary. Errors also accumulate during sampling, since every pixel conditions on earlier generated values rather than on real data.

A frequent misconception is that a high likelihood means a realistic sample. Likelihood rewards covering all data modes and predicting fine pixel noise, which does not guarantee visual quality. Another is that PixelCNN is obsolete: the autoregressive principle it popularized is central to modern token-based image and video generation.

The simple explanation

PixelCNN paints a picture one dot at a time. It starts in the top-left corner. For each new dot, it looks only at the dots already painted. Then it guesses the most likely color for the next one.

During learning, it sees many real pictures and gets better at these guesses. It can check all of its guesses at once, so learning is quick. Painting a new picture is slow, though. Every dot must wait for the one before it. The key idea is simple: a whole image is built from many small, ordered guesses.

How it is used in practice

A research team building a lossless image compressor for medical scans needs an accurate probability for every pixel value. They train a PixelCNN++ model on 50,000 grayscale scans at 128 by 128 pixels. An arithmetic coder then uses the predicted distributions to encode each pixel in the same raster order.

Because the model predicts well, frequent intensity patterns receive short codes. The team measures about 3.4 bits per pixel against roughly 4.1 for a classical lossless codec on the same scans. Decoding is slow, since each pixel must be predicted in turn, so the system is used for archival storage, where file size matters more than speed.