如何在Tensorflow上训练分类器进行训练

时间:2017-10-15 17:34:20

标签: python machine-learning tensorflow classification feed

我读了将用于在数据集中训练我的分类器的图像,如下所示:

filename_strings = []
label_strings = []
for dirname, dirnames, filenames in os.walk('training'):
    for filename in filenames:
        filename_strings.append(dirname + '\\' + filename)
        label_strings.append(dirname)

filenames = tf.constant(filename_strings)
labels    = tf.constant(label_strings)
dataset = tf.contrib.data.Dataset.from_tensor_slices((filenames, labels))
dataset_train = dataset.map(_parse_function)

_parse_function:

# Reads an image from a file, decodes it into a dense tensor, and resizes it
# to a fixed shape.
def _parse_function(filename, label):
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_png(image_string)
    image_resized = tf.image.resize_images(image_decoded, [28, 28])
    return image_decoded, label

但现在我无法为火车步骤提供动力。:

# Create the Estimator
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="/model")
# Set up logging for predictions
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)

# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x= {"x": dataset_train },
    y= dataset_train,
    batch_size=100,
    num_epochs=None,
    shuffle=True)
mnist_classifier.train(
    input_fn=train_input_fn,
    steps=200,
    hooks=[logging_hook])

我正在尝试按照本教程A Guide to TF Layers: Building a Convolutional Neural Network 使用我自己的图像集。

我是否可以不直接使用数据集来提供火车步骤?我的意思是,我只有一个张量,每个图像的特征和标签。

1 个答案:

答案 0 :(得分:0)

您引用的教程将数据读入numpy数组(请参阅the code here)并使用tf.estimator.inputs.numpy_input_fn传递输入。这是最简单的方法。

但是,如果您希望从一开始就使用张量运算,那么使用custom input function也是可能的。这是一个简单的例子:

def read_images(batch_size):
  # A stub. Should read the next batch, shuffle it, etc
  images = tf.zeros([batch_size, 28, 28, 1]) # 4-rank tensor
  labels = tf.ones([batch_size])             # 1-rank tensor (not one-hot encoded)
  return images, labels

def simple_input():
  x, labels = read_images(batch_size=10)
  return {"x": x}, labels

tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)

classifier.train(input_fn=simple_input,
                 steps=10,
                 hooks=[logging_hook])