-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
72 lines (56 loc) · 1.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
import numpy as np
from scipy import misc
import time
import cuconv as cu
import cpuconv as cp
from scipy import signal as sg
# Get numpy array from image
def from_img(fname):
return np.asarray(misc.imread(fname, flatten=True), dtype=np.float32)
# Write numpy array to image
def to_img(m):
return np.clip(np.absolute(m), 0, 255)
# Create a box blue kernel of radius r
def k_boxblur(r):
return np.ones([r+(0 if r % 2 is 1 else 1), r+(0 if r % 2 is 1 else 1)])
# Normalize kernel matrix
def nrm(m):
m = np.array(m)
return m/np.sum(np.abs(m))
# CREATE KERNELS
k_sv = [[-1., 0., 1.], [-2., 0., 2.], [-1., 0., 1.]]
k_sh = [[-1., -2., -1.], [0., 0., 0.], [1., 2., 1.]]
k_b5 = k_boxblur(5)
# LOAD IMAGE
a = from_img('img.png').astype(np.float32)
# GPU
start = time.time()
c = cu.convolve(a, k_sv)
misc.imsave('results/g_result_sv.png', to_img(c))
c = cu.convolve(a, k_sh)
misc.imsave('results/g_result_sh.png', to_img(c))
c = cu.convolve(a, k_b5)
misc.imsave('results/g_result_b5.png', to_img(c))
end = time.time()
print("GPU time: %.5f s" % (end-start))
# SCIPY
start = time.time()
c = sg.convolve(a, nrm(k_sv))
misc.imsave('results/s_result_sv.png', to_img(c))
c = sg.convolve(a, nrm(k_sh))
misc.imsave('results/s_result_sh.png', to_img(c))
c = sg.convolve(a, nrm(k_b5))
misc.imsave('results/s_result_b5.png', to_img(c))
end = time.time()
print("Scipy time: %.5f s" % (end-start))
# CPU
start = time.time()
c = cp.convolve(a, k_sv)
misc.imsave('results/c_result_sv.png', to_img(c))
c = cp.convolve(a, k_sh)
misc.imsave('results/c_result_sh.png', to_img(c))
c = cp.convolve(a, k_b5)
misc.imsave('results/c_result_b5.png', to_img(c))
end = time.time()
print("CPU time: %.5f s" % (end-start))