2017-02-01 42 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

我遇到了同樣的問題,無法解決我在互聯網上找到的任何解決方案(無需再培訓)的問題。所以我所做的是將每個模型加載到與主線程通信的兩個單獨的線程中。編寫代碼非常簡單,在同步線程時你必須小心。 在我的情況下,每個線程收到它的問題的輸入並返回到主線程的輸出。它工作時沒有任何可觀察的開銷。

相關問題