adapting the VGG 16 model for grayscale images

818 views
Skip to first unread message

Martin K

unread,
Jan 29, 2017, 6:01:00 AM1/29/17
to Keras-users
Hi,

I want to train a complete VGG16 model in keras on a set of new images. I have 100,000 grayscale images that are completely different than ImageNet.
They also have a different shape that I want to keep if possible.

The VGG16 model in keras is defined here:
https://github.com/fchollet/keras/blob/master/keras/applications/vgg16.py
and also here:
https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3


I want to make the following changes:

1. Only use grayscale input images

2. Change size of input images from 224x224 to maybe 200x350.



Regarding the first and second change, my input dimensions are then (1, 200, 350) so the first line has to changed from

model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))

to

model.add(ZeroPadding2D((1,1),input_shape=(1,200,350)))


Can I keep the rest of the model as is?

This is the first conv block in the default VGG16:

model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

I also have only two classes, so the top layer would be
model.add(Dense(2, activation='softmax'))

instead of the 1000 classes before.

Does this make sense at all? I am a beginner and have limited knowledge what the arguments for Convolution2D actually mean.

I 200x350 a reasonable size, I read it should be multiples of 2?



thanks

Klemen Grm

unread,
Jan 30, 2017, 5:30:11 AM1/30/17
to Keras-users
You can keep the rest of the model as is, but the final feature maps will be larger, since your input shape is larger. This will also result in much larger weight matrices on the first dense layer. It should still work if you have enough memory to fit the larger model.

GJ

unread,
Jan 30, 2017, 12:44:25 PM1/30/17
to Keras-users
Any tips from the group on using a trained RGB model on grayscale data?
What are the options? 

Daπid

unread,
Jan 30, 2017, 3:32:53 PM1/30/17
to GJ, Keras-users
On 30 January 2017 at 18:44, GJ <james.g...@gmail.com> wrote:
>> Any tips from the group on using a trained RGB model on grayscale data?
>
> What are the options?

Try it as it is, it may work. As a benchmark, you could try to
finetune it on the same training set, but on grayscale. If it looses
too much, you should consider retraining a smaller model from scratch,
or even artificially recoulor them. There have been some parpers that
use CNN to reproduce the colours of monocromatic images.

It all depends on how hard your task is, how much data you have, and
how good do you need your predictions to be.

gj

unread,
Jan 30, 2017, 3:52:43 PM1/30/17
to Keras-users, james.g...@gmail.com
It works, but is inefficient.
I tried using the same intensity in RGB channels (true greyscale) and also using just the red channel and zeros for GB.

I wonder whether there is a technique to convert a trained 3 channel model to a single channel model.
I also wonder whether that even make sense.
Reply all
Reply to author
Forward
0 new messages