ldavid
ldavid

Reputation: 2552

Extracting multiple patches of a same image with Keras

I'm training/testing ML models over a dataset containing images of multiple sizes. I know Keras allows us to extract a random patch of fixed size using the target_size parameter:

gen = ImageDataGenerator(width_shift_range=.9, height_shift_range=.9)
data = gen.flow_from_directory('/path/to/dataset/train',
          target_size=(224, 224),
            classes=10,
            batch_size=32,
            seed=0)

for _ in range(data.N // data.batch_size):
    X, y = next(data)

For each iteration, X contains 32 patches (one for each different sample). Across all iterations, I have access to one patch of each sample in the dataset.

Question: what is the best way to extract MULTIPLE patches of a same sample?

Something like:

data = gen.flow_from_directory(..., nb_patches=10)
X, y = next(data)
# X contains 320 rows (10 patches for each 32 sample in the batch)

I know I can write a second for loop and iterate multiple times over the dataset, but this seems a little bit messy. I also would like to have a more strong guarantee that I am really fetching patches of a sample sample.

Upvotes: 2

Views: 5179

Answers (2)

Uwais Iqbal
Uwais Iqbal

Reputation: 990

skimage has a utility method that allows you to patch images with overlapping or non-overlapping segments.

Check out view_as_windows and view_as_blocks

http://scikit-image.org/docs/dev/api/skimage.util.html?highlight=view_as_windows#skimage.util.view_as_windows

Upvotes: 2

ldavid
ldavid

Reputation: 2552

I decided to implement it myself. That's how it ended up:

n_patches = 10
labels = ('class1', 'class2', ...)

for label in labels:
    data_dir = os.path.join('path-to-dir', label)

    for name in os.listdir(data_dir):
        full_name = os.path.join(data_dir, name)
        img = Image.open(full_name).convert('RGB')

        patches = []

        for patch in range(n_patches):
            start = (np.random.rand(2) * (img.width - image_shape[1],
                                img.height -image_shape[0])).astype('int')
            end = start + (image_shape[1], image_shape[0])
            patches.append(img_to_array(img.crop((start[0], start[1],
                                                  end[0], end[1]))))

        X.append(patches)
        y.append(label)

X, y = np.array(X, dtype=np.float), np.array(y, dtype=np.float)

Upvotes: 1

Related Questions