首页 > 解决方案 > Keras:“ValueError:检查目标时出错”

问题描述

我正在尝试建立一个模型,它将视频分类到某个类别。

为此,我使用了预训练模型 - InceptionV3 并根据我自己的数据对其进行了训练。训练过程已成功完成,但是当我尝试对视频进行分类时出现错误:

ValueError: Error when checking : expected input_1 to have shape (None, None, None, 3) but got array with shape (1, 1, 104, 2048)

然而,对于预测,我使用了与训练过程相同的视频。

定义模型:

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    horizontal_flip=True,
    rotation_range=10.,
    width_shift_range=0.2,
    height_shift_range=0.2)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    os.path.join('data', 'train'),
    target_size=(299, 299),
    batch_size=32,
    classes=data.classes,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    os.path.join('data', 'test'),
    target_size=(299, 299),
    batch_size=32,
    classes=data.classes,
    class_mode='categorical')

base_model = InceptionV3(weights=weights, include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer
predictions = Dense(len(data.classes), activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
model.fit_generator(
    train_generator,
    steps_per_epoch=100,
    validation_data=validation_generator,
    validation_steps=10,
    epochs=nb_epoch,
    callbacks=callbacks)

预测:

#extract features from frames of video

files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    features = extractor_model.extract(f)
    sequence.append(features)

np.save(sequence_path, sequence)

sequences = np.load("data_final.npy")

#convert numpy array tp 4 dimensions
sequences = np.expand_dims(sequences, axis=0)
sequences = np.expand_dims(sequences, axis=0)

prediction = model.predict(sequences)

特征提取器:

def extract(self, image_path):
    #print(image_path)
    img = image.load_img(image_path, target_size=(299, 299))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    # Get the prediction.
    features = self.model.predict(x)

    if self.weights is None:
        # For imagenet/default network:
        features = features[0]
    else:
        # For loaded network:
        features = features[0]

    return features

Keras 抱怨的形状不是 None...

但是我希望收到模型的一些预测,但是得到了这个错误。请帮忙。谢谢

标签: tensorflowmachine-learningkerasdeep-learning

解决方案


推荐阅读