In [1]:
import tensorflow as tf
import os

In [7]:
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("./train/*.jpg"))
#filename_queue = tf.train.string_input_producer(os.listdir('./train/'))


ERROR! Session/line number was not unique in database. History logging moved to new session 297

In [8]:
image_reader = tf.WholeFileReader()

In [9]:
_, image_file = image_reader.read(filename_queue)

In [10]:
image = tf.image.decode_jpeg(image_file)

In [11]:
with tf.Session() as sess:
    # Required to get the filename matching to run.
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()
    # Coordinate the loading of image files.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    # Get an image tensor and print its value.
    for i in range(5):
        image_tensor = sess.run([image])
        print(image_tensor)

    # Finish off the filename queue coordinator.
    coord.request_stop()
    coord.join(threads)


[array([[[ 26,  28,  25],
        [ 26,  28,  25],
        [ 27,  29,  26],
        ..., 
        [232, 232, 230],
        [232, 232, 230],
        [232, 232, 230]],

       [[ 26,  28,  25],
        [ 26,  28,  25],
        [ 27,  29,  26],
        ..., 
        [232, 232, 230],
        [232, 232, 230],
        [232, 232, 230]],

       [[ 26,  28,  25],
        [ 27,  29,  26],
        [ 27,  29,  26],
        ..., 
        [232, 232, 230],
        [232, 232, 230],
        [232, 232, 230]],

       ..., 
       [[166, 171, 115],
        [156, 161, 103],
        [150, 158,  98],
        ..., 
        [ 84, 106,  34],
        [ 79, 101,  29],
        [ 92, 114,  42]],

       [[162, 168, 108],
        [162, 168, 106],
        [167, 176, 111],
        ..., 
        [ 81, 101,  29],
        [ 77,  98,  23],
        [108, 129,  54]],

       [[169, 175, 111],
        [174, 180, 116],
        [181, 190, 123],
        ..., 
        [ 87, 108,  33],
        [ 77,  98,  21],
        [122, 143,  66]]], dtype=uint8)]
[array([[[63, 63, 63],
        [34, 34, 34],
        [41, 41, 41],
        ..., 
        [38, 38, 38],
        [38, 38, 38],
        [37, 37, 37]],

       [[35, 35, 35],
        [ 5,  5,  5],
        [11, 11, 11],
        ..., 
        [ 6,  6,  6],
        [ 6,  6,  6],
        [ 5,  5,  5]],

       [[39, 39, 39],
        [ 9,  9,  9],
        [13, 13, 13],
        ..., 
        [11, 11, 11],
        [11, 11, 11],
        [11, 11, 11]],

       ..., 
       [[38, 38, 38],
        [ 7,  7,  7],
        [12, 12, 12],
        ..., 
        [ 8, 10,  9],
        [ 8, 10,  9],
        [ 9, 11, 10]],

       [[38, 38, 38],
        [ 7,  7,  7],
        [12, 12, 12],
        ..., 
        [ 9,  9,  9],
        [ 9,  9,  9],
        [ 9,  9,  9]],

       [[38, 38, 38],
        [ 7,  7,  7],
        [13, 13, 13],
        ..., 
        [ 9,  9,  9],
        [ 9,  9,  9],
        [ 9,  9,  9]]], dtype=uint8)]
[array([[[ 35,  34,  29],
        [ 35,  32,  25],
        [ 34,  30,  21],
        ..., 
        [197, 189, 168],
        [171, 160, 140],
        [131, 116,  95]],

       [[ 37,  34,  27],
        [ 36,  33,  26],
        [ 36,  32,  23],
        ..., 
        [189, 181, 160],
        [163, 152, 132],
        [128, 113,  94]],

       [[ 42,  39,  30],
        [ 39,  35,  26],
        [ 38,  31,  21],
        ..., 
        [173, 165, 144],
        [146, 133, 114],
        [106,  93,  74]],

       ..., 
       [[170, 163, 153],
        [171, 164, 154],
        [173, 166, 156],
        ..., 
        [161, 145, 130],
        [161, 143, 129],
        [159, 141, 127]],

       [[171, 164, 154],
        [172, 165, 155],
        [174, 167, 157],
        ..., 
        [159, 146, 130],
        [158, 145, 129],
        [156, 143, 127]],

       [[171, 164, 154],
        [172, 165, 155],
        [174, 167, 157],
        ..., 
        [159, 146, 130],
        [158, 145, 129],
        [156, 143, 127]]], dtype=uint8)]
[array([[[ 32,  41,  36],
        [ 20,  29,  24],
        [ 18,  27,  22],
        ..., 
        [161, 196, 228],
        [162, 197, 229],
        [163, 196, 229]],

       [[ 34,  44,  36],
        [ 18,  28,  20],
        [ 17,  27,  19],
        ..., 
        [160, 193, 226],
        [163, 196, 229],
        [164, 197, 228]],

       [[ 33,  43,  35],
        [ 12,  22,  14],
        [ 11,  21,  13],
        ..., 
        [158, 189, 220],
        [161, 192, 223],
        [164, 195, 224]],

       ..., 
       [[ 84, 102,  78],
        [ 93, 111,  87],
        [105, 123,  99],
        ..., 
        [ 29,  52,  26],
        [ 30,  53,  27],
        [ 31,  54,  28]],

       [[ 89, 107,  83],
        [ 91, 109,  85],
        [ 77,  95,  71],
        ..., 
        [ 38,  61,  35],
        [ 39,  62,  36],
        [ 43,  66,  40]],

       [[ 92, 110,  86],
        [ 88, 106,  82],
        [ 59,  77,  53],
        ..., 
        [ 47,  70,  44],
        [ 51,  74,  48],
        [ 57,  80,  54]]], dtype=uint8)]
[array([[[195, 208, 217],
        [189, 202, 211],
        [181, 194, 203],
        ..., 
        [101, 121, 128],
        [102, 122, 129],
        [103, 123, 130]],

       [[194, 207, 216],
        [188, 201, 210],
        [180, 193, 202],
        ..., 
        [102, 122, 129],
        [103, 123, 130],
        [104, 124, 131]],

       [[192, 205, 214],
        [186, 199, 208],
        [178, 191, 200],
        ..., 
        [103, 123, 130],
        [104, 124, 131],
        [104, 124, 131]],

       ..., 
       [[ 55,  74,  89],
        [ 55,  74,  89],
        [ 55,  74,  89],
        ..., 
        [ 77,  93, 106],
        [ 77,  93, 106],
        [ 77,  93, 106]],

       [[ 55,  74,  89],
        [ 55,  74,  89],
        [ 55,  74,  89],
        ..., 
        [ 76,  92, 105],
        [ 76,  92, 105],
        [ 76,  92, 105]],

       [[ 55,  74,  89],
        [ 55,  74,  89],
        [ 55,  74,  89],
        ..., 
        [ 75,  91, 104],
        [ 75,  91, 104],
        [ 75,  91, 104]]], dtype=uint8)]

上述代码读取单个文件是成功的,但是读取多个文件会出现错误。


In [12]:
import os
files = os.listdir('./train/')
for file in files:
    print(file)


1670415,4167450264.jpg
1704423,3803410313.jpg
1941723,3196569066.jpg
216384,2908428553.jpg
4721801,79314512.jpg

将图片转化为TFRecord文件


In [16]:
import os 
import tensorflow as tf 
import matplotlib.pyplot as plt 
import numpy as np

In [ ]:
cwd='D:\Python\data\dog\\' 
classes={'husky','chihuahua'} #人为 设定 2 类
writer= tf.python_io.TFRecordWriter("dog_train.tfrecords") #要生成的文件