【问题标题】:How to us Tensorflow boosted tree for image clasification task?如何使用 Tensorflow 增强树进行图像分类任务?
【发布时间】:2019-09-19 18:01:42
【问题描述】:

我正在使用 CNN 模型进行图像分类。我想尝试使用决策树或增强树模型进行图像分类。我发现 Tensorflow 是 Boosted 树模型,但我无法理解如何将图像作为模型的输入。如果您知道如何使用 tf.boosted 树训练图像分类,请指导我。

【问题讨论】:

    标签: python tensorflow image-processing boost tree


    【解决方案1】:

    最简单的方法是使用 flatten 图像特征向量作为输入。您可以使用我用 mnist Dataset 测试过的以下代码示例(针对 2 类进行了修改)。请注意,当前 tensorflow BoostedTreesClassifier 的实现不支持多类分类器的剪枝。

    import tensorflow as tf
    
    tf.enable_eager_execution()
    
    mnist = tf.keras.datasets.mnist
    
    (x_train, y_train),(x_test, y_test) = mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    
    for i in range(len(y_train)):
        if y_train[i] is not 0:
            y_train[i] = 1
    
    for i in range(len(y_test)):
        if y_test[i] is not 0:
            y_test[i] = 1
    
    NUM_FEATURES = 28*28
    
    train_input_fn = tf.estimator.inputs.numpy_input_fn({'x': x_train.reshape(-1,NUM_FEATURES)}, y_train, batch_size=128, num_epochs=5, shuffle=True)
    eval_input_fn = tf.estimator.inputs.numpy_input_fn({'x': x_test.reshape(-1,NUM_FEATURES)}, y_test, batch_size=128, num_epochs=1, shuffle=False)
    
    features = [tf.feature_column.numeric_column("x", shape=(NUM_FEATURES,))]
    
    est = tf.estimator.BoostedTreesClassifier(features, n_batches_per_layer=1)
    est.train(train_input_fn)
    results = est.evaluate(eval_input_fn)
    print('Accuracy : ', results['accuracy'])
    

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 2018-07-23
      • 2016-04-20
      • 1970-01-01
      • 2016-04-19
      • 2018-03-07
      • 1970-01-01
      • 2015-04-24
      • 2019-08-07
      相关资源
      最近更新 更多