TF |训练完成后如何从 CNN 进行预测

Posted

技术标签:

【中文标题】TF |训练完成后如何从 CNN 进行预测【英文标题】:TF | How to predict from CNN after training is done 【发布时间】:2018-10-16 08:40:26 【问题描述】:

尝试使用课程Stanford cs231n 中提供的框架,给出下面的代码。

我可以看到准确性越来越好,并且网络已经过训练,但是在训练过程和验证集上检查结果之后,我将如何将一张图像输入到模型中并查看其预测? 我四处搜索,在tensorflow 中找不到一些内置的预测功能,就像在keras 中一样。

初始化网络及其参数

# clear old variables
tf.reset_default_graph()

# setup input (e.g. the data that changes every batch)
# The first dim is None, and gets sets automatically based on batch size fed in
X = tf.placeholder(tf.float32, [None, 30, 30, 1])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)

def simple_model(X,y):
    # define our weights (e.g. init_two_layer_convnet)

    # setup variables
    Wconv1 = tf.get_variable("Wconv1", shape=[7, 7, 1, 32])  # Filter of size 7x7 with depth of 3. No. of filters is 32
    bconv1 = tf.get_variable("bconv1", shape=[32])
    W1 = tf.get_variable("W1", shape=[4608, 360])  # 5408 is 13x13x32 where 13x13 is the output of 7x7 filter on 32x32 image with padding of 2.
    b1 = tf.get_variable("b1", shape=[360])

    # define our graph (e.g. two_layer_convnet)
    a1 = tf.nn.conv2d(X, Wconv1, strides=[1,2,2,1], padding='VALID') + bconv1
    h1 = tf.nn.relu(a1)
    h1_flat = tf.reshape(h1,[-1,4608])
    y_out = tf.matmul(h1_flat,W1) + b1
    return y_out

y_out = simple_model(X,y)

# define our loss
total_loss = tf.losses.hinge_loss(tf.one_hot(y,360),logits=y_out)
mean_loss = tf.reduce_mean(total_loss)

# define our optimizer
optimizer = tf.train.AdamOptimizer(5e-4) # select optimizer and set learning rate
train_step = optimizer.minimize(mean_loss)

用于评估模型(无论是训练还是验证)并绘制结果的函数:

def run_model(session, predict, loss_val, Xd, yd,
              epochs=1, batch_size=64, print_every=100,
              training=None, plot_losses=False):
    # Have tensorflow compute accuracy
    correct_prediction = tf.equal(tf.argmax(predict,1), y)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # shuffle indicies
    train_indicies = np.arange(Xd.shape[0])
    np.random.shuffle(train_indicies)

    training_now = training is not None

    # setting up variables we want to compute and optimize
    # if we have a training function, add that to things we compute
    variables = [mean_loss,correct_prediction,accuracy]
    if training_now:
        variables[-1] = training

    # counter 
    iter_cnt = 0
    for e in range(epochs):
        # keep track of losses and accuracy
        correct = 0
        losses = []
        # make sure we iterate over the dataset once
        for i in range(int(math.ceil(Xd.shape[0]/batch_size))):
            # generate indicies for the batch
            start_idx = (i*batch_size)%Xd.shape[0]
            idx = train_indicies[start_idx:start_idx+batch_size]

            # create a feed dictionary for this batch
            feed_dict = X: Xd[idx,:],
                         y: yd[idx],
                         is_training: training_now 
            # get batch size
            actual_batch_size = yd[idx].shape[0]

            # have tensorflow compute loss and correct predictions
            # and (if given) perform a training step
            loss, corr, _ = session.run(variables,feed_dict=feed_dict)

            # aggregate performance stats
            losses.append(loss*actual_batch_size)
            correct += np.sum(corr)

            # print every now and then
            if training_now and (iter_cnt % print_every) == 0:
                print("Iteration 0: with minibatch training loss = 1:.3g and accuracy of 2:.2g"\
                      .format(iter_cnt,loss,np.sum(corr)/actual_batch_size))
            iter_cnt += 1
        total_correct = correct/Xd.shape[0]
        total_loss = np.sum(losses)/Xd.shape[0]
        print("Epoch 2, Overall loss = 0:.3g and accuracy of 1:.3g"\
              .format(total_loss,total_correct,e+1))
        if plot_losses:
            plt.plot(losses)
            plt.grid(True)
            plt.title('Epoch  Loss'.format(e+1))
            plt.xlabel('minibatch number')
            plt.ylabel('minibatch loss')
            plt.show()
    return total_loss,total_correct

训练模型的函数调用

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print('Training')
    run_model(sess,y_out,mean_loss,x_train,y_train,1,64,100,train_step,True)
    print('Validation')
    run_model(sess,y_out,mean_loss,x_val,y_val,1,64)

【问题讨论】:

【参考方案1】:

您不需要走太远,您只需将新的(测试)特征矩阵X_test 传递到您的网络并执行前向传递 - 输出层就是预测。所以代码是这样的

session.run(y_out, feed_dict=X: X_test)

【讨论】:

我必须发送以下feed_dict 才能使其工作。 feed_dict=X:X_test, is_training:False 否则我会收到一个错误,即必须填写占位符 2。谢谢。

以上是关于TF |训练完成后如何从 CNN 进行预测的主要内容,如果未能解决你的问题,请参考以下文章

如何使用训练有素的 Keras CNN 模型对新的未标记数据进行预测

如何保持查找表初始化以进行预测(而不仅仅是训练)?

Tensorflow Keras - 训练时准确率高,预测时准确率低

如何将 Landsat 图像裁剪成更小的块进行训练,然后在原始图像上进行预测

如何使用 tf.estimator 导入保存的 Tensorflow 模型训练并预测输入数据

利用CNN进行人脸年龄预测