programdemos/image-pipeline/image-pipeline.xtl
An image pipeline as array programs: a picture built from masks, blurred, its edges found with the Sobel kernels, thresholded, and shrunk by max-pooling. Every 3 x 3 filter is the same two steps: the stack of the nine shifted copies of the image (its windows), then the kernel times the stack, summed. Run it with "xetal run demos/image-pipeline/image-pipeline.xtl"; it prints the windows' and the pooled map's shapes, then the picture, the edge strength, the edges and the pooled edges, each drawn with characters. The live page lets you edit the kernels and pick a picture, and click a pixel for its window times each kernel: https://softwarewrighter.github.io/X_eTaL-demos/image-pipeline/
row : Int
-- coordinates: each pixel's row and column. row, col: by broadcasting (t_able).
row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols
img : Float
img ← 0.15 + (0.7 × f̲loat disk) + (0.45 × f̲loat box) + 0.001 × f̲loat r̲oll! (s̲hape row) r̲eshape 60
blur : Float
-- the kernels. blur: a Gaussian blur. kx, ky: Sobel edge kernels (ky is kx turned a quarter, o_\, the transpose). thresh: the edge strength that counts as an edge.
blur ← (3 c̲at 3) r̲eshape 0.0625 0.125 0.0625 0.125 0.25 0.125 0.0625 0.125 0.0625
ᵘw̲indows : a -> a
-- the windows: nine shifted copies. u:w_indows x: rotating by a list of amounts gives one copy per amount, on a new first axis. Columns are shifted first; then the rows, which the new axis moved to axis 2. Item [a;b;r;c] is the pixel at row r + a, column c + b, for a and b in -1 0 1.
ᵘw̲indows ← { x → -1 0 1 o̲-₂ -1 0 1 o̲-₂ x }
A filter: kernel times windows, summed
ᵘf̲ilter : Num a => a -> a -> a
u:f_ilter k x: the kernel spread over the picture (3 x 3 x rows x cols) times the windows, summed over its two axes.
ᵘf̲ilter ← { k x → '+ r̲/₁₂ (ᵘw̲indows x) × k 'l̲eft t̲able x }
The pipeline
smooth : Float
smooth, gx, gy, mag, edges: the blur, the two Sobel gradients, the edge strength (their length), and where it passes the threshold.
smooth ← blur ᵘf̲ilter img
pool : Float
-- max-pooling: 2 x 2 blocks by reshape, reduce. pool: reshaped so each 2 x 2 block has two axes of its own, then the largest over them.
pool ← 'm̲ax r̲/₂ 'm̲ax r̲/₄ ((rows d̲iv 2) c̲at 2 c̲at (cols d̲iv 2) c̲at 2) r̲eshape mag