python - 是否可以以字节为单位转换特征图?
问题描述
蟒蛇 3.6
团结 2019
我正在尝试找到将特征图数据传输到统一的最佳解决方案。
我想以字节为单位发送数据。但是我没有找到如何将其编码为字节,然后统一解码。
基本上是一个 4d 数组,需要根据我的理解将其转换为字节
蟒蛇片
for fmap in feature_maps:
bytes = []
bytes.append(fmap)
arrays_of_features.append(bytes)
data = np.array(arrays_of_features, dtype=float) # this is not working because of the fact is multidimensional array apparently.
print(fmap)
c.sendall(data.tobytes())
统一块:byte[] bytes = new byte[4000]; int idxUsedBytes = client.Receive(bytes);
floatsReceived = new float[idxUsedBytes / 4];
Buffer.BlockCopy(bytes, 0, floatsReceived, 0, idxUsedBytes);
print(floatsReceived[0]);
启示:如何让 Unity 中的 c# 与 Python 通信
特征图看起来像这样:
[[[[ 0. 0. 0. ... 0. 12.569366
0. ]
[ 0. 0. 0. ... 0. 4.421044
0. ]
[ 0. 0. 0. ... 0. 0.19193476
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
[[ 0. 0. 0. ... 0. 12.910363
0. ]
[ 0. 0. 0. ... 0. 3.987629
0. ]
[ 0. 0. 0. ... 0. 1.6041028
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]]
...
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
49.52598 ]
[ 0. 0. 0. ... 0. 0.
10.050183 ]
[ 0. 0. 0. ... 0. 9.6911745
0. ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
...
[ 0. 0. 0. ... 0. 0.
29.483086 ]
[ 0. 0. 0. ... 0. 0.
24.422682 ]
[ 0. 0. 2.253025 ... 0. 0.
15.935954 ]]
[[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 0. ... 0. 0.
0. ]
[ 0. 0. 18.458588 ... 15.824303 0.
0. ]
...
[ 0. 0. 0. ... 25.163502 56.87079
42.9939 ]
[ 0. 0. 11.397255 ... 36.644962 17.04247
44.108196 ]
[ 0. 0. 33.134758 ... 30.220499 8.817273
36.6427 ]]]]
解决方案
您的问题非常不清楚,我相信您对 numpy 的工作原理感到困惑。如果是这样,让我们解释一些事情。来自 numpy 的数组只不过是内存中的一串字节。特别是,当为您显示这些字节时,它们由 dtype 解释。dtype 不用于存储底层数据,而仅用于显示它。因此,更改 dtype 只会更改数据对您的外观,不会更改数据本身。尺寸也是一样。数据的维度只会改变数据的显示和访问方式,python 实际上并不会移动数据或改变数据本身。例如,
import numpy as np
x = np.array([[1,2,3],[4,5,6]],dtype='int64') #48 bytes, each int takes up 8 bytes.
print(x)
x.dtype = 'int32'
print(x)
x.dtype = 'float'
print(x)
x.dtype = 'int16'
print(x)
请注意,我们可以更改 dtype 并且绝对零计算由数组完成(因为基础数据已经是一个字节数组)。同样,我们可以改变形状,也可以完成绝对零计算。
x.shape = (2,2,6)
print(x)
shape 和 dtype 与内存中存储的数据无关。希望这可以清楚地说明我们现在如何将数组作为字节处理。
x = np.array([[1,2,3],[4,5,6]],dtype='int64')
print(x)
y = x.tobytes()
# Send y somewhere. Save to a file. Etc.
z = np.frombuffer(y)
z.dtype = 'int64'
z.shape = (2,3)
print(z)
推荐阅读
- ios - 从 Firestore 文档中获取数据并附加到数组
- javascript - 是否有一个 ES6 函数会返回一个包含属性更改的对象?
- antlr - 防火墙配置解析器无关输入
- xamarin - Xamarin 中的辅助触摸按钮
- c# - 用循环填充数组
- c# - 如何从 C# WebApi 禁用对 MarkLogic 服务器的预检请求
- javascript - 如何在javascript中为值添加单引号
- oracle - 将 SQL 语句重写为 PL/SQL
- elasticsearch - 在不知道 _id 的情况下更新大量文档
- python - 'GeoDataFrame' object has no attribute 'assign_centroids' in CLIMADA when using admin1