def read_data(file_queue):
"""
Data is saved in binary files.
Each row has:
1st byte -> label
2nd-last byte -> 3D Volume [height, width, depth, channels]
Args:
file_queue -> a queue of file names saved as strings
Rtns:
An object with:
height -> volume height
width -> volume width
depth -> volume depth
nChan -> number of channels
key -> scalar tensor with file name and record number
label -> 1D int32 tensor with the associated label
img3_uint8 -> 4D uint8 tensor with image data
"""
class record_data(object):
pass
img3_obj = record_data()
# Dimensions of data
label_bytes = 1
img3_obj.height = CFG['height']
img3_obj.width = CFG['width']
img3_obj.depth = CFG['depth']
img3_obj.nChan = CFG['nChan']
# Size in memory
img3_bytes = img3_obj.height*img3_obj.width*img3_obj.depth*img3_obj.nChan
record_bytes = label_bytes + img3_bytes
# Read a record
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
img3_obj.key,value = reader.read(file_queue)
# Convert from a string to a vector of uint8 that is record_bytes long
record_bytes = tf.decode_raw(value, tf.uint8)
# First byte represent the label, which we convert from uint8 -> int32
img3_obj.label = tf.cast(tf.slice(record_bytes,[0],[label_bytes]),tf.int32)
# Remaining bytes after the label represent the image, which we reshape from
# [depth * height * width] to [depth,height, width]
depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes],
[img3_bytes]),[img3_obj.depth,img3_obj.height,img3_obj.width,
img3_obj.nChan])
img3_obj.img3_uint8 = tf.transpose(depth_major,[2,1,0,3])
return img3_obj
评论列表
文章目录