将图像转换为tf记录并读取tf-records文件,不可读的代码

时间:2016-10-26 10:28:31

标签: tensorflow

我正在尝试将图片(PNG)转换为tf-records个文件。当我阅读tf-records个文件时。我在屏幕上看到了很多不可读的代码。请帮助找到问题所在。我在下面列出了我的问题和代码:

我需要将一系列图像(10个PNG)转换为单个tf-records文件。我有几个图像序列,每个序列(10个PNG)都在一个文件夹中。以下是我用于将图片转换为tf-records的代码:

import os, sys
import tensorflow as tf
import numpy as np
from PIL import Image

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

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

def read():
    # parent folder contains all sequence, each sequence (10 png) is in a sub-folder 
    parent_foler = sys.argv[1]
    for folder in os.listdir(parent_foler):
        images = read_images_from(parent_foler + '/' + folder)
        num_examples = len(images)

        print 'Number of images: ' + str(num_examples)
        outputFile = os.path.join(parent_foler, folder + '.tfrecords')
        writer = tf.python_io.TFRecordWriter(outputFile)
        fs = {}
        for index in range(num_examples):
            image_raw = images[index].tostring()
            image_name = 'move/' + str(index) + '/image/encoded'
            fs[image_name] = _bytes_feature(image_raw)
            print folder + ':' + image_name
        print 'Size of Features:' + str(len(fs))
        example = tf.train.Example(features=tf.train.Features(feature=fs))
        writer.write(example.SerializeToString())
        writer.close()

def read_images_from(folder_name):
    images = []
    files_to_read = [folder_name + '/' + folder_name.split('/')[-1] + '_' + str(i + 1) + '.png' for i in range(10)]
    for filename in files_to_read:
        im = Image.open(filename)
        im = np.asarray(im, np.uint8)
        images.append(im)
    images = np.array(images)
    print'shape of images: ' + str(images.shape)
    return images

if __name__ == "__main__":
    read()

以下是阅读这些tf-records文件的代码:(改编自this code

ORIGINAL_WIDTH = 2048
ORIGINAL_HEIGHT = 1536
COLOR_CHAN = 4

def build_tfrecord_input(tf_folder):
    filenames = [foldername + '/' + tf_file for tf_file in os.listdir(tf_folder)]
    filename_queue = tf.train.string_input_producer(filenames, shuffle=True)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    image_seq = []

    # FLAGS.sequence_length = 10
    for i in range(FLAGS.sequence_length): 
        image_name = 'move/' + str(i) + '/image/encoded'
        features = {image_name: tf.FixedLenFeature([1], tf.string)}
        features = tf.parse_single_example(serialized_example, features=features)
        image = tf.decode_raw(features[image_name], tf.uint8)
        image = tf.reshape(image, shape=[ORIGINAL_HEIGHT,ORIGINAL_WIDTH,COLOR_CHAN])
        image.set_shape([ORIGINAL_HEIGHT, ORIGINAL_WIDTH, COLOR_CHAN])

        crop_size = min(ORIGINAL_HEIGHT, ORIGINAL_WIDTH)
        image = tf.image.resize_image_with_crop_or_pad(image, crop_size, crop_size)
        image = tf.reshape(image, [1, crop_size, crop_size, COLOR_CHAN])
        image = tf.image.resize_bicubic(image, [IMG_HEIGHT, IMG_WIDTH])
        image = tf.cast(image, tf.float32) / 255.0
        image_seq.append(image)

        image_seq = tf.concat(0, image_seq)

    return image_seq

当训练代码运行到

tf.train.start_queue_runners(sess)
sess.run(tf.initialize_all_variables())

我看到了不可读的代码:

~~ ||〜〜}〜}〜 ~~~~~}〜|〜} {〜} {〜} {〜} {〜} { 〜|}}} ~~ ||||}}〜 {〜} Y} | X | {W | {瓦特} | X | {W {zvzyuxwswvrvuqutptsotsotsoutp tsoutputputptsotsoutputptsoutputpvuqwvrxwsyxtzyu {ZV |【W】| X} | X} | X } | X} | X} | X〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜 } Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜}ÿ 〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} Y〜} X〜Y〜瓦特~w��~w��~w��~w��~w��~w��~w��~Wtensorflow / core / framework / op_kernel.cc:968]无效的论点:可以不解析示例输入,值: ' < 移动/ 7 /图像/编码

任何人都可以告诉我代码的哪一部分是错的吗?

非常感谢, 安迪。

0 个答案:

没有答案
相关问题