我正在寻找一个gpu加速的n维滑动窗口操作在Python中使用Tensorflow实现。您可以在Torch、Caffe或Theano中发布您的实现,但我将选择Tensorflow实现作为接受的答案。请发布执行2d中值滤波操作的工作代码段(希望在没有代码更改或代码更改最小的情况下,可以应用于n维图像)
以我对张量流的有限知识,我相信两个潜在的模块是^{apply
,reshape
魔法?在
我的失败尝试贴在下面,作为娱乐。请注意,我在2年前发布了一个类似的question,要求实现Theano,现在,大多数人都在使用tf/keras或torch。在
import time
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import tensorflow as tf
from tensorflow.contrib.data.python.ops import sliding
from skimage import img_as_float, data
from scipy.signal import medfilt
imgs = img_as_float(data.camera())
### SCIPY median ###
stime = time.time()
scipysmoothed = medfilt(imgs,(9,9))
etime = time.time()
print('scipy smoothed: {:1.4f} seconds'.format(etime-stime))
### Failed attempt of TF median ###
method = 'Tensorflow'
stime = time.time()
window_func = lambda x: tf.contrib.distributions.percentile(x, 50.0)
# create TensorFlow Dataset object
data = tf.data.Dataset.from_tensor_slices(imgs)
# sliding window - only 1d is allowed?
window = 3
stride = 1
data = data.apply(sliding.sliding_window_batch(window, stride)).map(lambda x: window_func(x))
# create TensorFlow Iterator object
iterator = tf.data.Iterator.from_structure(data.output_types)
next_element = iterator.get_next()
# create initialization ops
init_op = iterator.make_initializer(data)
c=0
smoothed = np.zeros(imgs.shape)
with tf.Session() as sess:
# initialize the iterator on the data
sess.run(init_op)
while True:
try:
elem = sess.run(next_element)
smoothed[c,:]=elem
# obviously WRONG.
c+=1
except tf.errors.OutOfRangeError:
#print("End of dataset.")
break
#print(c)
etime = time.time()
print('tf smoothed: {:1.4f} seconds'.format(etime-stime))
plt.figure(figsize=(20,20))
plt.subplot(131)
plt.imshow(imgs,cmap='gray',interpolation='none')
plt.title('original')
plt.subplot(132)
plt.imshow(smoothed,cmap='gray',interpolation='none')
plt.title('actual smoothed\nwith {}'.format(method))
plt.subplot(133)
plt.imshow(scipysmoothed,cmap='gray',interpolation='none')
_=plt.title('expected smoothed')
一。在
^{pr2}$
建议1:我的尝试如下,因为它只使用},所以该实现只支持2d和3d图像。在
tf.image.extract_image_patches
和{建议2:可以将数据格式化为一个预处理步骤(通过
tf.data.Dataset.map
),但是这也需要很多时间,我还不知道为什么(例如https://gist.github.com/pangyuteng/ca5cb07fe383ebe59b521c832f2e2918)。在建议3:使用卷积块来并行化处理,参见“超列用于对象分割和细粒度定位”https://arxiv.org/abs/1411.5752。在
在
方案1代码:
相关问题 更多 >
编程相关推荐