Spaces:
Runtime error
Runtime error
oskarastrom
commited on
Commit
•
8b2b08b
1
Parent(s):
c9d11b2
fixed padding
Browse files- dataloader.py +46 -8
- inference.py +1 -0
- scripts/inferEval.py +1 -1
- scripts/infer_frames.py +5 -4
dataloader.py
CHANGED
@@ -10,6 +10,7 @@ from contextlib import contextmanager
|
|
10 |
import torch
|
11 |
from torch.utils.data import Dataset
|
12 |
import torchvision.transforms as T
|
|
|
13 |
|
14 |
# assumes yolov5 on sys.path
|
15 |
from lib.yolov5.utils.general import xyxy2xywh
|
@@ -91,7 +92,7 @@ from torchvision.io import read_image
|
|
91 |
import re
|
92 |
|
93 |
class YOLOFrameDataset(Dataset):
|
94 |
-
def __init__(self, img_dir, img_size=896, batch_size=32):
|
95 |
self.img_dir = img_dir
|
96 |
self.img_size = img_size
|
97 |
|
@@ -99,11 +100,41 @@ class YOLOFrameDataset(Dataset):
|
|
99 |
self.files = list(filter(lambda f: f[-4:] == ".jpg", self.files))
|
100 |
self.files.sort(key=lambda f: int(re.sub('\D', '', f)))
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
n = len(self.files)
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
self.batches = []
|
105 |
for i in range(0,n,batch_size):
|
106 |
self.batches.append((i, min(n, i+batch_size)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
def __len__(self):
|
109 |
return len(self.batches)
|
@@ -112,21 +143,28 @@ class YOLOFrameDataset(Dataset):
|
|
112 |
for batch_idx in self.batches:
|
113 |
|
114 |
batch = []
|
|
|
115 |
shapes = []
|
116 |
for i in range(batch_idx[0], batch_idx[1]):
|
117 |
img_name = self.files[i]
|
118 |
img_path = os.path.join(self.img_dir, img_name)
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
img =
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
batch.append(img)
|
126 |
|
127 |
image = torch.stack(batch)
|
128 |
|
129 |
-
yield (image,
|
130 |
|
131 |
class ARISBatchedDataset(Dataset):
|
132 |
def __init__(self, aris_filepath, beam_width_dir, annotations_file, batch_size, num_frames_bg_subtract=1000, disable_output=False,
|
|
|
10 |
import torch
|
11 |
from torch.utils.data import Dataset
|
12 |
import torchvision.transforms as T
|
13 |
+
from PIL import Image
|
14 |
|
15 |
# assumes yolov5 on sys.path
|
16 |
from lib.yolov5.utils.general import xyxy2xywh
|
|
|
92 |
import re
|
93 |
|
94 |
class YOLOFrameDataset(Dataset):
|
95 |
+
def __init__(self, img_dir, img_size=896, batch_size=32, stride=64, pad=0.5):
|
96 |
self.img_dir = img_dir
|
97 |
self.img_size = img_size
|
98 |
|
|
|
100 |
self.files = list(filter(lambda f: f[-4:] == ".jpg", self.files))
|
101 |
self.files.sort(key=lambda f: int(re.sub('\D', '', f)))
|
102 |
|
103 |
+
temp_img = read_image(os.path.join(self.img_dir, self.files[0]))
|
104 |
+
size = temp_img.shape
|
105 |
+
|
106 |
+
self.ydim = size[1]
|
107 |
+
self.xdim = size[2]
|
108 |
+
|
109 |
n = len(self.files)
|
110 |
|
111 |
+
aspect_ratio = self.ydim / self.xdim
|
112 |
+
if aspect_ratio < 1:
|
113 |
+
shape = [aspect_ratio, 1]
|
114 |
+
elif aspect_ratio > 1:
|
115 |
+
shape = [1, 1 / aspect_ratio]
|
116 |
+
self.original_shape = (self.ydim, self.xdim)
|
117 |
+
self.shape = np.ceil(np.array(shape) * img_size / stride + pad).astype(int) * stride
|
118 |
+
|
119 |
self.batches = []
|
120 |
for i in range(0,n,batch_size):
|
121 |
self.batches.append((i, min(n, i+batch_size)))
|
122 |
+
|
123 |
+
@classmethod
|
124 |
+
def load_image(cls, img, img_size=896):
|
125 |
+
"""Loads and resizes 1 image from dataset, returns img, original hw, resized hw.
|
126 |
+
Modified from ScaledYOLOv4.datasets.load_image()
|
127 |
+
"""
|
128 |
+
|
129 |
+
h0, w0 = img.shape[:2]
|
130 |
+
h1, w1 = h0, w0
|
131 |
+
r = img_size / max(h0, w0)
|
132 |
+
if r != 1: # always resize down, only resize up if training with augmentation
|
133 |
+
interp = cv2.INTER_AREA if r < 1 else cv2.INTER_LINEAR
|
134 |
+
img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp)
|
135 |
+
h1, w1 = img.shape[:2]
|
136 |
+
|
137 |
+
return img, (h0, w0), (h1, w1) # img, hw_original, hw_resized
|
138 |
|
139 |
def __len__(self):
|
140 |
return len(self.batches)
|
|
|
143 |
for batch_idx in self.batches:
|
144 |
|
145 |
batch = []
|
146 |
+
labels = None
|
147 |
shapes = []
|
148 |
for i in range(batch_idx[0], batch_idx[1]):
|
149 |
img_name = self.files[i]
|
150 |
img_path = os.path.join(self.img_dir, img_name)
|
151 |
+
image = Image.open(img_path)
|
152 |
+
image = np.asarray(image)
|
153 |
+
img, (h0, w0), (h, w) = self.load_image(image, img_size=self.img_size)
|
154 |
+
|
155 |
+
# Letterbox
|
156 |
+
img, ratio, pad = letterbox(img, self.shape, auto=False, scaleup=False)
|
157 |
+
shape = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
|
158 |
+
img = img.transpose(2, 0, 1) # to -> C x H x W
|
159 |
+
img = np.ascontiguousarray(img)
|
160 |
+
img = torch.from_numpy(img)
|
161 |
+
|
162 |
+
shapes.append(shape)
|
163 |
batch.append(img)
|
164 |
|
165 |
image = torch.stack(batch)
|
166 |
|
167 |
+
yield (image, labels, shapes)
|
168 |
|
169 |
class ARISBatchedDataset(Dataset):
|
170 |
def __init__(self, aris_filepath, beam_width_dir, annotations_file, batch_size, num_frames_bg_subtract=1000, disable_output=False,
|
inference.py
CHANGED
@@ -125,6 +125,7 @@ def do_detection(dataloader, model, device, gp=None, batch_size=BATCH_SIZE, verb
|
|
125 |
size = tuple(img.shape)
|
126 |
nb, _, height, width = size # batch size, channels, height, width
|
127 |
|
|
|
128 |
# Run model & NMS
|
129 |
with torch.no_grad():
|
130 |
inf_out, _ = model(img, augment=False)
|
|
|
125 |
size = tuple(img.shape)
|
126 |
nb, _, height, width = size # batch size, channels, height, width
|
127 |
|
128 |
+
print(nb, _, height, width)
|
129 |
# Run model & NMS
|
130 |
with torch.no_grad():
|
131 |
inf_out, _ = model(img, augment=False)
|
scripts/inferEval.py
CHANGED
@@ -27,7 +27,7 @@ def main(args):
|
|
27 |
'min_hits': int(args.min_hits)
|
28 |
}
|
29 |
|
30 |
-
infer(infer_args, config=config)
|
31 |
|
32 |
evaluate("../frames/result_testing", "../frames/MOT", "../frames/metadata", "tracker", True)
|
33 |
|
|
|
27 |
'min_hits': int(args.min_hits)
|
28 |
}
|
29 |
|
30 |
+
infer(infer_args, config=config, verbose=False)
|
31 |
|
32 |
evaluate("../frames/result_testing", "../frames/MOT", "../frames/metadata", "tracker", True)
|
33 |
|
scripts/infer_frames.py
CHANGED
@@ -39,7 +39,7 @@ def main(args, config={}, verbose=True):
|
|
39 |
|
40 |
dirname = args.frames
|
41 |
|
42 |
-
locations = ["
|
43 |
for loc in locations:
|
44 |
|
45 |
in_loc_dir = os.path.join(dirname, loc)
|
@@ -59,9 +59,10 @@ def main(args, config={}, verbose=True):
|
|
59 |
for seq in seq_list:
|
60 |
pbar.update(1)
|
61 |
pbar.set_description("Processing " + seq)
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
idx += 1
|
66 |
in_seq_dir = os.path.join(in_loc_dir, seq)
|
67 |
infer_seq(in_seq_dir, out_dir, config, seq, model, device, metadata_path, verbose)
|
|
|
39 |
|
40 |
dirname = args.frames
|
41 |
|
42 |
+
locations = ["test"]
|
43 |
for loc in locations:
|
44 |
|
45 |
in_loc_dir = os.path.join(dirname, loc)
|
|
|
59 |
for seq in seq_list:
|
60 |
pbar.update(1)
|
61 |
pbar.set_description("Processing " + seq)
|
62 |
+
if verbose:
|
63 |
+
print(" ")
|
64 |
+
print("(" + str(idx) + "/" + str(len(seq_list)) + ") " + seq)
|
65 |
+
print(" ")
|
66 |
idx += 1
|
67 |
in_seq_dir = os.path.join(in_loc_dir, seq)
|
68 |
infer_seq(in_seq_dir, out_dir, config, seq, model, device, metadata_path, verbose)
|