2017-02-01 40 views
3

我在Tensorflow中编写了以下卷积神经网络(CNN)类[]为了清晰起见,我尝试省略一些代码行。]在Tensorflow中加载多个模型

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}) 

我已经训练的两名细胞神经网络的独立两个独立的任务。每个人花了大约1天。假设model1和model2分别保存为'snapshot-model1-10000'和'snapshot-model2-10000'(及其对应的元文件)。我可以测试每个模型并分别计算其性能。

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

cnn1 = CNN(..., restore_file='snapshot-model1-10000',..........) 
cnn2 = CNN(..., restore_file='snapshot-model2-10000',..........) 

我遇到的错误[错误信息长。我刚刚复制/粘贴了它的一个片段。]

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)]] 

有没有办法从这些两个文件两个独立的细胞神经网络的负荷?任何建议/意见/反馈是受欢迎的。

谢谢

回答

4

是的,有。使用单独的图表。

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',..........) 

编辑:

如果你希望他们到同一张图上。你将不得不重新命名一些变量。一个想法是让每个CNN在不同的范围,并让该范围如保护手柄变量:

saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES), scope='model1') 

,并在CNN包装所有的施工范围:

with tf.variable_scope('model1'): 
    ... 

EDIT2:

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

+0

非常感谢。你的第一个建议对我的情况很好。 – Amit

0

我遇到了同样的问题,无法解决我在互联网上找到的任何解决方案(无需再培训)的问题。所以我所做的是将每个模型加载到与主线程通信的两个单独的线程中。编写代码非常简单,在同步线程时你必须小心。 在我的情况下,每个线程收到它的问题的输入并返回到主线程的输出。它工作时没有任何可观察的开销。

相关问题