我正在尝试使用 resnet50 进行迁移学习,但不明白为什么它会给我一个错误。我尝试用 Mobilenet 做同样的事情并且它有效。我使用的电脑没有连接到互联网,因此我单独下载了权重。 这是我的代码
def resnet50(image_size,num_classes,num_channels, dense_size, drop_prec, include_top, preweights):
if preweights:
if include_top:
path = os.path.join('weights_pre_trianed', 'resnet50_weights_tf_dim_ordering_tf_kernels.h5')
res = applications.ResNet50(include_top=include_top,
input_shape=(image_size[0], image_size[1], num_channels),
weights=path)
x = res.output
preds = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=res.input, outputs=preds)
for layer in model.layers[:143]:
layer.trainable = False
for layer in model.layers[143:]:
layer.trainable = True
return model
我收到以下错误
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Computed output size would be negative: -227 [input_size: 56, effective_filter_size: 512, stride: 2]
[[{{node res3a_branch1/convolution}}]]
[[Mean/_2259]]
(1) Invalid argument: Computed output size would be negative: -227 [input_size: 56, effective_filter_size: 512, stride: 2]
[[{{node res3a_branch1/convolution}}]]
0 successful operations.
0 derived errors ignored.
我使用 tensorflow 1.15 和 keras 2.3.1。 我的图像是灰度和大小 224x224 a 3 通道 为什么我会收到这个错误?
EDIT1:节点 res3a_branch1 是层号 45 Conv2D EDIT2:我想要一个二元分类
Edit3:我按照链接的建议进行了尝试,但现在出现了与模型相关的另一个错误。我知道我应该使用 lambda 层,但我不知道如何使用。
'node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
这是我之后的代码
path=os.path.join('weights_pre_trianed',
'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
i = Input([None, None, 3], dtype='float32')
x = applications.resnet50.preprocess_input(i)
res = applications.ResNet50(include_top=include_top, weights=path)
x = res(x)
x = GlobalAveragePooling2D()(x)
preds = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=i, outputs=preds)
for layer in model.layers[:-2]:
layer.trainable = False
return model