我在Jupyter Notebook / Python 3.6中计算Cython函数内的Total Variation:
SHA512
计算电视规范p< 1
%%cython --compile-args=-O3 --compile-args=-fopenmp --link-args=-fopenmp
# cython: boundscheck=False
# cython: cdivision=True
# cython: wraparound=False
# cython: profile=False
import numpy as np
cimport numpy as np
cimport cython
from cython.parallel cimport parallel, prange
cdef extern from "stdlib.h":
float abs(float x) nogil
cdef void p_norm(float[:, :, ::1] img, float[:, ::1] output, long p) nogil:
cdef int M = img.shape[0]
cdef int N = img.shape[1]
cdef int i, j
with parallel(num_threads=8):
for i in prange(M, schedule="static"):
for j in range(N):
output[i, j] = (abs(img[i, j, 0]) ** p + abs(img[i, j, 1]) ** p) **(1/p)
def run_norm(a, output, p):
return p_norm(a, output, p)
使Jupyter内核崩溃而没有错误。改变
a = np.ascontiguousarray(np.arange(2*2048**2).reshape((2048, 2048, 2)), dtype=np.float32)
out = np.empty((a.shape[0], a.shape[1]), dtype=np.float32)
run_norm(a, out, 0.5)
让它无限期地运行。
我做错了什么?
答案 0 :(得分:0)
好吧发现它......它是权力中的1 / p分区......替换为1./p做了技巧(浮动除法)而p应该是浮点型。