gpt4 book ai didi

python - tensorflow from_generator() 给出错误 - 'generator` 产生无法转换为预期类型的​​元素

转载 作者:行者123 更新时间:2023-12-03 08:38:31 24 4
gpt4 key购买 nike

我已经为多输入nn编写了一个生成器,但是在使用tf.data.Dataset.from_generator()函数时出现错误,所有数据都在numpy中,其中:输入1的形状为(16,100,223,3),输入2 的形状为(100,223),输入 3 的形状为(16,),标签的形状为(2,)。数据是所有这些组合的数组

我的代码

def data_generator(train_list, batch_size):

i = 0
j = 0
flag = True
while True:
# inputs = []
# outputs = []
if i < len(train_list):
if flag == True:
train_path = os.path.join(training_dir, train_list[i])
data = np.load(train_path, allow_pickle=True)
flag = False

if j >= len(data):
j = 0
i += 1
flag = True
del data

else:
if len(data[j:]) >= batch_size:
input_1 = data[j:(j+batch_size), 0]
input_2 = data[j:(j+batch_size), 1]
input_3 = data[j:(j + batch_size), 2]
outputs= data[j:(j+batch_size), -1]
j += (batch_size)
yield {'Input_Branch-1' : input_1,'Input_Branch-2': input_2, 'Input_Branch-3': input_3}, outputs

elif len(data[j:])< batch_size:
input_1 = data[j:, 0]
input_2 = data[j:, 1]
input_3 = data[j:, 2]
outputs= data[j:, -1]
j = 0
i+= 1
flag = True
del data
yield {'Input_Branch-1': input_1, 'Input_Branch-2': input_2, 'Input_Branch-3': input_3}, outputs

else:
i = 0
del data
flag = True
np.random.shuffle(train_list)


batch_size = 5
dataset = tf.data.Dataset.from_generator(data_generator, args= [train_list, batch_size],
output_types = ({'Input_Branch-1': tf.uint8, 'Input_Branch-2': tf.uint8, 'Input_Branch-3': tf.float32}, tf.float32),)

# for seeing the output of data generator
num = 0
for data, labels in dataset:
print(data.shape, labels.shape)
print(labels)
print()
num = num + 1
if num > 1: break

我收到以下错误

 2020-08-04 17:43:30.653430: W tensorflow/core/framework/op_kernel.cc:1741] Invalid argument: TypeError: `generator` yielded an element that could not be converted to the expected type. The expected type was uint8, but the yielded element was [array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)].
TypeError: only size-1 arrays can be converted to Python scalars


The above exception was the direct cause of the following exception:


Traceback (most recent call last):

File "C:\Users\panwa\.conda\envs\Vision\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 801, in generator_py_func
ret, dtype=dtype.as_numpy_dtype))

File "C:\Users\panwa\.conda\envs\Vision\lib\site-packages\tensorflow\python\ops\script_ops.py", line 203, in _convert
result = np.asarray(value, dtype=dtype, order="C")

File "C:\Users\panwa\.conda\envs\Vision\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)

ValueError: setting an array element with a sequence.

同样的错误重复多次请帮忙!!!

最佳答案

我有一个类似的问题,我相信我通过指定生成器生成的形状解决了这个问题。对我来说,输出是 2 个图像 32x32x3,以及两个标签,所以我使用它作为 from_generator 方法的参数。

dataset = tf.data.Dataset.from_generator
generator,
# this is the part that you care about #
output_signature=(
tf.TensorSpec(shape=(2,32,32,3), dtype=tf.int32),
tf.TensorSpec(shape=(2,1), dtype=tf.int32,))
#
)

我尝试运行您的代码,但 train_list 未定义。我仍然相信这可能会起作用,也许需要进行一两次更改


output_signature = (
# the one is there to clarify that there is one of these objects
tf.TensorSpec(shape=(1,16,100,223,3), dtype=tf.int32)
tf.TensorSpec(shape=(1,100,223), dtype=tf.int32)
tf.TensorSpec(shape=(1,16,) ,dtype=tf.int32)
tf.TensorSpec(shape=(2,1),dtype=tf.int32)
)

这里是一个玩具数据集的链接,我制作的目的是为了弄清楚这个困惑的情况

https://colab.research.google.com/drive/17pu6jJYLGP-I1nJnzigOx2YOh3Ih4cvG?usp=sharing

关于python - tensorflow from_generator() 给出错误 - 'generator` 产生无法转换为预期类型的​​元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63247003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com