简体   繁体   English

无法准确地将一批图像输入到 model.fit 中

[英]not able to accurately input a batch of images into a model.fit

My model is designed to train dual images.我的模型旨在训练双图像。 Since the dataset is very huge I used tf.data.Dataset method to get them as batches as suggested here .由于数据集非常庞大,我使用tf.data.Dataset方法按照此处建议的方式将它们分批获取。 However I had a difficulty at properly inputting a batch of images for training.但是,我在正确输入一批图像进行训练时遇到了困难。 I looked up some possible solutions to no avail.我查找了一些可能的解决方案无济于事。 Still, after these modifications:尽管如此,在这些修改之后:

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, 224, 3)
val_examples = np.array(list(zip(*val_result))[0])      # val_examples[0][0].shape (64, 224, 224, 3)

The training code snippet is as follows:训练代码片段如下:

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)

And the error trace:和错误跟踪:

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], val_examples[0][1]), 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", line 90, 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'

Looking at the shapes of inputs ( in the code snippets' comments ), it should work.查看输入的形状(在代码片段的注释中),它应该可以工作。 I guess there is only one step left, but I am not sure what is missing.我想只剩下一步了,但我不确定缺少什么。

I am using python 3.5, keras 2.2.0, tensorflow-gpu 1.9.0 on Ubuntu 16.04.我在 Ubuntu 16.04 上使用 python 3.5、keras 2.2.0、tensorflow-gpu 1.9.0。

Help is much appreciated.非常感谢帮助。

EDIT: after correcting the parantheses, it threw this error:编辑:更正括号后,它抛出了这个错误:

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)

should be:应该:

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)

Note that while the validation_data parameter expects a tuple, the training input/label pair should not be a tuple (ie, remove the parenthesis).请注意,虽然validation_data参数需要一个元组,但训练输入/标签对不应是元组(即删除括号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM