本文介绍了轻柔地读取tfrecords数据的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下所有问题均基于tensorflow 1.0 API



我现在能够在以类名命名的目录下写入图像,这是我生成的tfrecords代码:

  def _convert_to_example(文件名,图像缓冲区,标签,文本,高度,宽度):
colorspace ='RGB'
通道= 3
image_format ='JPEG'

示例= tf.train.Example(features = tf.train.Features(feature = {
'图像/高度':_int64_feature(高度),
'图像/宽度':_int64_feature(宽度),
'图像/色彩空间':_bytes_feature(tf.compat.as_bytes(色彩空间)),
'图像/通道':_int64_feature(通道),
'图像/类/标签':_int64_feature(标签),
'图像/类/文本':_bytes_feature(tf .compat.as_bytes(text)),
'image / format':_bytes_feature(tf.compat.as_bytes(image_format)),
'image / filename':_bytes_feature(tf.compat.as_bytes(os) .path.basename(filenam e))),
'图像/编码':_bytes_feature(tf.compat.as_bytes(image_buffer))}))
返回示例

这是主要方法,因此我在此处存储了高度,宽度,通道(此值未读出)等。



我能够读取tfrecords,这是我的代码:

  def read_tfrecords():
print('从tfrecords文件中读取{}'。format(FLAGS.record_file))
record_iterator = tf.python_io.tf_record_iterator(path = FLAGS.record_file)

和tf.Session()作为sess:
在record_iterator中的string_record:
example = tf.train.Example()
example.ParseFromString(string_record)

height_ = int(example.features .feature ['image / height']。int64_list.value [0])
width_ = int(example.features.feature ['image / width']。int64_list.value [0])
channel_ = int(example.features.feature ['image / channels']。int64_list.value [0])

image_bytes_ = example.features.feature ['image / encoded']。bytes_list.value [0]
label_ = int(example.features.feature ['image / class / label'] .int64_list.value [0])
text_bytes_ = example.features.feature ['image / class / text']。bytes_list.value [0]

#image_array_ = np.fromstring( image_bytes_,dtype = np.uint8).reshape((height_,width_,3))
image_ = tf.image.decode_jpeg(image_bytes_)
image_ = sess.run(image_)
text_ = text_bytes_.decode('utf-8')

print('tfrecords高度{0},宽度{1},频道{2}:'。format(height_,width_,channels_))
print('decode image shape:',image_.shape)
print('label text:',text_)
print('label:',label_)
#io。 imshow(image_)
#plt.show()

一切正常,但是,我尝试加载tfrec时出现问题将数据分批排序并馈入网络



以下是我批量加载的所有代码:

  tf.app.flags.DEFINE_integer('target_image_height',150,'train input image height')
tf.app.flags.DEFINE_integer('target_image_width',200, '培训输入图像宽度')

tf.app.flags.DEFINE_integer('batch_size',12,'培训的批量大小。')
tf.app.flags.DEFINE_integer(' num_epochs',100,'培训时期。')
tf.app.flags.DEFINE_float('learning_rate',0.01,'培训学习率。')

标志= tf。 app.flags.FLAGS


def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_,serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized = serialized_example,
features = {
'image / height':tf.FixedLenFeature([],tf.int64),
'图片/宽度':tf.FixedLenFeature([],tf.int64),
'图像/通道':tf.FixedLenFeature([],tf.int64),
'图像/编码':tf.FixedLenFeature([],tf.string),
'图像/类/标签':tf.FixedLenFeature([],tf.int64),
})

image = tf.decode_raw(features ['image / encoded'],out_type = tf.uint8)
height = tf.cast(features ['image / height'],dtype = tf.int32)
width = tf.cast(features ['image / width'],dtype = tf.int32)
通道= tf.cast(功能['图像/通道'],dtype = tf.int32)
标签= tf.cast(功能['图像/类/标签'],dtype = tf.int32 )

#将图片int64投射到float32 [0,255]-> [-0.5,0.5]
image = tf.cast(image,tf.float32)*(1. / 255)-0.5
image_shape = tf.stack([height,width,3])
image = tf.reshape(image,image_shape)

返回图像,标签


def输入(train,batch_size,num_epochs):
如果不是num_epochs:
num_epochs =无
文件名= ['./data/tiny_5_tfrecords/train-00000-of-00002',
'./data/tiny_5_tfrecords/train-00001- of-00002']
print(filenames)
with tf.name_scope('input'):
filename_queue = tf.train.string_input_producer(文件名,num_epochs = num_epochs)
打印(文件名队列)
图像,标签= read_and_decode(文件名队列)
图像,稀疏标签= tf.train.shuffle_batch(
[图像,标签],
batch_size = batch_size,
num_threads = 2,
容量= 1000 + 3 * batch_size,
min_after_dequeue = 1000)

返回图像,sparse_labels


def run_training():
张图像,标签=输入(火车= True,batch_size = FLAGS.batch_size,
num_epochs = FLAGS.num_epochs)

张图片= tf.Print(images,[images],message ='this is images:')
images.eval()
预测=推理.lenet(images = images,num_classes = 5,activation_fn ='relu ')
slim.losses.softmax_cross_entropy(预测,标签)

total_loss = slim.losses.get_total_loss()
tf.summary.scalar('loss',total_loss)

优化器= tf.train.RMSPropOptimizer(0.001,0.9)

train_op = slim.learning.create_train_op(total_loss = total_loss,
Optimizer = optimizer,
summary_gradients = True)
slim.learning.train(train_op = train_op,save_summaries_secs = 20)


def main(_):
run_training()


如果__name__ =='__main__':
tf.app .run()

我运行此程序,出现此错误:

 竞赛(最近一次通话最后一次):
文件 train_tiny5_tensorflow.py,第111行,在< module>中
tf.app.run()
文件 /usr/local/lib/python3.6/site-packages/tensorflow/python/platform/app.py,行$ b中的第44行$ b _sys.exit(main(_sys.argv [:1] + flags_passthrough))
文件 train_tiny5_tensorflow.py,第107行,位于主
run_training()
文件 train_tiny5_tensorflow。 py,第88行,在run_training
num_epochs = FLAGS.num_epochs)
文件 train_tiny5_tensorflow.py,第81行,输入
min_after_dequeue = 1000)
文件 / usr /local/lib/python3.6/site-packages/tensorflow/python/training/input.py,行1165,在shuffle_batch中
name = name)
文件 / usr / local / lib / python3.6 / site-packages / tensorflow / python / training / input.py,行724,在_shuffle_batch中
dtypes = types,shapes = shapes,shared_name = shared_name)
文件 / usr / local /lib/python3.6/site-packages/tensorflow/python/ops/data_flow_ops.py,第624行,位于__init__
形状= _as_shape_list(形状,dtypes)
文件 / usr / local / lib / python3.6 / site-packages /tensorflow/python/ops/data_flow_ops.py,第77行,在_as_shape_list
中,抬起ValueError(必须完全定义所有形状:%s%个形状)
ValueError:必须完全定义所有形状:[TensorShape([Dimension(None),Dimension(None),Dimension(3)]),TensorShape([])]

显然,程序根本没有tfrecords文件。



我已经尝试过:
1.我认为文件名可能不正确,我将其更改为相对路径和绝对路径,都可以;
2.我将tfrecords文件放在脚本旁边,然后直接写tfrecords文件名不起作用。



因此,基本上,我遇到了这个问题: / p>

1。将尽可能短的程序加载tfrecords文件成批并馈入网络的正式合理方法是



2。顺便说一句,写张量流层最简单,最优雅的方法是什么?苗条是一个不错的选择,原始方式很难看而且很复杂!

解决方案

对于任何可能遇到相同问题的人,我在上面的代码中犯了一些错误。
根本不使用 decode_raw ,而是使用 tf.image.decode_jpeg 和我的代码功能

  def输入(train,batch_size,num_epochs):
