在Tensorflow中加载多个模型

Ami*_*mit 10 tensorflow

我在Tensorflow中编写了以下卷积神经网络(CNN)类[ 为了清楚起见,我试图省略一些代码行.]

class CNN:
def __init__(self,
                num_filters=16,        # initial number of convolution filters
             num_layers=5,           # number of convolution layers
             num_input=2,           # number of channels in input
             num_output=5,          # number of channels in output
             learning_rate=1e-4,    # learning rate for the optimizer
             display_step = 5000,   # displays training results every display_step epochs
             num_epoch = 10000,     # number of epochs for training
             batch_size= 64,        # batch size for mini-batch processing
             restore_file=None,      # restore file (default: None)

            ):

                # define placeholders
                self.image = tf.placeholder(tf.float32, shape = (None, None, None,self.num_input))  
                self.groundtruth = tf.placeholder(tf.float32, shape = (None, None, None,self.num_output)) 

                # builds CNN and compute prediction
                self.pred = self._build()

                # I have already created a tensorflow session and saver objects
                self.sess = tf.Session()
                self.saver = tf.train.Saver()

                # also, I have defined the loss function and optimizer as
                self.loss = self._loss_function()
                self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.loss)

                if restore_file is not None:
                    print("model exists...loading from the model")
                    self.saver.restore(self.sess,restore_file)
                else:
                    print("model does not exist...initializing")
                    self.sess.run(tf.initialize_all_variables())

def _build(self):
    #builds CNN

def _loss_function(self):
    # computes loss


# 
def train(self, train_x, train_y, val_x, val_y):
    # uses mini batch to minimize the loss
    self.sess.run(self.optimizer, feed_dict = {self.image:sample, self.groundtruth:gt})


    # I save the session after n=10 epochs as:
    if epoch%n==0:
        self.saver.save(sess,'snapshot',global_step = epoch)

# finally my predict function is
def predict(self, X):
    return self.sess.run(self.pred, feed_dict={self.image:X})
Run Code Online (Sandbox Code Playgroud)

我已经为两个单独的任务独立训练了两个CNN.每人约需1天.比如说,model1和model2分别保存为' snapshot-model1-10000'和' snapshot-model2-10000'(及其相应的元文件).我可以测试每个模型并分别计算其性能.

现在,我想在一个脚本中加载这两个模型.我自然会尝试做如下:

cnn1 = CNN(..., restore_file='snapshot-model1-10000',..........) 
cnn2 = CNN(..., restore_file='snapshot-model2-10000',..........)
Run Code Online (Sandbox Code Playgroud)

我遇到错误[ 错误消息很长.我刚刚复制/粘贴了它的片段.]

NotFoundError: Tensor name "Variable_26/Adam_1" not found in checkpoint files /home/amitkrkc/codes/A549_models/snapshot-hela-95000
     [[Node: save_1/restore_slice_85 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/restore_slice_85/tensor_name, save_1/restore_slice_85/shape_and_slice)]]
Run Code Online (Sandbox Code Playgroud)

有没有办法从这两个文件中加载两个独立的CNN?欢迎提出任何建议/意见/反馈.

谢谢,

nmi*_*nic 19

就在这里.使用单独的图表.

g1 = tf.Graph()
g2 = tf.Graph()

with g1.as_default():
    cnn1 = CNN(..., restore_file='snapshot-model1-10000',..........) 
with g2.as_default():
    cnn2 = CNN(..., restore_file='snapshot-model2-10000',..........)
Run Code Online (Sandbox Code Playgroud)

编辑:

如果你想让他们进入相同的图表.你必须重命名一些变量.一个想法是让每个CNN在不同的范围内,让saver处理该范围内的变量,例如:

saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES), scope='model1')
Run Code Online (Sandbox Code Playgroud)

并在cnn中包装您范围内的所有构造:

with tf.variable_scope('model1'):
    ...
Run Code Online (Sandbox Code Playgroud)

EDIT2:

其他想法是重命名保存管理的变量(因为我假设您想要使用已保存的检查点而不重新训练所有内容.保存允许在图形和检查点中使用不同的变量名称,查看初始化文档.


kal*_*ltu 7

这应该是对投票最多的答案的评论。但是我没有足够的声誉来做到这一点。

无论如何。如果您(任何人进行搜索并了解到这一点)仍然无法解决lpp提供的解决方案,并且您正在使用Keras,请检查github中的以下引用。

这是因为如果未提供默认的tf会话,则keras会共享一个全局会话

创建模型1时,它在graph1上。当模型1加载权重时,该权重在与graph1关联的keras全局会话上

创建model2时,它在graph2上。当model2加载权重时,全局会话不知道graph2

以下解决方案可能会有所帮助,

graph1 = Graph()
with graph1.as_default():
    session1 = Session()
    with session1.as_default():
        with open('model1_arch.json') as arch_file:
            model1 = model_from_json(arch_file.read())
        model1.load_weights('model1_weights.h5')
        # K.get_session() is session1

# do the same for graph2, session2, model2
Run Code Online (Sandbox Code Playgroud)