-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
85 lines (79 loc) · 2.7 KB
/
model.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<<<<<<< HEAD
=======
#
>>>>>>> d0120c1196b643641e5eeb06cea9402427753e8f
import os
import numpy as np
from PIL import Image
from imageio import imread
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tf_slim as slim
from tf_slim.nets import inception
import tf_slim as slim
import cv2
import matplotlib.pyplot as plt
ckpt_path = "input\inception_v3.ckpt"
images_path = "test_input_images\*"
img_width = 299
img_height = 299
batch_size = 16
batch_shape = [batch_size, img_height, img_width, 3]
num_classes = 1001
predict_output = []
class_names_path = "input\imagenet_class_names.txt"
with open(class_names_path) as f:
class_names = f.readlines()
X = tf.placeholder(tf.float32, shape=batch_shape)
with slim.arg_scope(inception.inception_v3_arg_scope()):
logits, end_points = inception.inception_v3(
X, num_classes = num_classes, is_training=False
)
predictions = end_points["Predictions"]
saver = tf.train.Saver(slim.get_model_variables())
def load_images(input_dir):
global batch_shape
images = np.zeros(batch_shape)
filenames = []
idx=0
batch_size = batch_shape[0]
files = tf.gfile.Glob(input_dir)[:20]
files.sort()
for filepath in files:
with tf.gfile.Open(filepath, "rb") as f:
imgRaw = np.array(Image.fromarray(imread(f, as_gray=False, pilmode="RGB")).resize((299,299))).astype(np.float)/255.0
images[idx, :, :, :] = imgRaw * 2.0 - 1.0
filenames.append(os.path.basename(filepath))
idx += 1
if idx == batch_size:
yield filenames, images
filenames = []
images = np.zeros(batch_shape)
idx = 0
if idx > 0:
yield filenames, images
session_creator=tf.train.ChiefSessionCreator(
scaffold=tf.train.Scaffold(saver=saver),
checkpoint_filename_with_path=ckpt_path,
master='')
with tf.train.MonitoredSession(session_creator=session_creator) as sess:
for filenames, images in load_images(images_path):
print("adding image")
labels = sess.run(predictions, feed_dict={X: images})
for filename, label, image in zip(filenames, labels, images):
predict_output.append([filename, label, image])
# print("predict_output: ", predict_output)
for x in predict_output:
# print("break?")
out_list = list(x[1])
topPredict = sorted(range(len(out_list)), key=lambda i: out_list[i], reverse=True)[:5]
plt.imshow((((x[2]+1)/2)*255).astype(int))
plt.show()
print("Filename:",x[0])
print("Displaying the top 5 Predictions for above image:")
for p in topPredict:
<<<<<<< HEAD
print(class_names[p-1].strip())
=======
print(class_names[p-1].strip())
>>>>>>> d0120c1196b643641e5eeb06cea9402427753e8f