-
Notifications
You must be signed in to change notification settings - Fork 197
/
Utils_Imagenet.py
executable file
·279 lines (235 loc) · 12.5 KB
/
Utils_Imagenet.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# """
# Preparing model:
# - Install bazel ( check tensorflow's github for more info )
# Ubuntu 14.04:
# - Requirements:
# sudo add-apt-repository ppa:webupd8team/java
# sudo apt-get update
# sudo apt-get install oracle-java8-installer
# - Download bazel, ( https://github.com/bazelbuild/bazel/releases )
# tested on: https://github.com/bazelbuild/bazel/releases/download/0.2.0/bazel-0.2.0-jdk7-installer-linux-x86_64.sh
# - chmod +x PATH_TO_INSTALL.SH
# - ./PATH_TO_INSTALL.SH --user
# - Place bazel onto path ( exact path to store shown in the output)
# - For retraining, prepare folder structure as
# - root_folder_name
# - class 1
# - file1
# - file2
# - class 2
# - file1
# - file2
# - Clone tensorflow
# - Go to root of tensorflow
# - bazel build tensorflow/examples/image_retraining:retrain
# - bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir /path/to/root_folder_name --output_graph /path/output_graph.pb -- output_labels /path/output_labels.txt -- bottleneck_dir /path/bottleneck
# ** Training done. **
# For testing through bazel,
# bazel build tensorflow/examples/label_image:label_image && \
# bazel-bin/tensorflow/examples/label_image/label_image \
# --graph=/path/output_graph.pb --labels=/path/output_labels.txt \
# --output_layer=final_result \
# --image=/path/to/test/image
# For testing through python, change and run this code.
# """
import numpy as np
import os
import tensorflow as tf
import sys
import vid_classes
import progressbar
import utils_image
import multiclass_rectangle
from PIL import Image
modelFullPath = 'output_model/retrained_graph.pb' ##### Put the
checkpoint_dir= "output_model/model.ckpt-250000"
label_file='output_model/retrained_labels.txt'
def create_graph():
"""Creates a graph from saved GraphDef file and returns a saver."""
# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(modelFullPath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def run_inception_once(picture_path):
if not tf.gfile.Exists(picture_path):
tf.logging.fatal('File does not exist %s', picture_path)
sys.exit()
image_data = tf.gfile.FastGFile(picture_path, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line in tf.gfile.GFile(label_file)]
# Creates graph from saved GraphDef.
create_graph()
saver = tf.train.Saver() # defaults to saving all variables - in this case w and b
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
# load_checkpoint(sess, saver)
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-3:][::-1] # Getting top 5 predictions
#CHECK OUTPUT
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
# CHECK BEST LABEL
print "Best Label: %s with conf: %.5f"%(label_lines[top_k[0]],predictions[top_k[0]])
return label_lines[top_k[0]],predictions[top_k[0]]
# def run_inception(pictures_path_array):
# labels=[]
# confidences=[]
# # Creates graph from saved GraphDef.
# # Creates graph from saved GraphDef.
# create_graph()
# saver = tf.train.Saver() # defaults to saving all variables - in this case w and b
# with tf.Session() as sess:
# sess.run(tf.initialize_all_variables())
# # load_checkpoint(sess, saver)
# softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
# for picture_path in pictures_path_array:
# if not tf.gfile.Exists(picture_path):
# tf.logging.fatal('File does not exist %s', picture_path)
# sys.exit()
# image_data = tf.gfile.FastGFile(picture_path, 'rb').read()
# predictions = sess.run(softmax_tensor,
# {'DecodeJpeg/contents:0': image_data})
# predictions = np.squeeze(predictions)
# top_k = predictions.argsort()[-5:][::-1] # Getting top 5 predictions
# #CHECK OUTPUT
# # for node_id in top_k:
# # human_string = vid_classes.code_comp_to_class(node_id)
# # score = predictions[node_id]
# # print('%s (score = %.5f)' % (human_string, score))
# #CHECK BEST LABEL
# #print "Best Label: %s with conf: %.5f"%(vid_classes.code_comp_to_class(top_k[0]),predictions[top_k[0]])
# labels.append(vid_classes.code_comp_to_class(top_k[0]), len(labels))
# confidences.append(predictions[top_k[0]], len(confidences))
# return labels, confidences
def label_video(video_info, frames):
progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()])
if not os.path.exists(frames[0].split("/")[0]+"/cropped_rects/"):
os.makedirs(frames[0].split("/")[0]+"/cropped_rects/")
print("Created Folder: %s"%(frames[0].split("/")[0]+"/cropped_rects/"))
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line in tf.gfile.GFile(label_file)]
# Creates graph from saved GraphDef.
create_graph()
saver = tf.train.Saver() # defaults to saving all variables - in this case w and b
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
# load_checkpoint(sess, saver)
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
idx=0
for frame_info in progress(video_info):
print "Tracking Frame Nr: %d"%frame_info.frame
print len(frame_info.rects)
rect_id=0
frame_info.filename = frames[idx]
for rect in frame_info.rects:
img= Image.open(frames[idx])
width, height= utils_image.get_Image_Size(frames[idx])
print rect.x1,rect.y1,rect.x2 ,rect.y2
x1,y1,x2,y2=utils_image.get_orig_rect(width, height, 640, 480, rect.x1,rect.y1,rect.x2 ,rect.y2)
print x1,y1,x2,y2
if(x1==x2):
x2=x2-10
if(y1==y2):
y2=y2-10
cor = (min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2))
print cor
cropped_img=img.crop(cor)
cropped_img_name=frames[0].split("/")[0]+"/cropped_rects/cropped_frame_%d_rect_%d.JPEG"%(frame_info.frame, rect_id)
cropped_img.save(cropped_img_name)
print "Frame: %d Rect: %d conf: %.2f"%(frame_info.frame, rect_id, rect.true_confidence)
if not tf.gfile.Exists(cropped_img_name):
tf.logging.fatal('File does not exist %s', cropped_img_name)
sys.exit()
image_data = tf.gfile.FastGFile(cropped_img_name, 'rb').read()
predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-3:][::-1] # Getting top 5 predictions
#CHECK OUTPUT
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
# CHECK BEST LABEL
print "Best Label: %s with conf: %.5f"%(vid_classes.code_to_class_string(label_lines[top_k[0]]),predictions[top_k[0]])
rect.set_rect_coordinates(x1,x2,y1,y2)
rect.set_label(predictions[top_k[0]], vid_classes.code_to_class_string(label_lines[top_k[0]]), top_k[0], label_lines[top_k[0]])
rect_id=rect_id+1
idx=idx+1
return video_info
def recurrent_label_video(video_info, frames):
progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()])
decomposed_path =frames[0].split("/")
folder = decomposed_path[len(decomposed_path)-2]
if not os.path.exists(folder+"/cropped_rects/"):
os.makedirs(folder+"/cropped_rects/")
print("Created Folder: %s"%(folder+"/cropped_rects/"))
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line in tf.gfile.GFile(label_file)]
# Creates graph from saved GraphDef.
create_graph()
saver = tf.train.Saver() # defaults to saving all variables - in this case w and b
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
# load_checkpoint(sess, saver)
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
idx=0
video_labels=[]
for frame_info in progress(video_info):
print "Tracking Frame Nr: %d"%frame_info.frame
print len(frame_info.rects)
rect_id=0
frame_labels=[]
frame_info.filename = frames[idx]
for rect in frame_info.rects:
img= Image.open(frames[idx])
width, height= utils_image.get_Image_Size(frames[idx])
print rect.x1,rect.y1,rect.x2 ,rect.y2
x1,y1,x2,y2=utils_image.get_orig_rect(width, height, 640, 480, rect.x1,rect.y1,rect.x2 ,rect.y2)
print x1,y1,x2,y2
cor = (min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2))
print cor
cropped_img=img.crop(cor)
cropped_img_name=folder+"/cropped_rects/cropped_frame_%d_rect_%d.JPEG"%(frame_info.frame, rect_id)
cropped_img.save(cropped_img_name)
print "Frame: %d Rect: %d conf: %.2f"%(frame_info.frame, rect_id, rect.true_confidence)
if not tf.gfile.Exists(cropped_img_name):
tf.logging.fatal('File does not exist %s', cropped_img_name)
sys.exit()
image_data = tf.gfile.FastGFile(cropped_img_name, 'rb').read()
predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-3:][::-1] # Getting top 5 predictions
#CHECK OUTPUT
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
if(len(video_labels)>0):
if(video_labels[idx-1][rect_id][0]==top_k[0]):
# CHECK BEST LABEL
print "Best Label: %s with conf: %.5f"%(vid_classes.code_to_class_string(label_lines[top_k[0]]),predictions[top_k[0]])
rect.set_rect_coordinates(x1,x2,y1,y2)
rect.set_label(predictions[top_k[0]], vid_classes.code_to_class_string(label_lines[top_k[0]]), top_k[0], label_lines[top_k[0]])
frame_labels.append((top_k[0], predictions[top_k[0]]))
else:
label = video_labels[idx-1][rect_id][0]
print "Best Label setted recurrently: %s "%(vid_classes.code_to_class_string(label_lines[label]))
rect.set_rect_coordinates(x1,x2,y1,y2)
rect.set_label(video_labels[idx-1][rect_id][1], vid_classes.code_to_class_string(label_lines[label]), label, label_lines[label])
frame_labels.append((label, video_labels[idx-1][rect_id][1]))
else:
# CHECK BEST LABEL
print "Best Label: %s with conf: %.5f"%(vid_classes.code_to_class_string(label_lines[top_k[0]]),predictions[top_k[0]])
rect.set_rect_coordinates(x1,x2,y1,y2)
rect.set_label(predictions[top_k[0]], vid_classes.code_to_class_string(label_lines[top_k[0]]), top_k[0], label_lines[top_k[0]])
frame_labels.append((top_k[0], predictions[top_k[0]]))
rect_id=rect_id+1
video_labels.append(frame_labels)
idx=idx+1
return video_info