-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInference.py
More file actions
60 lines (51 loc) 路 2.25 KB
/
Copy pathInference.py
File metadata and controls
60 lines (51 loc) 路 2.25 KB
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
# -*- coding: UTF-8 -*-
# Define graph automatically on cpu or on gpu.
import tensorflow as tf
slim = tf.contrib.slim
import numpy as np
from inception_utils import inception_arg_scope as arg_scope
from inception_v3 import inception_v3 as graph
class Inference():
visual_loaded = False
def _define_graph(self, num_classes=1001):
with tf.Graph().as_default():
with slim.arg_scope(arg_scope()):
# A tensor of size [batch_size, height, width, channels], default
# is 299 * 299.
self.images = tf.placeholder(tf.float32, [1, 299, 299, 3])
logits, _ = graph(
self.images,
num_classes=num_classes,
is_training=False,
spatial_squeeze=True,
scope=None)
# Probably on class and prediction index.
self.prob_on_class = tf.nn.softmax(logits)
self.pred_op = tf.argmax(logits, axis=1)
self.prob_op = tf.reduce_max(self.prob_on_class, axis=1)
# Define topk predictor.
self.values_tensor, self.indices_tensor = tf.nn.top_k(
self.prob_on_class, self.topk)
self.variables_to_restore = slim.trainable_variables()
# Generate sessions and get features op
tf_config = tf.ConfigProto()
if self.growth:
tf_config.gpu_options.allow_growth = True
else:
tf_config.gpu_options.per_process_gpu_memory_fraction = self.memory_fraction
# Definition of session.
self.sess = tf.Session(config=tf_config)
def load_model(self, model_path):
# Restore model.
self._define_graph()
self.saver = tf.train.Saver(self.variables_to_restore)
self.saver.restore(self.sess, model_path)
self.visual_loaded = True
def get_prediction(self, images):
if self.visual_loaded:
pred, prob = self.sess.run(
[self.pred_op, self.prob_op], {self.images: images})
pred = np.squeeze(pred)
prob = np.squeeze(prob)
else:
print('Firstly please load pre-trained model.')