如果不是num_epochs:
num_epochs = None
文件名= ['./data/tiny_5_tfrecords/train-00000-of-00002',
'./data/tiny_5_tfrecords/train-00001-of-00002']
print(文件名)$ b带有tf.name_scope('input')的$ b:
filename_queue = tf.train.string_input_producer(文件名,num_epochs = num_epochs)
print(filename_queue)
图片,标签= read_and_decode(filename_queue)
张图片,sparse_labels = tf.train.shuffle_batch(
[image,label],
batch_size = batch_size,
num_threads = 2,
容量= 1000 + 3 * batch_size,
min_after_dequeue = 1000)

返回图像,sparse_labels

我错过了最后两行的标签。


all the follow question are based on tensorflow 1.0 API

I am now be able to write images under directory which named by class name, and this is I generate tfrecords codes:

def _convert_to_example(filename, image_buffer, label, text, height, width):
    colorspace = 'RGB'
    channels = 3
    image_format = 'JPEG'

    example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(height),
      'image/width': _int64_feature(width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/class/label': _int64_feature(label),
      'image/class/text': _bytes_feature(tf.compat.as_bytes(text)),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/filename': _bytes_feature(tf.compat.as_bytes(os.path.basename(filename))),
      'image/encoded': _bytes_feature(tf.compat.as_bytes(image_buffer))}))
    return example

this is the main method, so here I stored height, widht, channels(this value con't readout) etc.

And I am able read tfrecords out, this is my code:

def read_tfrecords():
    print('reading from tfrecords file {}'.format(FLAGS.record_file))
    record_iterator = tf.python_io.tf_record_iterator(path=FLAGS.record_file)

    with tf.Session() as sess:
        for string_record in record_iterator:
            example = tf.train.Example()
            example.ParseFromString(string_record)

            height_ = int(example.features.feature['image/height'].int64_list.value[0])
            width_ = int(example.features.feature['image/width'].int64_list.value[0])
            channels_ = int(example.features.feature['image/channels'].int64_list.value[0])

            image_bytes_ = example.features.feature['image/encoded'].bytes_list.value[0]
            label_ = int(example.features.feature['image/class/label'].int64_list.value[0])
            text_bytes_ = example.features.feature['image/class/text'].bytes_list.value[0]

            # image_array_ = np.fromstring(image_bytes_, dtype=np.uint8).reshape((height_, width_, 3))
            image_ = tf.image.decode_jpeg(image_bytes_)
            image_ = sess.run(image_)
            text_ = text_bytes_.decode('utf-8')

            print('tfrecords height {0}, width {1}, channels {2}: '.format(height_, width_, channels_))
            print('decode image shape: ', image_.shape)
            print('label text: ', text_)
            print('label: ', label_)
            # io.imshow(image_)
            # plt.show()

All goes fair, however, the problem occurs when I try load tfrecords data into batches and feed it into network

Here is all the code I load batches:

tf.app.flags.DEFINE_integer('target_image_height', 150, 'train input image height')
tf.app.flags.DEFINE_integer('target_image_width', 200, 'train input image width')

tf.app.flags.DEFINE_integer('batch_size', 12, 'batch size of training.')
tf.app.flags.DEFINE_integer('num_epochs', 100, 'epochs of training.')
tf.app.flags.DEFINE_float('learning_rate', 0.01, 'learning rate of training.')

FLAGS = tf.app.flags.FLAGS


def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized=serialized_example,
        features={
            'image/height': tf.FixedLenFeature([], tf.int64),
            'image/width': tf.FixedLenFeature([], tf.int64),
            'image/channels': tf.FixedLenFeature([], tf.int64),
            'image/encoded': tf.FixedLenFeature([], tf.string),
            'image/class/label': tf.FixedLenFeature([], tf.int64),
        })

    image = tf.decode_raw(features['image/encoded'], out_type=tf.uint8)
    height = tf.cast(features['image/height'], dtype=tf.int32)
    width = tf.cast(features['image/width'], dtype=tf.int32)
    channels = tf.cast(features['image/channels'], dtype=tf.int32)
    label = tf.cast(features['image/class/label'], dtype=tf.int32)

    # cast image int64 to float32 [0, 255] -> [-0.5, 0.5]
    image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
    image_shape = tf.stack([height, width, 3])
    image = tf.reshape(image, image_shape)

    return image, label


