Image-like Reshaping
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:
- Reshapes flat data to image shape (H, W, C)
- Also returns channel-first format (C, H, W)
- 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
data = [0..11], height = 2, width = 2, channels = 3
Image shape [2, 2, 3], channels_first shape [3, 2, 2]
- Inputs:
flat_datais the 12 numbers in one line;height,width,channels; slots, one per number. - Image layout : channel varies fastest, then column, then row. The value at row , column , channel is . Pixel holds
[0, 1, 2], pixel holds[3, 4, 5], and the second row holds[6, 7, 8]and[9, 10, 11]. - Channels first : each channel becomes its own plane: entry equals image entry . Channel 0 collects →
[[0, 3], [6, 9]]; channel 1 is[[1, 4], [7, 10]]; channel 2 is[[2, 5], [8, 11]]. Shape[3, 2, 2]. flatandshape: reading the image back in the same order returns through unchanged;shapeis[2, 2, 3].- Notice channels-first is not the 12 numbers re-cut in order into a block; each value keeps its pixel and channel.
Constraints
- len(flat_data) == height * width * channels
- Use np.transpose for axis reordering