Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions bench_info/blur.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"benchmark": {
"name": "blur",
"short_name": "blur",
"relative_path": "image_processing/blur",
"module_name": "blur",
"func_name": "blur",
"kind": "microbench",
"domain": "Image Processing",
"dwarf": "dense_linear_algebra",
"parameters": {
"S": { },
"M": { },
"L": { },
"paper": { }
},
"init": {
"func_name": "initialize",
"input_args": [],
"output_args": ["image", "output"]
},
"input_args": ["image", "output"],
"array_args": ["image", "output"],
"output_args": ["output"]
}
}
26 changes: 26 additions & 0 deletions bench_info/hist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"benchmark": {
"name": "hist",
"short_name": "hist",
"relative_path": "image_processing/hist",
"module_name": "hist",
"func_name": "hist",
"kind": "microbench",
"domain": "Image Processing",
"dwarf": "dense_linear_algebra",
"parameters": {
"S": { },
"M": { },
"L": { },
"paper": { }
},
"init": {
"func_name": "initialize",
"input_args": [],
"output_args": ["image", "output"]
},
"input_args": ["image", "output"],
"array_args": ["image", "output"],
"output_args": ["output"]
}
}
26 changes: 26 additions & 0 deletions bench_info/iir_blur.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"benchmark": {
"name": "iir_blur",
"short_name": "iir_blur",
"relative_path": "image_processing/iir_blur",
"module_name": "iir_blur",
"func_name": "iir_blur",
"kind": "microbench",
"domain": "Image Processing",
"dwarf": "dense_linear_algebra",
"parameters": {
"S": { },
"M": { },
"L": { },
"paper": { }
},
"init": {
"func_name": "initialize",
"input_args": [],
"output_args": ["image", "output"]
},
"input_args": ["image", "output"],
"array_args": ["image", "output"],
"output_args": ["output"]
}
}
Binary file added data/image_processing/gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/image_processing/rgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions npbench/benchmarks/image_processing/blur/blur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2021 ETH Zurich and the NPBench authors. All rights reserved.
import numpy as np

from PIL import Image
from pathlib import Path

def initialize():
image_path = Path(__file__).parent.parent.parent.parent.parent / "data/image_processing" / "gray.png"
image = np.array(Image.open(image_path), dtype=np.uint8)
output = np.empty((image.shape[0] - 2, image.shape[1] - 2), dtype=np.uint8)

return image, output
20 changes: 20 additions & 0 deletions npbench/benchmarks/image_processing/blur/blur_dace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import dace as dc

@dc.program
def blur(image: dc.uint8[2560, 1536], output: dc.uint8[2558, 1534]):
imagef = image.astype(np.float32)
blur_x = np.empty((imagef.shape[0], imagef.shape[1] - 2), dtype=np.float32)
blur_y = np.empty(output.shape, dtype=np.float32)

for y in range(blur_x.shape[0]):
for x in range(blur_x.shape[1]):
blur_x[y, x] = (imagef[y, x] + imagef[y, x + 1] + imagef[y, x + 2]) / 3.0

for y in range(blur_y.shape[0]):
for x in range(blur_y.shape[1]):
blur_y[y, x] = (blur_x[y, x] + blur_x[y + 1, x] + blur_x[y + 2, x]) / 3.0

blur_y[blur_y < 0.0] = 0.0
blur_y[blur_y > 255.0] = 255.0
output = blur_y.astype(np.uint8)
Comment thread
lukastruemper marked this conversation as resolved.
Outdated
21 changes: 21 additions & 0 deletions npbench/benchmarks/image_processing/blur/blur_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np

# from PIL import Image

def blur(image, output):
image = image.astype(np.float32)
blur_x = np.empty((image.shape[0], image.shape[1] - 2), dtype=np.float32)
blur_y = np.empty(output.shape, dtype=np.float32)

filter = np.array([1.0, 1.0, 1.0], dtype=np.float32) / 3.0

for y in range(blur_x.shape[0]):
blur_x[y, :] = np.convolve(image[y, :], filter, mode='valid')

