【发布时间】:2020-12-16 07:50:47
【问题描述】:
我试图让这个函数接受一个单元素张量。
def classi(i):
out = np.zeros((1, 49), np.uint8)
for j in range(len(classcount)):
i -= classcount[j]
if i<0:
break
out[0][j] += 1
return tf.convert_to_tensor(out)
#basically the error seems to be related to the if i<0 line
这个函数将在这里被另一个函数调用
def formatcars(elem):
return (elem['image'], tf.function(classi(elem['label'])))
#note elem['label'] is just a single element tensor of integer.
然后映射到汽车数据集。
dataset.map(formatcars)
我不断收到错误:
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
我尝试过启用急切执行。我试过使用 tf.function,使用 tf.cond、tf.greater、.tonumpy()、.eval() 等都无济于事。它不断给出同样的错误。我现在没有主意了。
classcount列表定义如下:
classcount = [ 1, 6, 4, 14, 13, 6, 2, 4, 3, 22, 6, 1, 15, 1, 2, 4, 1,
12, 5, 1, 2, 4, 11, 2, 1, 1, 5, 4, 2, 1, 1, 1, 1, 1,
6, 1, 4, 1, 1, 1, 3, 1, 2, 4, 1, 4, 3, 3, 1]
它只是一个从
创建的整数列表import scipy
import tensorflow_datasets as tfds
dataset = tfds.load('cars196', split = 'train')
mat = scipy.io.loadmat('cars_annos.mat')
classcount = []
starti = 0
curmake = ''
for i in range(len(mat['class_names'][0])):
print(mat['class_names'][0][i][0].split(' ', 1)[0])
if mat['class_names'][0][i][0].split(' ', 1)[0] != curmake:
print(i-starti)
if i-starti != 0:
classcount.append(i-starti)
starti = i
curmake = mat['class_names'][0][i][0].split(' ', 1)[0]
classcount.append(1)
cars_annos.mat 来自http://imagenet.stanford.edu/internal/car196/cars_annos.mat
【问题讨论】:
-
欢迎来到stackoverflow。你能确定你提供的代码是minimal reproducible example吗?
-
谢谢。我添加了用于生成类计数的代码。虽然如果你只是复制 [1, 6, 4, 14, ....] 并将其分配为 classcount 那么它应该是完全相同的列表。
标签: python tensorflow tensorflow-datasets