0

I was having trouble with my transfer learning implementation. I guess I found the root cause but it is not clear to me why it works like that. Here is the explanation...

If I create a model (e.g. resnet50 from keras.applications), and then try to use it in a tensorflow session, weights all of a sudden change. Here is a simple example:

First import the necessary libraries:

import tensorflow as tf
from keras.applications.resnet50 import ResNet50
from keras.models import Model

Then define the model as following:

model = ResNet50(weights='imagenet')

Now print out parameters from one of the layers as following:

model.get_layer('conv1').get_weights()

The output is long but it starts as following:

[array([[[[ 2.82526277e-02, -1.18737184e-02,  1.51488732e-03, ...,
           -1.07003953e-02, -5.27982824e-02, -1.36667420e-03],
          [ 5.86827798e-03,  5.04415408e-02,  3.46324709e-03, ...,
            1.01423981e-02,  1.39493728e-02,  1.67549420e-02],
          [-2.44090753e-03, -4.86173332e-02,  2.69966386e-03, ...,
           -3.44439060e-04,  3.48098315e-02,  6.28910400e-03]],

Later in the program, I need to read some data from csv files. I try to read the data by using the dataset API from tensorflow. For this purpose, I create a tensorflow session. And if I want to use the model within the tensorflow session, I see that model parameters change.

Here is a code example:

init_global_var = tf.global_variables_initializer() 
with tf.Session() as sess:
  sess.run(init_global_var)
  print(model.get_layer('conv1').get_weights())

And the output starts as following:

[array([[[[ 3.95432524e-02, -2.38095019e-02, -1.64129660e-02, ...,
          -2.83494107e-02,  2.25975104e-02, -1.48569904e-02],
         [ 2.40861587e-02,  1.48933977e-02, -4.10864130e-02, ...,
          -3.18703875e-02, -9.43836942e-03,  1.18204653e-02],
         [ 2.99405716e-02,  1.69009715e-03, -1.43084712e-02, ...,
          -2.93575712e-02,  2.70796008e-02, -3.17203328e-02]],

Since I have not trained the model yet, I expect to see the same parameter values but they are not the same!

So the question is: Do I have to create my model within the tensorflow session? Why is my resnet50 model accessible but with different parameter values in the tensorflow session?

edn
  • 1,981
  • 3
  • 26
  • 56
  • 4
    All weights are saved in `save_weights`. It would be better if you can provide the error message and example code to reproduce the error. – Yu-Yang Jul 05 '18 at 05:22
  • @Yu-Yang, thank you for your answer! I kept working on the problem and updated the question above to a great extent. Your new comments are more than welcome. – edn Jul 08 '18 at 19:39
  • I think you shouldn't run `global_variables_initializer`. That would re-initialize the weights. – Yu-Yang Jul 09 '18 at 10:03
  • Yes I figured out that running the initializer is destroying everything. But the general recommendation seems to be not to create new tf sessions in this kind of situations. There is a similar question related to this in Stackoverflow. I put the link down below in the answer. – edn Jul 09 '18 at 23:28

1 Answers1

0

Seemingly, this question is a replicate of another question in Tensorflow. Check this question out instead for the answer:

Integrating Keras model into TensorFlow

edn
  • 1,981
  • 3
  • 26
  • 56