Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Deepseek ai Janus Image Post Processing AR

From Leeroopedia


Knowledge Sources
Domains Computer_Vision, Image_Generation
Last Updated 2026-02-10 09:30 GMT

Overview

Pattern for converting decoded VQ-VAE output tensors to displayable images and saving them to disk in the autoregressive generation pipeline.

Description

This user-defined pattern converts the VQ-VAE decoder's output tensor from float [-1, 1] to uint8 [0, 255] images. It handles NCHW→NHWC conversion, rescaling, clipping, and saving via PIL.

Usage

Implement this pattern after VQModel.decode_code() to produce final output images.

Code Reference

Source Location

  • Repository: Janus
  • File: generation_inference.py
  • Lines: L98-109

Pattern Implementation

import numpy as np
import PIL.Image

# dec: [parallel_size, 3, 384, 384] from VQModel.decode_code()
dec = dec.to(torch.float32).cpu().numpy().transpose(0, 2, 3, 1)  # NCHW -> NHWC
dec = np.clip((dec + 1) / 2 * 255, 0, 255)  # [-1,1] -> [0,255]

visual_img = np.zeros((parallel_size, img_size, img_size, 3), dtype=np.uint8)
visual_img[:, :, :] = dec

for i in range(parallel_size):
    save_path = os.path.join('generated_samples', f"img_{i}.jpg")
    PIL.Image.fromarray(visual_img[i]).save(save_path)

Import

import numpy as np
import PIL.Image
import os

I/O Contract

Inputs

Name Type Required Description
dec torch.Tensor [B, 3, H, W] Yes Decoded image tensor from VQ-VAE, range [-1, 1]
img_size int No Image dimension (default 384)
parallel_size int Yes Number of images in batch

Outputs

Name Type Description
visual_img np.ndarray [B, H, W, 3] uint8 images ready for display or saving
saved files JPEG/PNG files Images saved to disk

Usage Examples

Post-Process and Save

import numpy as np
import PIL.Image
import os

dec = vl_gpt.gen_vision_model.decode_code(
    generated_tokens.to(dtype=torch.int),
    shape=[parallel_size, 8, img_size // patch_size, img_size // patch_size]
)

dec = dec.to(torch.float32).cpu().numpy().transpose(0, 2, 3, 1)
dec = np.clip((dec + 1) / 2 * 255, 0, 255)

os.makedirs('generated_samples', exist_ok=True)
for i in range(parallel_size):
    PIL.Image.fromarray(dec[i].astype(np.uint8)).save(
        f'generated_samples/img_{i}.jpg'
    )

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment