将python代码传输到tensorflow,而AutoGraph不起作用

时间:2019-09-19 12:41:28

标签: python tensorflow

一个简单的例子来说明我的代码的功能:

map = [[0,0,1,1,4],
       [0,1,2,1,4],
       [2,2,0,0,3]
       ]
core = [2,2]
map_new = most_down(map,core)
print(map_new)
[[0 1 4]
 [2 0 3]]

地图和核心是int。我用最高计数值对int特征图进行了升采样。例如 map_new[0][0] = 0来自 map[0:2,0:2],因为有三个0和一个1 Python中的工作代码:

import numpy as np
import statistics
def most_down(map, core=(16,16)):
    map = np.array(map)
    width = core[0]
    core0 = width

    height = core[1]
    core1=height
    shape = np.shape(map)

    pro1 = int(shape[0]/width)
    pro2 = int(shape[1]/height)
    if not shape[0]%width==0:
        pro1+=1
    if not shape[1]%height==0:
        pro2+=1
    map_new = np.zeros([pro1,pro2],dtype=np.int)

    for i in range(pro1):
        for j in range(pro2):
            if i<pro1-1:
                w = np.array([i*core0,(i+1)*core0])
            else:
                w = np.array([i * core0, shape[0]])
            if j<pro2-1:
                h = np.array([j*core1,(j+1)*core1])
            else:
                h = np.array([j*core1,shape[1]])

            block = map[w[0]:w[1], h[0]:h[1]]
            block = np.reshape(block,[-1,])

            most_hit =np.argmax(np.bincount(block))
            map_new[i,j]=most_hit
    return map_new

map = [[0,0,1,1,4],
       [0,1,2,1,4],
       [2,2,0,0,3]
       ]
core = [2,2]
map_new = most_down(map,core)
print(map_new)

然后我尝试@autograph.convert(),然后在AutoGraph上出现无休止的错误。我不能全部张贴。

我的问题是

1。。如何使用tf核心代码?我只能完成其中的一部分:

def get_proporttion(map, core=(16,16)):

    width = core[0]
    height = core[1]
    shape = map.shape
    pro1 = tf.to_int32(tf.to_float(shape[0])/tf.to_float(width))
    pro2 = tf.to_int32(tf.to_float(shape[1])/tf.to_float(height))
    pro1=tf.cond(tf.not_equal(shape[0]%width,0),
            lambda :tf.add(pro1,1),
            lambda :pro1)
    pro2=tf.cond(tf.not_equal(shape[1]%height,0),
            lambda :tf.add(pro2,1),
            lambda :pro2)

2 。如何使@autograph.convert()工作?

该函数的明确说明:

def most_down:
#Input1: map tensor tf.int32 shape=[width,height,channels]
#Input2:core tensor tf.int32 shape=[2,2]
#Output:new_map tensor tf.int32 shape will depend on the input.

0 个答案:

没有答案