【问题标题】:Tensorflow tutorial on MNISTMNIST 上的 TensorFlow 教程
【发布时间】:2018-11-05 22:02:58
【问题描述】:

ThisTensorflow 教程将一个已经存在的数据集 (MNIST) 加载到代码中。相反,我想插入我自己的训练和测试图像。

def main(unused_argv):
# Load training and eval data
mnist = tf.contrib.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)

它说它返回一个原始像素值的 np 数组。

我的问题:

1.如何为我自己的图像集创建这样一个 numpy 数组? 我想这样做,这样我就可以在示例代码中直接替换我的 numpy 数组而不是这个 MNIST 数据,并根据我的数据(0-9 和 A-Z)训练模型。

编辑:经过进一步分析,我意识到mnist.train.imagesmnist.test.images 中的像素值已在 0 到 1 之间从 0 到 255 标准化(我想)这是怎么回事标准化帮助?

文件夹结构:训练和测试文件夹在同一个文件夹中

Training folder:
--> 0
    -->Image_Of_0.png
--> 1
    -->Image_Of_1.png
.
.
.
--> Z
    -->Image_Of_Z.png

Testing folder:
--> 0
    -->Image_Of_0.png
--> 1
    -->Image_Of_1.png
.
.
.
--> Z
    -->Image_Of_Z.png

我写的代码:

Names = [['C:\\Users\\xx\\Project\\training-images', 'train',9490], ['C:\\Users\\xx\\Project\\test-images', 'test',3175]]

#9490 is the number of training files in total (All the PNGs)
#3175 is the number of testing files in total (All the PNGs)
for name in Names:
FileList = []
for dirname in os.listdir(name[0]):
    path = os.path.join(name[0], dirname)
    for filename in os.listdir(path):
        if filename.endswith(".png"):
            FileList.append(os.path.join(name[0], dirname, filename))
print(FileList) 


## Creates list of all PNG files in training and testing folder

x_data = np.array([np.array(cv2.imread(filename)) for filename in FileList])
pixels = x_data.flatten().reshape(name[2], 2352)   #2352 = 28 * 28 * 3 image
print(pixels)

创建的像素数组能否作为训练和测试数据提供,即它是否与示例代码中提供的数据具有相同的格式?

2。同样,必须为所有标签创建什么 numpy 数组? (文件夹名称)

【问题讨论】:

    标签: python numpy tensorflow deep-learning tensor


    【解决方案1】:

    1.如何为自己的图像集创建这样一个 numpy 数组?

    TensorFlow 以多种方式接受数据(tf.data、feed_dict、QueueRunner)。 您应该使用的是 TFRecord,它可以通过 tf.data API 访问。它也是recommended format。假设您有包含图像的文件夹,并且您想将其转换为 tfrecord 文件。

    import tensorflow as tf 
    import numpy as np
    import glob
    from PIL import Image
    
    # Converting the values into features
    # _int64 is used for numeric values
    
    def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
    
    # _bytes is used for string/char values
    
    def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
    tfrecord_filename = 'something.tfrecords'
    
    # Initiating the writer and creating the tfrecords file.
    
    writer = tf.python_io.TFRecordWriter(tfrecord_filename)
    
    # Loading the location of all files - image dataset
    # Considering our image dataset has apple or orange
    # The images are named as apple01.jpg, apple02.jpg .. , orange01.jpg .. etc.
    
    images = glob.glob('data/*.jpg')
    for image in images[:1]:
      img = Image.open(image)
      img = np.array(img.resize((32,32)))
    label = 0 if 'apple' in image else 1
    feature = { 'label': _int64_feature(label),
                  'image': _bytes_feature(img.tostring()) }
    
    #create an example protocol buffer
     example = tf.train.Example(features=tf.train.Features(feature=feature))
    
    #writing the serialized example.
     writer.write(example.SerializeToString())
    writer.close() 
    

    现在读取这个 tfrecord 文件并做一些事情

    import tensorflow as tf 
    import glob
    reader = tf.TFRecordReader()
    filenames = glob.glob('*.tfrecords')
    filename_queue = tf.train.string_input_producer(
       filenames)
    _, serialized_example = reader.read(filename_queue)
    feature_set = { 'image': tf.FixedLenFeature([], tf.string),
                   'label': tf.FixedLenFeature([], tf.int64)
               }
    
    features = tf.parse_single_example( serialized_example, features= feature_set )
    label = features['label']
    
    with tf.Session() as sess:
      print sess.run([image,label]) 
    

    这是tensorflow/examples 中的 MNIST 示例

    干杯!

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2018-02-03
      • 2018-08-17
      • 1970-01-01
      • 2017-04-25
      • 2017-08-15
      相关资源
      最近更新 更多