代码之家  ›  专栏  ›  技术社区  ›  Elia

TF |培训结束后如何从CNN预测

  •  2
  • Elia  · 技术社区  · 6 年前

    尝试使用课程中提供的框架 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 回复  |  直到 6 年前
        1
  •  2
  •   Jan K    6 年前

    您不需要走得太远,只需通过新的(测试)功能矩阵即可 X_test 进入网络并执行前向传递-输出层是预测层。代码是这样的

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