質問編集履歴
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -244,356 +244,34 @@
|
|
244
244
|
|
245
245
|
inference.py以外のコードは
|
246
246
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
im
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
NUM_CLASSES = 10
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
def _get_weights(shape,stddev=1.0):
|
272
|
-
|
273
|
-
var = tf.get_variable(
|
274
|
-
|
275
|
-
'weights',
|
276
|
-
|
277
|
-
shape,
|
278
|
-
|
279
|
-
initializer=tf.truncated_normal_initializer(stddev=stddev)
|
280
|
-
|
281
|
-
)
|
282
|
-
|
283
|
-
return var
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
def _get_biases(shape,value=0.0):
|
288
|
-
|
289
|
-
var = tf.get_variable(
|
290
|
-
|
291
|
-
'biases',
|
292
|
-
|
293
|
-
shape,
|
294
|
-
|
295
|
-
initializer=tf.constant_initializer(value)
|
296
|
-
|
297
|
-
)
|
298
|
-
|
299
|
-
return var
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
def inference(image_node):
|
304
|
-
|
305
|
-
# conv1
|
306
|
-
|
307
|
-
with tf.variable_scope('conv1') as scope:
|
308
|
-
|
309
|
-
weights = _get_weights(shape=[5,5,3,64],stddev=1e-4)
|
310
|
-
|
311
|
-
conv = tf.nn.conv2d(image_node,weights,[1,1,1,1],padding='SAME')
|
312
|
-
|
313
|
-
biases = _get_biases([64],value=0.1)
|
314
|
-
|
315
|
-
bias = tf.nn.bias_add(conv,biases)
|
316
|
-
|
317
|
-
conv1 = tf.nn.relu(bias,name=scope.name)
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
# pool
|
322
|
-
|
323
|
-
pool1 = tf.nn.max_pool(conv1,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool1')
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
# conv2
|
328
|
-
|
329
|
-
with tf.variable_scope('conv2') as scope:
|
330
|
-
|
331
|
-
weights = _get_weights(shape=[5,5,64,64],stddev=1e-4)
|
332
|
-
|
333
|
-
conv = tf.nn.conv2d(pool1,weights,[1,1,1,1],padding='SAME')
|
334
|
-
|
335
|
-
biases = _get_biases([64],value=0.1)
|
336
|
-
|
337
|
-
bias = tf.nn.bias_add(conv,biases)
|
338
|
-
|
339
|
-
conv2 = tf.nn.relu(bias,name=scope.name)
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
# pool2
|
344
|
-
|
345
|
-
pool2 = tf.nn.max_pool(conv2,ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME',name='pool2')
|
346
|
-
|
347
|
-
reshape = tf.reshape(pool2,[1,-1])
|
348
|
-
|
349
|
-
dim = reshape.get_shape()[1].value
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
# fc3
|
354
|
-
|
355
|
-
with tf.variable_scope('fc3') as scope:
|
356
|
-
|
357
|
-
weights = _get_weights(shape=[dim,384],stddev=0.04)
|
358
|
-
|
359
|
-
biases = _get_biases([384],value=0.1)
|
360
|
-
|
361
|
-
fc3 = tf.nn.relu(
|
362
|
-
|
363
|
-
tf.matmul(reshape,weights) + biases,
|
364
|
-
|
365
|
-
name=scope.name
|
366
|
-
|
367
|
-
)
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
# fc4
|
372
|
-
|
373
|
-
with tf.variable_scope('fc4') as scope:
|
374
|
-
|
375
|
-
weights = _get_weights(shape=[384,192],stddev=0.04)
|
376
|
-
|
377
|
-
biases = _get_biases([192],value=0.1)
|
378
|
-
|
379
|
-
fc4 = tf.nn.relu(tf.matmul(fc3,weights) + biases,name=scope.name)
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
# output
|
384
|
-
|
385
|
-
with tf.variable_scope('output') as scope:
|
386
|
-
|
387
|
-
weights = _get_weights(shape=[192,NUM_CLASSES],stddev=1/192.0)
|
388
|
-
|
389
|
-
biases = _get_biases([NUM_CLASSES],value=0.0)
|
390
|
-
|
391
|
-
logits = tf.add(tf.matmul(fc4,weights),biases,name='logits')
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
return logits
|
398
|
-
|
399
|
-
```
|
400
|
-
|
401
|
-
reader.py
|
402
|
-
|
403
|
-
```ここに言語を入力
|
404
|
-
|
405
|
-
# -*- coding: utf-8 -*-
|
406
|
-
|
407
|
-
from __future__ import absolute_import
|
408
|
-
|
409
|
-
from __future__ import division
|
410
|
-
|
411
|
-
from __future__ import print_function
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
import os
|
416
|
-
|
417
|
-
import numpy as np
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
class Cifar10Record(object):
|
422
|
-
|
423
|
-
width = 32
|
424
|
-
|
425
|
-
height = 32
|
426
|
-
|
427
|
-
depth = 3
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
def set_label(self,label_byte):
|
432
|
-
|
433
|
-
#self.label = np.frombuffer(label_byte,dtype=np.unit8)
|
434
|
-
|
435
|
-
self.label = np.frombuffer(label_byte,dtype=np.uint8) # unit8 -> uint8に修正
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
def set_image(self,image_bytes):
|
440
|
-
|
441
|
-
byte_buffer = np.frombuffer(image_bytes,dtype=np.int8)
|
442
|
-
|
443
|
-
reshaped_array = np.reshape(byte_buffer,[self.depth,self.height,self.width])
|
444
|
-
|
445
|
-
self.byte_array = np.transpose(reshaped_array,[1,2,0])
|
446
|
-
|
447
|
-
self.byte_array = self.byte_array.astype(np.float32)
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
class Cifar10Reader(object):
|
452
|
-
|
453
|
-
def __init__(self,filename):
|
454
|
-
|
455
|
-
if not os.path.exists(filename):
|
456
|
-
|
457
|
-
print(filename + ' is not exist')
|
458
|
-
|
459
|
-
return
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
self.bytestream = open(filename,mode="rb")
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
def close(self):
|
468
|
-
|
469
|
-
if not self.bytestream:
|
470
|
-
|
471
|
-
self.bytestream.close()
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
def read(self,index):
|
476
|
-
|
477
|
-
result = Cifar10Record()
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
label_bytes = 1
|
482
|
-
|
483
|
-
image_bytes = result.height * result.width * result.depth
|
484
|
-
|
485
|
-
record_bytes = label_bytes + image_bytes
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
self.bytestream.seek(record_bytes * index,0)
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
result.set_label(self.bytestream.read(label_bytes))
|
494
|
-
|
495
|
-
result.set_image(self.bytestream.read(image_bytes))
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
return result
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
print(self.bytestream)
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
# 追加
|
508
|
-
|
509
|
-
reader = Cifar10Reader("lena_std.tif")
|
510
|
-
|
511
|
-
ret = reader.read(0)
|
512
|
-
|
513
|
-
print(ret)
|
514
|
-
|
515
|
-
```
|
516
|
-
|
517
|
-
png10.py
|
518
|
-
|
519
|
-
```ここに言語を入力
|
520
|
-
|
521
|
-
# coding: utf-8
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
from __future__ import absolute_import
|
526
|
-
|
527
|
-
from __future__ import division
|
528
|
-
|
529
|
-
from __future__ import print_function
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
import os
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
import numpy as np
|
538
|
-
|
539
|
-
import tensorflow as tf
|
540
|
-
|
541
|
-
from PIL import Image
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
from reader import Cifar10Reader
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
FLAGS = tf.app.flags.FLAGS
|
550
|
-
|
551
|
-
tf.app.flags.DEFINE_string('file',None,"処理するファイルのパス")
|
552
|
-
|
553
|
-
tf.app.flags.DEFINE_integer('offset',0,"読み飛ばすレコード数")
|
554
|
-
|
555
|
-
tf.app.flags.DEFINE_integer('length',16,"読み込んで変換するレコード数")
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
basename = os.path.basename(FLAGS.file)
|
560
|
-
|
561
|
-
path = os.path.dirname(FLAGS.file)
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
reader = Cifar10Reader(FLAGS.file)
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
stop = FLAGS.offset + FLAGS.length
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
for index in range(FLAGS.offset,stop):
|
247
|
+
前回に記載した通りになっています。
|
248
|
+
|
249
|
+
https://teratail.com/questions/71848?whotofollow=
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
ちなみにエラーの全体は
|
254
|
+
|
255
|
+
Traceback (most recent call last):
|
256
|
+
|
257
|
+
File "inference.py", line 72, in <module>
|
258
|
+
|
259
|
+
tf.app.run()
|
260
|
+
|
261
|
+
File "/Users/XXX/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 44, in run
|
262
|
+
|
263
|
+
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
|
264
|
+
|
265
|
+
File "inference.py", line 49, in main
|
574
266
|
|
575
267
|
image = reader.read(index)
|
576
268
|
|
577
|
-
|
269
|
+
File "/Users/XXX/Desktop/cifar/reader.py", line 42, in read
|
578
|
-
|
270
|
+
|
579
|
-
|
271
|
+
self.bytestream.seek(record_bytes * index,0)
|
580
|
-
|
581
|
-
|
272
|
+
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
273
|
+
AttributeError: 'Cifar10Reader' object has no attribute 'bytestream'
|
586
|
-
|
587
|
-
|
274
|
+
|
588
|
-
|
589
|
-
|
275
|
+
|
590
|
-
|
591
|
-
imageshow.save(out,format='png')
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
reader.close()
|
596
|
-
|
597
|
-
```
|
598
276
|
|
599
277
|
のようになっています。
|