0

Just a head's up: I'm new to this, so please go gentle.

I'm trying to create a function that will shift every image in the MNIST dataset and add the shifted image to the original dataset, effectively doubling the dataset size.

My code (a warning, it might be a hot mess, I'll have to eventually learn how to write more elegant functions):

def four_point(i_set):
o_set = i_set
    for i in i_set:
        copy1 = np.ndarray(shape=(28,28))
        shift(i, shift=(1,0), output=copy1)
        copy1 = copy1.reshape(1,28,28)
        o_set = np.concatenate((o_set, copy1))
    return o_set

I've examined the outputted dataset, and it doesn't seem to be applying a shift. Can anyone guide me past this?

Yehuda
  • 1,787
  • 2
  • 15
  • 49

1 Answers1

0

Instead of writing your own function to do this, depend on the built-in functions a higher-level machine learning/deep learning module provides.

Like in the Keras module there is a built-in function called ImageDataGenerator()

This function has two arguments for generating shifts in the image. One for horizontal shift and the other for vertical shift. These two arguments are:

width_shift_range,
height_shift_range

Each of these arguments take: a Float, a 1-D array-like or an int.

1). float: fraction of total height, if < 1, or pixels if >= 1.

2). 1-D array-like: random elements from the array.

3). int: integer number of pixels from interval (-height_shift_range, +height_shift_range)

Now, coming to the fact that you want to augment these images and save them all in the same folder, use this piece of code:

aug = ImageDataGenerator(width_shift_range=0.2,height_shift_range=0.2)

### Make sure to have "/" at the end of the path

list_of_images=os.listdir("/path/to/the/folder/of/images/")

total = 0
#Change the value of "const" to the number of new augmented images to be created
const= 300

for i in range(const):
    curr_image = random.choice(list_of_images)
    image = load_img("/path/to/the/folder/of/images/"+curr_image)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    imageGen = aug.flow(image, batch_size=1, save_to_dir='/path/to/folder/to/save/images/',save_prefix="augment_image",save_format="jpg")
    for image in imageGen:
        total += 1
        if total == const:
            break
        break

This above snippet will create 300 new images in a folder called: "/path/to/folder/to/save/images/". Then all you have to do is paste your original images into this folder.

There are other arguments you can give for ImageDataGenerator() like brightness, zoom, vertical flip, horizontal flip, etc. Look into the documentation for more such arguments.

Sushanth
  • 2,224
  • 13
  • 29