-
Notifications
You must be signed in to change notification settings - Fork 2
/
CS2_DataPipeline.py
204 lines (148 loc) · 5.98 KB
/
CS2_DataPipeline.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().system('git clone https://github.com/nagi1995/tensorflow-yolov4-tflite')
# In[2]:
get_ipython().run_line_magic('cd', 'tensorflow-yolov4-tflite')
# In[3]:
import os
os.getcwd()
# In[11]:
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app, flags, logging
from absl.flags import FLAGS
import core.utils as utils
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
import cv2
import numpy as np
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
from math import ceil
from time import time
import sys
IN_COLAB = "google.colab" in sys.modules
if IN_COLAB:
from google.colab.patches import cv2_imshow
# In[9]:
IN_COLAB
# In[5]:
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
FLAGS = {}
FLAGS["tiny"] = True
FLAGS["model"] = "yolov4"
# In[15]:
def detect_tiles():
'''
This functions prints
the time taken for
object detection in tensorflow.
'''
score = .25
iou = 0.5
input_size = 416
image_path = "./test_images/00000032.png"
weights_path = "./checkpoints/yolov4-tiny-custom_best-416"
im_name = image_path.split("/")[-1]
# ext = im_name.split(".")[-1]
start = time()
original_image = cv2.imread(image_path)
h, w, _ = original_image.shape
h_new = ceil(h/input_size) * input_size
w_new = ceil(w/input_size) * input_size
# scaled_h = h_new/h
# scaled_w = w_new/w
resized_image = cv2.resize(original_image, (w_new, h_new), cv2.INTER_LINEAR)
col_list = []
for i in range(h_new//input_size):
row_list = []
for j in range(w_new//input_size):
tiled_image_original = resized_image[i*input_size : (i+1)*input_size, j*input_size : (j+1)*input_size, :]
tiled_image = tiled_image_original / 255.
tiled_image = np.asarray(tiled_image).astype(np.float32)
saved_model_loaded = tf.saved_model.load(weights_path, tags=[tag_constants.SERVING])
infer = saved_model_loaded.signatures['serving_default']
batch_data = tf.constant([tiled_image])
pred_bbox = infer(batch_data)
for key, value in pred_bbox.items():
boxes = value[:, :, 0:4]
pred_conf = value[:, :, 4:]
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape(pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=iou,
score_threshold=score)
pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
row_list.append(utils.draw_bbox(tiled_image_original, pred_bbox))
col_list.append(cv2.hconcat(row_list))
scaled_image = cv2.vconcat(col_list)
reconstructed_image = cv2.resize(scaled_image, (w, h), interpolation = cv2.INTER_AREA)
end = time()
cv2_imshow(reconstructed_image)
print("time taken: ", end-start)
# In[16]:
detect_tiles()
# In[13]:
def detect_tiles_lite():
'''
This functions prints
the time taken for
object detection in
tensorflow lite.
'''
score = .25
iou = 0.5
input_size = 416
image_path = "./test_images/00000032.png"
weights_path = "./checkpoints/yolov4tiny-custom_best-416-tflite-fp16.tflite"
interpreter = tf.lite.Interpreter(model_path=weights_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
im_name = image_path.split("/")[-1]
# ext = im_name.split(".")[-1]
start = time()
original_image = cv2.imread(image_path)
h, w, _ = original_image.shape
h_new = ceil(h/input_size) * input_size
w_new = ceil(w/input_size) * input_size
# scaled_h = h_new/h
# scaled_w = w_new/w
resized_image = cv2.resize(original_image, (w_new, h_new), cv2.INTER_LINEAR)
col_list = []
for i in range(h_new//input_size):
row_list = []
for j in range(w_new//input_size):
tiled_image_original = resized_image[i*input_size : (i+1)*input_size, j*input_size : (j+1)*input_size, :]
tiled_image = tiled_image_original / 255.
tiled_image = np.asarray(tiled_image).astype(np.float32)
interpreter.set_tensor(input_details[0]['index'], [tiled_image])
interpreter.invoke()
pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
scores=tf.reshape(pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
max_output_size_per_class=50,
max_total_size=50,
iou_threshold=iou,
score_threshold=score)
pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
row_list.append(utils.draw_bbox(tiled_image_original, pred_bbox))
col_list.append(cv2.hconcat(row_list))
scaled_image = cv2.vconcat(col_list)
reconstructed_image = cv2.resize(scaled_image, (w, h), interpolation = cv2.INTER_AREA)
end = time()
cv2_imshow(reconstructed_image)
print("time taken:", end-start)
# In[14]:
#%%time
detect_tiles_lite()
# In[ ]: