图像和标签在TFRecord中不对应

时间:2019-03-31 06:01:10

标签: python-3.x tensorflow machine-learning

我的环境

  • Python 3.6.8
  • Tensorflow 1.12.0

我的问题

我制作了一个tfrecords文件,其中包含图像信息(原始图像,宽度,高度,通道)和标签(0或1)。我尝试从该文件获取数据,但是图像和标签不匹配。

代码

我通过以下代码制作了tfrecords文件。

# make lists of paths to the image file and label
cat_dir = './training_set/cats/'
dog_dir = './training_set/dogs/'

image_paths = []
labels = []

for fname in os.listdir(cat_dir):
    if '.jpg' in fname:
        image_paths.append(cat_dir + fname)
        labels.append(1)

for fname in os.listdir(dog_dir):
    if '.jpg' in fname:
        image_paths.append(dog_dir + fname)
        labels.append(0)



# shuffled to separate training and test data
shuffle_ind = np.random.permutation(len(labels))
image_paths = np.array(image_paths)[shuffle_ind]
labels = np.array(labels)[shuffle_ind]



# store the data
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _float_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

from PIL import Image


with tf.python_io.TFRecordWriter('training_data.tfrecords') as writer:
    for fname, label in zip(image_paths[:-1000], labels[:-1000]):
        image = Image.open(fname)
        image_np = np.array(image)
        image_shape = image_np.shape
        image = open(fname, 'rb').read()

        feature = {
            'height' : _int64_feature(image_shape[0]),
            'width' : _int64_feature(image_shape[1]),
            'channel' : _int64_feature(image_shape[2]),
            'image_raw' : _bytes_feature(image),
            'label' : _int64_feature(label)
        }
        tf_example = tf.train.Example(features=tf.train.Features(feature=feature))
        writer.write(tf_example.SerializeToString())

接下来,我从该文件中获取数据。

image_feature_description = {
    'height' : tf.FixedLenFeature([], tf.int64),
    'width' : tf.FixedLenFeature([], tf.int64),
    'channel' : tf.FixedLenFeature([], tf.int64),
    'image_raw' : tf.FixedLenFeature([], tf.string),
    'label' : tf.FixedLenFeature([], tf.int64),
}

def _parse_fun(example_proto):
    feature = tf.parse_single_example(example_proto, image_feature_description)
    feature['image_raw'] = tf.image.decode_jpeg(feature['image_raw'])
    feature['image_raw'] = tf.cast(feature['image_raw'], tf.float32) / 255.0
    feature['image_raw'] = tf.image.resize_images(feature['image_raw'], (150, 150))

    feature['label'] = tf.cast(feature['label'], tf.int32)

    return feature

with tf.Session() as sess:
    raw_image_dataset = tf.data.TFRecordDataset('training_data.tfrecords')

    parsed_image_dataset = raw_image_dataset.map(_parse_fun)
    batched_dataset = parsed_image_dataset#.batch(1)

    init = tf.global_variables_initializer()
    init.run()

    iterator = batched_dataset.make_one_shot_iterator()
    for i in range(10):
        data = iterator.get_next()
        X_batch = data['image_raw'].eval()
        y_batch = data['label'].eval()

        plt.imshow(X_batch)
        plt.show()

        print(y_batch)

在此代码中,我将图像进行比较并一一标注,但它们不匹配。为什么?

1 个答案:

答案 0 :(得分:1)

我是tensorflow的新手,据我所知,问题似乎是您运行了两次,所以结果不匹配。您可以尝试:

    data = iterator.get_next()
    data_dict = data.eval()
    X_batch = data_dict ['image_raw']
    y_batch = data_dict ['label']