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: Difference between revisions

From Leeroopedia
Auto-imported from implementations/Tensorflow_Tfjs_Resizing_Layer.md
 
Sync from local file
 
Line 90: Line 90:
== Related Pages ==
== Related Pages ==


* [[Tensorflow_Tfjs_CenterCrop_Layer]] - Center crop preprocessing layer
* [[Implementation:Tensorflow_Tfjs_CenterCrop_Layer]] - Center crop preprocessing layer
* [[Tensorflow_Tfjs_Rescaling_Layer]] - Pixel value rescaling layer
* [[Implementation:Tensorflow_Tfjs_Rescaling_Layer]] - Pixel value rescaling layer
* [[Tensorflow_Tfjs_RandomHeight_Layer]] - Random height augmentation
* [[Implementation:Tensorflow_Tfjs_RandomHeight_Layer]] - Random height augmentation
* [[Tensorflow_Tfjs_RandomWidth_Layer]] - Random width augmentation
* [[Implementation:Tensorflow_Tfjs_RandomWidth_Layer]] - Random width augmentation


[[Category:Implementations]]
[[Category:Implementations]]


[[Category:Implementations]]
[[Category:Implementations]]

Latest revision as of 10:52, 27 September 2026


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