生成器应该传递给predict_generator()返回什么?

时间:2017-03-28 14:02:49

标签: keras

我打电话给Keras predict_generator(),如:

bottleneck_features_train = model.predict_generator(train_gen, len(telemetry))

其中train_gen()定义为

def train_gen(): # ... yield (X, y)

X是一个有形状的凹凸不平的数组(48,299,299,3),y是一个形状为(48,)的numpy数组

我收到以下错误。我该怎么做呢?

否则,指向工作示例的链接会有所帮助。我找到的示例仅适用于Keras 1或使用ImageDataGenerator.flow()

我正在运行Keras 2.0.2。

这里的错误:

Traceback (most recent call last):
  File "/home/fanta/workspace/CarND-Behavioral-Cloning-P3/cache.py", line 143, in <module>
    tf.app.run()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/fanta/workspace/CarND-Behavioral-Cloning-P3/cache.py", line 138, in main
    bottleneck_features_train = model.predict_generator(train_gen, len(telemetry))
  File "/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 2094, in predict_generator
    outs = self.predict_on_batch(x)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 1677, in predict_on_batch
    self._feed_input_shapes)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 100, in _standardize_input_data
    'Found: array with shape ' + str(data.shape))
ValueError: The model expects 0 input arrays, but only received one array. Found: array with shape (48, 299, 299, 3)

Process finished with exit code 1

=====更新=====

问题与发电机无关。下面是一个重现它的简短程序。请注意,如果您将networkinception切换为vgg,则可以正常使用。

from keras.applications.inception_v3 import InceptionV3
from keras.applications.vgg16 import VGG16
from keras.layers import Input, AveragePooling2D
from keras.models import Model
from keras.datasets import cifar10
from scipy.misc import imresize
import pickle
import tensorflow as tf
import keras.backend as K
import numpy as np

network='inception'  # Must be 'inception' or 'vgg'
dataset='cifar10'
batch_size=64

if network == 'vgg':
    size = (224, 224)
elif network == 'inception':
    size = (299, 299)
else:
    assert False, "network must be either 'inception' or 'vgg'"

def create_model():
    input_tensor = Input(shape=(size[0], size[1], 3))
    if network == 'inception':
        model = InceptionV3(input_tensor=input_tensor, include_top=False)
        x = model.output
        x = AveragePooling2D((8, 8), strides=(8, 8))(x)
        model = Model(model.input, x)
    elif network == 'vgg':
        model = VGG16(input_tensor=input_tensor, include_top=False)
        x = model.output
        x = AveragePooling2D((7, 7))(x)
        model = Model(model.input, x)
    else:
        assert False
    return model

def main():

    # Download and load cifar10 dataset
    (X_train, y_train), (_, _) = cifar10.load_data()

    # Reduce the dataset to the first 1000 entries, to save memory and computation time
    X_train = X_train[0:1000]
    y_train = y_train[0:1000]

    # Resize dataset images to comply with expected input image size
    X_train = [imresize(image, size) for image in X_train]
    X_train = np.array(X_train)

    # File name where to save bottlenecked features
    train_output_file = "{}_{}_{}.p".format(network, dataset, 'bottleneck_features_train')
    print("Saving to", train_output_file)

    with tf.Session() as sess:
        K.set_session(sess)
        K.set_learning_phase(1)
        model = create_model()
        # We skip pre-processing and bottleneck the features
        bottleneck_features_train = model.predict(X_train, batch_size=batch_size, verbose=1)
        data = {'features': bottleneck_features_train, 'labels': y_train}
        pickle.dump(data, open(train_output_file, 'wb'))

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

在预测步骤中,您的生成器应该只生成输入而不是目标。所以只有X,而不是y。

这有帮助吗?