我有一个特征图和一些坐标
我想做类似下面的事情
通过坐标
从特征图裁剪数据
然后执行argmax
,concat
(或stack
?)
我怎么能在Tensorflow中做到这一点?
# num_coor is Not fixed, but fixed is ok!
# it defined before run
num_coor = 4
feature_map = np.transpose(np.reshape(np.arange(100*100*3), (3, 100, 100)), [1, 2, 0])
x_coor = np.array([[0, 20, 40, 60],
[15, 35, 55, 75]]) # shape(?, num_coor)
y_coor = np.array([[0, 20, 40, 60],
[15, 35, 55, 75]]) # shape(?, num_coor)
crop = []
for batch in xrange(x_coor.shape[0]):
temp1 = []
for i in xrange(num_coor - 1):
temp2 = []
for j in xrange(num_coor - 1):
x1 = x_coor[batch, j]
y1 = y_coor[batch, i]
x2 = x_coor[batch, j + 1]
y2 = y_coor[batch, i + 1]
# slice by coordinate and len(slice_map) is depth
slice_map = [feature_map[x1:x2, y1:y2, c] for c in xrange(feature_map.shape[2])]
resmax = [np.amax(slice_map[c]) for c in xrange(len(slice_map))]
resmax = np.reshape(np.array(resmax), (1, 1, feature_map.shape[2]))
temp2.append(resmax)
temp1.append(np.vstack(temp2))
temp_arr = np.concatenate(temp1, axis=1)
crop.append(np.array(temp_arr))
crop = np.array(crop) # (2, 3, 3, 3)
x,y中的四个坐标可构成9个bin
我想在每个箱子和每个通道中做最大池
这样我就可以获得池结果,在这种情况下形状为(2, 3, 3, 3) # (2, h, w, c)
我想我可以用tf.while_loop
来做这件事
但我不知道如何访问外部张量并返回结果
我做了一个小测试,似乎没有访问外部张量
i = tf.constant(0)
first_coor = x_coor[i, :]
while_cond = lambda i, _: tf.less(i, 5)
def body(i, the_coor):
# print every loop
i = i + 1
i = tf.Print(i, [i], 'i is :')
i = i + 0
# Never print!!
coor = x_coor
coor = tf.Print(coor, [coor], 'coor is :')
coor = coor + 0
# print every loop
the_coor = tf.Print(the_coor, [the_coor], 'the_coor is :')
the_coor = the_coor + 0
return i, the_coor
r = tf.while_loop(while_cond, body, [i, first_coor])
答案 0 :(得分:0)
我认为你可以在这种情况下使用常规for循环(注意lambda如何能够访问外部张量,在这种情况下feature_map
):
batch_feature_map_max_fn = lambda xy : tf.reduce_max(feature_map[xy[0]:xy[1], xy[2]:xy[3], -1])
crop = []
for j in range(num_coor-1):
x1 = x_coor[:, j]
x2 = x_coor[:, j+1]
y1 = y_coor[:, j]
y2 = y_coor[:, j+1]
temp = tf.map_fn(fn, [x1,x2,y1,y2], dtype=tf.float32)
crop.append(temp)
crop = tf.pack(crop, axis=1)
对于tf.while_loop
,我相信您将能够访问您在loop_vars
参数中传递的所有张量。您可以在不做任何更改的情况下返回额外的张量。我不确定tf.while_loop
在这种情况下是否真的适合。