如何使用Tensorflow从文件队列中读取无符号16位整数二进制数据



我正在尝试修改Tensorflow的高级卷积神经网络教程,该教程最初使用CIFAR-10数据集(https://www.tensorflow.org/tutorials/images/deep_cnn)以处理无符号16位整数数据。我会从二进制格式读取数据,类似于CIFAR-10数据集。

原始代码从二进制格式读取无符号的8位整数数据。我设法从我自己的数据集中创建了具有各种维度的8位样本(下面的result.depth变量(,并且训练没有任何问题。

但我自己的数据是一个无符号的16位整数数据集。如果我将它重新采样为8位,它将丢失重要信息。因此,我想将无符号的16位数据输入到图中进行训练。

前2个字节包含数据的标签,其余字节(高x宽x深x 2字节(包含记录的数据。我使用这种方法将二进制文件写入光盘:

out = np.array(outp, dtype = np.uint16) #this variable contains the data
out.tofile("d:\TF\my_databatch_0.bin") 

这部分似乎还可以。如果我把它读回记忆中,写着:

in = np.fromfile("d:\TF\my_databatch_0.bin", dtype=np.uint16)

它给了我完全相同的数组,这是写入光盘的。当我试图通过这个修改后的输入函数将其输入到图形中时,它在后面的步骤中失败了:

import tensorflow as tf
import numpy as np
##########################Reader for unsigned 16 bit numpy array###############
def read_cifar10(filename_queue):
class CIFAR10Record(object):
pass
result = CIFAR10Record()
label_bytes = 2  
result.height = 32
result.width = 32
result.depth = 1
result.bitdepth = 2 #2 if 16bit, 1 if 8bit
image_bytes = result.height * result.width * result.depth * result.bitdepth
record_bytes_ = label_bytes + image_bytes
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes_)
result.key, value = reader.read(filename_queue)
record_bytes__ = tf.decode_raw(value, tf.uint16, little_endian=True)
result.label = tf.strided_slice(record_bytes__, [0], [label_bytes])
data_img = tf.strided_slice(record_bytes__, [label_bytes], [label_bytes + image_bytes])
depth_major = tf.reshape(data_img, [result.depth, result.height, result.width])
result.uint16image = tf.transpose(depth_major, [1, 2, 0])
return result

我对网络架构进行了修改,以接受各种维度的数据(在下面的示例代码中,它被设置为1(。用我的8位样本,它起作用了。我用16位数据得到的错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1023 values, but the requested shape has 1024
[[Node: data_augmentation/Reshape = Reshape[T=DT_UINT16, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](data_augmentation/StridedSlice_1, data_augmentation/Reshape/shape)]] 

在图的读取或数据论证部分,我的数据似乎丢失了1或2个字节。我的张量缺少最后一个元素。

这是我试图修改的原始输入文件:https://github.com/tensorflow/models/blob/master/tutorials/image/cifar10/cifar10_input.py

我使用的是Windows 10上的Visual Studio 2017和Python 3.5.2。Tensorflow的版本是1.9.0。

我提出了一个解决方案,它使用TFRecords格式(https://www.tensorflow.org/api_guides/python/python_io#tfrecords_format_details)。

该解决方案的灵感来自这个线程:如何将jpeg图像目录转换为tensorflow中的TFRecords文件?

从4D图像阵列([IMG_ID][y][x][BAND_ID](和1D标签阵列创建TFRecords数据集是使用接受答案中引用的convert_to(图像、标签、名称(函数完成的。

根据Tensorflow:的最新API对读取功能进行了修改

def read_TFrecords(filename_queue):
class CIFAR10Record(object):
pass
result = CIFAR10Record()
item_type = tf.uint16
label_bytes = item_type.size  
result.height = 32
result.width = 32
result.depth = 1
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
image = tf.decode_raw(features['image_raw'], item_type)
image = tf.reshape(image, [result.depth, result.height, result.width])
image.set_shape([result.depth, result.height, result.width])
result.uint16image = tf.transpose(image, [1, 2, 0])# tf.cast(image, tf.float32)
result.label = tf.cast(features['label'], tf.int32)
return result

在Tensorflow的高级卷积神经网络教程的输入中(https://github.com/tensorflow/models/blob/master/tutorials/image/cifar10/cifar10_input.py)需要通过从distorted_inputs(data_dir,batch_size(函数中删除以下行来进行进一步的修改:

read_input.label.set_shape([1])

由于它已经作为1D张量从读取器传递。

通过这种方式,我可以将自己的无符号16位整数数据从文件队列馈送到图中。

最新更新