tensorflow - 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...
但是我希望收到模型的一些预测,但是得到了这个错误。请帮忙。谢谢
解决方案
推荐阅读
- azure-web-app-service - DNN UrlRewrite ("DotNetNuke.HttpModules.UrlRewriteModule, DotNetNuke.HttpModules") 不在 web.config 上运行自定义重写规则
- github - 使用 GitHub 安装 R 包
- azure - 尝试在指定配额和保留期的 Azure Web 应用上启用 Web 服务器日志记录
- c++ - 如何使用 sqlite3 db 从 cpp 中的表中打印数据?
- javascript - Vue 正在将 [object Module] 返回到 img src
- sql - 使用 foreach 循环容器在表中创建新列 - 无法解决“'@P1' 附近的语法错误”错误
- javascript - 电子邮件验证的最后一个符号检查
- node.js - NodeJS,如何编写正则表达式以动态路由路径
- javascript - 聚焦新打开的标签
- mysql - 如何将两个 laravel 应用程序与 Vue.js 前端合二为一