Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Tfjs CPU ResizeBilinear Kernel

From Leeroopedia
Revision as of 16:51, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_CPU_ResizeBilinear_Kernel.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

  1. Computes effective input and output sizes based on alignCorners setting.
  2. Calculates row and column scaling ratios: effectiveInputSize / effectiveOutputSize.
  3. For each output pixel [b, r, c, d]:
    1. Computes the fractional source row and column. When halfPixelCenters is true, applies a 0.5-pixel offset.
    2. Determines the floor and ceil source rows/columns, clamped to valid bounds.
    3. Retrieves the four nearest neighbor values: topLeft, topRight, bottomLeft, bottomRight.
    4. Performs bilinear interpolation: first interpolates horizontally (top and bottom), then vertically between the results.
  4. 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