質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -75,3 +75,175 @@
|
|
75
75
|
|
76
76
|
|
77
77
|
長くなるとアレなので、なるべく短く説明しましたので、不足があったら申し訳ありません。
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
### 追記
|
82
|
+
|
83
|
+
念の為、全コードを追記します。
|
84
|
+
|
85
|
+
```python
|
86
|
+
|
87
|
+
import os
|
88
|
+
|
89
|
+
import cv2
|
90
|
+
|
91
|
+
import numpy as np
|
92
|
+
|
93
|
+
import TensorFlow as tf
|
94
|
+
|
95
|
+
from フォルダ名 import cifar10
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
image_path = '画像フォルダ'
|
100
|
+
|
101
|
+
classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
|
102
|
+
|
103
|
+
#画像の読み込み
|
104
|
+
|
105
|
+
images = []
|
106
|
+
|
107
|
+
files = os.listdir(image_path)
|
108
|
+
|
109
|
+
for file in files:
|
110
|
+
|
111
|
+
img = cv2.imread(os.path.join(image_path, file))
|
112
|
+
|
113
|
+
img = img[:, :, ::-1]
|
114
|
+
|
115
|
+
height = img.shape[0]
|
116
|
+
|
117
|
+
width = img.shape[1]
|
118
|
+
|
119
|
+
cropped_size = min(width, height)
|
120
|
+
|
121
|
+
sx = (width - cropped_size) // 2
|
122
|
+
|
123
|
+
sy = (height - cropped_size) // 2
|
124
|
+
|
125
|
+
cropped_img = img[sy:sy + cropped_size, sx:sx + cropped_size]
|
126
|
+
|
127
|
+
resized_img = cv2.resize(cropped_img, (32, 32))
|
128
|
+
|
129
|
+
images.append(resized_img)
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
images = np.array(images)
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
FLAGS = tf.app.flags.FLAGS
|
138
|
+
|
139
|
+
FLAGS.batch_size = len(images)
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
image, = tf.train.slice_imput_producer([images], shuffle = False)
|
144
|
+
|
145
|
+
reshaped_image = tf.cast(image, tf.float32)
|
146
|
+
|
147
|
+
resized_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, 24, 24)
|
148
|
+
|
149
|
+
float_image = tf.image.per_image_standardization(resized_image)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
#バッチ入力の設定
|
154
|
+
|
155
|
+
images = tf.train.batch([float_image], batch_size = FLAGS.batch_size)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
#予測器の作成
|
160
|
+
|
161
|
+
logits = cifar10.inference(images)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
softmax = tf.nn.softmax(logits)
|
166
|
+
|
167
|
+
prediction = tf.argmax(softmax, 1)
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
#移動平均版の学習データを復元するように設定
|
172
|
+
|
173
|
+
variable_averages = tf.train.ExponentialMovingAverage(cifar10.MOVING_AVERAGE_DECAY)
|
174
|
+
|
175
|
+
variables_to_restore = variable_averages.variables_to_restore()
|
176
|
+
|
177
|
+
saver = tf.train.Saver(variables_to_restore)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
sess = tf.Session()
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
checkpoint = tf.train.latest_checkpoint('cifar10_train')
|
186
|
+
|
187
|
+
if checkpoint:
|
188
|
+
|
189
|
+
saver.restore(session, checkpoint)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
#複数の画像がキューに詰められた状態なので、一つずつ取り出して処理するランナーの生成
|
194
|
+
|
195
|
+
coord = tf.train.Coordinator()
|
196
|
+
|
197
|
+
try:
|
198
|
+
|
199
|
+
#処理を行うスレッドの生成
|
200
|
+
|
201
|
+
threads = []
|
202
|
+
|
203
|
+
for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
|
204
|
+
|
205
|
+
threads.extend(qr.create_threads(sess, coord = coord, daemon = True, start = True))
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
softmaxs, predictions = session.run([softmax, prediction])
|
210
|
+
|
211
|
+
for f, s, p in zip(files, softmaxs, predictions):
|
212
|
+
|
213
|
+
print(f, classes[p])
|
214
|
+
|
215
|
+
print(list(s))
|
216
|
+
|
217
|
+
print()
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
except Exception as e:
|
222
|
+
|
223
|
+
coord.request_stop(e)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
#スレッドを止める
|
228
|
+
|
229
|
+
coord.request_stop()
|
230
|
+
|
231
|
+
coord.join(threads, stop_grace_period_secs = 10)
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
結果は,
|
238
|
+
|
239
|
+
画像ファイル名 dog
|
240
|
+
|
241
|
+
[0.02342324234, 0.00324532453, ..., ..., ..., ..., ..., ..., ..., ...]
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
以下省略
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
のような形です。
|