0
点赞
收藏
分享

微信扫一扫

NVIDIA DALI从入门到放弃之六:Geometric Transforms

seuleyang 2022-08-08 阅读 65

NVIDIA DALI从入门到放弃之一:概述

NVIDIA DALI从入门到放弃之二:入门示例

NVIDIA DALI从入门到放弃之三:Data Loading

NVIDIA DALI从入门到放弃之四:Multiple GPU

NVIDIA DALI从入门到放弃之五:Image Processing

NVIDIA DALI从入门到放弃之六:Geometric Transforms

NVIDIA DALI从入门到放弃之七:Sequence Processing

NVIDIA DALI从入门到放弃之八:PyTorch Plugin API

1 Catalogue

  • rotation - rotate by given angle (in degrees) around given point and axis (for 3D only)
  • translation - translate by given offset
  • scale - scale by given factor
  • shear - shear by given factors or angles; there are 2 shear factors for 2D and 6 factors for 3D
  • crop - translates and scales so that input corners (from_start, from_end) map to output corners (to_start, to_end).

2 多个转换组合(Combinining Multiple Transforms)

2-1 passing transform matrix

pipe = dali.pipeline.Pipeline(6, 3, 0, seed = 1234)
with pipe:
jpegs, _ = fn.file_reader(file_root=root_dir, files=image_files)
images = fn.image_decoder(jpegs, device="mixed")

size = encoded_images_sizes(jpegs)
center = size / 2

keypoints = fn.numpy_reader(file_root=root_dir, files=keypoint_files)
mt = fn.transforms.rotation(angle = fn.random.uniform(range=(-45, 45)), center = center)
mt = fn.transforms.translation(mt, offset = (400,0))
images = fn.warp_affine(images, matrix = mt, fill_value=0, inverse_map=False)
keypoints = fn.coord_transform(keypoints, MT = mt)
pipe.set_outputs(images, keypoints)

pipe.build()
images, keypoints = pipe.run()

2-2 transforms.combine

pipe = dali.pipeline.Pipeline(6, 3, 0, seed = 1234)
with pipe:
jpegs, _ = fn.file_reader(file_root=root_dir, files=image_files)
images = fn.image_decoder(jpegs, device="mixed")

size = encoded_images_sizes(jpegs)
center = size / 2

keypoints = fn.numpy_reader(file_root=root_dir, files=keypoint_files)
tr1 = fn.transforms.translation(offset=-center)
tr2 = fn.transforms.translation(offset=center)
rot = fn.transforms.rotation(angle = fn.random.uniform(range=(-45, 45)))
mt = fn.transforms.combine(tr1, rot, np.float32([[1, 1, 0],
[0, 1, 0]]), tr2)
images = fn.warp_affine(images, matrix = mt, fill_value=0, inverse_map=False)
keypoints = fn.coord_transform(keypoints, MT = mt)
pipe.set_outputs(images, keypoints)

pipe.build()
images, keypoints = pipe.run()

3 示例

pipe = dali.pipeline.Pipeline(6, 3, 0, seed = 1234)
with pipe:
jpegs, _ = fn.file_reader(file_root=root_dir, files=image_files)
images = fn.image_decoder(jpegs, device="mixed")
keypoints = fn.numpy_reader(file_root=root_dir, files=keypoint_files)

size = encoded_images_sizes(jpegs)
center = size / 2

outputs = []

transforms = [
fn.transforms.translation(offset = fn.random.uniform(range=(-100, 100), shape = 2)),
fn.transforms.rotation(angle = fn.random.uniform(range=(-45, 45)), center = center),
fn.transforms.scale(scale = fn.random.uniform(range=(0.5, 2), shape=[2]), center = center),
fn.transforms.shear(shear = fn.random.uniform(range=(-1, 1), shape=[2]), center = center),
fn.transforms.crop(from_start = size * 0.1, from_end = size * 0.8, to_start = [0,0], to_end=size*1.0)
]

for mt in transforms:
out_img = fn.warp_affine(images, matrix = mt, fill_value=0, inverse_map=False)
out_kp = fn.coord_transform(keypoints, MT = mt)
outputs += [out_img, out_kp]
pipe.set_outputs(*outputs)

pipe.build()
pipe_out = pipe.run()


举报

相关推荐

0 条评论