簡體   English   中英

如何在Tensorflow中重用模型

[英]How to reuse model in Tensorflow

我是TensorFlow的新手。 我有以下圖表。 我得到的測試精度是90%。 我想重用這個模型。 我想出來的一種方法是從學習的權重中啟動我的變量(查看下面的REUSE_MODEL)。 但是,當我通過模型運行測試數據集時,我現在獲得2.0%的准確度。

我正在做的方式有什么問題,最好的方法是什么?

圖形構建和運行

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

我認為正確的方法是保存和恢復元圖,這是官方文檔: https ://www.tensorflow.org/api_docs/python/state_ops/exporting_and_importing_meta_graphs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM