我在 KERAS 中编辑了一个 AlexNet,试图学习一个类(即狗/脸或非狗/脸)。我有正确图像的训练样本,所以 Ytrain 是[1, 0]和不正确的图像,所以 Ytrain 是[0,1]。目标是知道图像是否是狗(例如)。
训练时损失很大~100,000,000
所以我删除了regularisers行并且它起作用了......
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2.0,l2=0.0)))
在 Caffe 中,模型可以很好地处理卷积层中的这些行:
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
这是 Keras 中的模型(图像大小 3、68、56):
Xtrain = self.Xtrain
Ytrain = self.Ytrain
batch_size = 10
nb_classes = 2
nb_epoch = 3
# input image dimensions
img_rows, img_cols = np.shape(self.Xtrain)[2], np.shape(self.Xtrain)[3]
#########################
### AlexNet - Start ##
#########################
print("Defining AlexNet...")
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,img_rows,img_cols)))
model.add(Convolution2D(96, 11, 11, border_mode='valid',
init='glorot_normal',
activation='relu',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2.0,l2=0.0)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3, 3),strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 5, 5, border_mode='valid',
init='glorot_normal',
activation='relu',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3, 3),strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(384, 3, 3, border_mode='valid',
init='glorot_normal',
activation='relu',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(384, 3, 3, border_mode='valid',
init='glorot_normal',
activation='relu',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, border_mode='valid',
init='glorot_normal',
activation='relu',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
model.add(MaxPooling2D(pool_size=(3, 3),strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096,
activation='relu',
init='glorot_normal',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
model.add(Dropout(0.5))
model.add(Dense(4096,
activation='relu',
init='glorot_normal',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
model.add(Dropout(0.5))
model.add(Dense(nb_classes,
activation='softmax',
activity_regularizer=ActivityRegularizer(l1=1, l2=1),
W_regularizer=WeightRegularizer(l1=2,l2=0)))
#######################
### AlexNet - End ##
#######################
add = Adadelta(lr=0.1, rho=0.95, epsilon=1e-06, decay=0.995)
print("Compiling AlexNet...")
model.compile(loss='categorical_crossentropy', optimizer=add)
print("Fitting AlexNet...")
model.fit(Xtrain, Ytrain, batch_size=batch_size, nb_epoch=nb_epoch,
show_accuracy=True, verbose=1, shuffle=True)
是吗??