質問編集履歴
2
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,6 +4,136 @@
|
|
4
4
|
|
5
5
|
```ここに言語を入力
|
6
6
|
|
7
|
+
# -*- coding: utf-8 -*-
|
8
|
+
|
9
|
+
from __future__ import absolute_import
|
10
|
+
|
11
|
+
from __future__ import division
|
12
|
+
|
13
|
+
from __future__ import print_function
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
import os
|
18
|
+
|
19
|
+
import numpy as np
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
class Cifar10Record(object):
|
24
|
+
|
25
|
+
width = 32
|
26
|
+
|
27
|
+
height = 32
|
28
|
+
|
29
|
+
depth = 3
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def set_label(self,label_byte):
|
34
|
+
|
35
|
+
#self.label = np.frombuffer(label_byte,dtype=np.unit8)
|
36
|
+
|
37
|
+
self.label = np.frombuffer(label_byte,dtype=np.uint8) # unit8 -> uint8に修正
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def set_image(self,image_bytes):
|
42
|
+
|
43
|
+
byte_buffer = np.frombuffer(image_bytes,dtype=np.int8)
|
44
|
+
|
45
|
+
reshaped_array = np.reshape(byte_buffer,[self.depth,self.height,self.width])
|
46
|
+
|
47
|
+
self.byte_array = np.transpose(reshaped_array,[1,2,0])
|
48
|
+
|
49
|
+
self.byte_array = self.byte_array.astype(np.float32)
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
class Cifar10Reader(object):
|
54
|
+
|
55
|
+
def __init__(self,filename):
|
56
|
+
|
57
|
+
if not os.path.exists(filename):
|
58
|
+
|
59
|
+
print(filename + ' is not exist')
|
60
|
+
|
61
|
+
return
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
self.bytestream = open(filename,mode="rb")
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def close(self):
|
70
|
+
|
71
|
+
if not self.bytestream:
|
72
|
+
|
73
|
+
self.bytestream.close()
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
def read(self,index):
|
78
|
+
|
79
|
+
result = Cifar10Record()
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
label_bytes = 1
|
84
|
+
|
85
|
+
image_bytes = result.height * result.width * result.depth
|
86
|
+
|
87
|
+
record_bytes = label_bytes + image_bytes
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
self.bytestream.seek(record_bytes * index,0)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
result.set_label(self.bytestream.read(label_bytes))
|
96
|
+
|
97
|
+
result.set_image(self.bytestream.read(image_bytes))
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
return result
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
print(self.bytestream)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
# 追加
|
110
|
+
|
111
|
+
reader = Cifar10Reader("lena_std.tif")
|
112
|
+
|
113
|
+
ret = reader.read(0)
|
114
|
+
|
115
|
+
print(ret)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
というCIFAR-10形式のデータセットを読み込むプログラム を書きました。
|
124
|
+
|
125
|
+
参考url:
|
126
|
+
|
127
|
+
http://www.buildinsider.net/small/booktensorflow/0201
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
他に使用しているスクリプトは
|
132
|
+
|
133
|
+
png10.py
|
134
|
+
|
135
|
+
```ここに言語を入力
|
136
|
+
|
7
137
|
# coding: utf-8
|
8
138
|
|
9
139
|
|
@@ -18,103 +148,71 @@
|
|
18
148
|
|
19
149
|
import os
|
20
150
|
|
151
|
+
|
152
|
+
|
21
153
|
import numpy as np
|
22
154
|
|
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
|
-
label_bytes = 1
|
84
|
-
|
85
|
-
image_bytes = result.height * result.width * result.depth
|
86
|
-
|
87
|
-
record_bytes = label_bytes + image_bytes
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
self.bytestream.seek(record_bytes * index,0)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
result.set_label(self.bytestream.read(label_bytes))
|
96
|
-
|
97
|
-
result.set_image(self.bytestream.read(image_bytes))
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
return result
|
102
|
-
|
103
|
-
|
155
|
+
import tensorflow as tf
|
156
|
+
|
157
|
+
from PIL import Image
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
from reader import Cifar10Reader
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
FLAGS = tf.app.flags.FLAGS
|
166
|
+
|
167
|
+
tf.app.flags.DEFINE_string('file',None,"処理するファイルのパス")
|
168
|
+
|
169
|
+
tf.app.flags.DEFINE_integer('offset',0,"読み飛ばすレコード数")
|
170
|
+
|
171
|
+
tf.app.flags.DEFINE_integer('length',16,"読み込んで変換するレコード数")
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
basename = os.path.basename(FLAGS.file)
|
176
|
+
|
177
|
+
path = os.path.dirname(FLAGS.file)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
reader = Cifar10Reader(FLAGS.file)
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
stop = FLAGS.offset + FLAGS.length
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
for index in range(FLAGS.offset,stop):
|
190
|
+
|
191
|
+
image = reader.read(index)
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
print('label: %d' % image.label)
|
196
|
+
|
197
|
+
imageshow = Image.fromarray(image.byte_array.astype(np.unit8))
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
file_name = '%s-%02d-%d.png' % (basename,index,image.label)
|
202
|
+
|
203
|
+
file = os.path.join(path,file_name)
|
204
|
+
|
205
|
+
with open(file,mode='wb') as out:
|
206
|
+
|
207
|
+
imageshow.save(out,format='png')
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
reader.close()
|
104
212
|
|
105
213
|
```
|
106
214
|
|
107
|
-
というCIFAR-10形式のデータセットを読み込むプログラム を書きました。
|
108
|
-
|
109
|
-
参考url:
|
110
|
-
|
111
|
-
http://www.buildinsider.net/small/booktensorflow/0201
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
他に使用しているスクリプトは
|
116
|
-
|
117
|
-
|
215
|
+
model.py
|
118
216
|
|
119
217
|
```ここに言語を入力
|
120
218
|
|
@@ -130,15 +228,169 @@
|
|
130
228
|
|
131
229
|
|
132
230
|
|
231
|
+
import tensorflow as tf
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
NUM_CLASSES = 10
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
def _get_weights(shape,stddev=1.0):
|
240
|
+
|
241
|
+
var = tf.get_variable(
|
242
|
+
|
243
|
+
'weights',
|
244
|
+
|
245
|
+
shape,
|
246
|
+
|
247
|
+
initializer=tf.truncated_normal_initializer(stddev=stddev)
|
248
|
+
|
249
|
+
)
|
250
|
+
|
251
|
+
return var
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def _get_biases(shape,value=0.0):
|
256
|
+
|
257
|
+
var = tf.get_variable(
|
258
|
+
|
259
|
+
'biases',
|
260
|
+
|
261
|
+
shape,
|
262
|
+
|
263
|
+
initializer=tf.constant_initializer(value)
|
264
|
+
|
265
|
+
)
|
266
|
+
|
267
|
+
return var
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
def inference(image_node):
|
272
|
+
|
273
|
+
# conv1
|
274
|
+
|
275
|
+
with tf.variable_scope('conv1') as scope:
|
276
|
+
|
277
|
+
weights = _get_weights(shape=[5,5,3,64],stddev=1e-4)
|
278
|
+
|
279
|
+
conv = tf.nn.conv2d(image_node,weights,[1,1,1,1],padding='SAME')
|
280
|
+
|
281
|
+
biases = _get_biases([64],value=0.1)
|
282
|
+
|
283
|
+
bias = tf.nn.bias_add(conv,biases)
|
284
|
+
|
285
|
+
conv1 = tf.nn.relu(bias,name=scope.name)
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
# pool
|
290
|
+
|
291
|
+
pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool1')
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
# conv2
|
296
|
+
|
297
|
+
with tf.variable_scope('conv2') as scope:
|
298
|
+
|
299
|
+
weights = _get_weights(shape=[5,5,64,64],stddev=1e-4)
|
300
|
+
|
301
|
+
conv = tf.nn.conv2d(pool1,weights,[1,1,1,1],padding='SAME')
|
302
|
+
|
303
|
+
biases = _get_biases([64],value=0.1)
|
304
|
+
|
305
|
+
bias = tf.nn.bias_add(conv,biases)
|
306
|
+
|
307
|
+
conv2 = tf.nn.relu(bias,name=scope.name)
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
# pool2
|
312
|
+
|
313
|
+
pool2 = tf.nn.max_pool(conv2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool2')
|
314
|
+
|
315
|
+
reshape = tf.reshape(pool2,[1,-1])
|
316
|
+
|
317
|
+
dim = reshape.get_shape()[1].value
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
# fc3
|
322
|
+
|
323
|
+
with tf.variable_scope('fc3') as scope:
|
324
|
+
|
325
|
+
weights = _get_weights(shape=[dim,384],stddev=0.04)
|
326
|
+
|
327
|
+
biases = _get_biases([384],value=0.1)
|
328
|
+
|
329
|
+
fc3 = tf.nn.relu(
|
330
|
+
|
331
|
+
tf.matmul(reshape,weights) + biases,
|
332
|
+
|
333
|
+
name=scope.name
|
334
|
+
|
335
|
+
)
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
# fc4
|
340
|
+
|
341
|
+
with tf.variable_scope('fc4') as scope:
|
342
|
+
|
343
|
+
weights = _get_weights(shape=[384,192],stddev=0.04)
|
344
|
+
|
345
|
+
biases = _get_biases([192],value=0.1)
|
346
|
+
|
347
|
+
fc4 = tf.nn.relu(tf.matmul(fc3,weights) + biases,name=scope.name)
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
# output
|
352
|
+
|
353
|
+
with tf.variable_scope('output') as scope:
|
354
|
+
|
355
|
+
weights = _get_weights(shape=[192,NUM_CLASSES],stddev=1/192.0)
|
356
|
+
|
357
|
+
biases = _get_biases([NUM_CLASSES],value=0.0)
|
358
|
+
|
359
|
+
logits = tf.add(tf.matmul(fc4,weights),biases,name='logits')
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
return logits
|
366
|
+
|
367
|
+
```
|
368
|
+
|
369
|
+
inference.py
|
370
|
+
|
371
|
+
```ここに言語を入力
|
372
|
+
|
373
|
+
# coding: utf-8
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
from __future__ import absolute_import
|
378
|
+
|
379
|
+
from __future__ import division
|
380
|
+
|
381
|
+
from __future__ import print_function
|
382
|
+
|
383
|
+
|
384
|
+
|
133
385
|
import os
|
134
386
|
|
135
|
-
|
136
|
-
|
137
|
-
import
|
387
|
+
import time
|
138
388
|
|
139
389
|
import tensorflow as tf
|
140
390
|
|
391
|
+
|
392
|
+
|
141
|
-
|
393
|
+
import model as model
|
142
394
|
|
143
395
|
|
144
396
|
|
@@ -148,358 +400,122 @@
|
|
148
400
|
|
149
401
|
FLAGS = tf.app.flags.FLAGS
|
150
402
|
|
151
|
-
tf.app.flags.DEFINE_string('file',None,"処理するファイルのパス")
|
152
|
-
|
153
|
-
tf.app.flags.DEFINE_integer('o
|
403
|
+
tf.app.flags.DEFINE_integer('epoch',30,"訓練するEpoch数")
|
154
|
-
|
404
|
+
|
155
|
-
tf.app.flags.DEFINE_in
|
405
|
+
tf.app.flags.DEFINE_string('data_dir','./data/',"訓練データのディレクトリ")
|
406
|
+
|
156
|
-
|
407
|
+
tf.app.flags.DEFINE_string('checkpoint_dir','./checkpoints/',"チェックポイントを保存するディレクトリ")
|
408
|
+
|
409
|
+
|
410
|
+
|
157
|
-
|
411
|
+
filenames = [
|
412
|
+
|
158
|
-
|
413
|
+
os.path.join(FLAGS.data_dir,'data_batch_%d.bin' % i) for i in range(1,6)
|
414
|
+
|
415
|
+
]
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
def main(argv=None):
|
422
|
+
|
423
|
+
train_placeholder = tf.placeholder(tf.float32,shape=[32,32,3],name='input_image')
|
424
|
+
|
159
|
-
|
425
|
+
image_node = tf.expand_dims(train_placeholder,0)
|
160
|
-
|
426
|
+
|
427
|
+
|
428
|
+
|
161
|
-
|
429
|
+
logits = model.inference(image_node)
|
430
|
+
|
431
|
+
|
432
|
+
|
162
|
-
|
433
|
+
with tf.Session() as sess:
|
434
|
+
|
163
|
-
|
435
|
+
sess.run(tf.initialize_all_variables())
|
436
|
+
|
437
|
+
|
438
|
+
|
164
|
-
|
439
|
+
total_duration = 0
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
for epoch in range(1,FLAGS.epoch+1):
|
444
|
+
|
445
|
+
start_time = time.time()
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
for file_index in range(5):
|
450
|
+
|
451
|
+
print('Epoch %d: %s' % (epoch,filenames[file_index]))
|
452
|
+
|
165
|
-
reader = Cifar10Reader(
|
453
|
+
reader = Cifar10Reader(filenames[file_index])
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
454
|
+
|
170
|
-
|
171
|
-
|
172
|
-
|
455
|
+
|
456
|
+
|
173
|
-
for index in range(
|
457
|
+
for index in range(10000):
|
458
|
+
|
459
|
+
image = reader.read(index)
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
logits_value = sess.run([logits],feed_dict={
|
464
|
+
|
465
|
+
train_placeholder:image.byte_array,
|
466
|
+
|
467
|
+
})
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
if index % 1000 ==0:
|
472
|
+
|
473
|
+
print('[%d]: %r'% (image.label,logits_value))
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
reader.close()
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
duration = time.time() - start_time
|
482
|
+
|
483
|
+
total_duration += duration
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
print('epoch %d duration = %d sec'%(epoch,duration))
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
tf.train.SummaryWriter(FLAGS.checkpoint_dir,sess.graph)
|
492
|
+
|
493
|
+
print('Total duration = %d sec'% total_duration)
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
if __name__ == '__main__':
|
498
|
+
|
499
|
+
tf.app.run()
|
500
|
+
|
501
|
+
```
|
502
|
+
|
503
|
+
しかし、inference.py を実行すると
|
504
|
+
|
505
|
+
```ここに言語を入力
|
506
|
+
|
507
|
+
File "inference.py", line 44, in main
|
174
508
|
|
175
509
|
image = reader.read(index)
|
176
510
|
|
177
|
-
|
511
|
+
File "/Users/XXX/Desktop/cifar/reader.py", line 43, in read
|
178
|
-
|
512
|
+
|
179
|
-
|
513
|
+
self.bytestream.seek(record_bytes * index,0)
|
180
|
-
|
181
|
-
|
514
|
+
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
515
|
+
AttributeError: 'Cifar10Reader' object has no attribute 'bytestream'
|
186
|
-
|
187
|
-
file = os.path.join(path,file_name)
|
188
|
-
|
189
|
-
with open(file,mode='wb') as out:
|
190
|
-
|
191
|
-
imageshow.save(out,format='png')
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
reader.close()
|
196
516
|
|
197
517
|
```
|
198
518
|
|
199
|
-
model.py
|
200
|
-
|
201
|
-
```ここに言語を入力
|
202
|
-
|
203
|
-
# coding: utf-8
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
from __future__ import absolute_import
|
208
|
-
|
209
|
-
from __future__ import division
|
210
|
-
|
211
|
-
from __future__ import print_function
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
import tensorflow as tf
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
NUM_CLASSES = 10
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
def _get_weights(shape,stddev=1.0):
|
224
|
-
|
225
|
-
var = tf.get_variable(
|
226
|
-
|
227
|
-
'weights',
|
228
|
-
|
229
|
-
shape,
|
230
|
-
|
231
|
-
initializer=tf.truncated_normal_initializer(stddev=stddev)
|
232
|
-
|
233
|
-
)
|
234
|
-
|
235
|
-
return var
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
def _get_biases(shape,value=0.0):
|
240
|
-
|
241
|
-
var = tf.get_variable(
|
242
|
-
|
243
|
-
'biases',
|
244
|
-
|
245
|
-
shape,
|
246
|
-
|
247
|
-
initializer=tf.constant_initializer(value)
|
248
|
-
|
249
|
-
)
|
250
|
-
|
251
|
-
return var
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
def inference(image_node):
|
256
|
-
|
257
|
-
# conv1
|
258
|
-
|
259
|
-
with tf.variable_scope('conv1') as scope:
|
260
|
-
|
261
|
-
weights = _get_weights(shape=[5,5,3,64],stddev=1e-4)
|
262
|
-
|
263
|
-
conv = tf.nn.conv2d(image_node,weights,[1,1,1,1],padding='SAME')
|
264
|
-
|
265
|
-
biases = _get_biases([64],value=0.1)
|
266
|
-
|
267
|
-
bias = tf.nn.bias_add(conv,biases)
|
268
|
-
|
269
|
-
conv1 = tf.nn.relu(bias,name=scope.name)
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
# pool
|
274
|
-
|
275
|
-
pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool1')
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
# conv2
|
280
|
-
|
281
|
-
with tf.variable_scope('conv2') as scope:
|
282
|
-
|
283
|
-
weights = _get_weights(shape=[5,5,64,64],stddev=1e-4)
|
284
|
-
|
285
|
-
conv = tf.nn.conv2d(pool1,weights,[1,1,1,1],padding='SAME')
|
286
|
-
|
287
|
-
biases = _get_biases([64],value=0.1)
|
288
|
-
|
289
|
-
bias = tf.nn.bias_add(conv,biases)
|
290
|
-
|
291
|
-
conv2 = tf.nn.relu(bias,name=scope.name)
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
# pool2
|
296
|
-
|
297
|
-
pool2 = tf.nn.max_pool(conv2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool2')
|
298
|
-
|
299
|
-
reshape = tf.reshape(pool2,[1,-1])
|
300
|
-
|
301
|
-
dim = reshape.get_shape()[1].value
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
# fc3
|
306
|
-
|
307
|
-
with tf.variable_scope('fc3') as scope:
|
308
|
-
|
309
|
-
weights = _get_weights(shape=[dim,384],stddev=0.04)
|
310
|
-
|
311
|
-
biases = _get_biases([384],value=0.1)
|
312
|
-
|
313
|
-
fc3 = tf.nn.relu(
|
314
|
-
|
315
|
-
tf.matmul(reshape,weights) + biases,
|
316
|
-
|
317
|
-
name=scope.name
|
318
|
-
|
319
|
-
)
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
# fc4
|
324
|
-
|
325
|
-
with tf.variable_scope('fc4') as scope:
|
326
|
-
|
327
|
-
weights = _get_weights(shape=[384,192],stddev=0.04)
|
328
|
-
|
329
|
-
biases = _get_biases([192],value=0.1)
|
330
|
-
|
331
|
-
fc4 = tf.nn.relu(tf.matmul(fc3,weights) + biases,name=scope.name)
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
# output
|
336
|
-
|
337
|
-
with tf.variable_scope('output') as scope:
|
338
|
-
|
339
|
-
weights = _get_weights(shape=[192,NUM_CLASSES],stddev=1/192.0)
|
340
|
-
|
341
|
-
biases = _get_biases([NUM_CLASSES],value=0.0)
|
342
|
-
|
343
|
-
logits = tf.add(tf.matmul(fc4,weights),biases,name='logits')
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
return logits
|
350
|
-
|
351
|
-
```
|
352
|
-
|
353
|
-
inference.py
|
354
|
-
|
355
|
-
```ここに言語を入力
|
356
|
-
|
357
|
-
# coding: utf-8
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
from __future__ import absolute_import
|
362
|
-
|
363
|
-
from __future__ import division
|
364
|
-
|
365
|
-
from __future__ import print_function
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
import os
|
370
|
-
|
371
|
-
import time
|
372
|
-
|
373
|
-
import tensorflow as tf
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
import model as model
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
from reader import Cifar10Reader
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
FLAGS = tf.app.flags.FLAGS
|
386
|
-
|
387
|
-
tf.app.flags.DEFINE_integer('epoch',30,"訓練するEpoch数")
|
388
|
-
|
389
|
-
tf.app.flags.DEFINE_string('data_dir','./data/',"訓練データのディレクトリ")
|
390
|
-
|
391
|
-
tf.app.flags.DEFINE_string('checkpoint_dir','./checkpoints/',"チェックポイントを保存するディレクトリ")
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
filenames = [
|
396
|
-
|
397
|
-
os.path.join(FLAGS.data_dir,'data_batch_%d.bin' % i) for i in range(1,6)
|
398
|
-
|
399
|
-
]
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
def main(argv=None):
|
406
|
-
|
407
|
-
train_placeholder = tf.placeholder(tf.float32,shape=[32,32,3],name='input_image')
|
408
|
-
|
409
|
-
image_node = tf.expand_dims(train_placeholder,0)
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
logits = model.inference(image_node)
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
with tf.Session() as sess:
|
418
|
-
|
419
|
-
sess.run(tf.initialize_all_variables())
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
total_duration = 0
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
for epoch in range(1,FLAGS.epoch+1):
|
428
|
-
|
429
|
-
start_time = time.time()
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
for file_index in range(5):
|
434
|
-
|
435
|
-
print('Epoch %d: %s' % (epoch,filenames[file_index]))
|
436
|
-
|
437
|
-
reader = Cifar10Reader(filenames[file_index])
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
for index in range(10000):
|
442
|
-
|
443
|
-
image = reader.read(index)
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
logits_value = sess.run([logits],feed_dict={
|
448
|
-
|
449
|
-
train_placeholder:image.byte_array,
|
450
|
-
|
451
|
-
})
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
if index % 1000 ==0:
|
456
|
-
|
457
|
-
print('[%d]: %r'% (image.label,logits_value))
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
reader.close()
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
duration = time.time() - start_time
|
466
|
-
|
467
|
-
total_duration += duration
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
print('epoch %d duration = %d sec'%(epoch,duration))
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
tf.train.SummaryWriter(FLAGS.checkpoint_dir,sess.graph)
|
476
|
-
|
477
|
-
print('Total duration = %d sec'% total_duration)
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
if __name__ == '__main__':
|
482
|
-
|
483
|
-
tf.app.run()
|
484
|
-
|
485
|
-
```
|
486
|
-
|
487
|
-
しかし、inference.py を実行すると
|
488
|
-
|
489
|
-
```ここに言語を入力
|
490
|
-
|
491
|
-
File "inference.py", line 44, in main
|
492
|
-
|
493
|
-
image = reader.read(index)
|
494
|
-
|
495
|
-
File "/Users/XXX/Desktop/cifar/reader.py", line 43, in read
|
496
|
-
|
497
|
-
self.bytestream.seek(record_bytes * index,0)
|
498
|
-
|
499
|
-
AttributeError: 'Cifar10Reader' object has no attribute 'bytestream'
|
500
|
-
|
501
|
-
```
|
502
|
-
|
503
519
|
というエラーが出てしまいました。
|
504
520
|
|
505
521
|
Cifar10Reader にbytestream を引数として持たせなければならない、という意味ですよね?でも参考urlの書き方ではCifar10Readerクラスの引数にbytestream を指定していません。
|
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
AttributeError: 'Cifar10Reader' object has no attribute 'bytestream' のエラー が出ました。
|
2
2
|
|
3
|
+
reader.py
|
4
|
+
|
3
5
|
```ここに言語を入力
|
4
6
|
|
5
7
|
# coding: utf-8
|
@@ -110,15 +112,387 @@
|
|
110
112
|
|
111
113
|
|
112
114
|
|
115
|
+
他に使用しているスクリプトは
|
116
|
+
|
113
|
-
|
117
|
+
png10.py
|
114
118
|
|
115
119
|
```ここに言語を入力
|
116
120
|
|
121
|
+
# coding: utf-8
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
from __future__ import absolute_import
|
126
|
+
|
127
|
+
from __future__ import division
|
128
|
+
|
129
|
+
from __future__ import print_function
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
import os
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
import numpy as np
|
138
|
+
|
139
|
+
import tensorflow as tf
|
140
|
+
|
141
|
+
from PIL import Image
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
from reader import Cifar10Reader
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
FLAGS = tf.app.flags.FLAGS
|
150
|
+
|
151
|
+
tf.app.flags.DEFINE_string('file',None,"処理するファイルのパス")
|
152
|
+
|
153
|
+
tf.app.flags.DEFINE_integer('offset',0,"読み飛ばすレコード数")
|
154
|
+
|
155
|
+
tf.app.flags.DEFINE_integer('length',16,"読み込んで変換するレコード数")
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
basename = os.path.basename(FLAGS.file)
|
160
|
+
|
161
|
+
path = os.path.dirname(FLAGS.file)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
reader = Cifar10Reader(FLAGS.file)
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
stop = FLAGS.offset + FLAGS.length
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
for index in range(FLAGS.offset,stop):
|
174
|
+
|
175
|
+
image = reader.read(index)
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
print('label: %d' % image.label)
|
180
|
+
|
181
|
+
imageshow = Image.fromarray(image.byte_array.astype(np.unit8))
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
file_name = '%s-%02d-%d.png' % (basename,index,image.label)
|
186
|
+
|
187
|
+
file = os.path.join(path,file_name)
|
188
|
+
|
189
|
+
with open(file,mode='wb') as out:
|
190
|
+
|
191
|
+
imageshow.save(out,format='png')
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
reader.close()
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
model.py
|
200
|
+
|
201
|
+
```ここに言語を入力
|
202
|
+
|
203
|
+
# coding: utf-8
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
from __future__ import absolute_import
|
208
|
+
|
209
|
+
from __future__ import division
|
210
|
+
|
211
|
+
from __future__ import print_function
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
import tensorflow as tf
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
NUM_CLASSES = 10
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
def _get_weights(shape,stddev=1.0):
|
224
|
+
|
225
|
+
var = tf.get_variable(
|
226
|
+
|
227
|
+
'weights',
|
228
|
+
|
229
|
+
shape,
|
230
|
+
|
231
|
+
initializer=tf.truncated_normal_initializer(stddev=stddev)
|
232
|
+
|
233
|
+
)
|
234
|
+
|
235
|
+
return var
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
def _get_biases(shape,value=0.0):
|
240
|
+
|
241
|
+
var = tf.get_variable(
|
242
|
+
|
243
|
+
'biases',
|
244
|
+
|
245
|
+
shape,
|
246
|
+
|
247
|
+
initializer=tf.constant_initializer(value)
|
248
|
+
|
249
|
+
)
|
250
|
+
|
251
|
+
return var
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def inference(image_node):
|
256
|
+
|
257
|
+
# conv1
|
258
|
+
|
259
|
+
with tf.variable_scope('conv1') as scope:
|
260
|
+
|
261
|
+
weights = _get_weights(shape=[5,5,3,64],stddev=1e-4)
|
262
|
+
|
263
|
+
conv = tf.nn.conv2d(image_node,weights,[1,1,1,1],padding='SAME')
|
264
|
+
|
265
|
+
biases = _get_biases([64],value=0.1)
|
266
|
+
|
267
|
+
bias = tf.nn.bias_add(conv,biases)
|
268
|
+
|
269
|
+
conv1 = tf.nn.relu(bias,name=scope.name)
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
# pool
|
274
|
+
|
275
|
+
pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool1')
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
# conv2
|
280
|
+
|
281
|
+
with tf.variable_scope('conv2') as scope:
|
282
|
+
|
283
|
+
weights = _get_weights(shape=[5,5,64,64],stddev=1e-4)
|
284
|
+
|
285
|
+
conv = tf.nn.conv2d(pool1,weights,[1,1,1,1],padding='SAME')
|
286
|
+
|
287
|
+
biases = _get_biases([64],value=0.1)
|
288
|
+
|
289
|
+
bias = tf.nn.bias_add(conv,biases)
|
290
|
+
|
291
|
+
conv2 = tf.nn.relu(bias,name=scope.name)
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
# pool2
|
296
|
+
|
297
|
+
pool2 = tf.nn.max_pool(conv2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool2')
|
298
|
+
|
299
|
+
reshape = tf.reshape(pool2,[1,-1])
|
300
|
+
|
301
|
+
dim = reshape.get_shape()[1].value
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
# fc3
|
306
|
+
|
307
|
+
with tf.variable_scope('fc3') as scope:
|
308
|
+
|
309
|
+
weights = _get_weights(shape=[dim,384],stddev=0.04)
|
310
|
+
|
311
|
+
biases = _get_biases([384],value=0.1)
|
312
|
+
|
313
|
+
fc3 = tf.nn.relu(
|
314
|
+
|
315
|
+
tf.matmul(reshape,weights) + biases,
|
316
|
+
|
317
|
+
name=scope.name
|
318
|
+
|
319
|
+
)
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
# fc4
|
324
|
+
|
325
|
+
with tf.variable_scope('fc4') as scope:
|
326
|
+
|
327
|
+
weights = _get_weights(shape=[384,192],stddev=0.04)
|
328
|
+
|
329
|
+
biases = _get_biases([192],value=0.1)
|
330
|
+
|
331
|
+
fc4 = tf.nn.relu(tf.matmul(fc3,weights) + biases,name=scope.name)
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
# output
|
336
|
+
|
337
|
+
with tf.variable_scope('output') as scope:
|
338
|
+
|
339
|
+
weights = _get_weights(shape=[192,NUM_CLASSES],stddev=1/192.0)
|
340
|
+
|
341
|
+
biases = _get_biases([NUM_CLASSES],value=0.0)
|
342
|
+
|
343
|
+
logits = tf.add(tf.matmul(fc4,weights),biases,name='logits')
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
return logits
|
350
|
+
|
351
|
+
```
|
352
|
+
|
353
|
+
inference.py
|
354
|
+
|
355
|
+
```ここに言語を入力
|
356
|
+
|
357
|
+
# coding: utf-8
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
from __future__ import absolute_import
|
362
|
+
|
363
|
+
from __future__ import division
|
364
|
+
|
365
|
+
from __future__ import print_function
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
import os
|
370
|
+
|
371
|
+
import time
|
372
|
+
|
373
|
+
import tensorflow as tf
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
import model as model
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
from reader import Cifar10Reader
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
FLAGS = tf.app.flags.FLAGS
|
386
|
+
|
387
|
+
tf.app.flags.DEFINE_integer('epoch',30,"訓練するEpoch数")
|
388
|
+
|
389
|
+
tf.app.flags.DEFINE_string('data_dir','./data/',"訓練データのディレクトリ")
|
390
|
+
|
391
|
+
tf.app.flags.DEFINE_string('checkpoint_dir','./checkpoints/',"チェックポイントを保存するディレクトリ")
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
filenames = [
|
396
|
+
|
397
|
+
os.path.join(FLAGS.data_dir,'data_batch_%d.bin' % i) for i in range(1,6)
|
398
|
+
|
399
|
+
]
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
def main(argv=None):
|
406
|
+
|
407
|
+
train_placeholder = tf.placeholder(tf.float32,shape=[32,32,3],name='input_image')
|
408
|
+
|
409
|
+
image_node = tf.expand_dims(train_placeholder,0)
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
logits = model.inference(image_node)
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
with tf.Session() as sess:
|
418
|
+
|
419
|
+
sess.run(tf.initialize_all_variables())
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
total_duration = 0
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
for epoch in range(1,FLAGS.epoch+1):
|
428
|
+
|
429
|
+
start_time = time.time()
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
for file_index in range(5):
|
434
|
+
|
435
|
+
print('Epoch %d: %s' % (epoch,filenames[file_index]))
|
436
|
+
|
437
|
+
reader = Cifar10Reader(filenames[file_index])
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
for index in range(10000):
|
442
|
+
|
443
|
+
image = reader.read(index)
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
logits_value = sess.run([logits],feed_dict={
|
448
|
+
|
449
|
+
train_placeholder:image.byte_array,
|
450
|
+
|
451
|
+
})
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
if index % 1000 ==0:
|
456
|
+
|
457
|
+
print('[%d]: %r'% (image.label,logits_value))
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
reader.close()
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
duration = time.time() - start_time
|
466
|
+
|
467
|
+
total_duration += duration
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
print('epoch %d duration = %d sec'%(epoch,duration))
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
tf.train.SummaryWriter(FLAGS.checkpoint_dir,sess.graph)
|
476
|
+
|
477
|
+
print('Total duration = %d sec'% total_duration)
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
if __name__ == '__main__':
|
482
|
+
|
483
|
+
tf.app.run()
|
484
|
+
|
485
|
+
```
|
486
|
+
|
487
|
+
しかし、inference.py を実行すると
|
488
|
+
|
489
|
+
```ここに言語を入力
|
490
|
+
|
117
491
|
File "inference.py", line 44, in main
|
118
492
|
|
119
493
|
image = reader.read(index)
|
120
494
|
|
121
|
-
File "/Users/
|
495
|
+
File "/Users/XXX/Desktop/cifar/reader.py", line 43, in read
|
122
496
|
|
123
497
|
self.bytestream.seek(record_bytes * index,0)
|
124
498
|
|