PIXELBANKv9.1.0
Menu

Apply ToTensor Transform

Problem Statement

Apply the ToTensor transform to convert a NumPy array (simulating an image) into a PyTorch tensor.

Background

ToTensor from torchvision.transforms converts image data (PIL Image or NumPy array) into a normalized FloatTensor. It handles both the type conversion and value scaling automatically.

Your Task

Write a function apply_to_tensor(numpy_array) that applies the ToTensor transform and returns a dictionary describing the resulting tensor's shape, dtype, and value range.

Output Format

Return a dictionary with keys: "shape" (list), "dtype" (string), "min_val" (float, 2 decimals), "max_val" (float, 2 decimals).

Example:

Input:
numpy array of shape (2, 2, 3) with values 0-255
Output:
{"shape": [3, 2, 2], "dtype": "torch.float32", "min_val": 0.0, "max_val": 1.0}
Reasoning:

ToTensor converts (H,W,C) to (C,H,W) and scales values to [0,1]

Constraints:

  • Use torchvision.transforms.ToTensor
  • Input will be a NumPy array with shape (H, W, C) and dtype uint8
  • Values are in range [0, 255]
  • Round min_val and max_val to 2 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Apply ToTensor Transform - Easy | PixelBank