How to overcome overfitting in a convolutional neural network when all else fails?

I am training a convolutional neural network with siamese architecture and buckling function for a face verification task. And I ran into a huge difference in training and validation accuracy from literally the first three or five eras. When the training accuracy reaches 95%, the validation accuracy is ~ 65%. It fluctuates somewhere around 70%, but never reaches that number. it is training and validation accuracy built on one diagram

So, to avoid this, I've tried a number of standard methods when it comes to overfitting, but before listing them here, I must say that none of them really change the picture. The gap between training and validation accuracy remains unchanged. So I used:

  • L1 regularization with lambda from 0.0001 to 10000.0
  • L2 regularization with lambda from 0.0001 to 10000.0
  • Drop at a rate of 0.2 to 0.8
  • Data augmentation methods (rotation, displacement, scaling)
  • Removing fully connected layers except for the last layer.

None of this really helps, so I appreciate any advice from you guys. And some information about the network itself. I am using tensorflow. This is what the model itself looks like:

net = tf.layers.conv2d(
    inputs,
    kernel_size=(7, 7),
    filters=15,
    strides=1,
    activation=tf.nn.relu,
    kernel_initializer=w_init,
    kernel_regularizer=reg)
# 15 x 58 x 58
net = tf.layers.max_pooling2d(net, pool_size=(2, 2), strides=2)
# 15 x 29 x 29
net = tf.layers.conv2d(
    net,
    kernel_size=(6, 6),
    filters=45,
    strides=1,
    activation=tf.nn.relu,
    kernel_initializer=w_init,
    kernel_regularizer=reg)
# 45 x 24 x 24
net = tf.layers.max_pooling2d(net, pool_size=(4, 4), strides=4)
# 45 x 6 x 6
net = tf.layers.conv2d(
    net,
    kernel_size=(6, 6),
    filters=256,
    strides=1,
    activation=tf.nn.relu,
    kernel_initializer=w_init,
    kernel_regularizer=reg)
# 256 x 1 x 1
net = tf.reshape(net, [-1, 256])
net = tf.layers.dense(net, units=512, activation=tf.nn.relu, kernel_regularizer=reg, kernel_initializer=w_init)
net = tf.layers.dropout(net, rate=0.2)
# net = tf.layers.dense(net, units=256, activation=tf.nn.relu, kernel_regularizer=reg, kernel_initializer=w_init)
# net = tf.layers.dropout(net, rate=0.75)
return tf.layers.dense(net, units=embedding_size, activation=tf.nn.relu, kernel_initializer=w_init)

      

Here's how the loss function is implemented:

def contrastive_loss(out1, out2, labels, margin):
distance = compute_euclidian_distance_square(out1, out2)
positive_part = labels * distance
negative_part = (1 - labels) * tf.maximum(tf.square(margin) - distance, 0.0)
return tf.reduce_mean(positive_part + negative_part) / 2

      

This is how I get and augment data (I am using LFW dataset):

ROTATIONS_RANGE = range(1, 25)
SHIFTS_RANGE = range(1, 18)
ZOOM_RANGE = (1.05, 1.075, 1.1, 1.125, 1.15, 1.175, 1.2, 1.225, 1.25, 1.275, 1.3, 1.325, 1.35, 1.375, 1.4)
IMG_SLICE = (slice(0, 64), slice(0, 64))


def pad_img(img):
    return np.pad(img, ((0, 2), (0, 17)), mode='constant')


def get_data(rotation=False, shifting=False, zooming=False):
    train_data = fetch_lfw_pairs(subset='train')
    test_data = fetch_lfw_pairs(subset='test')

    x1s_trn, x2s_trn, ys_trn, x1s_vld, x2s_vld = [], [], [], [], []

    for (pair, y) in zip(train_data.pairs, train_data.target):
        img1, img2 = pad_img(pair[0]), pad_img(pair[1])
        x1s_trn.append(img1)
        x2s_trn.append(img2)
        ys_trn.append(y)

        if rotation:
            for angle in ROTATIONS_RANGE:
                x1s_trn.append(np.asarray(rotate(img1, angle))[IMG_SLICE])
                x2s_trn.append(np.asarray(rotate(img2, angle))[IMG_SLICE])
                ys_trn.append(y)
                x1s_trn.append(np.asarray(rotate(img1, -angle))[IMG_SLICE])
                x2s_trn.append(np.asarray(rotate(img2, -angle))[IMG_SLICE])
                ys_trn.append(y)

        if shifting:
            for pixels_to_shift in SHIFTS_RANGE:
                x1s_trn.append(shift(img1, pixels_to_shift))
                x2s_trn.append(shift(img2, pixels_to_shift))
                ys_trn.append(y)
                x1s_trn.append(shift(img1, -pixels_to_shift))
                x2s_trn.append(shift(img2, -pixels_to_shift))
                ys_trn.append(y)

        if zooming:
            for zm in ZOOM_RANGE:
                x1s_trn.append(np.asarray(zoom(img1, zm))[IMG_SLICE])
                x2s_trn.append(np.asarray(zoom(img2, zm))[IMG_SLICE])
                ys_trn.append(y)

    for (img1, img2) in test_data.pairs:
        x1s_vld.append(pad_img(img1))
        x2s_vld.append(pad_img(img2))

    return (
        np.array(x1s_trn),
        np.array(x2s_trn),
        np.array(ys_trn),
        np.array(x1s_vld),
        np.array(x2s_vld),
        np.array(test_data.target)
    )

      

Thanks everyone!

+3


source to share


2 answers


This is a common problem with small datasets (LFW dataset size = 13,000 images).

you may try:



+2


source


You can try using batch normalization instead of dropout. Or even both (although some weird things usually happen when using both).



Or, as @ Abdu307 suggests, use preprocessed layers. You can train a model on huge generic datasets and then fine-tune with your face dataset.

+1


source







All Articles