Sha*_*war 2 python tensorflow tensorflow-datasets
我已经为多输入 nn 编写了一个生成器,但是在使用 tf.data.Dataset.from_generator() 函数时出现错误,所有数据都在 numpy 中,其中:输入 1 的形状为(16,100,223,3),输入 2 的形状shape(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
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
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.
Run Code Online (Sandbox Code Playgroud)
同样的错误重复多次,请帮忙!!!
小智 5
我有一个类似的问题,我相信我通过指定生成器生成的形状解决了这个问题。对我来说,输出是 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,))
#
)
Run Code Online (Sandbox Code Playgroud)
我尝试运行您的代码,但 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)
)
Run Code Online (Sandbox Code Playgroud)
这是我制作的一个玩具数据集的链接,目的是为了理清这个混乱的局面
https://colab.research.google.com/drive/17pu6jJYLGP-I1nJnzigOx2YOh3Ih4cvG?usp=sharing
| 归档时间: |
|
| 查看次数: |
4524 次 |
| 最近记录: |