Following function is from http://scikit-learn.org/stable/auto_examples/neural_networks/plot_rbm_logistic_classification.html#sphx-glr-auto-examples-neural-networks-plot-rbm-logistic-classification-py where it is used to augment digits data:

Its documentation says:

    This produces a dataset 5 times 
    bigger than the original one,
    by moving the 8x8 images in X 
    around by 1px to left, right, 
    down, up
def nudge_dataset(X, Y):
    direction_vectors = [
        [[0, 1, 0],
         [0, 0, 0],
         [0, 0, 0]],

        [[0, 0, 0],
         [1, 0, 0],
         [0, 0, 0]],

        [[0, 0, 0],
         [0, 0, 1],
         [0, 0, 0]],

        [[0, 0, 0],
         [0, 0, 0],
         [0, 1, 0]]]

    shift = lambda x, w: convolve(x.reshape((8, 8)), mode='constant',
    X = np.concatenate([X] +
                       [np.apply_along_axis(shift, 1, X, vector)
                        for vector in direction_vectors])
    Y = np.concatenate([Y for _ in range(5)], axis=0)
    return X, Y

Can this function be used for any kind of image dataset?


1 Answer 1


Data augmentation is a technique for increasing the dataset size by performing certain operations over images such as translation, rotation, hue shift etc and increase the training dataset size. It could be applied to any kind of image dataset.

The above mentioned function seems to be applying shift with images of shape 8x8 and as far as the shape of the image is of this dimension, it will definitely work.

Refer to this paper for further reference.

  • $\begingroup$ Do you mean above function will work for images which are not 8x8 but say 64x64? Will it work for rectangular images, e.g. 64x32 ? $\endgroup$
    – rnso
    Commented Sep 26, 2018 at 8:55
  • $\begingroup$ See the line that says x.reshape((8,8)). When it is fed with image dimensions anything greater than 8x8 is ought to throw error and the function should simply not work. $\endgroup$
    – thanatoz
    Commented Sep 26, 2018 at 9:17
  • $\begingroup$ How can this function be modified to handle larger matrices? I think direction_vectors will have to be rewritten. Can we simply make larger sub-matrices there and put 1 on one side (left, right, up and down)? $\endgroup$
    – rnso
    Commented Sep 26, 2018 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.