Apply VGG preprocessing to flow_from_data()

1,168 views
Skip to first unread message

Stephan Baier

unread,
Nov 18, 2016, 4:36:44 AM11/18/16
to Keras-users
Hi,

I want to fine-tune a VGG16 model from the keras.applications package. Therefore, the data needs to be preprocessed using the VGG preprocessing
keras.applications.vgg16.preprocess_input(x).

How can I apply this function to the input data when using the
ImageDataGenerator with the flow_from_directory(directory) method?

Thanks in advance.

haava...@gmail.com

unread,
Nov 18, 2016, 6:20:45 AM11/18/16
to Keras-users
To subtract the imagenet mean from the images you have to set the ImageDataGenerator.mean values then pass the featurewise center parameter.

datagen = ImageDataGenerator(featurewise_center=True)
datagen
.mean = np.array([103.939, 116.779, 123.68], dtype=np.float32).reshape((3, 1, 1))

flow_from_directory only supports RGB or grayscale images, so to convert them to BGR you need to customize the DirectoryIterator.next function in keras.

You can find a function for doing so here (line 148):
https://github.com/Arsey/keras-transfer-learning-for-oxford102/blob/master/util.py

pau.clim...@vcatechnology.com

unread,
Sep 4, 2017, 12:08:24 PM9/4/17
to Keras-users, haava...@gmail.com
Hi if I do that, I get:

ValueError: operands could not be broadcast together with shapes (150,150,3) (3,1,1) (150,150,3) 

Daπid

unread,
Sep 5, 2017, 2:03:47 AM9/5/17
to pau.clim...@vcatechnology.com, Keras-users, haava...@gmail.com
The mean is formatted as channels_first, but your images have channels_last. Your mean should have shape (1, 1, 3), then.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/5833654f-9f00-4dd8-8cf0-f65e3ed0a79a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages