llmcompressor.modeling.kimi_k3.media_utils
Classes:
-
TransparentBgConfig–The config of the transparent background.
Functions:
-
fill_transparent_bg_with–Composite a (possibly) transparent image onto a configured background.
-
image_to_np–Convert an image to a numpy array.
-
navit_patchify–Reshape the pixel values to a navit shape.
-
normalize–Normalize the image.
TransparentBgConfig
dataclass
TransparentBgConfig(
pattern: Literal[
"white", "black", "gray", "chessboard"
] = "black",
chessboard_square_size: int = 16,
chessboard_square_on_top_left: bool = True,
chessboard_white_value: int = 255,
chessboard_gray_value: int = 200,
)
The config of the transparent background.
Attributes:
-
chessboard_gray_value(int) –The value of the gray pixels in the background.
-
chessboard_square_on_top_left(bool) –Whether to start the chessboard with a white square on the top left.
-
chessboard_square_size(int) –The size of the squares in the chessboard background.
-
chessboard_white_value(int) –The value of the white pixels in the background.
-
pattern(Literal['white', 'black', 'gray', 'chessboard']) –The pattern of the transparent background.
chessboard_gray_value
class-attribute
instance-attribute
The value of the gray pixels in the background.
chessboard_square_on_top_left
class-attribute
instance-attribute
Whether to start the chessboard with a white square on the top left.
chessboard_square_size
class-attribute
instance-attribute
The size of the squares in the chessboard background.
chessboard_white_value
class-attribute
instance-attribute
The value of the white pixels in the background.
fill_transparent_bg_with
fill_transparent_bg_with(
image: Image,
transparent_bg_config: TransparentBgConfig
| None = None,
) -> Image.Image
Composite a (possibly) transparent image onto a configured background.
When transparent_bg_config is None, the image is simply converted
to RGB (preserving the historical behavior). Otherwise the alpha channel
is alpha-composited over a background generated according to the config.
Source code in src/llmcompressor/modeling/kimi_k3/media_utils.py
image_to_np
image_to_np(
image: Image,
resize_to: tuple[int, int] | None = None,
mode: str = "resize",
raise_error_for_ill_resize: bool = True,
transparent_bg_config: TransparentBgConfig
| None = None,
transparent_bg_fill_stage: Literal[
"before_resize", "after_resize"
] = "before_resize",
) -> np.ndarray
Convert an image to a numpy array.
Args:
content: The image to convert.
resize_to: The size to resize the image to.
mode: The mode to resize the image to.
raise_error_for_ill_resize: Whether to raise an error for ill-sized resize.
transparent_bg_config: The config of the transparent background. Only
used when transparent_bg_fill_stage == "after_resize" (the
caller is responsible for filling before resize otherwise).
transparent_bg_fill_stage: When to composite the transparent
background — before or after the resize step.
Returns: A numpy array.
Source code in src/llmcompressor/modeling/kimi_k3/media_utils.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
navit_patchify
Reshape the pixel values to a navit shape.
Args: pixel_values: np.ndarray, shape (t, h, w, c) patch_size: int
Returns: dict[str, np.ndarray] - patches: np.ndarray, shape (t * h//patch_size * w//patch_size, c, patch_size, patch_size) - grid_thw: np.ndarray, (t, h//patch_size, w//patch_size)
Source code in src/llmcompressor/modeling/kimi_k3/media_utils.py
normalize
Normalize the image.
Args: x: The image to normalize. The shape is (..., 3). The dtype is uint8. The range is [0, 255]. mean: The mean of the image. std_inv: The inverse of the std of the image. pixels_dtype: The dtype of the image. Returns: The normalized image. The shape is (..., 3). The dtype is determined by the pixels_dtype.