for x in range(blur_y.shape[1]):
blur_y[:, x] = np.convolve(blur_x[:, x], filter, mode='valid')

output = np.clip(blur_y, 0.0, 255.0).astype(np.uint8)

# img = Image.fromarray(output)
# img.save("blur_output.png")
12 changes: 12 additions & 0 deletions npbench/benchmarks/image_processing/hist/hist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2021 ETH Zurich and the NPBench authors. All rights reserved.
import numpy as np

from PIL import Image
from pathlib import Path

def initialize():
image_path = Path(__file__).parent.parent.parent.parent.parent / "data/image_processing" / "rgb.png"
image = np.array(Image.open(image_path), dtype=np.uint8)
output = np.empty((image.shape[0], image.shape[1], 3), dtype=np.uint8)

return image, output
59 changes: 59 additions & 0 deletions npbench/benchmarks/image_processing/hist/hist_dace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from mimetypes import init
import numpy as np
import dace

@dace.program
def hist(image: dace.uint8[2560, 1536, 3], output: dace.uint8[2560, 1536, 3]):
imagef = image.astype(np.float32)
gray = 0.299 * imagef[:,:,0] + 0.587 * imagef[:,:,1] + 0.114 * imagef[:,:,2]

Cr = (imagef[:,:,0] - gray) * 0.713 + 128
Cb = (imagef[:,:,2] - gray) * 0.564 + 128

gray[gray < 0] = 0
gray[gray > 255] = 255
binned_gray = gray.astype(np.uint8)

# histogram
hist = np.zeros((256,), dtype=np.uint32)
for y, x in dace.map[0:2560, 0:1536]:
with dace.tasklet:
p << binned_gray[y, x]
out >> hist(1, lambda x, y: x + y)[:]

out[p] = 1

npixels = gray.shape[0] * gray.shape[1]
density = np.ndarray((256,), dtype=np.float32)
density[:] = hist / npixels

# cumsum
cdf = np.ndarray((256,), dtype=np.float32)
sum: dace.float32 = 0.0
for i in range(256):
sum = sum + density[i]
cdf[i] = sum

eq = np.ndarray((2560, 1536), dtype=np.float32)
for y, x in dace.map[0:2560, 0:1536]:
eq[y, x] = cdf[binned_gray[y, x]]

eq = eq * 255.0
eq[eq < 0] = 0
eq[eq > 255] = 255

red = eq + (Cr - 128.0) * 1.4
red[red < 0.0] = 0.0
red[red > 255.0] = 255.0

green = eq - 0.343 * (Cb - 128.0) - 0.711 * (Cr - 128.0)
green[green < 0.0] = 0.0
green[green > 255.0] = 255.0

blue = eq + 1.765 * (Cb - 128.0)
blue[blue < 0.0] = 0.0
blue[blue > 255.0] = 255.0

output[:,:,0] = red.astype(np.uint8)
output[:,:,1] = green.astype(np.uint8)
output[:,:,2] = blue.astype(np.uint8)
41 changes: 41 additions & 0 deletions npbench/benchmarks/image_processing/hist/hist_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import numpy as np

# from PIL import Image

def hist(image, output):
image = image.astype(np.float32)
gray = 0.299 * image[:,:,0] + 0.587 * image[:,:,1] + 0.114 * image[:,:,2]

Cr = (image[:,:,0] - gray) * 0.713 + 128
Cb = (image[:,:,2] - gray) * 0.564 + 128

binned_gray = np.clip(gray, 0, 255).astype(np.uint8)

# [0, ..., 256] where 256 is the rightmost edge
hist, _ = np.histogram(binned_gray, bins=np.arange(257))

npixels = gray.shape[0] * gray.shape[1]
density = np.ndarray((256,), dtype=np.float32)
density[:] = hist / npixels

cdf = np.cumsum(density)

eq = cdf[binned_gray]
eq = eq * 255.0
eq = np.clip(eq, 0, 255)

red = eq + (Cr - 128.0) * 1.4
red = np.clip(red, 0, 255)

