Keras预测:TypeError:只能将整数标量数组转换为标量索引

时间:2017-10-09 08:54:21

标签: numpy tensorflow keras

我使用两个输入训练模型并连接两个CNN,它的工作方式应该如此。现在,当我尝试预测时,我收到以下错误:

Traceback (most recent call last):
  File "vggFace_predict.py", line 83, in <module>
    score = model.predict([value[0], value[1]], value[2])
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1710, in predict
    verbose=verbose, steps=steps)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1262, in _predict_loop
    batch_ids = index_array[batch_start:batch_end]
TypeError: only integer scalar arrays can be converted to a scalar index

这是我使用的代码:

'''
Load the models
'''
model = load_model('weights/Fuse_1Frontalization_2Patch_Freezed_MaxPooling.epoch-00_val-accu-0.32.hdf5')


'''
Dataset Generators
'''
train_datagen = ImageDataGenerator(
    rescale=1. / 224,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# Datagen
def obtain_datagen(datagen, train_path, seed_number=7):
    return datagen.flow_from_directory(
                train_path,
                target_size=(img_height, img_width),
                batch_size=batch_size,
                seed=seed_number,
                class_mode='binary')

# Training data generators
train_generator_1 = obtain_datagen(train_datagen, train_data_dir_1)
train_generator_2 = obtain_datagen(train_datagen, train_data_dir_2)

# Yield for data generators
def generate_data_generator_for_two_images(genX1, genX2):
    while True:
            X1i = genX1.next()
            X2i = genX2 .next()
            yield X1i[0], X2i[0], X1i[1]

# Yield for data generators
dataset_train_gen = generate_data_generator_for_two_images(train_generator_1, train_generator_2)

train_true = []
train_pred = []
for train_sample in range(train_samples):
    value = dataset_train_gen.next()

    score = model.predict([value[0], value[1]], value[2])

    train_true.append(value[2])
    train_pred.append(score)

confusion_matrix(train_true, train_pred)

两个输入都是图像。我认为问题与输入格式有关。

0 个答案:

没有答案