PIXELBANKv9.1.0
Menu

Problem Statement

Reshape flat data into image-like 3D arrays and back.

Background

Images are typically 3D arrays: (height, width, channels)

  • Grayscale: (H, W, 1)
  • RGB: (H, W, 3)

Flattening and reshaping is common in deep learning:

  • Flatten for fully-connected layers
  • Reshape for convolutional layers

Your Task

Write a function image_reshape(flat_data, height, width, channels) that:

  1. Reshapes flat data to image shape (H, W, C)
  2. Also returns channel-first format (C, H, W)
  3. Returns flattened form

Output Format

Return a dictionary with:

  • "image": Shape (H, W, C) as list
  • "channels_first": Shape (C, H, W) as list
  • "flat": Flattened as list
  • "shape": Original image shape as list

Example:

Input:
data = [0..11], height = 2, width = 2, channels = 3
Output:
Image shape [2, 2, 3], channels_first shape [3, 2, 2]
Reasoning:

Reshape to (H,W,C), transpose to (C,H,W)

Constraints:

  • len(flat_data) == height * width * channels
  • Use np.transpose for axis reordering
solution.py

Test Results

0/0
Run code to see test results.
Image-like Reshaping - Medium | PixelBank