将图层添加到 keras 预训练模型中
add layers into keras pretrained model
我正在尝试构建一个 ResNet50 CNN,用于将图像分类为 5 个不同的 类。我首先导入这个模型:
ResNet = ResNet50(
include_top= None, weights='imagenet', input_tensor=None, input_shape=([128, 217, 3]),
pooling=None, classes=5)
然后我尝试为分类添加一些最终层:
ResNet.add(Flatten())
ResNet.add(Dense(units=512, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=256, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=5, activation='softmax'))
但是当我尝试这个时,我收到一条错误消息:
AttributeError: 'Functional' object has no attribute 'add'
有人知道如何解决这个问题吗?或者我应该如何尝试添加图层?
您应该执行以下操作:
from tensorflow.keras import models
ResNet = ResNet50(
include_top= None, weights='imagenet', input_tensor=None, input_shape=([128, 217, 3]),
pooling=None, classes=5)
model = models.Sequential()
model.add(ResNet)
model.add(Flatten())
model.add(Dense(units=512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=5, activation='softmax'))
您使用的语法是 Sequential()
模块的一部分。
这是因为您的模型不是顺序的。
您必须执行以下任一操作:
last_layer = ResNet.output
x = Flatten()(last_layer)
x = Dense(units=512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(units=256, activation='relu')(x)
x = (Dropout(0.5)(x)
x = Dense(units=5, activation='softmax')(x)
# prevent the weights from being updated during training
ResNet.trainable = False
model = Model(inputs=ResNet.input, outputs=x)
要么使用顺序模式:
ResNet_load = ResNet50(
include_top= None, weights='imagenet', input_tensor=None, input_shape=([128, 217, 3]),
pooling=None, classes=5)
Resnet = Sequential()
ResNet.add(ResNet_load)
ResNet.add(Flatten())
ResNet.add(Dense(units=512, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=256, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=5, activation='softmax'))
我正在尝试构建一个 ResNet50 CNN,用于将图像分类为 5 个不同的 类。我首先导入这个模型:
ResNet = ResNet50(
include_top= None, weights='imagenet', input_tensor=None, input_shape=([128, 217, 3]),
pooling=None, classes=5)
然后我尝试为分类添加一些最终层:
ResNet.add(Flatten())
ResNet.add(Dense(units=512, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=256, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=5, activation='softmax'))
但是当我尝试这个时,我收到一条错误消息:
AttributeError: 'Functional' object has no attribute 'add'
有人知道如何解决这个问题吗?或者我应该如何尝试添加图层?
您应该执行以下操作:
from tensorflow.keras import models
ResNet = ResNet50(
include_top= None, weights='imagenet', input_tensor=None, input_shape=([128, 217, 3]),
pooling=None, classes=5)
model = models.Sequential()
model.add(ResNet)
model.add(Flatten())
model.add(Dense(units=512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=5, activation='softmax'))
您使用的语法是 Sequential()
模块的一部分。
这是因为您的模型不是顺序的。
您必须执行以下任一操作:
last_layer = ResNet.output
x = Flatten()(last_layer)
x = Dense(units=512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(units=256, activation='relu')(x)
x = (Dropout(0.5)(x)
x = Dense(units=5, activation='softmax')(x)
# prevent the weights from being updated during training
ResNet.trainable = False
model = Model(inputs=ResNet.input, outputs=x)
要么使用顺序模式:
ResNet_load = ResNet50(
include_top= None, weights='imagenet', input_tensor=None, input_shape=([128, 217, 3]),
pooling=None, classes=5)
Resnet = Sequential()
ResNet.add(ResNet_load)
ResNet.add(Flatten())
ResNet.add(Dense(units=512, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=256, activation='relu'))
ResNet.add(Dropout(0.5))
ResNet.add(Dense(units=5, activation='softmax'))