我正在尝试为CIFAR100数据制作一个resnet18,如下所示:
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(64, (3,3), strides = (1,1), padding = 'same', activation='linear',input_shape=x_train.shape[1:])
self.bn1 = BatchNormalization()
self.RL = ReLU()
self.avg = AveragePooling2D((4,4))
self.flatten = Flatten()
self.FC = Dense(100, activation='softmax')
def make_BB(self, x,num_filter, size_decrease):
if size_decrease == True:
C1 = Conv2D(num_filter, (3,3),padding = 'same',strides = (2,2),activation = 'linear')
B1 = BatchNormalization()
R1 = ReLU()
C2 = Conv2D(num_filter, (3,3),padding = 'same',strides = (1,1),activation = 'linear')
B2 = BatchNormalization()
C3 = Conv2D(num_filter, (1,1),padding= 'same', strides = (2,2),activation = 'linear')
B3 = BatchNormalization()
forward = B2(C2(R1(B1(C1(x))))) + B3(C3(x))
return forward
else:
C1 = Conv2D(num_filter, (3,3),padding = 'same',strides = (1,1),activation = 'linear')
B1 = BatchNormalization()
R1 = ReLU()
C2 = Conv2D(num_filter, (3,3),padding = 'same',strides = (1,1),activation = 'linear')
B2 = BatchNormalization()
forward = B2(C2(R1(B1(C1(x))))) + x
return forward
def call(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.RL(x)
# BasicBlock1
x = self.make_BB(x,64,False)
x = self.make_BB(x,64,False)
# BasicBlock2
x = self.make_BB(x,128,True)
x = self.make_BB(x,128,False)
# BasicBlock3
x = self.make_BB(x,256,True)
x = self.make_BB(x,256,False)
# BasicBlock4
x = self.make_BB(x,512,True)
x = self.make_BB(x,512,False)
x = self.avg(x)
x = self.flatten(x)
return self.FC(x)
def func(self):
x = tf.keras.layers.Input(shape=(32, 32, 3))
return Model(inputs=[x], outputs=self.call(x))
model = MyModel()
optimizer = tf.keras.optimizers.Adam(0.01)
model.compile(optimizer= optimizer,
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train, epochs = 50)
但是此模型记录了1%的准确度,这意味着它什么都不学。
我尝试了更简单的模型,并且奏效了。
我还尝试将学习率更改为0.1、0.001、0.0005等,但所有结果都相同。
我想make_BB部分做错了,但我不知道自己做错了什么,甚至找不到我的错误。
我做错了什么?