green = eq - 0.343 * (Cb - 128.0) - 0.711 * (Cr - 128.0)
green = np.clip(green, 0, 255)

blue = eq + 1.765 * (Cb - 128.0)
blue = np.clip(blue, 0, 255)

output[:,:,0] = red.astype(np.uint8)
output[:,:,1] = green.astype(np.uint8)
output[:,:,2] = blue.astype(np.uint8)

# img = Image.fromarray(output)
# img.save("hist_output.png")
12 changes: 12 additions & 0 deletions npbench/benchmarks/image_processing/iir_blur/iir_blur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2021 ETH Zurich and the NPBench authors. All rights reserved.
import numpy as np

from PIL import Image
from pathlib import Path

def initialize():
image_path = Path(__file__).parent.parent.parent.parent.parent / "data/image_processing" / "rgb.png"
image = np.array(Image.open(image_path), dtype=np.uint8)
output = np.empty_like(image)

return image, output
30 changes: 30 additions & 0 deletions npbench/benchmarks/image_processing/iir_blur/iir_blur_dace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
import dace

def blur_cols_transpose(img, alpha: dace.float32):
res = np.empty(img.shape, dtype=np.float32)
res[0, :, :] = img[0, :, :]

for y in range(1, res.shape[0]):
res[y, :, :] = (1.0 - alpha) * res[y - 1, :, :] + alpha * img[y, :, :]

for y in range(res.shape[0] - 2, 0):
res[y, :, :] = (1.0 - alpha) * res[y + 1, :, :] + alpha * res[y, :, :]

res_T = np.empty((img.shape[1], img.shape[0], 3), dtype=np.float32)
for y, x in dace.map[0:img.shape[0],0:img.shape[1]]:
res_T[x, y] = res[y, x]

return res_T

@dace.program
def iir_blur(image: dace.uint8[2560, 1536, 3], output: dace.uint8[2560, 1536, 3]):
imagef = image.astype(np.float32)
alpha = 0.1

blur_y = blur_cols_transpose(imagef, alpha)
blur = blur_cols_transpose(blur_y, alpha)

blur[blur < 0] = 0
blur[blur > 255] = 255
output = blur.astype(np.uint8)
28 changes: 28 additions & 0 deletions npbench/benchmarks/image_processing/iir_blur/iir_blur_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np

# from PIL import Image

def blur_cols_transpose(img, alpha):
blur = np.empty(img.shape, dtype=np.float32)
blur[0, :, :] = img[0, :, :]

for y in range(1, blur.shape[0]):
blur[y, :, :] = (1.0 - alpha) * blur[y - 1, :, :] + alpha * img[y, :, :]

for y in range(blur.shape[0] - 2, 0):
blur[y, :, :] = (1.0 - alpha) * blur[y + 1, :, :] + alpha * blur[y, :, :]

blur_T = np.transpose(blur, axes=(1, 0, 2))
return blur_T

def iir_blur(image, output):
image = image.astype(np.float32)
alpha = 0.1

blur_y = blur_cols_transpose(image, alpha)
blur = blur_cols_transpose(blur_y, alpha)

output = np.clip(blur, 0, 255).astype(np.uint8)

# img = Image.fromarray(output)
# img.save("iir_blur_output.png")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ numpy
pandas
pygount
scipy
Pillow
26 changes: 26 additions & 0 deletions scripts/image_difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import argparse
import numpy as np
from PIL import Image

parser = argparse.ArgumentParser(description='Provide two images.')
parser.add_argument('imageA', help='')
parser.add_argument('imageB', help='')

args = parser.parse_args()

image_a = np.array(Image.open(args.imageA)).astype(np.float32)
image_b = np.array(Image.open(args.imageB)).astype(np.float32)

assert image_a.shape == image_b.shape

diff_image = np.abs(image_b - image_a)
print(diff_image)
diff = diff_image[diff_image > 0]

if diff.shape[0] > 0:
print("Max abs deviation: ", diff.max())

img = Image.fromarray(diff_image.astype(np.uint8))
img.save("difference.png")

assert (diff <= 5).all()