如何解决无法将一批图像准确输入到模型中
我的模型旨在训练双图像。由于数据集非常庞大,因此我按照建议here使用tf.data.Dataset
方法将其作为批处理进行获取。但是我很难正确输入一批图像进行训练。我没有找到任何可能的解决方案。经过这些修改后:
ds_train = tf.data.Dataset.zip((tr_inputs,tr_labels)).batch(64)
iterator = ds_train.make_one_shot_iterator()
next_batch = iterator.get_next()
result = list()
with tf.Session() as sess:
try:
while True:
result.append(sess.run(next_batch))
except tf.errors.OutOfRangeError:
pass
train_examples = np.array(list(zip(*result))[0]) # tr_examples[0][0].shape (64,224,3)
val_examples = np.array(list(zip(*val_result))[0]) # val_examples[0][0].shape (64,3)
培训代码段如下:
hist = base_model.fit((tr_examples[0][0],tr_examples[0][1]),epochs=epochs,verbose=1,validation_data=(val_examples[0][0],val_examples[0][1]),shuffle=True)
错误跟踪:
Traceback (most recent call last):
File "/home/user/00_files/project/DOUBLE_INPUT/dual_input.py",line 177,in <module>
validation_data=(val_examples[0][0],shuffle=True)
File "/home/user/.local/lib/python3.5/site-packages/keras/engine/training.py",line 955,in fit
batch_size=batch_size)
File "/home/user/.local/lib/python3.5/site-packages/keras/engine/training.py",line 754,in _standardize_user_data
exception_prefix='input')
File "/home/user/.local/lib/python3.5/site-packages/keras/engine/training_utils.py",line 90,in standardize_input_data
data = [standardize_single_array(x) for x in data]
File "/home/user/.local/lib/python3.5/site-packages/keras/engine/training_utils.py",in <listcomp>
data = [standardize_single_array(x) for x in data]
File "/home/user/.local/lib/python3.5/site-packages/keras/engine/training_utils.py",line 25,in standardize_single_array
elif x.ndim == 1:
AttributeError: 'tuple' object has no attribute 'ndim'
查看输入的形状(代码段注释中的 ),它应该可以工作。我猜只剩下一步了,但是我不确定缺少什么。
我在Ubuntu 16.04上使用python 3.5,keras 2.2.0,tensorflow-gpu 1.9.0。
非常感谢您的帮助。
编辑::更正了括号之后,抛出了此错误:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s),but instead got the following list of 1 arrays: [array([[[[0.9607844,0.9607844,0.9607844 ],[0.9987745,0.9987745,0.9987745 ],[0.9960785,0.9960785,0.9960785 ],...,[0.9609069,0.9609069,0.96017164...
Process finished with exit code 1
解决方法
hist = base_model.fit((tr_examples[0][0],tr_examples[0][1]),epochs=epochs,verbose=1,validation_data=(val_examples[0][0],val_examples[0][1]),shuffle=True)
应为:
hist = base_model.fit(tr_examples[0][0],tr_examples[0][1],shuffle=True)
请注意,尽管validation_data
参数需要一个元组,但训练输入/标签对不应为元组(即,删除括号)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。