python - 使用 ImageDataGenerator 进行迁移学习时出现形状不兼容错误
问题描述
我想创建一个分类模型。为此,我从 3 个不同的类别中收集了一些图像。首先,我已经实现了 Xception 模型(冻结了除最后一层之外的所有层)。然而,它过拟合了。然后,我决定使用数据增强策略。这是我第一次为此目的使用 Keras 模块。我相信我已经正确使用了它。但得到错误ValueError: Shapes (None, None) and (None, None, None, 3) are incompatible
。我已经尝试了我从网上找到的东西,但没有奏效。谁能指出我做错了什么?这是代码。
from tensorflow import keras
from matplotlib import pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.imagenet_utils import preprocess_input
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Activation
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.models import Model
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
'data2/train', # this is the target directory
target_size=(299, 299), # all images will be resized to 299x299 for the Xception
batch_size=32,
class_mode="categorical")
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
'data2/validation',
target_size=(299, 299),
batch_size=32,
class_mode="categorical")
Xception = keras.applications.Xception(weights='imagenet', include_top=False)
num_classes=3
inp = Xception.input
new_classification_layer = Dense(num_classes, activation='softmax')
out = new_classification_layer(Xception.layers[-2].output)
model_Xception = Model(inp, out)
model_Xception.summary()
for l, layer in enumerate(model_Xception.layers[:-1]):
layer.trainable = False
for l, layer in enumerate(model_Xception.layers[-1:]):
layer.trainable = True
opt=keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07)
model_Xception.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
model_Xception.summary()
model_Xception.fit_generator(
train_generator,
epochs=5,
validation_data=validation_generator)
model_Xception.save_weights('first_try.h5')
解决方案
那是因为您将卷积的输出提供给 Dense 层。
您需要添加或之一Flatten
,以便将输出转换为. 您可以更改这些行:GlobalMaxPooling2D
GlobalAveragePooling2D
(batch_size, input_size)
inp = Xception.input
out_xception = Xception.layers[-2].output
flatten = tf.keras.layers.Flatten()(out_xception)
new_classification_layer = tf.keras.layers.Dense(num_classes, activation='softmax')
out = new_classification_layer(flatten)
model_Xception = tf.keras.Model(inp, out)
model_Xception.summary()
第二件事是由于您在定义 Xception 模型时没有指定 input_shape,因此 Flatten 会抛出错误。只需将其更改为:
Xception = tf.keras.applications.Xception(weights='imagenet', include_top=False,
input_shape = (299,299,3))
推荐阅读
- docker - npm install can't find package.json
- excel - Office JS: Is there a way to find the last row with data in on an excel sheet so that i can then copy that information onto another sheet?
- javascript - Failed to execute 'pushState' on 'History'. Function could not be cloned
- c# - Why is Automapper causing a loading delay in Blazor WebAssembly?
- vue.js - Vue:Vue 路由器没有导航到任何路线
- python - 如何使 python discord bot 执行/使用命令
- jakarta-mail - 带有 OpenJDK 11 的 Jakarta Mail:java.util.ServiceConfigurationError: jakarta.mail.Provider: Provider com.sun.mail.imap.IMAPProvider not a subtype
- python - 在列表中查找每个系统出现一次的值
- python-3.x - grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC 终止于:status = StatusCode.UNAVAILABLE
- firebase - 在前端使用 Firebase Firestore 文档 ID 是不是一个坏主意?