Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding blur, hist, iir_blur benchmarks for numpy & dace #7

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion npbench/benchmarks/image_processing/blur/blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

def initialize():
image_path = Path(__file__).parent.parent / "images" / "gray.png"
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)

Expand Down
2 changes: 0 additions & 2 deletions npbench/benchmarks/image_processing/blur/blur_dace.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ def blur(image: dc.uint8[2560, 1536], output: dc.uint8[2558, 1534]):
blur_y[blur_y < 0.0] = 0.0
blur_y[blur_y > 255.0] = 255.0
output = blur_y.astype(np.uint8)
lukastruemper marked this conversation as resolved.
Show resolved Hide resolved

return output
2 changes: 0 additions & 2 deletions npbench/benchmarks/image_processing/blur/blur_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ def blur(image, output):

# img = Image.fromarray(output)
# img.save("blur_output.png")

return output
2 changes: 1 addition & 1 deletion npbench/benchmarks/image_processing/hist/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

def initialize():
image_path = Path(__file__).parent.parent / "images" / "rgb.png"
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)

Expand Down
41 changes: 21 additions & 20 deletions npbench/benchmarks/image_processing/hist/hist_dace.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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 = np.empty((imagef.shape[0], imagef.shape[1]), dtype=np.float32)
gray[:,:] = 0.299 * imagef[:,:,0] + 0.587 * imagef[:,:,1] + 0.114 * imagef[:,:,2]

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

Expand All @@ -16,43 +15,45 @@ def hist(image: dace.uint8[2560, 1536, 3], output: dace.uint8[2560, 1536, 3]):
binned_gray = gray.astype(np.uint8)

# histogram
hist = np.ndarray((256,), dtype=np.float32)
for y, x in dace.map[0:binned_gray.shape[0], 0:binned_gray.shape[1]]:
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 = hist / npixels
density = np.ndarray((256,), dtype=np.float32)
density[:] = hist / npixels

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

eq = cdf[binned_gray]
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) * 1.4
red[red < 0] = 0
red[red > 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.711 * (Cr - 128)
green[green < 0] = 0
green[green > 255] = 255
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)
blue[blue < 0] = 0
blue[blue > 255] = 255
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)

return output
30 changes: 20 additions & 10 deletions npbench/benchmarks/image_processing/hist/hist_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,38 @@

def hist(image, output):
image = image.astype(np.float32)

gray = np.empty(image.shape[:2], dtype=np.float32)
gray[:,:] = 0.299 * image[:,:,0] + 0.587 * image[:,:,1] + 0.114 * image[:,:,2]
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, bins = np.histogram(binned_gray, bins=np.arange(257), density=True)
cdf = np.cumsum(hist)
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)

output[:,:,0] = np.clip(eq + (Cr - 128) * 1.4, 0, 255).astype(np.uint8)
output[:,:,1] = np.clip(eq - 0.343 * (Cb - 128) - 0.711 * (Cr - 128), 0, 255).astype(np.uint8)
output[:,:,2] = np.clip(eq + 1.765 * (Cb - 128), 0, 255).astype(np.uint8)
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")

return output
2 changes: 1 addition & 1 deletion npbench/benchmarks/image_processing/iir_blur/iir_blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

def initialize():
image_path = Path(__file__).parent.parent / "images" / "rgb.png"
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)

Expand Down
2 changes: 0 additions & 2 deletions npbench/benchmarks/image_processing/iir_blur/iir_blur_dace.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ def iir_blur(image: dace.uint8[2560, 1536, 3], output: dace.uint8[2560, 1536, 3]
blur[blur < 0] = 0
blur[blur > 255] = 255
output = blur.astype(np.uint8)

return output
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@ def iir_blur(image, output):

# img = Image.fromarray(output)
# img.save("iir_blur_output.png")

return output
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed npbench/benchmarks/image_processing/images/rgba.png
Binary file not shown.