I try to test ResNet approach on cifar10 dataset with the following python code:
# load data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float32')/256
X_test = X_test.astype('float32')/256
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
# build a model
def res_unit(x):
x_shortcut = x
x = Conv2D(16, (1, 1), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(16, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(32, (1, 1), padding='same')(x)
x = BatchNormalization()(x)
x = Add()([x_shortcut, x])
x = Activation('relu')(x)
return x
X_input = Input((X_train.shape[1:]))
X = Conv2D(32, (3, 3), padding='same')(X_input)
X = BatchNormalization()(X)
X = Activation('relu')(X)
X = res_unit(X)
X = res_unit(X)
X = res_unit(X)
X = Flatten()(X)
X = Dense(32)(X)
X = Activation('relu')(X)
X = Dense(10)(X)
X = Activation('softmax')(X)
model = Model(inputs=X_input, outputs=X)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# run model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5)
which outputs the worst result for 10-class classification: 0.1 accuracy on train and validation.
But when I comment all lines with BatchNormalization, everything starts to be OK: I got 0.57 accuracy after first epoch and then it even rises up.
What is the problem with BatchNormalization in my case? Am I using it in a correct way?