Keras:“ ValueError:检查目标时出错”

时间:2019-05-10 13:56:06

标签: tensorflow machine-learning keras deep-learning

我正在尝试建立一个模型,该模型会将视频分类为某些类别。

为此,我使用了预训练的模型-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 ...

但是我希望收到模型的一些预测,但出现此错误。请帮忙。谢谢

0 个答案:

没有答案
相关问题