This repository was archived by the owner on Jul 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
500 lines (420 loc) · 18.7 KB
/
demo.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
'''
This notebook will walk you step by step through the process of using a pre-trained model to build up a contextual memory bank for a set of images, and then detect objects in those images+context using Context R-CNN.
Setup
Important: If you're running on a local machine, be sure to follow the installation instructions. This notebook includes only what's necessary to run in Colab.
'''
'''Install
!pip install -U --pre tensorflow=="2.*"
!pip install tf_slim
# Make sure you have pycocotools installed
!pip install pycocotools
'''
'''Get tensorflow/models or cd to parent directory of the repository.
import os
import pathlib
if "models" in pathlib.Path.cwd().parts:
while "models" in pathlib.Path.cwd().parts:
os.chdir('..')
elif not pathlib.Path('models').exists():
!git clone --depth 1 https://github.com/tensorflow/models
# Compile protobufs and install the object_detection package
%%bash
cd models/research/
protoc object_detection/protos/*.proto --python_out=. <-- ??
%%bash
cd models/research
pip install .
'''
########################################################################
# Imports
import numpy as np
import os
import six
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import pathlib
import json
import datetime
import matplotlib.pyplot as plt
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display
import os
# Import the object detection module.
# os.chdir('C:/Users/asses/Desktop/Dev/contextRCNN/models/research')
# print(os.getcwd())
from object_detection.utils import ops as utils_ops
# print(os.getcwd())
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utils
# Patches:
# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1
# Patch the location of gfile
tf.gfile = tf.io.gfile
##########################################################################
def splitall(path):
allparts = []
while 1:
parts = os.path.split(path)
if parts[0] == path: # sentinel for absolute paths
allparts.insert(0, parts[0])
break
elif parts[1] == path: # sentinel for relative paths
allparts.insert(0, parts[1])
break
else:
path = parts[0]
allparts.insert(0, parts[1])
return allparts
############################################################################
# Model preparation
# Loader
def load_model(model_name):
base_url = 'http://download.tensorflow.org/models/object_detection/'
model_file = model_name + '.tar.gz'
model_dir = tf.keras.utils.get_file(
fname=model_name,
origin=base_url + model_file,
untar=True)
model_dir = pathlib.Path(model_dir)/"saved_model"
model = tf.saved_model.load(str(model_dir))
model = model.signatures['serving_default']
return model
# Loading label map
# Label maps map indices to category names, so that when our convolution network predicts 5, we know that this corresponds to zebra. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'models/research/object_detection/data/snapshot_serengeti_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=False)
# We will test on a context group of images from one month at one camera from the Snapshot Serengeti val split defined on LILA.science, which was not seen during model training:
# If you want to test the code with your images, just add path to the images to
# the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('models/research/object_detection/test_images/snapshot_serengeti')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpeg")))
print(TEST_IMAGE_PATHS)
# Load the metadata for each image
test_data_json = 'models/research/object_detection/test_images/snapshot_serengeti/context_rcnn_demo_metadata.json'
with open(test_data_json, 'r') as f:
test_metadata = json.load(f)
image_id_to_datetime = {im['id']:im['date_captured'] for im in test_metadata['images']}
image_path_to_id = {im['file_name']: im['id']
for im in test_metadata['images']}
print(image_path_to_id)
############################################################################
# Generate Context Features for each image
faster_rcnn_model_name = 'faster_rcnn_resnet101_snapshot_serengeti_2020_06_10'
faster_rcnn_model = load_model(faster_rcnn_model_name)
# Check the model's input signature, it expects a batch of 3-color images of type uint8.
print(faster_rcnn_model.inputs)
# And it returns several outputs. Note this model has been exported with additional output 'detection_features' which will be used to build the contextual memory bank.
print(faster_rcnn_model.output_dtypes)
print(faster_rcnn_model.output_shapes)
# Add a wrapper function to call the model, and cleanup the outputs:
def run_inference_for_single_image(model, image):
'''Run single image through tensorflow object detection saved_model.
This function runs a saved_model on a (single) provided image and returns
inference results in numpy arrays.
Args:
model: tensorflow saved_model. This model can be obtained using
export_inference_graph.py.
image: uint8 numpy array with shape (img_height, img_width, 3)
Returns:
output_dict: a dictionary holding the following entries:
`num_detections`: an integer
`detection_boxes`: a numpy (float32) array of shape [N, 4]
`detection_classes`: a numpy (uint8) array of shape [N]
`detection_scores`: a numpy (float32) array of shape [N]
`detection_features`: a numpy (float32) array of shape [N, 7, 7, 2048]
'''
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
output_dict = model(input_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_dets = output_dict.pop('num_detections')
num_detections = int(num_dets)
for key,value in output_dict.items():
output_dict[key] = value[0, :num_detections].numpy()
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] = output_dict['detection_classes'].astype(
np.int64)
return output_dict
# Functions for embedding context features
def embed_date_captured(date_captured):
"""Encodes the datetime of the image.
Takes a datetime object and encodes it into a normalized embedding of shape
[5], using hard-coded normalization factors for year, month, day, hour,
minute.
Args:
date_captured: A datetime object.
Returns:
A numpy float32 embedding of shape [5].
"""
embedded_date_captured = []
month_max = 12.0
day_max = 31.0
hour_max = 24.0
minute_max = 60.0
min_year = 1990.0
max_year = 2030.0
year = (date_captured.year-min_year)/float(max_year-min_year)
embedded_date_captured.append(year)
month = (date_captured.month-1)/month_max
embedded_date_captured.append(month)
day = (date_captured.day-1)/day_max
embedded_date_captured.append(day)
hour = date_captured.hour/hour_max
embedded_date_captured.append(hour)
minute = date_captured.minute/minute_max
embedded_date_captured.append(minute)
return np.asarray(embedded_date_captured)
def embed_position_and_size(box):
"""Encodes the bounding box of the object of interest.
Takes a bounding box and encodes it into a normalized embedding of shape
[4] - the center point (x,y) and width and height of the box.
Args:
box: A bounding box, formatted as [ymin, xmin, ymax, xmax].
Returns:
A numpy float32 embedding of shape [4].
"""
ymin = box[0]
xmin = box[1]
ymax = box[2]
xmax = box[3]
w = xmax - xmin
h = ymax - ymin
x = xmin + w / 2.0
y = ymin + h / 2.0
return np.asarray([x, y, w, h])
def get_context_feature_embedding(date_captured, detection_boxes,
detection_features, detection_scores):
"""Extracts representative feature embedding for a given input image.
Takes outputs of a detection model and focuses on the highest-confidence
detected object. Starts with detection_features and uses average pooling to
remove the spatial dimensions, then appends an embedding of the box position
and size, and an embedding of the date and time the image was captured,
returning a one-dimensional representation of the object.
Args:
date_captured: A datetime string of format '%Y-%m-%d %H:%M:%S'.
detection_features: A numpy (float32) array of shape [N, 7, 7, 2048].
detection_boxes: A numpy (float32) array of shape [N, 4].
detection_scores: A numpy (float32) array of shape [N].
Returns:
A numpy float32 embedding of shape [2057].
"""
date_captured = datetime.datetime.strptime(date_captured,'%Y-%m-%d %H:%M:%S')
temporal_embedding = embed_date_captured(date_captured)
embedding = detection_features[0]
pooled_embedding = np.mean(np.mean(embedding, axis=1), axis=0)
box = detection_boxes[0]
position_embedding = embed_position_and_size(box)
bb_embedding = np.concatenate((pooled_embedding, position_embedding))
embedding = np.expand_dims(np.concatenate((bb_embedding,temporal_embedding)),
axis=0)
score = detection_scores[0]
return embedding, score
# Run it on each test image and use the output detection features and metadata to build up a context feature bank:
def run_inference(model, image_path, date_captured, resize_image=True):
"""Runs inference over a single input image and extracts contextual features.
Args:
model: A tensorflow saved_model object.
image_path: Absolute path to the input image.
date_captured: A datetime string of format '%Y-%m-%d %H:%M:%S'.
resize_image: Whether to resize the input image before running inference.
Returns:
context_feature: A numpy float32 array of shape [2057].
score: A numpy float32 object score for the embedded object.
output_dict: The saved_model output dictionary for the image.
"""
with open(image_path,'rb') as f:
image = Image.open(f)
if resize_image:
image.thumbnail((640,640),Image.ANTIALIAS)
image_np = np.array(image)
# Actual detection.
output_dict = run_inference_for_single_image(model, image_np)
context_feature, score = get_context_feature_embedding(
date_captured, output_dict['detection_boxes'],
output_dict['detection_features'], output_dict['detection_scores'])
return context_feature, score, output_dict
context_features = []
scores = []
faster_rcnn_results = {}
for image_path in TEST_IMAGE_PATHS:
image_id = image_path_to_id[str(image_path).replace("\\", "/")]
date_captured = image_id_to_datetime[image_id]
context_feature, score, results = run_inference(
faster_rcnn_model, image_path, date_captured)
faster_rcnn_results[image_id] = results
context_features.append(context_feature)
scores.append(score)
# Concatenate all extracted context embeddings into a contextual memory bank.
context_features_matrix = np.concatenate(context_features, axis=0)
#######################################################################
# Run Detection With Context
# Load a context r-cnn object detection model:
context_rcnn_model_name = 'context_rcnn_resnet101_snapshot_serengeti_2020_06_10'
context_rcnn_model = load_model(context_rcnn_model_name)
# We need to define the expected context padding size for the model, this must match the definition in the model config (max_num_context_features).
context_padding_size = 2000
# Check the model's input signature, it expects a batch of 3-color images of type uint8, plus context_features padded to the maximum context feature size for this model (2000) and valid_context_size to represent the non-padded context features:
print(context_rcnn_model.inputs)
# And returns several outputs:
print(context_rcnn_model.output_dtypes)
print(context_rcnn_model.output_shapes)
def run_context_rcnn_inference_for_single_image(
model, image, context_features, context_padding_size):
'''Run single image through a Context R-CNN saved_model.
This function runs a saved_model on a (single) provided image and provided
contextual features and returns inference results in numpy arrays.
Args:
model: tensorflow Context R-CNN saved_model. This model can be obtained
using export_inference_graph.py and setting side_input fields.
Example export call -
python export_inference_graph.py \
--input_type image_tensor \
--pipeline_config_path /path/to/context_rcnn_model.config \
--trained_checkpoint_prefix /path/to/context_rcnn_model.ckpt \
--output_directory /path/to/output_dir \
--use_side_inputs True \
--side_input_shapes 1,2000,2057/1 \
--side_input_names context_features,valid_context_size \
--side_input_types float,int \
--input_shape 1,-1,-1,3
image: uint8 numpy array with shape (img_height, img_width, 3)
context_features: A numpy float32 contextual memory bank of shape
[num_context_examples, 2057]
context_padding_size: The amount of expected padding in the contextual
memory bank, defined in the Context R-CNN config as
max_num_context_features.
Returns:
output_dict: a dictionary holding the following entries:
`num_detections`: an integer
`detection_boxes`: a numpy (float32) array of shape [N, 4]
`detection_classes`: a numpy (uint8) array of shape [N]
`detection_scores`: a numpy (float32) array of shape [N]
'''
image = np.asarray(image)
# The input image needs to be a tensor, convert it using
# `tf.convert_to_tensor`.
image_tensor = tf.convert_to_tensor(
image, name='image_tensor')[tf.newaxis,...]
context_features = np.asarray(context_features)
valid_context_size = context_features.shape[0]
valid_context_size_tensor = tf.convert_to_tensor(
valid_context_size, name='valid_context_size')[tf.newaxis,...]
padded_context_features = np.pad(
context_features,
((0,context_padding_size-valid_context_size),(0,0)), mode='constant')
padded_context_features_tensor = tf.convert_to_tensor(
padded_context_features,
name='context_features',
dtype=tf.float32)[tf.newaxis,...]
# Run inference
output_dict = model(
inputs=image_tensor,
context_features=padded_context_features_tensor,
valid_context_size=valid_context_size_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_dets = output_dict.pop('num_detections')
num_detections = int(num_dets)
for key,value in output_dict.items():
output_dict[key] = value[0, :num_detections].numpy()
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
return output_dict
def show_context_rcnn_inference(
model, image_path, context_features, faster_rcnn_output_dict,
context_padding_size, resize_image=True):
"""Runs inference over a single input image and visualizes Faster R-CNN vs.
Context R-CNN results.
Args:
model: A tensorflow saved_model object.
image_path: Absolute path to the input image.
context_features: A numpy float32 contextual memory bank of shape
[num_context_examples, 2057]
faster_rcnn_output_dict: The output_dict corresponding to this input image
from the single-frame Faster R-CNN model, which was previously used to
build the memory bank.
context_padding_size: The amount of expected padding in the contextual
memory bank, defined in the Context R-CNN config as
max_num_context_features.
resize_image: Whether to resize the input image before running inference.
Returns:
context_rcnn_image_np: Numpy image array showing Context R-CNN Results.
faster_rcnn_image_np: Numpy image array showing Faster R-CNN Results.
"""
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
with open(image_path, 'rb') as f:
image = Image.open(f)
if resize_image:
image.thumbnail((640, 640), Image.ANTIALIAS)
image_np = np.array(image)
image.thumbnail((400, 400), Image.ANTIALIAS)
context_rcnn_image_np = np.array(image)
faster_rcnn_image_np = np.copy(context_rcnn_image_np)
# Actual detection.
output_dict = run_context_rcnn_inference_for_single_image(
model, image_np, context_features, context_padding_size)
# Visualization of the results of a context_rcnn detection.
vis_utils.visualize_boxes_and_labels_on_image_array(
context_rcnn_image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
use_normalized_coordinates=True,
line_thickness=2)
# Visualization of the results of a faster_rcnn detection.
vis_utils.visualize_boxes_and_labels_on_image_array(
faster_rcnn_image_np,
faster_rcnn_output_dict['detection_boxes'],
faster_rcnn_output_dict['detection_classes'],
faster_rcnn_output_dict['detection_scores'],
category_index,
use_normalized_coordinates=True,
line_thickness=2)
return context_rcnn_image_np, faster_rcnn_image_np
# Define Matplotlib parameters for pretty visualizations
# %matplotlib inline
plt.rcParams['axes.grid'] = False
plt.rcParams['xtick.labelsize'] = False
plt.rcParams['ytick.labelsize'] = False
plt.rcParams['xtick.top'] = False
plt.rcParams['xtick.bottom'] = False
plt.rcParams['ytick.left'] = False
plt.rcParams['ytick.right'] = False
plt.rcParams['figure.figsize'] = [15,10]
# Run Context R-CNN inference and compare results to Faster R-CNN
for count, image_path in enumerate(TEST_IMAGE_PATHS):
image_id = image_path_to_id[str(image_path).replace("\\", "/")]
faster_rcnn_output_dict = faster_rcnn_results[image_id]
context_rcnn_image, faster_rcnn_image = show_context_rcnn_inference(
context_rcnn_model, image_path, context_features_matrix,
faster_rcnn_output_dict, context_padding_size)
plt.subplot(4,2,count*2+1)
plt.imshow(faster_rcnn_image)
plt.title('Faster R-CNN')
plt.subplot(4,2,count*2+2)
plt.imshow(context_rcnn_image)
plt.title('Context R-CNN')
# plt.show()
plt.savefig("result.png")
im = Image.open(r"./result.png")
im.show()
# The images used in this demo are from the Snapshot Serengeti dataset, and released under the Community Data License Agreement (permissive variant).