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
numpy array of shape (2, 2, 3) with values 0-255
{"shape": [3, 2, 2], "dtype": "torch.float32", "min_val": 0.0, "max_val": 1.0}- Input: a tiny image with 3 colour channels, laid out height × width × channels, , as unsigned 8-bit integers in . The top-left pixel is the colour triple .
- Layout: the tensor is channels-first, , so the shape becomes
[3, 2, 2]. Channel 0 is a grid holding the first value of every pixel: . - Scaling: every value is divided by and stored as 32-bit floating-point numbers: , , .
- Range: the smallest raw value is , the largest is . Rounded to 2 decimal places,
min_valis0.0andmax_valis1.0. - Output: a dictionary whose
"shape"is a plain list of the three dimension sizes and whose"dtype"names the 32-bit float type as text. - Notice: values are only rearranged and rescaled. A uniform image of s gives , so both
min_valandmax_valround to0.5.
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