teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2019/09/24 17:08

投稿

salah0615
salah0615

スコア49

title CHANGED
File without changes
body CHANGED
@@ -36,4 +36,90 @@
36
36
 
37
37
  この三つについてご教授願います。
38
38
 
39
- 長くなるとアレなので、なるべく短く説明しましたので、不足があったら申し訳ありません。
39
+ 長くなるとアレなので、なるべく短く説明しましたので、不足があったら申し訳ありません。
40
+
41
+ ### 追記
42
+ 念の為、全コードを追記します。
43
+ ```python
44
+ import os
45
+ import cv2
46
+ import numpy as np
47
+ import TensorFlow as tf
48
+ from フォルダ名 import cifar10
49
+
50
+ image_path = '画像フォルダ'
51
+ classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
52
+ #画像の読み込み
53
+ images = []
54
+ files = os.listdir(image_path)
55
+ for file in files:
56
+ img = cv2.imread(os.path.join(image_path, file))
57
+ img = img[:, :, ::-1]
58
+ height = img.shape[0]
59
+ width = img.shape[1]
60
+ cropped_size = min(width, height)
61
+ sx = (width - cropped_size) // 2
62
+ sy = (height - cropped_size) // 2
63
+ cropped_img = img[sy:sy + cropped_size, sx:sx + cropped_size]
64
+ resized_img = cv2.resize(cropped_img, (32, 32))
65
+ images.append(resized_img)
66
+
67
+ images = np.array(images)
68
+
69
+ FLAGS = tf.app.flags.FLAGS
70
+ FLAGS.batch_size = len(images)
71
+
72
+ image, = tf.train.slice_imput_producer([images], shuffle = False)
73
+ reshaped_image = tf.cast(image, tf.float32)
74
+ resized_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, 24, 24)
75
+ float_image = tf.image.per_image_standardization(resized_image)
76
+
77
+ #バッチ入力の設定
78
+ images = tf.train.batch([float_image], batch_size = FLAGS.batch_size)
79
+
80
+ #予測器の作成
81
+ logits = cifar10.inference(images)
82
+
83
+ softmax = tf.nn.softmax(logits)
84
+ prediction = tf.argmax(softmax, 1)
85
+
86
+ #移動平均版の学習データを復元するように設定
87
+ variable_averages = tf.train.ExponentialMovingAverage(cifar10.MOVING_AVERAGE_DECAY)
88
+ variables_to_restore = variable_averages.variables_to_restore()
89
+ saver = tf.train.Saver(variables_to_restore)
90
+
91
+ sess = tf.Session()
92
+
93
+ checkpoint = tf.train.latest_checkpoint('cifar10_train')
94
+ if checkpoint:
95
+ saver.restore(session, checkpoint)
96
+
97
+ #複数の画像がキューに詰められた状態なので、一つずつ取り出して処理するランナーの生成
98
+ coord = tf.train.Coordinator()
99
+ try:
100
+ #処理を行うスレッドの生成
101
+ threads = []
102
+ for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
103
+ threads.extend(qr.create_threads(sess, coord = coord, daemon = True, start = True))
104
+
105
+ softmaxs, predictions = session.run([softmax, prediction])
106
+ for f, s, p in zip(files, softmaxs, predictions):
107
+ print(f, classes[p])
108
+ print(list(s))
109
+ print()
110
+
111
+ except Exception as e:
112
+ coord.request_stop(e)
113
+
114
+ #スレッドを止める
115
+ coord.request_stop()
116
+ coord.join(threads, stop_grace_period_secs = 10)
117
+ ```
118
+
119
+ 結果は,
120
+ 画像ファイル名 dog
121
+ [0.02342324234, 0.00324532453, ..., ..., ..., ..., ..., ..., ..., ...]
122
+
123
+ 以下省略
124
+
125
+ のような形です。