我想用Keras制作ResNet50模型。数据集为CIFAR10。在我的猜测中,错误是因为跳过层的维度。
class ResidualUnit50(keras.layers.Layer): # ResNet-50
def __init__(self, filters, strides=1, activation="relu", **kwargs):
super().__init__(**kwargs)
self.activation = keras.activations.get(activation) # relu activation in this example
self.main_layers = [
keras.layers.Conv2D(filters, 1, strides=strides, padding="same", use_bias=False),
keras.layers.BatchNormalization(),
keras.layers.Activation(self.activation),
keras.layers.Conv2D(filters, 3, strides = 1, padding="same", use_bias=False),
keras.layers.BatchNormalization(),
keras.layers.Activation(self.activation),
keras.layers.Conv2D(4 * filters, 1, strides = 1, padding="same", use_bias=False),
keras.layers.BatchNormalization(),
]
self.skip_layers = [] # to make short cut
if strides > 1:
self.skip_layers = [
keras. layers.Conv2D(4 * filters, 1, strides=strides, padding="same", use_bias=False),
keras. layers.BatchNormalization()]
def call (self, inputs):
Z = inputs
for layer in self.main_layers:
Z = layer(Z)
skip_Z = inputs
for layer in self.skip_layers: # adding short-cut
skip_Z = layer(skip_Z)
return self.activation(Z + skip_Z)
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64, 7, strides=2, input_shape= [32, 32, 3], padding="same", use_bias = False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.MaxPool2D(pool_size=3, strides=2, padding="same"))
prev_filters = 64
for filters in [64] * 3 + [128] * 4 + [256] * 6 + [512] * 3:
# [64, 64, 64, 128, 128, 128, 128, 256, 256, 256, 256, 256, 256, 512, 512, 512]
strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit50(filters, strides=strides))
prev_filters = filters
model.add(keras.layers. GlobalAvgPool2D())
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense (10, activation="softmax"))
但是
返回如下错误:
请让我知道为什么会发生这个错误以及如何修复它。
**添加更多详细信息
那么我如何修改我的代码呢?
if strides > 1 or prev_filters != 4*filters:
print()
self.skip_layers = [
keras.layers.Conv2D(4 * filters, 1, strides=strides, padding="same", use_bias=False),
keras.layers.BatchNormalization()
]
for filters in [64] * 3 + [128] * 4 + [256] * 6 + [512] * 3:
strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit50(filters, strides=strides))
prev_filters = 4 * filters
我只是修改了一下,但是看起来不对。这段代码工作,但验证损失是如此之大(超过正常范围)
1条答案
按热度按时间dw1jzc5e1#
在初始化
skip_layers
的if
块中,strides
始终等于1
,考虑提供的filters
:strides = 1 if filters == prev_filters else 2
。条件strides > 1
永远不会是True
并且self.skip_layers
总是等于[]
。在
call
方法中,由于self.skip_layers
为空,for
循环永远不会执行,skip_Z
采用inputs
的形状,即( 8 , 8 , 64 )
。Z
的形状是( 8 , 8 , 256 )
,skip_Z
的形状是( 8 , 8 , 64 )
,它们不适合加法。