Implementation:Tensorflow Tfjs RandomHeight Layer
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Layers_API, Preprocessing, Data_Augmentation |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The RandomHeight layer is a data augmentation preprocessing layer that randomly adjusts the height of images during training. The height is scaled by a random factor drawn uniformly from the range [1 + heightLower, 1 + heightUpper]. The layer supports both bilinear and nearest interpolation methods. It extends BaseRandomLayer for reproducible random number generation.
Code Reference
Source Location
tfjs-layers/src/layers/preprocessing/random_height.ts (GitHub)
Key Imports
import {image, Rank, serialization, Tensor, tidy} from '@tensorflow/tfjs-core';
import {BaseRandomLayerArgs, BaseRandomLayer} from '../../engine/base_random_layer';
import {randomUniform} from '@tensorflow/tfjs-core';
Layer Class
export class RandomHeight extends BaseRandomLayer {
static override className = 'RandomHeight';
constructor(args: RandomHeightArgs);
override getConfig(): serialization.ConfigDict;
override computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
override call(inputs: Tensor<Rank.R3> | Tensor<Rank.R4>,
kwargs: Kwargs): Tensor[] | Tensor;
}
RandomHeightArgs
export interface RandomHeightArgs extends BaseRandomLayerArgs {
factor: number | [number, number]; // height adjustment factor range
interpolation?: InterpolationType; // 'bilinear' (default) or 'nearest'
seed?: number;
autoVectorize?: boolean;
}
Factor Interpretation
- Single number (e.g.,
0.2): Interpreted as[-0.2, 0.2], so height is scaled by a factor in[0.8, 1.2]. - Tuple (e.g.,
[-0.1, 0.3]): Height is scaled by a factor in[0.9, 1.3]. - Both bounds must be >= -1.0, and upper bound must be >= lower bound.
I/O Contract
| Method | Input | Output |
|---|---|---|
call |
3D or 4D image tensor | Tensor with randomly adjusted height, width unchanged |
computeOutputShape |
Input shape | [-1, imgWidth, numChannels] (height is dynamic)
|
Usage Example
import * as tf from '@tensorflow/tfjs';
const augmenter = tf.layers.randomHeight({
factor: 0.2, // height varies by +/- 20%
interpolation: 'bilinear',
seed: 42
});
const images = tf.randomNormal([4, 100, 100, 3]);
const augmented = augmenter.apply(images, {training: true});
// height will be randomly adjusted between 80 and 120 pixels
Related Pages
- Tensorflow_Tfjs_RandomWidth_Layer - Random width augmentation layer (symmetric counterpart)
- Tensorflow_Tfjs_Resizing_Layer - Deterministic image resizing layer
- Tensorflow_Tfjs_CenterCrop_Layer - Center crop preprocessing layer
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment