Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to allow for preprocessing of image channel values in ImageDataGenerator and flow_from_directory? #4630

Closed
pchankh opened this issue Dec 7, 2016 · 2 comments

Comments

@pchankh
Copy link

pchankh commented Dec 7, 2016

In short we only see rescale option in ImageDataGenerator. How do you do the following for pertained VGG16 network that needs the channels values preprocessed as follows:
im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
Where do we specify these options in ImageDataGenerator?

Thanks
Dr

@mabirck
Copy link

mabirck commented Dec 7, 2016

There is a tread #3338 and gives a solution by adding a pipeline with fork of imageGenerator in keras. I think is that your looking for, sorry if I am wrong.

A more manual way of doing this is:

Pass the batches created in the form below trough a method that treats you data as you want it.

for e in range(nb_epoch):
    print 'Epoch', e
    batches = 0
    for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size=32):
        loss = model.train(X_batch, Y_batch)
        batches += 1
        if batches >= len(X_train) / 32:
            # we need to break the loop by hand because
            # the generator loops indefinitely
            break

@stale stale bot added the stale label May 23, 2017
@stale
Copy link

stale bot commented May 23, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.

@stale stale bot closed this as completed Jun 22, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants