Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified Inference #1045

Closed
wants to merge 39 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2be4a8c
initial commit
glenn-jocher Sep 25, 2020
c9ef269
batch inference update
glenn-jocher Sep 27, 2020
c599075
initial commit
glenn-jocher Sep 25, 2020
68c78a0
batch inference update
glenn-jocher Sep 27, 2020
c8141ba
Merge remote-tracking branch 'origin/simple_inference' into simple_in…
glenn-jocher Oct 10, 2020
623c568
add torch capability
glenn-jocher Oct 10, 2020
097aca2
empty image bug fix
glenn-jocher Oct 10, 2020
9896ce0
comment update
glenn-jocher Oct 10, 2020
11ea358
extract NMS to allow for augment
glenn-jocher Oct 10, 2020
82e865a
update NMS thresholds to CoreML defaults
glenn-jocher Oct 10, 2020
c2403d7
fuse() bug fix
glenn-jocher Oct 10, 2020
d87cf7e
Update requirements.txt coremltools==4.0
glenn-jocher Oct 11, 2020
d45e349
Rearrange export input after checks (#1118)
glenn-jocher Oct 11, 2020
10c85bf
FROM nvcr.io/nvidia/pytorch:20.09-py3
glenn-jocher Oct 11, 2020
0ada058
Generalized regression criterion renaming (#1120)
glenn-jocher Oct 11, 2020
00917a6
update expt name comment and folder parsing for training (#978)
Borda Oct 13, 2020
4346b13
Dataset download bash script updates (#1132)
glenn-jocher Oct 13, 2020
4d3680c
Minor import and spelling updates (#1133)
glenn-jocher Oct 13, 2020
c67e722
fix compatibility for hyper config (#1146)
Borda Oct 15, 2020
fe1d90a
fuse() bug fix
glenn-jocher Oct 10, 2020
70432a5
Update requirements.txt coremltools==4.0
glenn-jocher Oct 11, 2020
e63bf4d
Rearrange export input after checks (#1118)
glenn-jocher Oct 11, 2020
bfa2f89
FROM nvcr.io/nvidia/pytorch:20.09-py3
glenn-jocher Oct 11, 2020
402095a
Generalized regression criterion renaming (#1120)
glenn-jocher Oct 11, 2020
7363872
update expt name comment and folder parsing for training (#978)
Borda Oct 13, 2020
6088171
Dataset download bash script updates (#1132)
glenn-jocher Oct 13, 2020
330bdfb
Minor import and spelling updates (#1133)
glenn-jocher Oct 13, 2020
d7e6f4d
fix compatibility for hyper config (#1146)
Borda Oct 15, 2020
34282b0
update copied attributes
glenn-jocher Oct 15, 2020
8668c2b
optimize imports
glenn-jocher Oct 15, 2020
91a029a
initial commit
glenn-jocher Sep 25, 2020
a34b35b
batch inference update
glenn-jocher Sep 27, 2020
a9db87b
initial commit
glenn-jocher Sep 25, 2020
37a07a2
comment update
glenn-jocher Oct 10, 2020
5159d10
extract NMS to allow for augment
glenn-jocher Oct 10, 2020
0be772e
update NMS thresholds to CoreML defaults
glenn-jocher Oct 10, 2020
8144436
update copied attributes
glenn-jocher Oct 15, 2020
dc53110
optimize imports
glenn-jocher Oct 15, 2020
efa5e3f
Merge remote-tracking branch 'origin/simple_inference' into simple_in…
glenn-jocher Oct 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
batch inference update
  • Loading branch information
glenn-jocher committed Oct 15, 2020
commit a34b35b4b496d0e7ca15a900e1e664b47e115598
34 changes: 25 additions & 9 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,38 @@ def forward(self, x):


class autoShape(nn.Module):
# auto-reshape image size model wrapper
# auto-reshape input image model wrapper
img_size = 640 # inference size (pixels)

def __init__(self, model):
super(autoShape, self).__init__()
self.model = model

def forward(self, x, shape=640, augment=False, profile=False): # x = cv2.imread('img.jpg')
x0shape = x.shape[:2]
p = next(self.model.parameters())
x, ratio, (dw, dh) = letterbox(x, new_shape=make_divisible(shape or max(x0shape), int(self.stride.max())))
x1shape = x.shape[:2]
x = np.ascontiguousarray(x[:, :, ::-1].transpose(2, 0, 1)) # BGR to RGB, to 3x640x640
x = torch.from_numpy(x).to(p.device).type_as(p).unsqueeze(0) / 255. # uint8 to fp16/32
def forward(self, x, shape=640, augment=False, profile=False):
# x is cv2/np/PIL RGB image, or list of images for batched inference, i.e. x = Image.open('image.jpg')
p = next(self.model.parameters()) # for device and type
if not isinstance(x, list):
x = [x]
batch = range(len(x)) # batch size

shape0, shape1 = [], [] # image and inference shapes
for i in batch:
x[i] = np.array(x[i])[:, :, :3] # up to 3 channels if png
s = x[i].shape[:2] # HWC
shape0.append(s) # image shape
g = (shape / max(s)) # gain
shape1.append([y * g for y in s])
shape1 = [make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)] # inference shape

x = [letterbox(x[i], new_shape=shape1, auto=False)[0] for i in batch] # pad
x = np.stack(x, 0) if batch[-1] else x[0][None] # stack
x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW
x = torch.from_numpy(x).to(p.device).type_as(p) / 255. # uint8 to fp16/32

x = self.model(x, augment, profile) # forward
x[0][:, :4] = scale_coords(x1shape, x[0][:, :4], x0shape)

for i in batch:
x[i][:, :4] = scale_coords(shape1, x[i][:, :4], shape0[i]) # postprocess
return x


Expand Down