无法将一批图像准确输入到模型中 [英] not able to accurately input a batch of images into a model.fit

查看:115
本文介绍了无法将一批图像准确输入到模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型旨在训练双图像.由于数据集非常庞大,我按照建议的

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. 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)

培训代码段如下:

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], 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.

我在Ubuntu 16.04上使用python 3.5,keras 2.2.0,tensorflow-gpu 1.9.0.

I am using python 3.5, keras 2.2.0, tensorflow-gpu 1.9.0 on Ubuntu 16.04.

非常感谢您的帮助.

:更正了括号之后,抛出了此错误:

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)

应为:

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)

请注意,虽然validation_data参数需要一个元组,但训练输入/标签对不应为元组(即,删除括号).

Note that while the validation_data parameter expects a tuple, the training input/label pair should not be a tuple (i.e., remove the parenthesis).

这篇关于无法将一批图像准确输入到模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