Tensorflow Resnet18在CIFAR100数据集中无法正常工作

Tensorflow Resnet18在CIFAR100数据集中无法正常工作,tensorflow,keras,tensorflow-datasets,resnet,Tensorflow,Keras,Tensorflow Datasets,Resnet,我为CIFAR100数据集编写了Resnet18代码,但效果不好 我试着把resnet18复制到 我通过模型摘要检查了模型架构。(参数编号与链接的相同) 然而,模型什么也学不到 我想我在代码中犯了错误,但我找不到 代码如下: EPOCHS = 50 hist = np.empty(shape=(2,EPOCHS)) class MyModel(Model): def __init__(self): super(MyModel, self).__init__() self.co

我为CIFAR100数据集编写了Resnet18代码,但效果不好

我试着把resnet18复制到

我通过模型摘要检查了模型架构。(参数编号与链接的相同)

然而,模型什么也学不到

我想我在代码中犯了错误,但我找不到

代码如下:

EPOCHS = 50
hist = np.empty(shape=(2,EPOCHS))
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 ReLU()(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 ReLU()(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)

  # func is just for displaying model summary.
  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.001)
model.compile(optimizer= optimizer,
          loss='sparse_categorical_crossentropy',
          metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train, epochs = 50)
Train on 50000 samples
Epoch 1/50
50000/50000 [==============================] - 6380s 128ms/sample - loss: 15.9479 - sparse_categorical_accuracy: 0.0100
Epoch 2/50
50000/50000 [==============================] - 6313s 126ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 3/50
50000/50000 [==============================] - 6388s 128ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 4/50
50000/50000 [==============================] - 6390s 128ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 5/50
50000/50000 [==============================] - 6333s 127ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 6/50
50000/50000 [==============================] - 6364s 127ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 7/50
12352/50000 [======>.......................] - ETA: 1:20:05 - loss: 15.9654 - sparse_categorical_accuracy: 0.0095
当我编译上述代码时,结果如下:

EPOCHS = 50
hist = np.empty(shape=(2,EPOCHS))
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 ReLU()(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 ReLU()(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)

  # func is just for displaying model summary.
  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.001)
model.compile(optimizer= optimizer,
          loss='sparse_categorical_crossentropy',
          metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train, epochs = 50)
Train on 50000 samples
Epoch 1/50
50000/50000 [==============================] - 6380s 128ms/sample - loss: 15.9479 - sparse_categorical_accuracy: 0.0100
Epoch 2/50
50000/50000 [==============================] - 6313s 126ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 3/50
50000/50000 [==============================] - 6388s 128ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 4/50
50000/50000 [==============================] - 6390s 128ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 5/50
50000/50000 [==============================] - 6333s 127ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 6/50
50000/50000 [==============================] - 6364s 127ms/sample - loss: 15.9569 - sparse_categorical_accuracy: 0.0100
Epoch 7/50
12352/50000 [======>.......................] - ETA: 1:20:05 - loss: 15.9654 - sparse_categorical_accuracy: 0.0095
进一步的结果是相同的

有什么问题吗