Skip to content

Commit

Permalink
[Enhancement]Replace numpy ascontiguousarray with torch contiguous to…
Browse files Browse the repository at this point in the history
… speed-up (#2604)

## Motivation

Original motivation was after [MMDetection PR
#9533](open-mmlab/mmdetection#9533)

With several experiments I found out that if a ndarray is contiguous,
numpy.transpose + torch.contiguous perform better, while if not, then
use numpy.ascontiguousarray + numpy.transpose

## Modification

Replace numpy.ascontiguousarray with torch.contiguous in
[PackSegInputs](https://github.com/open-mmlab/mmsegmentation/blob/1.x/mmseg/datasets/transforms/formatting.py)

Co-authored-by: MeowZheng <meowzheng@outlook.com>
  • Loading branch information
csatsurnh and MeowZheng authored Feb 15, 2023
1 parent 9b8e8b7 commit 2e27f8b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mmseg/datasets/transforms/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ def transform(self, results: dict) -> dict:
img = results['img']
if len(img.shape) < 3:
img = np.expand_dims(img, -1)
img = np.ascontiguousarray(img.transpose(2, 0, 1))
packed_results['inputs'] = to_tensor(img)
if not img.flags.c_contiguous:
img = to_tensor(np.ascontiguousarray(img.transpose(2, 0, 1)))
else:
img = img.transpose(2, 0, 1)
img = to_tensor(img).contiguous()
packed_results['inputs'] = img

data_sample = SegDataSample()
if 'gt_seg_map' in results:
Expand Down

0 comments on commit 2e27f8b

Please sign in to comment.