Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
936 views
in Technique[技术] by (71.8m points)

keras - shouldn't model.trainable=False freeze weights under the model?

I am trying to freeze the free trained VGG16's layers ('conv_base' below) and add new layers on top of them for feature extracting. I expect to get same prediction results from 'conv_base' before(ret1) / after(ret2) fit of model but it is not. Is this wrong way to check weight freezing?

loading VGG16 and set to untrainable

conv_base  = applications.VGG16(weights='imagenet', include_top=False, input_shape=[150, 150, 3])?
conv_base.trainable = False

result before model fit

ret1 = conv_base.predict(np.ones([1, 150, 150, 3]))

add layers on top of the VGG16 and compile a model

model = models.Sequential()
model .add(conv_base)
model .add(layers.Flatten())
model .add(layers.Dense(10, activation='relu'))
model .add(layers.Dense(1, activation='sigmoid'))
m.compile('rmsprop', 'binary_crossentropy', ['accuracy'])

fit the model

m.fit_generator(train_generator, 100, validation_data=validation_generator, validation_steps=50)

result after model fit

ret2 = conv_base.predict(np.ones([1, 150, 150, 3]))

hope this is True but it is not.

np.equal(ret1, ret2)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an interesting case. Why something like this happen is caused by the following thing:

You cannot freeze a whole model after compilation and it's not freezed if it's not compiled

If you set a flag model.trainable=False then while compiling keras sets all layers to be not trainable. If you set this flag after compilation - then it will not affect your model at all. The same - if you set this flag before compiling and then you'll reuse a part of a model for compiling another one - it will not affect your reused layers. So model.trainable=False works only when you'll apply it in a following order:

# model definition
model.trainable = False
model.compile()

In any other scenario it wouldn't work as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...