1.cifar10数据集:
数据 - 一个10000x3072 uint8的numpy数组。阵列的每一行存储32x32彩色图像即每一行存储32323=3072个数字信息。前1024个条目包含红色通道值,下一个1024个绿色,最后1024个蓝色。图像以行优先顺序存储,以便数组的前32个条目是图像第一行的红色通道值。
2.流程及实战
1)构造文件名队列
2)读取与解码
2.1)图片切片
第一个[]填起始位置,第二个[]填切多长
2.2)图片的转换
1)图像顺序(由于cifar数据特殊性,所以使得图片解码后为nchw型,必须转为nhwc型才能被tensor学习)
3)批处理队列
开启会话
手动开启线程
代码实现:
import tensorflow as tf
import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
class Cifar():
def __init__(self):
#定义数据,即长宽高通道数,还有种类数,字节数
#长宽高
self.height = 32
self.width = 32
self.channel = 3
#定义图像字节数
self.image = self.channel*self.height*self.width
self.label = 1
self.sample = self.image + self.label
#读取图片处理阶段
def read_binary(self,file_list):
#1.加入文件名列表构造文件名队列
file_queue = tf.train.string_input_producer(file_list)
#2.读码和解码
#读码,注意传入总共的字节:目标+字节
reader = tf.FixedLengthRecordReader(self.sample)
#读取
key,value = reader.read(file_queue)
print(key)
print(value)
#解码
image_decoded = tf.decode_raw(value, tf.uint8)
#3.进行文件分块以及图片转换操作
#分块
#这里就把实际的label和image给弄出来了,定义数据的时候只是定义了一个值可以在后面分片时用
label = tf.slice(image_decoded,[0],[self.label])
image = tf.slice(image_decoded,[self.label],[self.image])
#目标值做好了,而image还是一个需要重新变形的图片字节,所以需要重新对图片塑形,传入的数据就是之前定义的,注意这里是c.w.h
image_reshaped = tf.reshape(image,[self.channel,self.width,self.height])
#图片转换
#运用了转置,且【】【】【】为转置之后的顺序
image_transposed = tf.transpose(image_reshaped, [1, 2, 0])
#print(image_transposed)
#因为解码时候是uint8格式,最后要转换为float32格式
image_cast = tf.cast(image_transposed,tf.float32)
#4.批处理:两个值放入,两个值接受
label_batch,image_batch = tf.train.batch([label,image_cast],capacity=100,batch_size=100,num_threads=1)
print('label_batch',label_batch)
print('image_batch', image_batch)
with tf.Session() as sess:
#开启线程:
# 开启线程协调员,对线程进行管理,注意先协调员后线程开启
coord = tf.train.Coordinator()
# 开启线程:所有文件名队列才会运作,且必需在会话中开启
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
#开启对话:
print('key:',key.eval())
print('value',value.eval())
print('image_transposed',image_transposed.eval())
print('label_batch', label_batch.eval())
print('image_batch', image_batch.eval())
#回收线程
coord.request_stop()
coord.join(threads)
if __name__ == "__main__":
#导入文件列表
filename_list = os.listdir('./cifar-10-batches-bin')
#print(filename_list)
#将路径+文件名进行组合操作,且筛选掉不需要的
file_list = [os.path.join('./cifar-10-batches-bin',i) for i in filename_list if i[-3:] == 'bin']
#print(file_list)
#实例化一个对象将文件传入类
cifar = Cifar()
cifar.read_binary(file_list)
结果
Tensor("ReaderReadV2:0", shape=(), dtype=string)
Tensor("ReaderReadV2:1", shape=(), dtype=string)
label_batch Tensor("batch:0", shape=(100, 1), dtype=uint8)
image_batch Tensor("batch:1", shape=(100, 32, 32, 3), dtype=float32)
key: b'./cifar-10-batches-bin\\data_batch_3.bin:0'
value b'\x00\xb7\x9e\xa6\xa7\xa9\xab\xa3\xa3\xa0\xa1\xa1\x9b\x9d\x9f\x9c\x9a\x9c\x96\x92\x96\x97\x97\x99\x9b\x98\x92\x91\x94\x93\x8e\x89\x90}adk\x9b|_^V[ZU]ZYZUJEIMNXWREBDB?;CkUXs\xcb\x95TOX\\ZX[]^VTIJOMJKE==9?K?5<wVR\x84\xe1\xb5ZL[`aXY^[PPNRSLGB<=A:@J7(/\x7fRL\x90\xe2\xcdqMXYSRRMKKHMNQJC<8?D>986(,sKP\x9b\xdf\xdd\x8aU[YPLLGKQQQOQME>;CKC76;0.gJ[\xa1\xe1\xe6\xaalc[RORNKOTTSNHABBHL<78:<AeMZ\x9e\xe3\xe6\xc4qTRMJKMHDPYf]RMF@BK;:G]swlWV\xa2\xde\xdb\xd3\xa1aLKKAKSYjo\x81m_eM>FPATs\x8f\xa6\x94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8a\x87\x86\x88\x8b\x89\x86\x82\x85\x85\x80xolgOB29/+\x89\x8a\x92\x85nGD}\xab\x92\x87\x8b\x8a\x89\x89\x89\x88\x88\x86\x88\x88\x83\x7f~ylA.!\x1e$,\xa5\x8eqaY<Bp\xa6\xa5\x8d\x81\x80}~}\x83\x84\x83\x82}\x82\x82\x80~pM. \'#3{^\\b\\KQ^rnoqkcbajmptx\x81~}xh]B&;%8y`dliakqg[Xcwspokia_grxvq^YD#3&:z[]onoojb`]Xs\x8c\x89\x88\x84{nbZ[`cbVL-\x1e"/Ataixqog`ZZaZY\x7f\x84xy|}ye[TTR@-\x1a\x1e#2@zmlokf`^SPWY`ku}\x89\x88\x84\x82pfZGA*\x1d\x1b\x1a,4Axc_a`djmikw\x86\x8e\x82{z~{wzr]F3("\x1f\x1d(F9GjU`ddu\x8d\x9e\x9f\x9d\x92\x84\x7fsi]TPJDC?A7.3.3FV<Clchkkmqlc^^bURNB>BQPSTQG?CI?H`BCxexz`^jficqxcimhYTksngbPEEGDVJ:B~^mh\\_ipzs\x80vtnm\x85niwvyxzhYODDU?:B\x8alchjigicj}isnk\x89~syz|\x86\x7fog[MEJ@AJ\x9bychh_dd[ntbcmsxzvohctgXTQKFBI]c\x81a]f^Wksn\x84th^flklm__b]YTMNQPMT[ciQ_c]Uiv{\x8a{vdaai\x81p]o|mYVPT_k^WIYtk\x82\x81\x82{sn\x87\x91\x92\x8a\x82kfv\x81uv\x81\x8axhgoz{yi}se\xcd\xcb\xd6\xd6\xd7\xd4\xc5\xad\xba\xc0\xc8\xc2\xc0\xb4\xaf\xb0\xae\xb1\xbb\xb5\xb7\xae\xaf\xa9\xab\xc7\xce\xc8\xc4\xd1\xcd\xc2\xff\xfc\xfd\xfc\xfd\xfe\xf0\xc4\xc1\xc0\xc1\xca\xc7\xbd\xbf\xc2\xbf\xc4\xcc\xc0\xca\xbe\xbb\xb0\xb3\xe6\xfc\xfc\xfc\xfb\xfa\xfb\xb2\x98\xa0\x9e\x96\x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