2017-02-13 69 views
4

我是新的使用TensorFlow,我不知道如何分類與訓練有素的模型的圖片。我已經爲我的訓練和所有作品構建了火車,驗證和測試數據集,但是我想要預測第二個測試數據集(稱爲test2)。我正在分類數字的圖片。TensorFlow - 如何使用訓練好的模型預測不同的測試數據集?

我都試過,但它不工作:

def train_and_predict(restore=False, test_set=None): 
    """ 
    Training of the model, posibility to restore a trained model and predict on another dataset. 
    """ 
    batch_size = 50 
    # Regular datasets for training 
    train_dataset, train_labels, test_dataset, test_labels, valid_dataset, valid_labels = load_dataset(dataset_size) 
    if restore: 
     # change the testset if restoring the trained model 
     test_dataset, test_labels = create_dataset(test_set) 
     test_dataset, test_labels = reformat(test_dataset, test_labels) 
     batch_size = number_predictions 

    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_valid_dataset = tf.constant(valid_dataset) 
     tf_test_dataset = tf.constant(test_dataset) 

     # Variables. 
     K = 32 # first convolutional layer output depth 
     L = 64 # second convolutional layer output depth 
     N = 1024 # fully connected layer 

     W1 = tf.Variable(tf.truncated_normal([5, 5, 1, K], stddev=0.1)) # 5x5 patch, 1 input channel 
     B1 = tf.Variable(tf.constant(0.1, tf.float32, [K])) 
     W2 = tf.Variable(tf.truncated_normal([5, 5, K, L], stddev=0.1)) 
     B2 = tf.Variable(tf.constant(0.1, tf.float32, [L])) 

     W3 = tf.Variable(tf.truncated_normal([7 * 7 * L, N], stddev=0.1)) 
     B3 = tf.Variable(tf.constant(0.1, tf.float32, [N])) 
     W4 = tf.Variable(tf.truncated_normal([N, 10], stddev=0.1)) 
     B4 = tf.Variable(tf.constant(0.1, tf.float32, [10])) 

     # Model. 
     def model(data, train = True): 
      stride = 1 
      Y1 = tf.nn.relu(tf.nn.conv2d(data, W1, strides=[1, stride, stride, 1], padding='SAME') + B1) 
      Y1 = tf.nn.max_pool(Y1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 
      Y2 = tf.nn.relu(tf.nn.conv2d(Y1, W2, strides=[1, stride, stride, 1], padding='SAME') + B2) 
      Y2 = tf.nn.max_pool(Y2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 
      Y3 = tf.reshape(Y2, [-1, 7*7*64]) 
      Y4 = tf.nn.relu(tf.matmul(Y3, W3) + B3) 
      if train: 
       # drop-out during training 
       Y4 = tf.nn.dropout(Y4, 0.5) 
      return tf.matmul(Y4, W4) + B4 

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

     # Optimizer. 
     optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss) 

     # Predictions for the training, validation, and test data. 
     train_prediction = tf.nn.softmax(logits) 
     valid_prediction = tf.nn.softmax(model(tf_valid_dataset, False)) 
     test_prediction = tf.nn.softmax(model(tf_test_dataset, False)) 

     # Saver 
     saver = tf.train.Saver() 

    num_steps = 1001 
    with tf.Session(graph=graph) as session: 
     if restore: 
      ckpt = tf.train.get_checkpoint_state('./model/') 
      saver.restore(session, ckpt.model_checkpoint_path) 
      _, l, predictions = session.run([optimizer, loss, test_prediction]) 
     else: 
      tf.global_variables_initializer().run() 
      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 % 100 ==0): 
        saver.save(session, './model/' + 'model.ckpt', global_step=step+1) 
       if (step % 1000 == 0): 
        print('\nMinibatch loss at step %d: %f' % (step, l)) 
     test_accuracy = accuracy(test_prediction.eval(), test_labels) 
    return test_accuracy , predictions 

所以第一次,我訓練模型和測試,然後我想預測對其他測試設置:

t,p = train_and_predict() #training 
t_test2, p_test2 = train_and_predict(restore=True, test_set='./test2') 

功能load_dataset,create_datasetreformat給我形狀的數據集:(nb_pictures,28,28,1)和形狀標籤:(nb_pictures,10)。

非常感謝您的任何幫助

回答

5

你有你需要的任何東西。如果你只是想預知你可以提取功能:

with tf.Session(graph=graph) as session: 
     ckpt = tf.train.get_checkpoint_state('./model/') 
     saver.restore(session, ckpt.model_checkpoint_path) 
     feed_dict = {tf_train_dataset : batch_data} 
     predictions = session.run([test_prediction], feed_dict) 
+0

但我必須重新定義另一個圖表,因爲我用於訓練圖'test_prediction = tf.nn.softmax(模型(tf_test_dataset,FALSE)) '和'tf_test_dataset = tf.constant(test_dataset)'。雖然我想要另一個測試數據集(可能與第一個測試數據集的圖片數量不同) –

+0

當我嘗試添加具有相同圖形的另一個測試集時,出現此錯誤'Tensor(「Variable:0」,shape =(5,5,1,32),dtype = float32_ref)必須來自與Tensor(「Const_1:0」,shape =(9,28,28,1),dtype = float32).'相同的圖形。 雖然'(「變量:0」,形狀=(5,5,1,32)似乎是W1和'張量(「Const_1:0」,形狀=(9,28,28,1),dtype = float32).'s似乎是新的tf_testset –

+0

您不能使用該功能,您必須創建一個新的,在新圖中定義相同的網絡,從保存程序中恢復變量,並運行預測節點輸入設置爲你的新數據 – fabrizioM

相關問題