2017-07-07 160 views
2

我对在自己的图像集上训练和评估卷积神经网络模型感兴趣。我想为我的模型定义使用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.Estimatorfit()evaluate()方法的输入。


问题

我如何格式化我jpg(或TFRecord)的数据,这样我可以把它们作为输入到Estimator对象的功能呢?

我假设我必须将我的图像和标签转换为NumPy数组,因为它显示在上面的代码中,但是,不清楚mnist.train.imagesmnist.train.validation是如何格式化的。

是否有人将jpg文件和标签转换为NumPy数组,该Estimator类预期作为输入?

任何帮助将不胜感激。

+1

我知道这个问题是Tensorflow(我试图找到如何在Tensorflow做到这一点),但是这在PyTorch中超级容易:https://github.com/pytorch/vision#imagefolder – finbarr

+1

这个答案可能对你有用:https://stackoverflow.com/questions/34340489/tensorflow-read-images-with-标签 – finbarr

回答

2

您引用的文件cnn_mnist.py,特别是以下函数mnist_classifier.fit需要Numpy数组作为xy的输入。因此,我将解决您的第二个和第三个问题,因为TFRecords可能不容易并入引用的代码中。

然而,目前尚不清楚如何mnist.train.images和mnist.train.validation被格式化

mnist.train.images是numpy的阵列形状(55000,784),其中,55000是图像数量和784是每个展平图像的尺寸(28 x 28)。 mnist.validation.images也是一个形状为Numpy的数组(5000,784)。

有没有人有任何将jpg文件和标签转换为此Estimator类期望作为输入的NumPy数组的经验?

下面的代码在一个JPEG图像读为三维NumPy的数组:

from scipy.misc import imread 
    filename = '1.jpg' 
    np_1 = imread(filename) 

我认为所有这些图像的大小相同,或者你可以将它们调整到相同大小,因为您已经从此数据集生成了TFRecords文件。所有剩下要做的就是平铺图像,反复读取其他图像并将它们弄平,然后垂直堆叠所有图像。该对象可以输入Estimator函数。

下面是代码扁平化和垂直堆叠两个三维numpy的数组:

import numpy as np 
    np_1_2 = np.vstack((np_1.flatten(), np_2.flatten()))