Implementation:Tensorflow Tfjs CPU ResizeBilinear Kernel
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, CPU_Backend |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The ResizeBilinear kernel resizes 4D image tensors to a specified size using bilinear interpolation on the CPU backend. It supports both alignCorners and halfPixelCenters modes, which control how source pixel coordinates are computed during the interpolation.
Code Reference
Source Location
tfjs-backend-cpu/src/kernels/ResizeBilinear.ts (GitHub)
Signature
export function resizeBilinear(args: {
inputs: ResizeBilinearInputs,
backend: MathBackendCPU,
attrs: ResizeBilinearAttrs
}): TensorInfo
Imports
import {KernelConfig, KernelFunc, ResizeBilinear, ResizeBilinearAttrs, ResizeBilinearInputs, TensorInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';
Kernel Config
export const resizeBilinearConfig: KernelConfig = {
kernelName: ResizeBilinear,
backendName: 'cpu',
kernelFunc: resizeBilinear as unknown as KernelFunc
};
I/O Contract
Inputs
| Name | Type | Description |
|---|---|---|
images |
TensorInfo |
4D input tensor of shape [batch, height, width, channels]
|
Attributes
| Name | Type | Description |
|---|---|---|
size |
[number, number] |
Target [newHeight, newWidth]
|
alignCorners |
boolean |
If true, aligns the corner pixels of input and output |
halfPixelCenters |
boolean |
If true, pixel centers are at half-pixel offsets |
Output
Returns a TensorInfo with float32 dtype and shape [batch, newHeight, newWidth, channels].
Algorithm
- Computes effective input and output sizes based on
alignCornerssetting. - Calculates row and column scaling ratios:
effectiveInputSize / effectiveOutputSize. - For each output pixel
[b, r, c, d]:- Computes the fractional source row and column. When
halfPixelCentersis true, applies a 0.5-pixel offset. - Determines the floor and ceil source rows/columns, clamped to valid bounds.
- Retrieves the four nearest neighbor values:
topLeft,topRight,bottomLeft,bottomRight. - Performs bilinear interpolation: first interpolates horizontally (top and bottom), then vertically between the results.
- Computes the fractional source row and column. When
- Returns the result as a new tensor via
backend.makeTensorInfo.
Usage Example
import * as tf from '@tensorflow/tfjs-core';
import '@tensorflow/tfjs-backend-cpu';
const image = tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]);
const resized = tf.image.resizeBilinear(image, [4, 4]);
resized.print();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment