如何在Tensorflow中重用模型

时间:2017-02-10 18:41:30

标签: python tensorflow

我是TensorFlow的新手。我有以下图表。我得到的测试精度是90%。我想重复使用该模型。我想出来的一种方法是从学习的权重中启动我的变量(查看下面的REUSE_MODEL)。但是,当我通过模型运行测试数据集时,我现在的准确率为2.0%。

我做这件事的方式有什么问题,以及最好的方法是什么?

GRAPH BUILD AND RUN

graph = tf.Graph()

with graph.as_default():

  # input data
  tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size, image_size, num_channels))
  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) 
  tf_test_dataset = tf.constant(test_dataset)

  layer1_weights = tf.Variable(tf.truncated_normal([kernel_size, kernel_size, num_channels, num_kernels]))
  layer1_biases = tf.Variable(tf.zeros([num_kernels]))
  layer2_weights = tf.Variable(tf.truncated_normal([kernel_size, kernel_size, num_kernels, num_kernels]))
  layer2_biases = tf.Variable(tf.constant(1.0, shape=[num_kernels]))
  layer3_weights = tf.Variable(tf.truncated_normal([image_size // 4 * image_size // 4 * num_kernels, num_hidden], stddev=0.1))
  layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))
  layer4_weights = tf.Variable(tf.truncated_normal([num_hidden, num_labels], stddev=0.1))
  layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))

  # model
  def model(data):
    conv = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')   
    hidden = tf.nn.relu(conv + layer1_biases)    
    conv = tf.nn.conv2d(hidden, layer2_weights, [1, 2, 2, 1], padding='SAME')
    hidden = tf.nn.relu(conv + layer2_biases)    
    shape = hidden.get_shape().as_list()   
    # reshape is of size batch_size X features_vector. We flatten the output of the layer2 to a features vector
    reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])    
    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)
    return tf.matmul(hidden, layer4_weights) + layer4_biases

  # training computation
  logits = model(tf_train_dataset)
  loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))

  optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

  # predictions 
  train_prediction = tf.nn.softmax(logits)
  test_prediction = tf.nn.softmax(model(tf_test_dataset))

def accuracy(predictions, labels):
  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

num_steps = 1001
num_epochs = 100

with tf.Session(graph=graph) as session:

  tf.global_variables_initializer().run()
  print('Initialized')      
  for epoch in range(num_epochs):
      for step in range(num_steps):

        offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
        batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
        batch_labels = train_labels[offset:(offset + batch_size), :]
        feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
        _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)

        if (step % 50 == 0):
           print('Minibatch loss at step %d: %f' % (step, l))
           print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))

  print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))

重新使用模型

with graph.as_default():

  tf_test_dataset2 = tf.constant(test_dataset)

  layer1_weights2 = tf.Variable(layer1_weights.initialized_value())
  layer1_biases2 = tf.Variable(layer1_biases.initialized_value())
  layer2_weights2 = tf.Variable(layer2_weights.initialized_value())
  layer2_biases2 = tf.Variable(layer2_biases.initialized_value())
  layer3_weights2 = tf.Variable(layer3_weights.initialized_value())
  layer3_biases2 = tf.Variable(layer3_biases.initialized_value())
  layer4_weights2 = tf.Variable(layer4_weights.initialized_value())
  layer4_biases2 = tf.Variable(layer4_biases.initialized_value())

  # model
  def model(data):
    conv = tf.nn.conv2d(data, layer1_weights2, [1, 2, 2, 1], padding='SAME')   
    hidden = tf.nn.relu(conv + layer1_biases2)    
    conv = tf.nn.conv2d(hidden, layer2_weights2, [1, 2, 2, 1], padding='SAME')
    hidden = tf.nn.relu(conv + layer2_biases2)    
    shape = hidden.get_shape().as_list()   
    # reshape is of size batch_size X features_vector. We flatten the output of the layer2 to a features vector
    reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])    
    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights2) + layer3_biases2)
    return tf.matmul(hidden, layer4_weights2) + layer4_biases2

  test_prediction2 = tf.nn.softmax(model(tf_test_dataset2))

with tf.Session(graph=graph) as session:    
    tf.global_variables_initializer().run()
    session.run(test_prediction2)
    print('Test accuracy: %.1f%%' % accuracy(test_prediction2.eval(), test_labels))

1 个答案:

答案 0 :(得分:1)

我认为正确的方法是保存和恢复元图,这是关于此的官方文档: https://www.tensorflow.org/api_docs/python/state_ops/exporting_and_importing_meta_graphs