首页 > 解决方案 > 如何在 Tensorflow 中恢复两个图?

问题描述

我有 LSTM 训练,tensorflow整个sess都保存在saver = tf.train.Saver(). 整个代码如下所示。

def LSTM_RNN(_X, _weights, _biases):
    # model architecture based on "guillaume-chevalier" and "aymericdamien" under the MIT license.

    _X = tf.transpose(_X, [1, 0, 2])  # permute n_steps and batch_size
    _X = tf.reshape(_X, [-1, n_input])   
    # Rectifies Linear Unit activation function used
    _X = tf.nn.relu(tf.matmul(_X, _weights['hidden']) + _biases['hidden'])
    # Split data because rnn cell needs a list of inputs for the RNN inner loop
    _X = tf.split(_X, n_steps, 0) 

    # Define two stacked LSTM cells (two recurrent layers deep) with tensorflow
    lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
    lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
    lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)
    outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)

    # A single output is produced, in style of "many to one" classifier, refer to http://karpathy.github.io/2015/05/21/rnn-effectiveness/ for details
    lstm_last_output = outputs[-1]

    # Linear activation
    return tf.matmul(lstm_last_output, _weights['out']) + _biases['out']

# Graph input/output
x = tf.placeholder(tf.float32, [None, n_steps, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])

# Graph weights
weights = {
'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights
'out': tf.Variable(tf.random_normal([n_hidden, n_classes], mean=1.0))
}
biases = {
'hidden': tf.Variable(tf.random_normal([n_hidden])),
'out': tf.Variable(tf.random_normal([n_classes]))
}

pred = LSTM_RNN(x, weights, biases)

# Loss, optimizer and evaluation
l2 = lambda_loss_amount * sum(
tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()
) # L2 loss prevents this overkill neural network to overfit the data
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred)) + l2 # Softmax loss
if decaying_learning_rate:
    learning_rate = tf.train.exponential_decay(init_learning_rate, global_step*batch_size, decay_steps, decay_rate, staircase=True)


#decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps) #exponentially decayed learning rate
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost,global_step=global_step) # Adam Optimizer
while step * batch_size <= training_iters:
    #print (sess.run(learning_rate)) #decaying learning rate
    #print (sess.run(global_step)) # global number of iterations
    if len(unsampled_indices) < batch_size:
        unsampled_indices = list(range(0,len(X_train)))
    batch_xs, raw_labels, unsampled_indicies = extract_batch_size(X_train, y_train, unsampled_indices, batch_size)
    batch_ys = one_hot(raw_labels)
    # check that encoded output is same length as num_classes, if not, pad it 
    if len(batch_ys[0]) < n_classes:
        temp_ys = np.zeros((batch_size, n_classes))
        temp_ys[:batch_ys.shape[0],:batch_ys.shape[1]] = batch_ys
        batch_ys = temp_ys



    # Fit training using batch data
    _, loss, acc = sess.run(
    [optimizer, cost, accuracy],
    feed_dict={
        x: batch_xs, 
        y: batch_ys
    }
    )
    train_losses.append(loss)
    train_accuracies.append(acc)

    # Evaluate network only at some steps for faster training: 
    if (step*batch_size % display_iter == 0) or (step == 1) or (step * batch_size > training_iters):

    # To not spam console, show training accuracy/loss in this "if"
        print("Iter #" + str(step*batch_size) + ":  Learning rate = " + "{:.6f}".format(sess.run(learning_rate)) + ":   Batch Loss = " + "{:.6f}".format(loss) + ", Accuracy = {}".format(acc))

    # Evaluation on the test set (no learning made here - just evaluation for diagnosis)
        loss, acc = sess.run([cost, accuracy], feed_dict={x: X_test,y: one_hot(y_test)})
        test_losses.append(loss)
        test_accuracies.append(acc)
        print("PERFORMANCE ON TEST SET:             " + "Batch Loss = {}".format(loss) +   ", Accuracy = {}".format(acc))
    step += 1

print("Optimization Finished!")
save_path = saver.save(sess, "ActivityTrainedModels/model.ckpt")

然后我恢复模型进行部署。那时,我需要与另一个人体姿态估计模型一起使用人体姿态估计。姿势估计器加载了get_graph_path().

我不能同时加载。我只能加载任何一个。如果我同时加载两个我有错误

NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key smoothing/gauss_weight not found in checkpoint
     [[node save/RestoreV2 (defined at ActivityDetection.py:219)  = RestoreV2[dtypes=[DT_INT32, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

我的部署代码如下。

n_steps = 32 # 32 timesteps per series
n_input = 36  # num input parameters per timestep
n_hidden = 34 # Hidden layer num of features
n_classes = 3 
global_step = tf.Variable(0, trainable=False)
# Graph input/output
x = tf.placeholder(tf.float32, [None, n_steps, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])

# Graph weights
weights = {
'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights
'out': tf.Variable(tf.random_normal([n_hidden, n_classes], mean=1.0))
}
biases = {
'hidden': tf.Variable(tf.random_normal([n_hidden])),
'out': tf.Variable(tf.random_normal([n_classes]))
}

pred = LSTM_RNN(x, weights, biases)
sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True))
init = tf.global_variables_initializer()

if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Activity Recognition')
     parser.add_argument('--video', type=str, default='../../tf-openpose/TestVideos/2019_01-Feb Recording/C4-13.mp4')
     parser.add_argument('--resolution', type=str, default='640x360', help='network input resolution. default=432x368')
     parser.add_argument('--model', type=str, default='mobilenet_thin', help='cmu / mobilenet_thin')
     parser.add_argument('--resize', type=str, default='0x0',
                        help='if provided, resize images before they are processed. default=0x0, Recommends : 432x368 or 656x368 or 1312x736 ')
     parser.add_argument('--resize-out-ratio', type=float, default=4.0,
                        help='if provided, resize heatmaps before they are post-processed. default=1.0')
     parser.add_argument('--show-process', type=bool, default=False,
                        help='for debug purpose, if enabled, speed for inference is dropped.')
     parser.add_argument('--showBG', type=bool, default=True, help='False to show skeleton only.')
     parser.add_argument('--s', type=str, default='00:00', help='start time to crop')
     parser.add_argument('--e', type=str, default='00:00', help='end time to crop')
     args = parser.parse_args()

     w, h = model_wh(args.resize)
     if w > 0 and h > 0:
        e = TfPoseEstimator(get_graph_path(args.model), target_size=(w, h))
     else:
        e = TfPoseEstimator(get_graph_path(args.model), target_size=(img_w, img_h))    

     '''with tf.Session() as sess:
         sess.run(init)
         saver = tf.train.Saver()
         saver.restore(sess, tf.train.latest_checkpoint('/home/coie/venvp3/HumanActivityRecognition/HumanActivityRecognition/ActivityTrainedModels/'))
         print("Model restored.")
         all_vars = tf.trainable_variables()
         for i in range(len(all_vars)):
              name = all_vars[i].name
              values = sess.run(name)
              print('name', name)
              #print('value', values)
              print('shape',values.shape)'''
          #result = sess.run(pred, feed_dict={x: X_test[24:27]})
          #for r in range(len(result)):
         #print("predicted activity:", LABELS[result[r].argmax(0)])

如果我加载TfPoseEstimator,我无法恢复 LSTM 模型。

我该如何解决这个问题?

标签: pythontensorflow

解决方案


我为 LSTM 创建了一个单独的类,并且 LSTM 图在类中加载了一个 sess。所以主要的python代码有另一个带有默认图的会话。主 python 加载后估计图的默认图。

我的 LSTM 类定义为

class ActivityRecognition:
     #Utility functions for training:
     def LSTM_RNN(self,_X, _weights, _biases):
      # model architecture based on "guillaume-chevalier" and "aymericdamien" under the MIT license.
          _X = tf.transpose(_X, [1, 0, 2])  # permute n_steps and batch_size
          _X = tf.reshape(_X, [-1, self.n_input])
          # Rectifies Linear Unit activation function used
          _X = tf.nn.relu(tf.matmul(_X, _weights['hidden']) + _biases['hidden'])
      # Split data because rnn cell needs a list of inputs for the RNN inner loop
          _X = tf.split(_X, self.n_steps, 0) 

      # Define two stacked LSTM cells (two recurrent layers deep) with tensorflow
          lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(self.n_hidden, forget_bias=1.0, state_is_tuple=True)
          lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(self.n_hidden, forget_bias=1.0, state_is_tuple=True)
          lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)
          outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)
          lstm_last_output = outputs[-1]
          return tf.matmul(lstm_last_output, _weights['out']) + _biases['out']
     def __init__(self):
          self.n_steps = 32 # 32 timesteps per series
          self.n_input = 36  # num input parameters per timestep
          self.n_hidden = 34 # Hidden layer num of features
          self.n_classes = 3 
          self.global_step = tf.Variable(0, trainable=False)     
          # Graph input/output
          self.x = tf.placeholder(tf.float32, [None, self.n_steps, self.n_input])
          self.y = tf.placeholder(tf.float32, [None, self.n_classes])
          # Graph weights
          self.weights = {
             'hidden': tf.Variable(tf.random_normal([self.n_input, self.n_hidden])), # Hidden layer weights
             'out': tf.Variable(tf.random_normal([self.n_hidden, self.n_classes], mean=1.0))
           }
          self.biases = {
             'hidden': tf.Variable(tf.random_normal([self.n_hidden])),
             'out': tf.Variable(tf.random_normal([self.n_classes]))
          }
          self.pred = self.LSTM_RNN(self.x, self.weights, self.biases)
          self.sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True))
          self.init = tf.global_variables_initializer()
          with tf.Session() as sess:
                  self.sess.run(self.init)
                  saver = tf.train.Saver()
                  saver.restore(sess, tf.train.latest_checkpoint('ActivityTrainedModels/'))
                  print("Model restored.")
                  #all_vars = tf.trainable_variables()
                  #for i in range(len(all_vars)):
                      #name = all_vars[i].name
                      #values = sess.run(name)
                      #print('name', name)
              #print('value', values)
                      #print('shape',values.shape)

     def inference(self,test):
          result = self.sess.run(selfpred, feed_dict={x: test})
          for r in range(len(result)):
              activity=LABELS[result[r].argmax(0)]
          return activity

if __name__ == "__main__":
     ActivityRecognition()  

推荐阅读