Tensorflow: how to use pretrained weights in new graph?

前端 未结 2 894
感动是毒
感动是毒 2021-01-05 14:24

I\'m trying to build an object detector with CNN using tensorflow with python framework. I would like to train my model to do just object recognition (classification) at fir

2条回答
  •  忘掉有多难
    2021-01-05 14:38

    Although I agree with Aechlys to restore variables. The problem is harder when we want to fix these variables. For example, we trained these variables and we want to use them in another model, but this time without training them (training new variables like in transfer-learning). You can see the answer I posted here.

    Quick example:

     with tf.session() as sess:
        new_saver = tf.train.import_meta_graph(pathToMeta)
        new_saver.restore(sess, pathToNonMeta) 
    
        weight1 = sess.run(sess.graph.get_tensor_by_name("w1:0")) 
    
    
     tf.reset_default_graph() #this will eliminate the variables we restored
    
    
     with tf.session() as sess:
        weights = 
           {
           '1': tf.Variable(weight1 , name='w1-bis', trainable=False)
           }
    ...
    

    We are now sure the restored variables are not a part of the graph.

提交回复
热议问题