如何对float32类型的张量应用“或”运算?

问题描述

我想合并两个稀疏的Bird-Eye-View特征图。

它们在大多数像素上的值为0。并且它们是对齐的,因此对应的像素代表相同的位置,这使得合并合理。

  • 以两个张量为例:
net1=[
[0,0.2,0],[0,0.1,0]]

net2_aligned=[
[0.3,0.4,0]]
  • 仅当net1的像素值为0时,才会被net2_aligned中相应的像素值替换。

    • (如果对两个张量中都具有值的像素使用max / mean,也可以接受)
  • 即我们假设在float32的“或运算”之后得到此结果:

net_result=[
[0.3,0]]

张量流中是否有这种方法?我在想是否可以通过特殊的1x1卷积来完成。

解决方法

您可以:

import tensorflow as tf

net1 = tf.convert_to_tensor([[0,0.2,0],[0,0.1,0]])
net2_aligned = tf.convert_to_tensor([[0.3,0.4,0]])

bools = (net1==0) # Gives a boolean tensor
bools = tf.cast(bools,tf.float32) # Converts the boolean tensor to float32 dtype,so you can multiply it with net2_aligned 

net_result = net1 + bools*net2_aligned # This is possible thanks to the particularity of your problem,as each time you want to replace a net1 value by one of net2_aligned,it is equal to 0. Thus the sum.

输出:

<tf.Tensor: shape=(3,3),dtype=float32,numpy=
array([[0.3,0. ],[0.,0.,0. ]],dtype=float32)>

如果您想替换net1的每个等于0.2的值,则可以这样做:

bools = tf.cast(net1==0.2,tf.float32)
net_result = net1*(1-bools) + bools*net2_aligned
,

您可以为此使用numpy:

import numpy as np

net1=np.array([
[0,0]])

net2_aligned=np.array([
[0.3,0]])

result = np.copy(net1)
mask = net1 == 0

result[mask] = net2_aligned[mask]
print(result)