Params Validator
Validates the parameters of an image generation against the model catalog before the call is sent to the provider.
On this page
Source: src/image_generation/utils/params_validator.py. This page's original notebook was empty; the content was written from the code.
#Overview
The ImageParamsValidator class takes an optional catalog in the constructor; without it, it uses IMAGE_MODELS_CATALOG (see Config). The validate() method returns nothing: if everything is correct, it returns silently; otherwise, it raises ValueError.
#validate parameters
All are keyword-only and required.
| Parameter | Type | Rule |
|---|---|---|
model | str | Must exist in the catalog. |
number_of_images | int | Must be greater than or equal to 1 and cannot exceed the model's max_output_images_per_prompt. |
output_mime_type | str | Must be in the model's mime_types. |
aspect_ratio | str | Must be in the model's supported_aspect_ratios. |
image_size | str | Must be in the model's supported_resolutions (for example 1K, 2K). |
The checks run in this order: model, image count, MIME type, aspect ratio and resolution. The first one that fails stops the validation.
#Usage example
from src.image_generation.utils.params_validator import ImageParamsValidator
validator = ImageParamsValidator()
# Passes silently when the parameters are valid
validator.validate(
model="imagen-4.0-fast-generate-001",
number_of_images=2,
output_mime_type="image/png",
aspect_ratio="16:9",
image_size="1K",
)
# Raises ValueError: the fast model only accepts 1K
validator.validate(
model="imagen-4.0-fast-generate-001",
number_of_images=2,
output_mime_type="image/png",
aspect_ratio="16:9",
image_size="2K",
)#Notes
The error messages of validate() are in English and, for MIME type, aspect ratio and resolution failures, list the values accepted by the model.
The validator is used by TextToImage in generation.py, before calling generate_images.
Source: src/image_generation/utils/params_validator.py