python - TensorFlow - 如何在不同的测试数据集上使用经过训练的模型进行预测?

标签 python tensorflow classification deep-learning prediction

我是 TensorFlow 的新手,我不知道如何使用经过训练的模型对图片进行分类。我已经为我的训练和所有工作构建了训练、验证和测试数据集,但我想预测第二个测试数据集(称为 test2)。我正在对数字图片进行分类。

我已经试过了,但它不起作用:

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_datasetcreate_datasetreformat 给我形状为 (nb_pictures, 28, 28, 1) 的数据集和形状为: (nb_pictures,10)。

非常感谢您的帮助

最佳答案

那里有你需要的一切。如果你只是想预测你可以提取函数:

 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)

关于python - TensorFlow - 如何在不同的测试数据集上使用经过训练的模型进行预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42211833/

相关文章:

machine-learning - 我认为是机器学习问题的最佳方法

python - python scikit 中更快的数据拟合(或学习)功能

python - 装有多个应用程序的 Bottle

python - 有没有一种快速方法将 Pandas 列数据框转换为字符串列表?

python - 使用 python 在 PERSONAL.XLSB 中运行宏

python - 如何转换Tensorflow循环中的循环?

python - 访问 classification_report 中的数字 - sklearn

python - python - 如何根据python中两个现有字典键值的条件匹配向字典添加新键?

python - 评估模型给出的准确度不等于 sklearn 分类报告准确度

python - tf.Graph 中的修改改变了我的 NN 权重初始化