我对在自己的图像集上训练和评估卷积神经网络模型感兴趣。我想为我的模型定义使用tf.layers
模块,并使用tf.learn.Estimator
对象分别使用fit()
和evaluate()
方法来训练和评估模型。TensorFlow:自定义图像数据集上的训练模型
Here是我一直关注的教程,有助于展示tf.layers
模块和tf.learn.Estimator
类。但是,它使用的数据集(MNIST)只是简单地导入并加载(作为NumPy数组)。请参阅以下主要功能从教程脚本:
def main(unused_argv):
# Load training and eval data
mnist = learn.datasets.load_dataset("mnist")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
# Create the Estimator
mnist_classifier = learn.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")
# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)
# Train the model
mnist_classifier.fit(
x=train_data,
y=train_labels,
batch_size=100,
steps=20000,
monitors=[logging_hook])
# Configure the accuracy metric for evaluation
metrics = {
"accuracy":
learn.MetricSpec(
metric_fn=tf.metrics.accuracy, prediction_key="classes"),
}
# Evaluate the model and print results
eval_results = mnist_classifier.evaluate(
x=eval_data, y=eval_labels, metrics=metrics)
print(eval_results)
的完整代码here
我有我自己的形象,这是我在某个目录结构中同时jpg
格式:
而且我还将我的图像目录转换为TFRecord格式,其中一个TFRecord文件为train
,另一个为validation
。我遵循this教程,该教程使用TensorFlow附带的Inception模型中的build_image_data.py脚本作为输出这些TFRecord文件的黑盒。我承认,我可能通过创造这些东西而将马车放在马前,但我想也许有一种方法可以将这些用作tf.learn.Estimator
的fit()
和evaluate()
方法的输入。
问题
我如何格式化我jpg
(或TFRecord)的数据,这样我可以把它们作为输入到Estimator
对象的功能呢?
我假设我必须将我的图像和标签转换为NumPy数组,因为它显示在上面的代码中,但是,不清楚mnist.train.images
和mnist.train.validation
是如何格式化的。
是否有人将jpg
文件和标签转换为NumPy数组,该Estimator
类预期作为输入?
任何帮助将不胜感激。
我知道这个问题是Tensorflow(我试图找到如何在Tensorflow做到这一点),但是这在PyTorch中超级容易:https://github.com/pytorch/vision#imagefolder – finbarr
这个答案可能对你有用:https://stackoverflow.com/questions/34340489/tensorflow-read-images-with-标签 – finbarr