是否可以将此numpy函数转换为tensorflow?

问题描述 投票:0回答:1

我有一个使用[32,32,3]张量并输出[256,256,3]张量的函数。

具体来说,该函数将较小的数组解释为.svg文件,然后使用this algorithm将其“渲染”为256x256数组作为画布。

关于为什么要这样做的解释,请参见This question

该函数的行为完全符合预期,直到我尝试将其包括在GAN的训练循环中为止。我看到的当前错误是:

NotImplementedError: Cannot convert a symbolic Tensor (mul:0) to a numpy array.

关于类似错误的许多其他答案似乎归结为“您需要使用张量流而不是numpy重新编写函数”

这是使用numpy的工作代码-是可能

重新编写它以专门使用tensorflow函数吗?
def convert_to_bitmap(input_tensor, target, j):
    #implied conversion to nparray - the tensorflow docs seem to indicate this is okay, but the error is thrown here when training
    array = input_tensor
    outputArray = target
    output = target

    for i in range(32):
        col = float(array[i,0,j])
        if ((float(array[i,0,0]))+(float(array[i,0,1]))+(float(array[i,0,2]))/3)< 0:
            continue  

        #slice only the red channel from the i line, multiply by 255
        red_array = array[i,:,0]*255

        #slice only the green channel, multiply by 255
        green_array = array[i,:,1]*255

        #combine and flatten them
        combined_array = np.dstack((red_array, green_array)).flatten()

        #remove the first two and last two indices of the combined array
        index = [0,1,62,63]
        clipped_array = np.delete(combined_array,index)

        #filter array to remove values less than 0
        filtered = clipped_array > 0
        filtered_array = clipped_array[filtered]

        #check array has an even number of values, delete the last index if it doesn't
        if len(filtered_array) % 2 == 0: 
            pass
        else:
            filtered_array = np.delete(filtered_array,-1)

        #convert into a set of tuples
        l = filtered_array.tolist()
        t = list(zip(l, l[1:] + l[:1]))

        if not t:
            continue

        output = fill_polygon(t, outputArray, col)

    return(output)

“填充多边形”功能是从“ mahotas”库中复制的:

def fill_polygon(polygon, canvas, color):
if not len(polygon):
    return

min_y = min(int(y) for y,x in polygon)
max_y = max(int(y) for y,x in polygon)
polygon = [(float(y),float(x)) for y,x in polygon]

if max_y < canvas.shape[0]:
    max_y += 1 

for y in range(min_y, max_y):
    nodes = []
    j = -1
    for i,p in enumerate(polygon):
        pj = polygon[j]
        if p[0] < y and pj[0] >= y or pj[0] < y and p[0] >= y:
            dy = pj[0] - p[0]
            if dy:
                nodes.append( (p[1] + (y-p[0])/(pj[0]-p[0])*(pj[1]-p[1])) )
            elif p[0] == y:
                nodes.append(p[1])

        j = i

    nodes.sort()

    for n,nn in zip(nodes[::2],nodes[1::2]):
        nn += 1
        canvas[y, int(n):int(nn)] = color

return(canvas)

注意:我不是想让别人为我改变整个事情!有些功能很明显(用tf.stack代替np.dstack),但是有些我什至不知道如何启动,例如上面fill_polygon函数的最后几行。

我有一个采用[32,32,3]张量并输出[256,256,3]张量的函数。具体来说,该函数将较小的数组解释为一个.svg文件,然后将其“渲染”到256x256 ...

python numpy tensorflow implicit-conversion
1个回答
0
投票

是的,您实际上可以执行此操作,实际上可以使用一个名为tf.pyfunc的python函数。它是一个python包装器,但是与普通的tensorflow相比非常慢。但是,例如tensorflow和Cuda是如此之快,因为它们使用的是vectorization之类的东西,这意味着您可以重写很多,实际上很多循环都非常快速,涉及到数学张量运算。

© www.soinside.com 2019 - 2024. All rights reserved.