ClipImagePreprocessor<T>

Models & Types · AiDotNet.Preprocessing.Image

Preprocesses images for CLIP (Contrastive Language-Image Pre-training) models.

For Beginners

Before CLIP can “see” an image, it needs to be prepared:

  1. Resize: Images come in all sizes (1000x2000, 50x50, etc.)

CLIP expects a specific size (like 224x224 pixels).

  1. Normalize: Pixel values (0-255) are scaled and shifted using

standard values from ImageNet dataset. This helps the model work consistently.

  1. Format: The image is arranged as [R, G, B] channels first,

then height and width. This is called “channels-first” format.

Example:

  • Original: 1920x1080 photo with RGB values 0-255
  • After preprocessing: 224x224 tensor with normalized values around [-2, 2]

How It Works

CLIP models expect images to be preprocessed in a specific way:

  1. Resize to a square size (typically 224x224 or 336x336)
  2. Normalize pixel values using ImageNet mean and standard deviation
  3. Convert to tensor format [channels, height, width]

Constructors

Constructor Summary
ClipImagePreprocessor(int imageSize, T[] mean, T[] std) Initializes a new instance of the ClipImagePreprocessor class.

Properties

Property Summary
int ImageSize { get; } Gets the target image size.

Methods

Method Summary
IEnumerable<Tensor<T>> PreprocessBatch(IEnumerable<Tensor<T>> images) Preprocesses a batch of images for CLIP input.
Tensor<T> Preprocess(Tensor<T> image) Preprocesses an image for CLIP input.