如何在Tensorflow中还原两个图?

时间:2019-02-20 05:42:15

标签: python tensorflow

我在tensorflow中接受了LSTM培训,整个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")

然后,我还原模型以进行部署。 那时,我需要与Human pose estimation的另一个人体姿势估计器模型一起使用。姿势估算器已加载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模型。

我该如何解决问题?

1 个答案:

答案 0 :(得分:0)

我为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()