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 Resizing Layer

From Leeroopedia


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

Overview

The Resizing layer is an image preprocessing layer that resizes images to a fixed target height and width. It supports two interpolation methods: bilinear (default) and nearest. An optional cropToAspectRatio flag controls whether the image is cropped to preserve its aspect ratio or stretched to fill the target size.

Code Reference

Source Location

tfjs-layers/src/layers/preprocessing/image_resizing.ts (GitHub)

Key Imports

import {image, Rank, serialization, Tensor, tidy} from '@tensorflow/tfjs-core';
import {Layer, LayerArgs} from '../../engine/topology';
import {ValueError} from '../../errors';

Layer Class

export class Resizing extends Layer {
  static className = 'Resizing';
  constructor(args: ResizingArgs);
  override computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
  override getConfig(): serialization.ConfigDict;
  override call(inputs: Tensor<Rank.R3> | Tensor<Rank.R4>, kwargs: Kwargs): Tensor[] | Tensor;
}

ResizingArgs

export interface ResizingArgs extends LayerArgs {
  height: number;                      // target height
  width: number;                       // target width
  interpolation?: InterpolationType;   // 'bilinear' (default) or 'nearest'
  cropToAspectRatio?: boolean;         // default: false
}

Implementation Details

  • Validates the interpolation method against supported options (bilinear, nearest).
  • Uses image.resizeBilinear or image.resizeNearestNeighbor from tfjs-core.
  • The alignCorners parameter is set to !cropToAspectRatio.

I/O Contract

Method Input Output
call 3D or 4D image tensor Resized tensor with spatial dims [height, width]
computeOutputShape Input shape [height, width, numChannels]

Usage Example

import * as tf from '@tensorflow/tfjs';

const resizer = tf.layers.resizing({
  height: 224,
  width: 224,
  interpolation: 'bilinear'
});

const img = tf.randomNormal([1, 480, 640, 3]);
const resized = resizer.apply(img);  // shape: [1, 224, 224, 3]

Related Pages

Page Connections

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