首页 > 解决方案 > 在张量流中对数组进行过采样和下采样

问题描述

我有一个数组(比如 2x2),我需要对其进行过采样以提高分辨率。使用过采样数组,我将执行一些操作(例如傅立叶变换),然后我需要返回到原始分辨率(下采样):

在此处输入图像描述

我可以使用标准库很容易地做到这一点,但我现在正试图在 tensorflow 上完成它,以便我可以在 GPU 上为大型数组运行它。

中央处理器:

过采样:

我只是过去常常numpy.kron去,例如从[[1,2],[3,4]]

[[1., 1., 2., 2.],
[1., 1., 2., 2.],
[3., 3., 4., 4.],
[3., 3., 4., 4.]]

经过 :

import numpy as np
arr = np.array([[1,2],[3,4]])
iden = np.ones((2,2))
c = np.kron(arr, iden)

下采样:

回到过去,我使用了来自astropy 的名为的好工具block_reduce,它

通过对本地块应用函数来对数据数组进行下采样

c后面回到[[1,2],[3,4]]我做的:

from astropy.nddata.utils import block_reduce
block_reduce(c,2, func=np.mean)
# array([[ 1.,  2.],
       [3., 4.]])

问题:我如何做同样的事情,但在tensorflow

想法:

GPU 和张量流

过采样:

我可以从以下位置使用LinearOperatorKroneckerlinalg

import tensorflow as tf
from tensorflow.linalg import LinearOperatorFullMatrix, LinearOperatorKronecker
operator_1 = LinearOperatorFullMatrix([[1., 2.], [3., 4.]])
operator_2 = LinearOperatorFullMatrix([[1., 1.], [1., 1.]])
operator = LinearOperatorKronecker([operator_1, operator_2])
operator.to_dense()
# <tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[1., 1., 2., 2.],
       [1., 1., 2., 2.],
       [3., 3., 4., 4.],
       [3., 3., 4., 4.]], dtype=float32)>

但我什至不相信这是最好的方法。不知道为什么从张量到 LinearOperators 有额外的抽象级别。

下采样

不知道。我正在看,tf.math.segment_sum但还没有完全理解它。

有什么想法/建议吗?

标签: pythontensorflow

解决方案


推荐阅读