Implementation:Kserve Kserve Paddle Image Preprocess
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Image Processing, PaddlePaddle |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
Concrete tool for preprocessing images for PaddlePaddle ResNet50 inference with resizing, center cropping, and ImageNet normalization provided by the KServe sample code.
Description
This module provides three image preprocessing functions designed for PaddlePaddle model inference:
- resize_short() -- Resizes an image so that its shortest side matches the target size while preserving the aspect ratio. Uses OpenCV's
cv2.resize(). - crop_image() -- Crops an image to a square of
target_size. Ifcenteris True, performs a center crop; otherwise, performs a random crop. - preprocess() -- Combines the full preprocessing pipeline: resizes the image to 224 on the short side, center-crops to 224x224, converts BGR to RGB, transposes from HWC to CHW format, normalizes to [0, 1], and applies ImageNet mean ([0.485, 0.456, 0.406]) and standard deviation ([0.229, 0.224, 0.225]) normalization. Returns the result with a batch dimension added.
Usage
Use these functions to preprocess images before sending them to a PaddlePaddle ResNet50 model deployed on KServe, ensuring consistency with the training-time transformations.
Code Reference
Source Location
- Repository: Kserve_Kserve
- File: docs/samples/v1beta1/paddle/img_preprocess.py
- Lines: 1-41
Signature
def resize_short(img, target_size):
"""resize_short"""
...
def crop_image(img, target_size, center):
"""crop_image"""
...
def preprocess(img):
...
Import
from img_preprocess import resize_short, crop_image, preprocess
I/O Contract
Inputs
resize_short()
| Name | Type | Required | Description |
|---|---|---|---|
| img | np.ndarray | Yes | Input image as a NumPy array (HWC format) |
| target_size | int | Yes | Target size for the shortest side of the image |
crop_image()
| Name | Type | Required | Description |
|---|---|---|---|
| img | np.ndarray | Yes | Input image as a NumPy array (HWC format) |
| target_size | int | Yes | Size of the square crop |
| center | bool | Yes | If True, performs center crop; if False, performs random crop |
preprocess()
| Name | Type | Required | Description |
|---|---|---|---|
| img | np.ndarray | Yes | Input image as a NumPy array in BGR format (HWC layout) |
Outputs
resize_short()
| Name | Type | Description |
|---|---|---|
| resized | np.ndarray | Resized image with shortest side equal to target_size |
crop_image()
| Name | Type | Description |
|---|---|---|
| img | np.ndarray | Cropped image of shape (target_size, target_size, channels) |
preprocess()
| Name | Type | Description |
|---|---|---|
| img | np.ndarray | Preprocessed image of shape (1, 3, 224, 224) with ImageNet normalization applied |
Usage Examples
Basic Usage
import cv2
from img_preprocess import preprocess
# Load an image using OpenCV (returns BGR format)
img = cv2.imread("sample_image.jpg")
# Preprocess for PaddlePaddle ResNet50
processed = preprocess(img)
print(processed.shape) # (1, 3, 224, 224)
# Send to KServe prediction endpoint
import requests
payload = {"instances": processed.tolist()}
response = requests.post("http://localhost:8080/v1/models/resnet50:predict", json=payload)
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment