Hike News
Hike News

深度學習-tensorflow基礎-讀取數據-API應用-二進制文件讀取

Introduction

  • 圖片也算是一種二進制文件格式
  • 將進行CIFAR-10二進制數據讀取
    • https://www.cs.toronto.edu/~kriz/cifar.html
    • 全部共60000萬張圖片,每張圖片為32x32彩色圖像所組成
      • 50000個訓練集及10000個測試集
      • 五個訓練批次(data_batch_1.bin, data_batch_2.bin…)
      • 一個測試批次(test_batch.bin)
    • 其有10個類別(飛機、汽車、鳥…等)的圖片
      • 每個類別共6000個圖片
    • 文件中的格式都是第1個像素為標籤(0~9) 剩下的 3072 (3*32*32)像素為特徵
      • 前1024像素為red_channel, 下1024像素為green_channel, 最後1024像素為blue_channel

構造圖片文件隊列

與讀取文字文件的文件隊列相同

構造讀取器

  • 讀取每個記錄是固定數量bytes的二進制文件
  • tf.FixedLengthRecordReader(record_bytes)
    • record_bytes:整型參數,指定每次讀取(一個樣本)為多少bytes
    • 返回一個讀取器實例

method

  • read(file_queue):輸出將是一個文件名(key)和該文件的內容(value)

二進制文件解碼

tf.decode_raw(bytes, out_type, little_endian=None, name=None)

  • bytes:欲解碼的數據,也就是read方法返回的value
  • 將bytes轉換為一個數字向量表示
    • bytes原本為一字符串類型的張量
  • 與函數tf.FixedLengthRecordReader()搭配使用
  • 讀取後優先為uint8類型,可改變out_type參數改變成不同類型
  • 其解碼出來的shape並不是固定,所以須自己額外去固定形狀

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import tensorflow as tf
import os

# 定義cifar數據等命令行參數
FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string("cifar_dir","./cifar-10-batches-bin/","where's cifar data dir?")


class CifarRead(object):
def __init__(self,filelist):
# 文件列表
self.filelist = filelist

# 定義讀取圖片的屬性
self.width = 32
self.height = 32
self.channel = 3

# 二進制文件圖片的bytes
self.label_pixel = 1 # 標籤的bytes
self.image_pixel = self.width * self.height * self.channel
self.whole_image = self.label_pixel + self.image_pixel


def read_and_decode(self):
# 1. 構造文件名隊列
file_queue = tf.train.string_input_producer(self.filelist)

# 2. 構造二進制文件讀取器,讀取內容,cifar-10每個樣本為3073bytes
binary_reader = tf.FixedLengthRecordReader(record_bytes=self.whole_image)

key, value = binary_reader.read(file_queue)

# 3. 解碼內容(進行二進制文件內容的解碼)
label_image = tf.decode_raw(value, out_type=tf.uint8)

# 4. 分割圖片和標籤數據,切出特徵值及目標值
label = tf.slice(label_image, [0], [self.label_pixel])
image = tf.slice(label_image, [self.label_pixel], [self.image_pixel])

# 將label轉換為int32類型
label = tf.cast(label,tf.int32)

# 5. 對圖片的特徵數據進行形狀的改變 [3072] ---> [3, 32, 32]
image_reshape = tf.reshape(image,[self.channel, self.height, self.width ])

# 6. 批處理數據
image_batch, label_batch = tf.train.batch([image_reshape, label], batch_size=10, num_threads=1, capacity=10)

print(image_batch, label_batch)

return image_batch, label_batch


if __name__ == '__main__':
# 1. 找到文件,放入列表 路徑+文件名字 --> 列表當中
file_name = os.listdir(FLAGS.cifar_dir)

# 對二進制文件來說我們只要裡面的bin文件
file_list = [os.path.join(FLAGS.cifar_dir, file) for file in file_name if file.endswith('bin')]

# 實例化我們自定義CifarRead實例用於讀取使用
cfR = CifarRead(file_list)

image_batch, label_batch = cfR.read_and_decode()

with tf.Session() as sess:
# 定義一個線程協調器
coord = tf.train.Coordinator()

# 開啟讀文件的線程
threads = tf.train.start_queue_runners(sess, coord=coord)

# 顯示讀取內容
print(sess.run([image_batch, label_batch]))



# 回收子線程
coord.request_stop()

coord.join(threads)

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
Tensor("batch:0", shape=(10, 32, 32, 3), dtype=uint8) Tensor("batch:1", shape=(10, 1), dtype=int32)
[array([[[[ 26, 17, 13],
[ 13, 13, 14],
[ 14, 15, 14],
...,
[ 12, 15, 21],
[ 36, 26, 22],
[ 17, 25, 31]],

[[ 13, 17, 14],
[ 14, 11, 9],
[ 19, 18, 11],
...,
[174, 229, 249],
[251, 244, 248],
[175, 29, 11]],

[[ 17, 17, 16],
[ 14, 12, 11],
[ 10, 11, 12],
...,
[251, 252, 252],
[242, 240, 160],
[ 18, 14, 16]],

...,

[[105, 40, 58],
[ 79, 79, 84],
[ 47, 5, 0],
...,
[ 69, 20, 21],
[ 21, 20, 23],
[ 26, 29, 25]],

[[139, 168, 57],
[ 4, 8, 21],
[ 34, 72, 105],
...,
[142, 29, 20],
[ 21, 25, 25],
[ 44, 34, 25]],

[[137, 95, 90],
[ 57, 46, 55],
[ 28, 64, 126],
...,
[237, 144, 33],
[ 29, 46, 28],
[ 27, 26, 27]]],


[[[ 94, 101, 95],
[ 94, 94, 97],
[111, 142, 166],
...,
[133, 136, 138],
[140, 142, 145],
[146, 147, 123]],

[[ 84, 88, 101],
[102, 100, 99],
[109, 126, 146],
...,
[154, 132, 124],
[135, 139, 141],
[144, 145, 121]],

[[ 90, 85, 85],
[119, 167, 218],
[224, 219, 207],
...,
[204, 189, 161],
[113, 107, 117],
[137, 137, 115]],

...,

[[193, 191, 178],
[167, 165, 170],
[176, 185, 193],
...,
[181, 176, 175],
[182, 181, 174],
[160, 140, 117]],

[[197, 198, 190],
[180, 178, 182],
[183, 181, 182],
...,
[175, 169, 163],
[161, 151, 144],
[141, 142, 117]],

[[204, 207, 200],
[185, 177, 178],
[181, 189, 195],
...,
[145, 147, 150],
[152, 163, 174],
[182, 184, 155]]],


[[[183, 158, 166],
[167, 169, 171],
[163, 163, 160],
...,
[ 69, 61, 61],
[ 57, 63, 75],
[ 63, 53, 60]],

[[119, 86, 82],
[132, 225, 181],
[ 90, 76, 91],
...,
[ 59, 67, 75],
[ 67, 55, 54],
[ 59, 48, 46]],

[[103, 74, 91],
[161, 225, 230],
[170, 108, 99],
...,
[ 62, 70, 80],
[ 65, 84, 115],
[143, 166, 148]],

...,

[[105, 75, 91],
[ 99, 79, 77],
[ 81, 76, 85],
...,
[ 86, 82, 76],
[ 65, 61, 67],
[ 57, 56, 67]],

[[136, 97, 76],
[ 79, 80, 75],
[ 75, 76, 76],
...,
[ 74, 68, 70],
[ 81, 94, 82],
[ 77, 62, 78]],

[[103, 87, 105],
[107, 111, 103],
[ 94, 84, 104],
...,
[174, 177, 227],
[250, 250, 250],
[250, 250, 250]]],


...,


[[[225, 214, 190],
[167, 169, 184],
[231, 254, 218],
...,
[160, 163, 167],
[165, 161, 157],
[154, 153, 157]],

[[123, 129, 118],
[ 78, 91, 166],
[170, 142, 125],
...,
[167, 171, 179],
[185, 175, 169],
[167, 165, 165]],

[[ 48, 63, 125],
[159, 109, 112],
[167, 167, 149],
...,
[169, 177, 181],
[192, 196, 185],
[180, 174, 178]],

...,

[[ 50, 28, 24],
[ 17, 13, 18],
[ 34, 71, 95],
...,
[140, 125, 153],
[181, 159, 154],
[182, 193, 195]],

[[ 39, 37, 46],
[ 50, 49, 45],
[ 45, 55, 61],
...,
[127, 123, 119],
[120, 123, 125],
[121, 114, 114]],

[[117, 115, 113],
[115, 125, 128],
[126, 128, 132],
...,
[123, 122, 117],
[114, 120, 128],
[144, 167, 171]]],


[[[ 82, 69, 63],
[ 65, 68, 65],
[ 58, 54, 51],
...,
[ 53, 54, 39],
[ 49, 68, 70],
[ 69, 77, 83]],

[[ 76, 74, 59],
[ 53, 56, 61],
[ 61, 57, 50],
...,
[ 39, 54, 56],
[ 42, 50, 65],
[ 73, 78, 83]],

[[ 54, 63, 66],
[ 56, 54, 56],
[ 55, 56, 66],
...,
[ 33, 44, 54],
[ 61, 55, 59],
[ 51, 49, 70]],

...,

[[ 73, 85, 87],
[ 78, 71, 79],
[103, 126, 124],
...,
[ 78, 65, 72],
[ 71, 57, 61],
[ 79, 65, 73]],

[[ 61, 47, 76],
[ 85, 88, 68],
[ 82, 105, 88],
...,
[ 27, 40, 60],
[ 62, 62, 78],
[ 67, 61, 64]],

[[ 81, 70, 65],
[ 66, 85, 96],
[ 91, 110, 118],
...,
[ 55, 69, 82],
[ 84, 80, 78],
[ 67, 57, 68]]],


[[[198, 173, 144],
[124, 96, 58],
[ 52, 56, 52],
...,
[ 47, 54, 56],
[ 53, 51, 46],
[ 46, 40, 45]],

[[185, 158, 134],
[124, 100, 55],
[ 49, 47, 46],
...,
[ 41, 40, 41],
[ 38, 37, 43],
[ 55, 58, 53]],

[[172, 151, 126],
[114, 88, 51],
[ 48, 48, 50],
...,
[ 42, 43, 42],
[ 46, 45, 40],
[ 43, 41, 37]],

...,

[[142, 120, 108],
[102, 74, 46],
[ 44, 45, 44],
...,
[ 56, 39, 34],
[ 30, 33, 51],
[ 61, 52, 42]],

[[146, 121, 107],
[ 94, 68, 44],
[ 39, 37, 39],
...,
[ 65, 55, 42],
[ 56, 51, 43],
[ 58, 57, 44]],

[[156, 126, 92],
[ 64, 60, 41],
[ 37, 36, 35],
...,
[ 40, 45, 46],
[ 49, 50, 49],
[ 40, 31, 26]]]], dtype=uint8), array([[8],
[5],
[0],
[6],
[9],
[2],
[8],
[3],
[6],
[2]], dtype=int32)]