def inputs(train, batch_size, num_epochs):
    if not num_epochs:
        num_epochs = None
    filenames = ['./data/tiny_5_tfrecords/train-00000-of-00002',
                 './data/tiny_5_tfrecords/train-00001-of-00002']
    print(filenames)
    with tf.name_scope('input'):
        filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs)
    print(filename_queue)
    image, label = read_and_decode(filename_queue)
    images, sparse_labels = tf.train.shuffle_batch(
        [image, label],
        batch_size=batch_size,
        num_threads=2,
        capacity=1000 + 3 * batch_size,
        min_after_dequeue=1000)

    return images, sparse_labels


def run_training():
    images, labels = inputs(train=True, batch_size=FLAGS.batch_size,
                            num_epochs=FLAGS.num_epochs)

    images = tf.Print(images, [images], message='this is images:')
    images.eval()
    predictions = inference.lenet(images=images, num_classes=5, activation_fn='relu')
    slim.losses.softmax_cross_entropy(predictions, labels)

    total_loss = slim.losses.get_total_loss()
    tf.summary.scalar('loss', total_loss)

    optimizer = tf.train.RMSPropOptimizer(0.001, 0.9)

    train_op = slim.learning.create_train_op(total_loss=total_loss,
                                             optimizer=optimizer,
                                             summarize_gradients=True)
    slim.learning.train(train_op=train_op, save_summaries_secs=20)


def main(_):
    run_training()


if __name__ == '__main__':
    tf.app.run()

I run this program, got this error:

raceback (most recent call last):
  File "train_tiny5_tensorflow.py", line 111, in <module>
    tf.app.run()
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "train_tiny5_tensorflow.py", line 107, in main
    run_training()
  File "train_tiny5_tensorflow.py", line 88, in run_training
    num_epochs=FLAGS.num_epochs)
  File "train_tiny5_tensorflow.py", line 81, in inputs
    min_after_dequeue=1000)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/training/input.py", line 1165, in shuffle_batch
    name=name)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/training/input.py", line 724, in _shuffle_batch
    dtypes=types, shapes=shapes, shared_name=shared_name)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/data_flow_ops.py", line 624, in __init__
    shapes = _as_shape_list(shapes, dtypes)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/data_flow_ops.py", line 77, in _as_shape_list
    raise ValueError("All shapes must be fully defined: %s" % shapes)
ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(3)]), TensorShape([])]

apparently, program didn't got tfrecords file at all.

I have tried this:1. I thought it maybe filenames not right, I change it into both relative path and absolute path, either works;2. I places tfrecords file just beside script, and write tfrecords file name directly didn't work.

So, basicly, I got this problem:

1. What's the officially and reasonable way to write a as short as possiable program load tfrecords file into batches and feed into network

2. BTW, what's the simplest and elegantest way to write tensorflow layer? slim is a good choice, original way is ugly and complicated!

解决方案

For anyone maybe occured same questions, I made some mistakes in above code.Simply do not using decode_raw, using tf.image.decode_jpeg instead, and my code function

def inputs(train, batch_size, num_epochs):
    if not num_epochs:
        num_epochs = None
    filenames = ['./data/tiny_5_tfrecords/train-00000-of-00002',
                 './data/tiny_5_tfrecords/train-00001-of-00002']
    print(filenames)
    with tf.name_scope('input'):
        filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs)
    print(filename_queue)
    image, label = read_and_decode(filename_queue)
    images, sparse_labels = tf.train.shuffle_batch(
        [image, label],
        batch_size=batch_size,
        num_threads=2,
        capacity=1000 + 3 * batch_size,
        min_after_dequeue=1000)

    return images, sparse_labels

I missed a tab for the last 2 lines.

这篇关于轻柔地读取tfrecords数据的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:45