mistral_common.tokens.tokenizers.image
ImageConfig(image_patch_size, max_image_size, spatial_merge_size=1)
dataclass
Configuration for image tokenization.
Attributes:
| Name | Type | Description |
|---|---|---|
image_patch_size |
int
|
Size of a single image patch in pixels. The image grid is made of patches of this size; must be > 0. |
max_image_size |
int
|
Maximum image dimension (width or height) in pixels before downsampling; must be > 0. |
spatial_merge_size |
int
|
Number of adjacent patches merged into one image token along each dimension; must be > 0. |
ImageEncoder(image_config, special_ids)
Encodes images into tokens and processed arrays.
An image is turned into a grid of patch tokens: each row is width_tokens
img tokens terminated by an img_break token, and the last row ends with an
img_end token. The image array is resized so its dimensions are multiples
of the patch and merge sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_config
|
ImageConfig
|
Configuration controlling patch size, max image size, and spatial merging. |
required |
special_ids
|
SpecialImageIDs
|
Token IDs used to mark image patches, row breaks, and the image end. |
required |
Source code in src/mistral_common/tokens/tokenizers/image.py
image_token
property
mm_config
property
__call__(content)
Convert an image chunk into an image encoding.
The image is loaded, resized so its token grid covers the image, and
encoded as img/img_break/img_end marker tokens plus the processed
pixel array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
ImageChunk | ImageURLChunk
|
Image chunk to be converted. |
required |
Returns:
| Type | Description |
|---|---|
ImageEncoding
|
An ImageEncoding with the marker tokens and the processed image |
ImageEncoding
|
of shape (C, H, W). |
Source code in src/mistral_common/tokens/tokenizers/image.py
ImageEncoding(tokens, image)
dataclass
A tokenized image.
Attributes:
| Name | Type | Description |
|---|---|---|
tokens |
list[int]
|
The token IDs representing the image (img/break/end markers). |
image |
ndarray
|
The processed image as a numpy array of shape (C, H, W), normalized and resized for the model. |
Examples:
>>> import numpy as np
>>> image_encoding = ImageEncoding(tokens=[1, 2, 3], image=np.array([[0., 0.5, 1.]]))
MultiModalVersion
Version of the image tokenizer.
Attributes:
| Name | Type | Description |
|---|---|---|
m1 |
The first multimodal version, patch size 16 and max image size 1024. |
config
property
The image config associated with this version.
Returns:
| Type | Description |
|---|---|
ImageConfig
|
The ImageConfig for this multimodal version. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the version has no config. |
SpecialImageIDs(img, img_break, img_end)
dataclass
image_from_chunk(chunk)
Load a serializable image from a chunk.
Resolves the image from the chunk's data, accepting base64 data URLs, file URIs, and http(s) URLs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
ImageURLChunk | ImageChunk
|
The chunk to get the image from. |
required |
Returns:
| Type | Description |
|---|---|
SerializableImage
|
The image as a PIL Image object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a data URL does not contain a base64 payload or the URL scheme is unsupported. |
Source code in src/mistral_common/tokens/tokenizers/image.py
normalize(np_image, mean, std)
Normalize an image array with per-channel mean and standard deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
np_image
|
ndarray
|
Image to normalize, with shape (H, W, C) and values in [0, 255]. |
required |
mean
|
tuple[float, float, float]
|
Mean for each channel, values in [0, 1]. |
required |
std
|
tuple[float, float, float]
|
Standard deviation for each channel, values in [0, 1]. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Normalized image with shape (C, H, W), scaled to unit variance. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in src/mistral_common/tokens/tokenizers/image.py
transform_image(image, new_size)
Resize and normalize an image for the model.
Converts to RGB (transparent backgrounds become white), resizes with cubic interpolation, then normalizes with the dataset statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL image to transform. |
required |
new_size
|
tuple[int, int]
|
Target (width, height) in pixels. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Transformed image with shape (C, H, W), normalized. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If opencv is not installed. |