Skip to content

Commit

Permalink
Improved BGR2RGB speeds (#3880)
Browse files Browse the repository at this point in the history
* Update BGR2RGB ops

* speed improvements

* cleanup
  • Loading branch information
glenn-jocher committed Jul 4, 2021
1 parent 9e8fb9f commit 3c3f8fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def forward(self, imgs, size=640, augment=False, profile=False):
files.append(Path(f).with_suffix('.jpg').name)
if im.shape[0] < 5: # image in CHW
im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input
im = im[..., :3] if im.ndim == 3 else np.tile(im[..., None], 3) # enforce 3ch input
s = im.shape[:2] # HWC
shape0.append(s) # image shape
g = (size / max(s)) # gain
Expand Down
9 changes: 4 additions & 5 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __next__(self):
img = letterbox(img0, self.img_size, stride=self.stride)[0]

# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB and HWC to CHW
img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
img = np.ascontiguousarray(img)

return path, img, img0, self.cap
Expand Down Expand Up @@ -264,7 +264,7 @@ def __next__(self):
img = letterbox(img0, self.img_size, stride=self.stride)[0]

# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB and HWC to CHW
img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
img = np.ascontiguousarray(img)

return img_path, img, img0, None
Expand Down Expand Up @@ -345,7 +345,7 @@ def __next__(self):
img = np.stack(img, 0)

# Convert
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB and BHWC to BCHW
img = img[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW
img = np.ascontiguousarray(img)

return self.sources, img, img0, None
Expand Down Expand Up @@ -526,7 +526,6 @@ def __getitem__(self, index):
if random.random() < hyp['mixup']:
img, labels = mixup(img, labels, *load_mosaic(self, random.randint(0, self.n - 1)))


else:
# Load image
img, (h0, w0), (h, w) = load_image(self, index)
Expand Down Expand Up @@ -579,7 +578,7 @@ def __getitem__(self, index):
labels_out[:, 1:] = torch.from_numpy(labels)

# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3 x img_height x img_width
img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
img = np.ascontiguousarray(img)

return torch.from_numpy(img), labels_out, self.img_files[index], shapes
Expand Down

0 comments on commit 3c3f8fb

Please sign in to comment.