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 CropAndResize Kernel

From Leeroopedia


Knowledge Sources
Domains Deep_Learning, CPU_Backend
Last Updated 2026-02-10 06:00 GMT

Overview

The CropAndResize kernel extracts crops from an input image tensor and resizes them to a uniform spatial size on the CPU backend. Each crop is defined by a bounding box and a batch index. The kernel supports both bilinear and nearest-neighbor interpolation methods, and fills out-of-bounds regions with a configurable extrapolation value. The implementation follows the reference TensorFlow C++ kernel.

Code Reference

Source Location

tfjs-backend-cpu/src/kernels/CropAndResize.ts (GitHub)

Signature

export function cropAndResize(args: {
  inputs: CropAndResizeInputs,
  backend: MathBackendCPU,
  attrs: CropAndResizeAttrs
}): TensorInfo

Imports

import {buffer, CropAndResize, CropAndResizeAttrs, CropAndResizeInputs, KernelConfig, KernelFunc, TensorInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';

Kernel Registration

export const cropAndResizeConfig: KernelConfig = {
  kernelName: CropAndResize,
  backendName: 'cpu',
  kernelFunc: cropAndResize as unknown as KernelFunc
};

I/O Contract

Parameter Type Description
inputs.image TensorInfo 4D image tensor of shape [batch, imageHeight, imageWidth, numChannels]
inputs.boxes TensorInfo 2D tensor of shape [numBoxes, 4] with normalized coordinates [y1, x1, y2, x2]
inputs.boxInd TensorInfo 1D tensor of shape [numBoxes] mapping each box to a batch index
attrs.cropSize [number, number] Target [cropHeight, cropWidth] for each crop
attrs.method 'nearest' Interpolation method
attrs.extrapolationValue number Value used for positions outside the image boundaries
Return TensorInfo Output tensor of shape [numBoxes, cropHeight, cropWidth, numChannels]

Implementation Details

  • Allocates an output buffer of shape [numBoxes, cropHeight, cropWidth, numChannels].
  • For each box, computes heightScale and widthScale to map crop positions to image coordinates.
  • Bilinear interpolation: Computes the weighted average of the four nearest pixels using floor/ceil indices and linear interpolation weights (xLerp, yLerp).
  • Nearest-neighbor interpolation: Rounds to the closest pixel via Math.round.
  • Out-of-bounds positions (where the mapped coordinate falls outside [0, imageHeight-1] or [0, imageWidth-1]) are filled with extrapolationValue.
  • Boxes referencing a batch index beyond the image batch size are skipped.

Usage Examples

import {cropAndResize} from './CropAndResize';

const result = cropAndResize({
  inputs: {image: imageTensor, boxes: boxesTensor, boxInd: boxIndTensor},
  backend: cpuBackend,
  attrs: {cropSize: [24, 24], method: 'bilinear', extrapolationValue: 0}
});

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment