Python 如何将numpy代码转换为tensorflow?
我正在尝试将numpy代码转换为tensorflow图形格式。但在某个地方,我缺少对维度的理解 以下是numpy代码:Python 如何将numpy代码转换为tensorflow?,python,numpy,tensorflow,Python,Numpy,Tensorflow,我正在尝试将numpy代码转换为tensorflow图形格式。但在某个地方,我缺少对维度的理解 以下是numpy代码: def delta_to_boxes3d(deltas, anchors, coordinate='lidar'): # Input: # deltas: (N, w, l, 14)(200,240,14) # feature_map_shape: (w, l) # anchors: (w, l, 2, 7)(200,240,2,7)
def delta_to_boxes3d(deltas, anchors, coordinate='lidar'):
# Input:
# deltas: (N, w, l, 14)(200,240,14)
# feature_map_shape: (w, l)
# anchors: (w, l, 2, 7)(200,240,2,7)
# Ouput:
# boxes3d: (N, w*l*2, 7)
#anchros shape 9200,240,2,7)
anchors_reshaped = anchors.reshape(-1, 7) #(96000,7)
deltas = deltas.reshape(-1, 7) #(96000,7)
anchors_d = np.sqrt(anchors_reshaped[:, 4]**2 + anchors_reshaped[:, 5]**2)
boxes3d = np.zeros_like(deltas)
boxes3d[..., [0, 1]] = deltas[..., [0, 1]] * \
anchors_d[:, np.newaxis] + anchors_reshaped[..., [0, 1]] #in this line I have the problem
boxes3d[..., [2]] = deltas[..., [2]] * \
1.73 + anchors_reshaped[..., [2]] #ANCHOR_H = 1.73
boxes3d[..., [3, 4, 5]] = np.exp(
deltas[..., [3, 4, 5]]) * anchors_reshaped[..., [3, 4, 5]]
boxes3d[..., 6] = deltas[..., 6] + anchors_reshaped[..., 6]
return boxes3d
以下是我一直在尝试的代码:
def delta_boxes3d():
anchors = tf.placeholder(tf.float32,shape=[None,None,2,7],name="anchor") #check the anchor type later
anchors_reshaped = tf.reshape(anchors,shape=[96000,7])
delta = tf.placeholder(tf.float32,shape=[None,None,14],name="delta")
anchors_d = tf.sqrt(tf.add(tf.pow(anchors_reshaped[:,4],2),tf.pow(anchors_reshaped[:,5],2))) #96000
deltas = tf.reshape(delta,[96000,7])
x_shape = tf.shape(deltas)
boxes3d_ = tf.multiply(deltas[:,0:2],tf.add(tf.expand_dims(anchors_d,-1),anchors_reshaped[:,0:2]))
boxes3d = tf.ones(x_shape[:-1]) + boxes3d_
elta_ = np.random.rand(200,240,14)
anchor_ = np.random.rand(200,240,2,7)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
result = sess.run(boxes3d1,feed_dict={anchors:anchor_,delta:delta_}) #(96000,7) #need to get boxes3d
print(result.shape)
我得到以下错误:
ValueError: Dimensions must be equal, but are 96000 and 2 for '{{node add_2}} = AddV2[T=DT_FLOAT](ones, Mul)' with input shapes: [96000], [96000,2].
有人能帮我吗
提前感谢错误来自行
boxes3d=tf.ones(x_形状[:-1])+boxes3d
。
您正在尝试添加形状(96000,)和(96000,2),如果不展开DIM,则无法添加这些形状。如果要添加标量,可以执行boxes3d=1+boxes3d
。
在上面的示例中,您希望先执行标量乘法,然后执行加法
请注意,在突出显示的行中的NumPy示例中,首先执行乘法,然后执行加法。在TensorFlow中,您使用了另一种方式(可能是错误的)
我将您的NumPy示例重写为Tensorflow 2,以便两个函数返回相同的输出
def delta_boxes3d(deltas, anchors):
deltas = tf.constant(deltas)
anchors = tf.constant(anchors)
anchors_reshaped = tf.reshape(anchors, shape=[96000, 7])
anchors_d = tf.sqrt(tf.add(tf.pow(anchors_reshaped[:, 4], 2), tf.pow(anchors_reshaped[:, 5], 2))) # 96000
deltas = tf.reshape(deltas, [96000, 7])
boxes3d_01 = tf.add(tf.multiply(deltas[:, 0:2], tf.expand_dims(anchors_d, -1)), anchors_reshaped[:, 0:2])
boxes3d_2 = deltas[..., 2:3] * 1.73 + anchors_reshaped[..., 2:3]
boxes3d_345 = tf.exp(deltas[..., 3:6]) * anchors_reshaped[..., 3:6]
boxes3d_6 = deltas[..., 6:7] + anchors_reshaped[..., 6:7]
boxes3d = tf.concat([boxes3d_01, boxes3d_2, boxes3d_345, boxes3d_6], axis=-1)
return boxes3d
deltas = np.random.rand(200, 240, 14)
anchors = np.random.rand(200, 240, 2, 7)
print(delta_to_boxes3d(deltas, anchors))
print(delta_boxes3d(deltas, anchors))
您可以注意到,我首先创建了较小的数组,然后连接了它们。这是因为Tensorflow不允许我修改张量
请注意
增量[…,2]
和增量[…,2:3]
之间的区别。第二个并没有减少最后一个维度。它们分别返回形状(96000,)和(96000,1)。尝试用delta[…,0:2]替换delta[:,[0,1]],并对锚进行重新整形。@LadislavOndris我根据您的建议对其进行了更改,以克服更新错误。我知道是什么错误,但我不能找出什么是修复?我们不能添加不同维度的张量谢谢你,你救了我一整天