load multiple models in Tensorflow

后端 未结 5 1905
北荒
北荒 2021-02-04 09:08

I have written the following convolutional neural network (CNN) class in Tensorflow [I have tried to omit some lines of code for clarity.]

class CNN:
de         


        
5条回答
  •  半阙折子戏
    2021-02-04 09:54

    You need to create 2 sessions and restore the 2 models separately. In order for this to work you need to do the following:

    1a. When you're saving the models you need to add scopes to the variable names. That way you will know which variables belong to which model:

    # The first model
    tf.Variable(tf.zeros([self.batch_size]), name="model_1/Weights")
    ...
    
    # The second model 
    tf.Variable(tf.zeros([self.batch_size]), name="model_2/Weights")
    ...
    

    1b. Alternatively, if you already saved the models you can rename the variables by adding scope with this script.

    2.. When you restore the different models you need to filter by variable name like this:

    # The first model
    sess_1 = tf.Session()
    sess_1.run(tf.initialize_all_variables())
    saver_1 = tf.train.Saver([v for v in tf.all_variables() if 'model_1' in v.name])
    saver_1.restore(sess_1, weights_1_file)
    sess_1.run(pred, feed_dict={image: X})
    
    # The second model
    sess_2 = tf.Session()
    sess_2.run(tf.initialize_all_variables())
    saver_2 = tf.train.Saver([v for v in tf.all_variables() if 'model_2' in v.name])
    saver_2.restore(sess_2, weights_2_file)
    sess_2.run(pred, feed_dict={image: X})
    

提交回复
热议问题