python - 在张量流中对数组进行过采样和下采样
问题描述
我有一个数组(比如 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
但还没有完全理解它。
有什么想法/建议吗?
解决方案
推荐阅读
- c# - 将 Azure 托管标识用于未授权新 SDK 的应用服务
- asp.net-core - 在 Azure DevOps/Local 的 .netCore 项目中如何/在何处存储敏感信息
- python - 如何使用 gstreamer 的绑定设置 Python 虚拟环境?
- plotly - Plotly:跨从不同数据框构建的绘图的链接刷亮/突出显示
- php - 使用 Guzzle 时从 Do 推送请求
- python - 在python中将函数传递给cProfile的正确方法是什么?
- javascript - 仅当搜索使用 JQuery 找到匹配项时才显示手风琴
- unity3d - 我如何在通常支持统一文本的统一脚本中实现 TMP
- mesh - CGAL detect_features() 创建不受保护的特征
- java - 如何在非活动中从房间数据库中获取数据。(在通知的 JobIntentService 中)