Problems
Loading...
Step 6 of 6
Data Augmentation
Medium

Implement the two deterministic image transformations used by this exercise. random_crop extracts a square crop at the supplied top-left coordinates. random_horizontal_flip reverses the width axis when flip_rand is less than p and otherwise returns the image unchanged.

Images use (H,W,C)(H,W,C) layout. Return a new float64 NumPy array from either function.

Loading visualization...

Examples

Input: method = "random_crop", image = [[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]], crop_size = 2, crop_y = 1, crop_x = 0

Output: [[[4],[5]],[[7],[8]]]

Explanation: Rows 1 and 2 and columns 0 and 1 form the requested crop.

Input: method = "random_horizontal_flip", image = [[[1],[2],[3]],[[4],[5],[6]]], p = 0.5, flip_rand = 0.2

Output: [[[3],[2],[1]],[[6],[5],[4]]]

Input: method = "random_horizontal_flip", image = [[[1,10],[2,20]],[[3,30],[4,40]]], p = 0.25, flip_rand = 0.8

Output: [[[1,10],[2,20]],[[3,30],[4,40]]]

Hint 1

A crop is image[crop_y:crop_y + crop_size, crop_x:crop_x + crop_size, :].

Hint 2

Horizontal flipping reverses axis 1 in HWC layout.

Hint 3

Call .copy() so the result does not share storage with the input.

Requirements

  • random_crop must return the crop beginning at (crop_y, crop_x).
  • random_horizontal_flip must reverse only the width axis when flip_rand is less than p.
  • Neither function may modify the input array.
  • Return a new float64 NumPy array.

Constraints

  • image has shape (H,W,C)(H,W,C).
  • crop_size is positive and the requested crop lies inside the image.
  • p and flip_rand are between 0 and 1, inclusive.
  • Inputs and outputs use float64.