π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Added random param gen tests. Added device awareness for parameter generators. (#757)
shijianjianb2757da7e7b1
diff --git a/kornia/augmentation/random_generator/random_generator.py b/kornia/augmentation/random_generator/random_generator.pyindex 8d184b350..4342d6ceb 100644--- a/kornia/augmentation/random_generator/random_generator.py+++ b/kornia/augmentation/random_generator/random_generator.py@@ -11,6 +11,8 @@ from ..utils import (_adapted_uniform,_adapted_beta,_joint_range_check,+ _common_param_check,+ _extract_device_dtype,)@@ -26,8 +28,9 @@ def random_prob_generator(Returns:torch.Tensor: parameters to be passed for transformation."""- if not isinstance(p, float):- raise TypeError(f"The probability should be a float number. Got {type(p)}")+ _common_param_check(batch_size, same_on_batch)+ if not isinstance(p, (int, float)) or p > 1 or p < 0:+ raise TypeError(f"The probability should be a float number within [0, 1]. Got {type(p)}.")probs: torch.Tensor = _adapted_sampling((batch_size,), Bernoulli(p), same_on_batch).bool()@@ -46,19 +49,29 @@ def random_color_jitter_generator(Args:batch_size (int): the number of images.- brightness (torch.Tensor, optional): Default value is 0.- contrast (torch.Tensor, optional): Default value is 0.- saturation (torch.Tensor, optional): Default value is 0.- hue (torch.Tensor, optional): Default value is 0.+ brightness (torch.Tensor, optional): Brightness factor tensor of range (a, b).+ The provided range must follow 0 <= a <= b <= 2. Default value is [0., 0.].+ contrast (torch.Tensor, optional): Contrast factor tensor of range (a, b).+ The provided range must follow 0 <= a <= b. Default value is [0., 0.].+ saturation (torch.Tensor, optional): Saturation factor tensor of range (a, b).+ The provided range must follow 0 <= a <= b. Default value is [0., 0.].+ hue (torch.Tensor, optional): Saturation factor tensor of range (a, b).+ The provided range must follow -0.5 <= a <= b < 0.5. Default value is [0., 0.].same_on_batch (bool): apply the same transformation across the batch. Default: False.Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""- brightness = torch.tensor(0.) if brightness is None else cast(torch.Tensor, brightness)- contrast = torch.tensor(0.) if contrast is None else cast(torch.Tensor, contrast)- hue = torch.tensor(0.) if hue is None else cast(torch.Tensor, hue)- saturation = torch.tensor(0.) if saturation is None else cast(torch.Tensor, saturation)+ _common_param_check(batch_size, same_on_batch)+ device, dtype = _extract_device_dtype([brightness, contrast, hue, saturation])+ brightness = torch.tensor([0., 0.], device=device, dtype=dtype) \+ if brightness is None else cast(torch.Tensor, brightness)+ contrast = torch.tensor([0., 0.], device=device, dtype=dtype) \+ if contrast is None else cast(torch.Tensor, contrast)+ hue = torch.tensor([0., 0.], device=device, dtype=dtype) \+ if hue is None else cast(torch.Tensor, hue)+ saturation = torch.tensor([0., 0.], device=device, dtype=dtype) \+ if saturation is None else cast(torch.Tensor, saturation)_joint_range_check(brightness, "brightness", (0, 2))_joint_range_check(contrast, "contrast", (0, float('inf')))@@ -96,15 +109,18 @@ def random_perspective_generator(Returns:params (Dict[str, torch.Tensor])"""+ _common_param_check(batch_size, same_on_batch)assert distortion_scale.dim() == 0 and 0 <= distortion_scale <= 1, \- f"'distortion_scale' must be a scalar within [0, 1]. Got {distortion_scale}"+ f"'distortion_scale' must be a scalar within [0, 1]. Got {distortion_scale}."+ assert type(height) == int and height > 0 and type(width) == int and width > 0, \+ f"'height' and 'width' must be integers. Got {height}, {width}."start_points: torch.Tensor = torch.tensor([[[0., 0],[width - 1, 0],[width - 1, height - 1],[0, height - 1],- ]]).expand(batch_size, -1, -1)+ ]], device=distortion_scale.device, dtype=distortion_scale.dtype).expand(batch_size, -1, -1)# generate random offset not larger than half of the imagefx = distortion_scale * width / 2@@ -113,14 +129,15 @@ def random_perspective_generator(factor = torch.stack([fx, fy], dim=0).view(-1, 1, 2)# TODO: This line somehow breaks the gradcheck- rand_val: torch.Tensor = _adapted_uniform(start_points.shape, 0, 1, same_on_batch)+ rand_val: torch.Tensor = _adapted_uniform(start_points.shape, 0, 1, same_on_batch).to(+ device=distortion_scale.device, dtype=distortion_scale.dtype)pts_norm = torch.tensor([[[1, 1],[-1, 1],[-1, -1],[1, -1]- ]])+ ]], device=distortion_scale.device, dtype=distortion_scale.dtype)end_points = start_points + factor * rand_val * pts_normreturn dict(start_points=start_points,@@ -143,28 +160,27 @@ def random_affine_generator(batch_size (int): the tensor batch size.height (int) : height of the image.width (int): width of the image.- degrees (float or tuple): Range of degrees to select from.- If degrees is a number instead of sequence like (min, max), the range of degrees- will be (-degrees, +degrees). Set to 0 to deactivate rotations.- translate (tuple, optional): tuple of maximum absolute fraction for horizontal+ degrees (tensor): Range of degrees to select from like (min, max).+ translate (tensor, optional): tuple of maximum absolute fraction for horizontaland vertical translations. For example translate=(a, b), then horizontal shiftis randomly sampled in the range -img_width * a < dx < img_width * a and vertical shift israndomly sampled in the range -img_height * b < dy < img_height * b. Will not translate by default.- scale (tuple, optional): scaling factor interval, e.g (a, b), then scale is+ scale (tensor, optional): scaling factor interval, e.g (a, b), then scale israndomly sampled from the range a <= scale <= b. Will keep original scale by default.- shear (sequence or float, optional): Range of degrees to select from.- If shear is a number, a shear parallel to the x axis in the range (-shear, +shear)- will be apllied. Else if shear is a tuple or list of 2 values a shear parallel to the x axis in the- range (shear[0], shear[1]) will be applied. Else if shear is a tuple or list of 4 values,- a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied.- Will not apply shear by default+ shear (tensor, optional): Range of degrees to select from.+ Shear is a 2x2 tensor, a x-axis shear in (shear[0][0], shear[0][1]) and y-axis shear in+ (shear[1][0], shear[1][1]) will be applied. Will not apply shear by default.same_on_batch (bool): apply the same transformation across the batch. Default: False.Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size, same_on_batch)_joint_range_check(degrees, "degrees")+ assert isinstance(width, (int,)) and isinstance(height, (int,)) and width > 0 and height > 0, \+ f"`width` and `height` must be positive integers. Got {width}, {height}."+ device, dtype = _extract_device_dtype([degrees, translate, scale, shear])angle = _adapted_uniform((batch_size,), degrees[0], degrees[1], same_on_batch)# compute tensor ranges@@ -175,7 +191,7 @@ def random_affine_generator(_joint_range_check(cast(torch.Tensor, scale[2:]), "scale_y")_scale[:, 1] = _adapted_uniform((batch_size,), scale[2], scale[3], same_on_batch)else:- _scale = torch.ones((batch_size, 2))+ _scale = torch.ones((batch_size, 2), device=device, dtype=dtype)if translate is not None:_joint_range_check(cast(torch.Tensor, translate), "translate")@@ -186,10 +202,10 @@ def random_affine_generator(_adapted_uniform((batch_size,), -max_dy, max_dy, same_on_batch)], dim=-1)else:- translations = torch.zeros(batch_size, 2)+ translations = torch.zeros((batch_size, 2), device=device, dtype=dtype)center: torch.Tensor = torch.tensor(- [width, height], dtype=torch.float32).view(1, 2) / 2. - 0.5+ [width, height], device=device, dtype=dtype).view(1, 2) / 2. - 0.5center = center.expand(batch_size, -1)if shear is not None:@@ -223,6 +239,7 @@ def random_rotation_generator(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size, same_on_batch)_joint_range_check(degrees, "degrees")degrees = _adapted_uniform((batch_size,), degrees[0], degrees[1], same_on_batch)@@ -287,6 +304,7 @@ def random_crop_generator([27, 24],[ 0, 24]]])}"""+ _common_param_check(batch_size, same_on_batch)if not isinstance(size, torch.Tensor):size = torch.tensor(size).repeat(batch_size, 1)assert size.shape == torch.Size([batch_size, 2]), \@@ -312,14 +330,16 @@ def random_crop_generator(if resize_to is None:crop_dst = bbox_generator(- torch.tensor([0] * batch_size), torch.tensor([0] * batch_size), size[:, 1] - 1, size[:, 0] - 1)+ torch.tensor([0] * batch_size, device=x_start.device, dtype=x_start.dtype),+ torch.tensor([0] * batch_size, device=x_start.device, dtype=x_start.dtype),+ size[:, 1] - 1, size[:, 0] - 1)else:crop_dst = torch.tensor([[[0, 0],[resize_to[1] - 1, 0],[resize_to[1] - 1, resize_to[0] - 1],[0, resize_to[0] - 1],- ]]).repeat(batch_size, 1, 1)+ ]], device=x_start.device, dtype=x_start.dtype).repeat(batch_size, 1, 1)return dict(src=crop_src,dst=crop_dst)@@ -351,8 +371,11 @@ def random_crop_size_generator([27, 28],[25, 28]], dtype=torch.int32)}"""+ _common_param_check(batch_size, same_on_batch)_joint_range_check(scale, "scale")_joint_range_check(ratio, "ratio")+ assert len(size) == 2 and type(size[0]) == int and size[1] > 0 and type(size[1]) == int and size[1] > 0, \+ f"'height' and 'width' must be integers. Got {size}."# 10 trails for each elementarea = _adapted_uniform(@@ -364,31 +387,31 @@ def random_crop_size_generator(w = torch.sqrt(area * aspect_ratio).int()h = torch.sqrt(area / aspect_ratio).int()# Element-wise w, h condition- cond = ((0 < h) * (h < size[1]) * (0 < w) * (w < size[0])).int()+ cond = ((0 < w) * (w < size[1]) * (0 < h) * (h < size[0])).int()cond_bool = torch.sum(cond, dim=1) > 0- w_out = w[torch.arange(0, batch_size), torch.argmax(cond, dim=1)]- h_out = h[torch.arange(0, batch_size), torch.argmax(cond, dim=1)]+ h_out = w[torch.arange(0, batch_size), torch.argmax(cond, dim=1)]+ w_out = h[torch.arange(0, batch_size), torch.argmax(cond, dim=1)]if not cond_bool.all():# Fallback to center cropin_ratio = float(size[0]) / float(size[1])if (in_ratio < min(ratio)):- w_ct = torch.tensor(size[0])- h_ct = torch.round(w_ct / min(ratio))+ h_ct = torch.tensor(size[0])+ w_ct = torch.round(h_ct / min(ratio))elif (in_ratio > max(ratio)):- h_ct = torch.tensor(size[1])- w_ct = torch.round(h_ct * max(ratio))+ w_ct = torch.tensor(size[1])+ h_ct = torch.round(w_ct * max(ratio))else: # whole image- w_ct = torch.tensor(size[0])- h_ct = torch.tensor(size[1])- w_ct = w_ct.int()+ h_ct = torch.tensor(size[0])+ w_ct = torch.tensor(size[1])h_ct = h_ct.int()+ w_ct = w_ct.int()- w_out = w_out.where(cond_bool, w_ct)h_out = h_out.where(cond_bool, h_ct)+ w_out = w_out.where(cond_bool, w_ct)- return dict(size=torch.stack([w_out, h_out], dim=1))+ return dict(size=torch.stack([h_out, w_out], dim=1))def random_rectangles_params_generator(@@ -414,17 +437,29 @@ def random_rectangles_params_generator(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size, same_on_batch)+ device, dtype = _extract_device_dtype([ratio, scale])+ assert type(height) == int and height > 0 and type(width) == int and width > 0, \+ f"'height' and 'width' must be integers. Got {height}, {width}."+ assert isinstance(value, (int, float)) and value >= 0 and value <= 1, \+ f"'value' must be a number between 0 - 1. Got {value}."_joint_range_check(scale, 'scale', bounds=(0, float('inf')))_joint_range_check(ratio, 'ratio', bounds=(0, float('inf')))- zeros = torch.zeros((batch_size,))images_area = height * widthtarget_areas = _adapted_uniform((batch_size,), scale[0], scale[1], same_on_batch) * images_areaif ratio[0] < 1. and ratio[1] > 1.:aspect_ratios1 = _adapted_uniform((batch_size,), ratio[0], 1, same_on_batch)aspect_ratios2 = _adapted_uniform((batch_size,), 1, ratio[1], same_on_batch)- rand_idxs = torch.round(torch.rand((batch_size,))).bool()+ if same_on_batch:+ rand_idxs = torch.round(_adapted_uniform(+ (1,), torch.tensor(0, device=device, dtype=dtype),+ torch.tensor(1, device=device, dtype=dtype), same_on_batch)).repeat(batch_size).bool()+ else:+ rand_idxs = torch.round(_adapted_uniform(+ (batch_size,), torch.tensor(0, device=device, dtype=dtype),+ torch.tensor(1, device=device, dtype=dtype), same_on_batch)).bool()aspect_ratios = torch.where(rand_idxs, aspect_ratios1, aspect_ratios2)else:aspect_ratios = _adapted_uniform((batch_size,), ratio[0], ratio[1], same_on_batch)@@ -432,24 +467,31 @@ def random_rectangles_params_generator(# based on target areas and aspect ratios, rectangle params are computedheights = torch.min(torch.max(torch.round((target_areas * aspect_ratios) ** (1 / 2)),- torch.tensor(1.)),- torch.tensor(float(height))- ).int()+ torch.tensor(1., device=device, dtype=dtype)),+ torch.tensor(height, device=device, dtype=dtype)+ )widths = torch.min(torch.max(torch.round((target_areas / aspect_ratios) ** (1 / 2)),- torch.tensor(1.)),- torch.tensor(float(width))- ).int()+ torch.tensor(1., device=device, dtype=dtype)),+ torch.tensor(width, device=device, dtype=dtype)+ )- xs = (_adapted_uniform((batch_size,), 0, 1, same_on_batch) * (torch.tensor(width) - widths + 1).float()).int()- ys = (_adapted_uniform((batch_size,), 0, 1, same_on_batch) * (torch.tensor(height) - heights + 1).float()).int()+ xs_ratio = _adapted_uniform(+ (batch_size,), torch.tensor(0, device=device, dtype=dtype),+ torch.tensor(1, device=device, dtype=dtype), same_on_batch)+ ys_ratio = _adapted_uniform(+ (batch_size,), torch.tensor(0, device=device, dtype=dtype),+ torch.tensor(1, device=device, dtype=dtype), same_on_batch)- return dict(widths=widths,- heights=heights,- xs=xs,- ys=ys,- values=torch.tensor([value] * batch_size))+ xs = xs_ratio * (torch.tensor(width, device=device, dtype=dtype) - widths + 1)+ ys = ys_ratio * (torch.tensor(height, device=device, dtype=dtype) - heights + 1)++ return dict(widths=widths.int(),+ heights=heights.int(),+ xs=xs.int(),+ ys=ys.int(),+ values=torch.tensor([value] * batch_size, device=device, dtype=dtype))def center_crop_generator(@@ -469,9 +511,14 @@ def center_crop_generator(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size)if not isinstance(size, (tuple, list,)) and len(size) == 2:raise ValueError("Input size must be a tuple/list of length 2. Got {}".format(size))+ assert type(height) == int and height > 0 and type(width) == int and width > 0, \+ f"'height' and 'width' must be integers. Got {height}, {width}."+ assert height >= size[0] and width >= size[1], \+ f"Crop size must be smaller than input size. Got ({height}, {width}) and {size}."# unpack input sizesdst_h, dst_w = size@@ -496,7 +543,7 @@ def center_crop_generator([end_x, start_y],[end_x, end_y],[start_x, end_y],- ]])+ ]]).expand(batch_size, -1, -1).long()# [y, x] destination# top-left, top-right, bottom-right, bottom-left@@ -505,7 +552,7 @@ def center_crop_generator([dst_w - 1, 0],[dst_w - 1, dst_h - 1],[0, dst_h - 1],- ]]).expand(points_src.shape[0], -1, -1)+ ]]).expand(batch_size, -1, -1).long()return dict(src=points_src,dst=points_dst)@@ -532,13 +579,17 @@ def random_motion_blur_generator(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size, same_on_batch)_joint_range_check(angle, 'angle')- _joint_range_check(direction, 'direction')+ _joint_range_check(direction, 'direction', (-1, 1))++ device, dtype = _extract_device_dtype([angle, direction])if isinstance(kernel_size, int):- ksize_factor = torch.tensor([kernel_size] * batch_size)+ ksize_factor = torch.tensor([kernel_size] * batch_size, device=device, dtype=dtype)elif isinstance(kernel_size, tuple):# kernel_size is fixed across the batch+ assert len(kernel_size) == 2, f"`kernel_size` must be (2,) if it is a tuple. Got {kernel_size}."ksize_factor = _adapted_uniform((batch_size,), kernel_size[0] // 2, kernel_size[1] // 2, same_on_batch=True).int() * 2 + 1else:@@ -550,7 +601,7 @@ def random_motion_blur_generator(direction_factor = _adapted_uniform((batch_size,), direction[0], direction[1], same_on_batch)- return dict(ksize_factor=ksize_factor,+ return dict(ksize_factor=ksize_factor.int(),angle_factor=angle_factor,direction_factor=direction_factor)@@ -569,15 +620,16 @@ def random_solarize_generator(Args:batch_size (int): the number of images.thresholds (torch.Tensor): Pixels less than threshold will selected. Otherwise, subtract 1.0 from the pixel.- Default value will be sampled from [0.4, 0.6].+ Takes in a range tensor of (0, 1). Default value will be sampled from [0.4, 0.6].additions (torch.Tensor): The value is between -0.5 and 0.5. Default value will be sampled from [-0.1, 0.1]same_on_batch (bool): apply the same transformation across the batch. Default: False.Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""- _joint_range_check(thresholds, 'thresholds')- _joint_range_check(additions, 'additions')+ _common_param_check(batch_size, same_on_batch)+ _joint_range_check(thresholds, 'thresholds', (0, 1))+ _joint_range_check(additions, 'additions', (-0.5, 0.5))thresholds_factor = _adapted_uniform((batch_size,), thresholds[0], thresholds[1], same_on_batch)@@ -593,19 +645,21 @@ def random_solarize_generator(def random_posterize_generator(batch_size: int,- bits: torch.Tensor = torch.tensor(3),+ bits: torch.Tensor = torch.tensor([3, 5]),same_on_batch: bool = False) -> Dict[str, torch.Tensor]:r"""Generate random posterize parameters for a batch of images.Args:batch_size (int): the number of images.- bits (int or tuple): Default value is 3. Integer that ranged from 0 ~ 8.+ bits (int or tuple): Takes in an integer tuple tensor that ranged from 0 ~ 8. Default value is [3, 5].same_on_batch (bool): apply the same transformation across the batch. Default: False.Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size, same_on_batch)+ _joint_range_check(bits, 'bits', (0, 8))bits_factor = _adapted_uniform((batch_size,), bits[0], bits[1], same_on_batch).int()return dict(@@ -628,6 +682,7 @@ def random_sharpness_generator(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ _common_param_check(batch_size, same_on_batch)_joint_range_check(sharpness, 'sharpness', bounds=(0, float('inf')))sharpness_factor = _adapted_uniform((batch_size,), sharpness[0], sharpness[1], same_on_batch)@@ -660,15 +715,16 @@ def random_mixup_generator(>>> random_mixup_generator(5, 0.7){'mixup_pairs': tensor([4, 0, 3, 1, 2]), 'mixup_lambdas': tensor([0.6323, 0.0000, 0.4017, 0.0223, 0.1689])}"""+ _common_param_check(batch_size, same_on_batch)if lambda_val is None:- lambda_val = torch.tensor([0., 1.])+ lambda_val = torch.tensor([0., 1.], dtype=torch.float64)_joint_range_check(lambda_val, 'lambda_val', bounds=(0, 1))batch_probs: torch.Tensor = random_prob_generator(batch_size, p, same_on_batch=same_on_batch)mixup_pairs: torch.Tensor = torch.randperm(batch_size)mixup_lambdas: torch.Tensor = _adapted_uniform((batch_size,), lambda_val[0], lambda_val[1], same_on_batch=same_on_batch)- mixup_lambdas = mixup_lambdas * batch_probs.float()+ mixup_lambdas = mixup_lambdas * batch_probs.to(device=lambda_val.device, dtype=lambda_val.dtype)return dict(mixup_pairs=mixup_pairs,@@ -694,9 +750,9 @@ def random_cutmix_generator(height (int): image height.p (float): probability of applying cutmix.num_mix (int): number of images to mix with. Default is 1.- beta (float or torch.Tensor, optional): hyperparameter for generating cut size from beta distribution.+ beta (torch.Tensor, optional): hyperparameter for generating cut size from beta distribution.If None, it will be set to 1.- cut_size ((float, float) or torch.Tensor, optional): controlling the minimum and maximum cut ratio from [0, 1].+ cut_size (torch.Tensor, optional): controlling the minimum and maximum cut ratio from [0, 1].If None, it will be set to [0, 1], which means no restriction.same_on_batch (bool): apply the same transformation across the batch. Default: False.@@ -739,15 +795,20 @@ def random_cutmix_generator([ 97, 69]]]])}"""+ device, dtype = _extract_device_dtype([beta, cut_size])if beta is None:- beta = torch.tensor(1.)+ beta = torch.tensor(1., device=device, dtype=dtype)if cut_size is None:- cut_size = torch.tensor([0., 1.])+ cut_size = torch.tensor([0., 1.], device=device, dtype=dtype)assert num_mix >= 1 and isinstance(num_mix, (int,)), \f"`num_mix` must be an integer greater than 1. Got {num_mix}."+ assert type(height) == int and height > 0 and type(width) == int and width > 0, \+ f"'height' and 'width' must be integers. Got {height}, {width}."_joint_range_check(cut_size, 'cut_size', bounds=(0, 1))+ _common_param_check(batch_size, same_on_batch)- batch_probs: torch.Tensor = random_prob_generator(batch_size * num_mix, p, same_on_batch)+ batch_probs: torch.Tensor = random_prob_generator(+ batch_size * num_mix, p, same_on_batch).to(device=device, dtype=dtype)mix_pairs: torch.Tensor = torch.rand(num_mix, batch_size).argsort(dim=1)cutmix_betas: torch.Tensor = _adapted_beta((batch_size * num_mix,), beta, beta, same_on_batch=same_on_batch)# Note: torch.clamp does not accept tensor, cutmix_betas.clamp(cut_size[0], cut_size[1]) throws:@@ -766,9 +827,11 @@ def random_cutmix_generator(# Reserve at least 1 pixel for cropping.x_start = _adapted_uniform(- _gen_shape, torch.zeros_like(cut_width, dtype=torch.float32), width - cut_width - 1, same_on_batch).long()+ _gen_shape, torch.zeros_like(cut_width, device=device, dtype=torch.long),+ (width - cut_width - 1).to(device=device, dtype=torch.long), same_on_batch)y_start = _adapted_uniform(- _gen_shape, torch.zeros_like(cut_height, dtype=torch.float32), height - cut_height - 1, same_on_batch).long()+ _gen_shape, torch.zeros_like(cut_height, device=device, dtype=torch.long),+ (height - cut_height - 1).to(device=device, dtype=torch.long), same_on_batch)crop_src = bbox_generator(x_start.squeeze(), y_start.squeeze(), cut_width, cut_height)diff --git a/kornia/augmentation/random_generator/random_generator3d.py b/kornia/augmentation/random_generator/random_generator3d.pyindex 61f8a9785..43187619a 100644--- a/kornia/augmentation/random_generator/random_generator3d.py+++ b/kornia/augmentation/random_generator/random_generator3d.py@@ -9,6 +9,7 @@ from kornia.geometry import bbox_generator3dfrom ..utils import (_adapted_uniform,_tuple_range_reader,+ _extract_device_dtype)@@ -61,17 +62,14 @@ def random_affine_generator3d(scale (torch.Tensor, optional): scaling factor interval, e.g (a, b), then scale israndomly sampled from the range a <= scale <= b. Will keep original scale by default.shear (sequence or float, optional): Range of degrees to select from.- If shear is a number, a shear to the 6 facets in the range (-shear, +shear) will be apllied.- If shear is a tuple of 2 values, a shear to the 6 facets in the range (shear[0], shear[1]) will be applied.- If shear is a tuple of 6 values, a shear to the i-th facet in the range (-shear[i], shear[i])- will be applied.- If shear is a tuple of 6 tuples, a shear to the i-th facet in the range (-shear[i, 0], shear[i, 1])- will be applied.+ Shaped as (6, 2) for 6 facet (xy, xz, yx, yz, zx, zy).+ The shear to the i-th facet in the range (-shear[i, 0], shear[i, 1]) will be applied.same_on_batch (bool): apply the same transformation across the batch. Default: FalseReturns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ device, dtype = _extract_device_dtype([degrees, translate, scale, shears])assert degrees.shape == torch.Size([3, 2]), f"'degrees' must be the shape of (3, 2). Got {degrees.shape}."yaw = _adapted_uniform((batch_size,), degrees[0][0], degrees[0][1], same_on_batch)pitch = _adapted_uniform((batch_size,), degrees[1][0], degrees[1][1], same_on_batch)@@ -87,7 +85,7 @@ def random_affine_generator3d(_adapted_uniform((batch_size,), scale[2, 0], scale[2, 1], same_on_batch),], dim=1)else:- scale = torch.ones(batch_size).repeat(1, 3)+ scale = torch.ones(batch_size, device=device, dtype=dtype).repeat(1, 3)if translate is not None:assert translate.shape == torch.Size([3]), f"'translate' must be the shape of (2). Got {translate.shape}."@@ -101,11 +99,11 @@ def random_affine_generator3d(_adapted_uniform((batch_size,), -max_dz, max_dz, same_on_batch)], dim=1)else:- translations = torch.zeros(batch_size, 3)+ translations = torch.zeros((batch_size, 3), device=device, dtype=dtype)# center should be in x,y,zcenter: torch.Tensor = torch.tensor(- [width, height, depth], dtype=torch.float32).view(1, 3) / 2. - 0.5+ [width, height, depth], device=device, dtype=dtype).view(1, 3) / 2. - 0.5center = center.expand(batch_size, -1)if shears is not None:@@ -117,7 +115,7 @@ def random_affine_generator3d(szx = _adapted_uniform((batch_size,), shears[4, 0], shears[4, 1], same_on_batch)szy = _adapted_uniform((batch_size,), shears[5, 0], shears[5, 1], same_on_batch)else:- sxy = sxz = syx = syz = szx = szy = torch.tensor([0] * batch_size)+ sxy = sxz = syx = syz = szx = szy = torch.tensor([0] * batch_size, device=device, dtype=dtype)return dict(translations=translations,center=center,@@ -153,12 +151,14 @@ def random_motion_blur_generator3d(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ device, dtype = _extract_device_dtype([angle, direction])if isinstance(kernel_size, int):- ksize_factor = torch.tensor([kernel_size] * batch_size)+ ksize_factor = torch.tensor([kernel_size] * batch_size, device=device, dtype=dtype)elif isinstance(kernel_size, tuple):# kernel_size is fixed across the batchksize_factor = _adapted_uniform((batch_size,), kernel_size[0] // 2, kernel_size[1] // 2, same_on_batch=True).int() * 2 + 1+ ksize_factor = ksize_factor.to(device=device, dtype=dtype)else:raise TypeError(f"Unsupported type: {type(kernel_size)}")@@ -171,7 +171,7 @@ def random_motion_blur_generator3d(direction_factor = _adapted_uniform((batch_size,), direction[0], direction[1], same_on_batch)- return dict(ksize_factor=ksize_factor,+ return dict(ksize_factor=ksize_factor.int(),angle_factor=angle_factor,direction_factor=direction_factor)@@ -195,6 +195,8 @@ def center_crop_generator3d(Returns:params Dict[str, torch.Tensor]: parameters to be passed for transformation."""+ # TODO: This function does not accept tensors at all.+ # Can't infer the device and dtypeif not isinstance(size, (tuple, list,)) and len(size) == 3:raise ValueError("Input size must be a tuple/list of length 3. Got {}".format(size))@@ -245,8 +247,8 @@ def center_crop_generator3d([dst_w - 1, dst_h - 1, dst_d - 1],[0, dst_h - 1, dst_d - 1],]]).expand(points_src.shape[0], -1, -1)- return dict(src=points_src,- dst=points_dst)+ return dict(src=points_src.long(),+ dst=points_dst.long())def random_crop_generator3d(@@ -271,8 +273,12 @@ def random_crop_generator3d("""if not isinstance(size, torch.Tensor):size = torch.tensor(size).repeat(batch_size, 1)+ device, dtype = size.device, size.dtypeassert size.shape == torch.Size([batch_size, 3]), \f"If `size` is a tensor, it must be shaped as (B, 3). Got {size.shape}."+ assert len(input_size) == 3 and isinstance(input_size[0], (int,)) and isinstance(input_size[1], (int,)) \+ and isinstance(input_size[2], (int,)) and input_size[0] > 0 and input_size[1] > 0 and input_size[2] > 0, \+ f"`input_size` must be a tuple of 3 positive integers. Got {input_size}."x_diff = input_size[2] - size[:, 2] + 1y_diff = input_size[1] - size[:, 1] + 1@@ -292,14 +298,26 @@ def random_crop_generator3d(y_start = _adapted_uniform((1,), 0, y_diff, same_on_batch).long()z_start = _adapted_uniform((1,), 0, z_diff, same_on_batch).long()- crop_src = bbox_generator3d(x_start.view(-1), y_start.view(-1), z_start.view(-1),- size[:, 2] - 1, size[:, 1] - 1, size[:, 0] - 1)+ crop_src = bbox_generator3d(+ x_start.view(-1).to(device=device, dtype=dtype),+ y_start.view(-1).to(device=device, dtype=dtype),+ z_start.view(-1).to(device=device, dtype=dtype),+ size[:, 2] - 1,+ size[:, 1] - 1,+ size[:, 0] - 1)if resize_to is None:crop_dst = bbox_generator3d(- torch.tensor([0] * batch_size), torch.tensor([0] * batch_size), torch.tensor([0] * batch_size),- size[:, 2] - 1, size[:, 1] - 1, size[:, 0] - 1)+ torch.tensor([0] * batch_size, device=device, dtype=dtype),+ torch.tensor([0] * batch_size, device=device, dtype=dtype),+ torch.tensor([0] * batch_size, device=device, dtype=dtype),+ size[:, 2] - 1,+ size[:, 1] - 1,+ size[:, 0] - 1)else:+ assert len(resize_to) == 3 and isinstance(resize_to[0], (int,)) and isinstance(resize_to[1], (int,)) \+ and isinstance(resize_to[2], (int,)) and resize_to[0] > 0 and resize_to[1] > 0 and resize_to[2] > 0, \+ f"`resize_to` must be a tuple of 3 positive integers. Got {resize_to}."crop_dst = torch.tensor([[[0, 0, 0],[resize_to[-1] - 1, 0, 0],@@ -309,7 +327,7 @@ def random_crop_generator3d([resize_to[-1] - 1, 0, resize_to[-3] - 1],[resize_to[-1] - 1, resize_to[-2] - 1, resize_to[-3] - 1],[0, resize_to[-2] - 1, resize_to[-3] - 1],- ]]).repeat(batch_size, 1, 1)+ ]], device=device, dtype=dtype).repeat(batch_size, 1, 1)return dict(src=crop_src,dst=crop_dst)@@ -338,6 +356,7 @@ def random_perspective_generator3d("""assert distortion_scale.dim() == 0 and 0 <= distortion_scale <= 1, \f"'distortion_scale' must be a scalar within [0, 1]. Got {distortion_scale}"+ device, dtype = distortion_scale.device, distortion_scale.dtypestart_points: torch.Tensor = torch.tensor([[[0., 0, 0],@@ -348,7 +367,7 @@ def random_perspective_generator3d([width - 1, 0, depth - 1],[width - 1, height - 1, depth - 1],[0, height - 1, depth - 1],- ]]).expand(batch_size, -1, -1)+ ]], device=device, dtype=dtype).expand(batch_size, -1, -1)# generate random offset not larger than half of the imagefx = distortion_scale * width / 2@@ -358,7 +377,8 @@ def random_perspective_generator3d(factor = torch.stack([fx, fy, fz], dim=0).view(-1, 1, 3)# TODO: This line somehow breaks the gradcheck- rand_val: torch.Tensor = _adapted_uniform(start_points.shape, 0, 1, same_on_batch)+ rand_val: torch.Tensor = _adapted_uniform(start_points.shape, 0, 1, same_on_batch).to(+ device=device, dtype=dtype)pts_norm = torch.tensor([[[1, 1, 1],@@ -369,7 +389,7 @@ def random_perspective_generator3d([-1, 1, -1],[-1, -1, -1],[1, -1, -1],- ]])+ ]], device=device, dtype=dtype)end_points = start_points + factor * rand_val * pts_normreturn dict(start_points=start_points,diff --git a/kornia/augmentation/utils/__init__.py b/kornia/augmentation/utils/__init__.pyindex 74eff580a..dbf471251 100644--- a/kornia/augmentation/utils/__init__.py+++ b/kornia/augmentation/utils/__init__.py@@ -1,4 +1,5 @@from kornia.augmentation.utils.param_validation import (+ _common_param_check,_range_bound,_joint_range_check,_singular_range_check,@@ -17,6 +18,7 @@ from kornia.augmentation.utils.helpers import (_adapted_uniform,_adapted_beta,_shape_validation,+ _extract_device_dtype,)__all__ = [@@ -32,6 +34,8 @@ __all__ = ["_adapted_uniform","_adapted_beta","_shape_validation",+ "_extract_device_dtype",+ "_common_param_check","_range_bound","_joint_range_check","_singular_range_check",diff --git a/kornia/augmentation/utils/helpers.py b/kornia/augmentation/utils/helpers.pyindex 175a41cbb..b5321e461 100644--- a/kornia/augmentation/utils/helpers.py+++ b/kornia/augmentation/utils/helpers.py@@ -154,12 +154,16 @@ def _adapted_uniform(If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]).By default, same_on_batch is set to False."""+ device, dtype = _extract_device_dtype([+ low if isinstance(low, torch.Tensor) else None,+ high if isinstance(high, torch.Tensor) else None,+ ])if not isinstance(low, torch.Tensor):- low = torch.tensor(low, dtype=torch.float32)+ low = torch.tensor(low, device=device, dtype=torch.float64)if not isinstance(high, torch.Tensor):- high = torch.tensor(high, dtype=torch.float32)- dist = Uniform(low, high)- return _adapted_rsampling(shape, dist, same_on_batch)+ high = torch.tensor(high, device=device, dtype=torch.float64)+ dist = Uniform(low.to(torch.float64), high.to(torch.float64))+ return _adapted_rsampling(shape, dist, same_on_batch).to(device=device, dtype=dtype)def _adapted_beta(@@ -169,15 +173,25 @@ def _adapted_beta(same_on_batch=False) -> torch.Tensor:r""" The beta sampling function that accepts 'same_on_batch'.+If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]).By default, same_on_batch is set to False."""+ device, dtype = _extract_device_dtype([+ a if isinstance(a, torch.Tensor) else None,+ b if isinstance(b, torch.Tensor) else None,+ ])+ dtype = torch.float32 if dtype is None else dtypeif not isinstance(a, torch.Tensor):- a = torch.tensor(a, dtype=torch.float32)+ a = torch.tensor(a, dtype=torch.float64)+ else:+ a = a.to(torch.float64)if not isinstance(b, torch.Tensor):- b = torch.tensor(b, dtype=torch.float32)+ b = torch.tensor(b, dtype=torch.float64)+ else:+ b = b.to(torch.float64)dist = Beta(a, b)- return _adapted_rsampling(shape, dist, same_on_batch)+ return _adapted_rsampling(shape, dist, same_on_batch).to(device=device, dtype=dtype)def _check_and_bound(factor: Union[torch.Tensor, float, Tuple[float, float], List[float]], name: str,@@ -219,3 +233,24 @@ def _check_and_bound(factor: Union[torch.Tensor, float, Tuple[float, float], Lisdef _shape_validation(param: torch.Tensor, shape: Union[tuple, list], name: str) -> None:assert param.shape == torch.Size(shape), f"Invalid shape for {name}. Expected {shape}. Got {param.shape}"+++def _extract_device_dtype(tensor_list: List[Optional[torch.Tensor]]):+ """This function will check if all the input tensors are in the same device.++ If so, it would return a tuple of (device, dtype)+ """+ device, dtype = None, None+ for tensor in tensor_list:+ if tensor is not None:+ if not isinstance(tensor, (torch.Tensor,)):+ raise ValueError(f"Expected None or Tensor. Got {tensor}.")+ _device = tensor.device+ _dtype = tensor.dtype+ if device is None and dtype is None:+ device = _device+ dtype = _dtype+ elif device != _device or dtype != _dtype:+ raise ValueError("Passed values are not in the same device and dtype."+ f"Got ({device}, {dtype}) and ({_device}, {_dtype}).")+ return (device, dtype)diff --git a/kornia/augmentation/utils/param_validation.py b/kornia/augmentation/utils/param_validation.pyindex ad1424331..71c0c8e75 100644--- a/kornia/augmentation/utils/param_validation.py+++ b/kornia/augmentation/utils/param_validation.py@@ -3,6 +3,13 @@ from typing import Tuple, Union, List, Optional, castimport torch+def _common_param_check(batch_size: int, same_on_batch: Optional[bool] = None):+ """Valid batch_szie and same_on_batch params."""+ assert type(batch_size) == int and batch_size >= 0, f"`batchsize` shall be a positive integer. Got {batch_size}."+ if same_on_batch is not None:+ assert type(same_on_batch) == bool, f"`same_on_batch` shall be boolean. Got {same_on_batch}."++def _range_bound(factor: Union[torch.Tensor, float, Tuple[float, float], List[float]], name: str,center: float = 0., bounds: Tuple[float, float] = (0, float('inf')),check: Optional[str] = 'joint') -> torch.Tensor:@@ -45,7 +52,7 @@ def _joint_range_check(ranged_factor: torch.Tensor, name: str, bounds: Optional[raise ValueError(f"{name}[0] should be smaller than {name}[1] got {ranged_factor}")else:raise TypeError(- f"{name} should be a float number or a tuple with length 2 whose values between {bounds}."+ f"{name} should be a tensor with length 2 whose values between {bounds}. "f"Got {ranged_factor}.")diff --git a/kornia/filters/kernels.py b/kornia/filters/kernels.pyindex 1c05bb9fd..717deb6c7 100644--- a/kornia/filters/kernels.py+++ b/kornia/filters/kernels.py@@ -712,7 +712,7 @@ def get_motion_kernel3d(kernel_size: int, angle: Union[torch.Tensor, Tuple[float[0.2195, 0.0743, 0.0000]])"""if not isinstance(kernel_size, int) or kernel_size % 2 == 0 or kernel_size < 3:- raise TypeError("ksize must be an odd integer >= than 3")+ raise TypeError(f"ksize must be an odd integer >= than 3. Got {kernel_size}.")if not isinstance(angle, torch.Tensor):angle = torch.tensor([angle])diff --git a/kornia/geometry/transform/crop/crop2d.py b/kornia/geometry/transform/crop/crop2d.pyindex 270f84461..08bd28281 100644--- a/kornia/geometry/transform/crop/crop2d.py+++ b/kornia/geometry/transform/crop/crop2d.py@@ -372,7 +372,7 @@ def bbox_generator([0, 0],[0, 0],[0, 0],- ]], device=x_start.device, dtype=x_start.dtype).repeat(len(x_start), 1, 1)+ ]], device=x_start.device, dtype=x_start.dtype).repeat(1 if x_start.dim() == 0 else len(x_start), 1, 1)bbox[:, :, 0] += x_start.view(-1, 1)bbox[:, :, 1] += y_start.view(-1, 1)diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex f22844bc2..aad07235c 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -457,30 +457,24 @@ class TestColorJitter:[0.7, 0.8, 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[0.2529, 0.3529, 0.4529],- [0.7529, 0.6529, 0.5529],- [0.8529, 0.9529, 1.0000]],-- [[0.2529, 0.3529, 0.4529],- [0.7529, 0.6529, 0.5529],- [0.8529, 0.9529, 1.0000]],-- [[0.2529, 0.3529, 0.4529],- [0.7529, 0.6529, 0.5529],- [0.8529, 0.9529, 1.0000]]],--- [[[0.2660, 0.3660, 0.4660],- [0.7660, 0.6660, 0.5660],- [0.8660, 0.9660, 1.0000]],-- [[0.2660, 0.3660, 0.4660],- [0.7660, 0.6660, 0.5660],- [0.8660, 0.9660, 1.0000]],-- [[0.2660, 0.3660, 0.4660],- [0.7660, 0.6660, 0.5660],- [0.8660, 0.9660, 1.0000]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3+ expected = torch.tensor([[[[0.0000, 0.0233, 0.1233],+ [0.4233, 0.3233, 0.2233],+ [0.5233, 0.6233, 0.8233]],+ [[0.0000, 0.0233, 0.1233],+ [0.4233, 0.3233, 0.2233],+ [0.5233, 0.6233, 0.8233]],+ [[0.0000, 0.0233, 0.1233],+ [0.4233, 0.3233, 0.2233],+ [0.5233, 0.6233, 0.8233]]],+ [[[0.0000, 0.0252, 0.1252],+ [0.4252, 0.3252, 0.2252],+ [0.5252, 0.6252, 0.8252]],+ [[0.0000, 0.0252, 0.1252],+ [0.4252, 0.3252, 0.2252],+ [0.5252, 0.6252, 0.8252]],+ [[0.0000, 0.0252, 0.1252],+ [0.4252, 0.3252, 0.2252],+ [0.5252, 0.6252, 0.8252]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -493,31 +487,24 @@ class TestColorJitter:[0.7, 0.8, 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[0.2529, 0.3529, 0.4529],- [0.7529, 0.6529, 0.5529],- [0.8529, 0.9529, 1.0000]],-- [[0.2529, 0.3529, 0.4529],- [0.7529, 0.6529, 0.5529],- [0.8529, 0.9529, 1.0000]],-- [[0.2529, 0.3529, 0.4529],- [0.7529, 0.6529, 0.5529],- [0.8529, 0.9529, 1.0000]]],--- [[[0.2660, 0.3660, 0.4660],- [0.7660, 0.6660, 0.5660],- [0.8660, 0.9660, 1.0000]],-- [[0.2660, 0.3660, 0.4660],- [0.7660, 0.6660, 0.5660],- [0.8660, 0.9660, 1.0000]],-- [[0.2660, 0.3660, 0.4660],- [0.7660, 0.6660, 0.5660],- [0.8660, 0.9660, 1.0000]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3- expected = expected.to(device)+ expected = torch.tensor([[[[0.0000, 0.0233, 0.1233],+ [0.4233, 0.3233, 0.2233],+ [0.5233, 0.6233, 0.8233]],+ [[0.0000, 0.0233, 0.1233],+ [0.4233, 0.3233, 0.2233],+ [0.5233, 0.6233, 0.8233]],+ [[0.0000, 0.0233, 0.1233],+ [0.4233, 0.3233, 0.2233],+ [0.5233, 0.6233, 0.8233]]],+ [[[0.0000, 0.0252, 0.1252],+ [0.4252, 0.3252, 0.2252],+ [0.5252, 0.6252, 0.8252]],+ [[0.0000, 0.0252, 0.1252],+ [0.4252, 0.3252, 0.2252],+ [0.5252, 0.6252, 0.8252]],+ [[0.0000, 0.0252, 0.1252],+ [0.4252, 0.3252, 0.2252],+ [0.5252, 0.6252, 0.8252]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -530,31 +517,24 @@ class TestColorJitter:[0.7, 0.8, 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[0.0953, 0.1906, 0.2859],- [0.5719, 0.4766, 0.3813],- [0.6672, 0.7625, 0.9531]],-- [[0.0953, 0.1906, 0.2859],- [0.5719, 0.4766, 0.3813],- [0.6672, 0.7625, 0.9531]],-- [[0.0953, 0.1906, 0.2859],- [0.5719, 0.4766, 0.3813],- [0.6672, 0.7625, 0.9531]]],--- [[[0.1184, 0.2367, 0.3551],- [0.7102, 0.5919, 0.4735],- [0.8286, 0.9470, 1.0000]],-- [[0.1184, 0.2367, 0.3551],- [0.7102, 0.5919, 0.4735],- [0.8286, 0.9470, 1.0000]],-- [[0.1184, 0.2367, 0.3551],- [0.7102, 0.5919, 0.4735],- [0.8286, 0.9470, 1.0000]]]], device=device, dtype=dtype)- expected = expected.to(device)+ expected = torch.tensor([[[[0.0849, 0.1699, 0.2548],+ [0.5097, 0.4247, 0.3398],+ [0.5946, 0.6795, 0.8494]],+ [[0.0849, 0.1699, 0.2548],+ [0.5097, 0.4247, 0.3398],+ [0.5946, 0.6795, 0.8494]],+ [[0.0849, 0.1699, 0.2548],+ [0.5097, 0.4247, 0.3398],+ [0.5946, 0.6795, 0.8494]]],+ [[[0.0821, 0.1642, 0.2463],+ [0.4926, 0.4105, 0.3284],+ [0.5747, 0.6568, 0.8210]],+ [[0.0821, 0.1642, 0.2463],+ [0.4926, 0.4105, 0.3284],+ [0.5747, 0.6568, 0.8210]],+ [[0.0821, 0.1642, 0.2463],+ [0.4926, 0.4105, 0.3284],+ [0.5747, 0.6568, 0.8210]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)@@ -567,31 +547,24 @@ class TestColorJitter:[0.7, 0.8, 1.]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[0.0953, 0.1906, 0.2859],- [0.5719, 0.4766, 0.3813],- [0.6672, 0.7625, 0.9531]],-- [[0.0953, 0.1906, 0.2859],- [0.5719, 0.4766, 0.3813],- [0.6672, 0.7625, 0.9531]],-- [[0.0953, 0.1906, 0.2859],- [0.5719, 0.4766, 0.3813],- [0.6672, 0.7625, 0.9531]]],--- [[[0.1184, 0.2367, 0.3551],- [0.7102, 0.5919, 0.4735],- [0.8286, 0.9470, 1.0000]],-- [[0.1184, 0.2367, 0.3551],- [0.7102, 0.5919, 0.4735],- [0.8286, 0.9470, 1.0000]],-- [[0.1184, 0.2367, 0.3551],- [0.7102, 0.5919, 0.4735],- [0.8286, 0.9470, 1.0000]]]], device=device, dtype=dtype)- expected = expected.to(device)+ expected = torch.tensor([[[[0.0849, 0.1699, 0.2548],+ [0.5097, 0.4247, 0.3398],+ [0.5946, 0.6795, 0.8494]],+ [[0.0849, 0.1699, 0.2548],+ [0.5097, 0.4247, 0.3398],+ [0.5946, 0.6795, 0.8494]],+ [[0.0849, 0.1699, 0.2548],+ [0.5097, 0.4247, 0.3398],+ [0.5946, 0.6795, 0.8494]]],+ [[[0.0821, 0.1642, 0.2463],+ [0.4926, 0.4105, 0.3284],+ [0.5747, 0.6568, 0.8210]],+ [[0.0821, 0.1642, 0.2463],+ [0.4926, 0.4105, 0.3284],+ [0.5747, 0.6568, 0.8210]],+ [[0.0821, 0.1642, 0.2463],+ [0.4926, 0.4105, 0.3284],+ [0.5747, 0.6568, 0.8210]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)@@ -612,31 +585,24 @@ class TestColorJitter:[0.8, 0.4, .5]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],- [6.2921e-01, 5.0000e-01, 4.0000e-01],- [7.0974e-01, 8.0000e-01, 1.0000e+00]],-- [[1.0000e+00, 5.2921e-01, 6.0974e-01],- [6.2921e-01, 3.1947e-01, 2.1947e-01],- [8.0000e-01, 1.6816e-01, 2.7790e-01]],-- [[6.3895e-01, 8.0000e-01, 7.0000e-01],- [9.0000e-01, 3.1947e-01, 2.1947e-01],- [8.0000e-01, 4.3895e-01, 5.4869e-01]]],--- [[[1.1921e-07, 1.2953e-01, 2.5302e-01],- [5.6476e-01, 5.0000e-01, 4.0000e-01],- [6.8825e-01, 8.0000e-01, 1.0000e+00]],-- [[1.0000e+00, 4.6476e-01, 5.8825e-01],- [5.6476e-01, 2.7651e-01, 1.7651e-01],- [8.0000e-01, 1.7781e-02, 1.0603e-01]],-- [[5.5556e-01, 8.0000e-01, 7.0000e-01],- [9.0000e-01, 2.7651e-01, 1.7651e-01],- [8.0000e-01, 3.5302e-01, 4.4127e-01]]]], device=device, dtype=dtype)- expected = expected.to(device)+ expected = torch.tensor([[[[0.0000, 0.0907, 0.2272],+ [0.5454, 0.5000, 0.4000],+ [0.6818, 0.8000, 1.0000]],+ [[1.0000, 0.4454, 0.5818],+ [0.5454, 0.2636, 0.1636],+ [0.8000, 0.0000, 0.0543]],+ [[0.5556, 0.8000, 0.7000],+ [0.9000, 0.2636, 0.1636],+ [0.8000, 0.3429, 0.4090]]],+ [[[0.0000, 0.0971, 0.2314],+ [0.5485, 0.5000, 0.4000],+ [0.6828, 0.8000, 1.0000]],+ [[1.0000, 0.4485, 0.5828],+ [0.5485, 0.2657, 0.1657],+ [0.8000, 0.0000, 0.0628]],+ [[0.5556, 0.8000, 0.7000],+ [0.9000, 0.2657, 0.1657],+ [0.8000, 0.3429, 0.4142]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)def test_random_saturation_tensor(self, device, dtype):@@ -656,31 +622,24 @@ class TestColorJitter:[0.8, 0.4, .5]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],- [6.2921e-01, 5.0000e-01, 4.0000e-01],- [7.0974e-01, 8.0000e-01, 1.0000e+00]],-- [[1.0000e+00, 5.2921e-01, 6.0974e-01],- [6.2921e-01, 3.1947e-01, 2.1947e-01],- [8.0000e-01, 1.6816e-01, 2.7790e-01]],-- [[6.3895e-01, 8.0000e-01, 7.0000e-01],- [9.0000e-01, 3.1947e-01, 2.1947e-01],- [8.0000e-01, 4.3895e-01, 5.4869e-01]]],--- [[[1.1921e-07, 1.2953e-01, 2.5302e-01],- [5.6476e-01, 5.0000e-01, 4.0000e-01],- [6.8825e-01, 8.0000e-01, 1.0000e+00]],-- [[1.0000e+00, 4.6476e-01, 5.8825e-01],- [5.6476e-01, 2.7651e-01, 1.7651e-01],- [8.0000e-01, 1.7781e-02, 1.0603e-01]],-- [[5.5556e-01, 8.0000e-01, 7.0000e-01],- [9.0000e-01, 2.7651e-01, 1.7651e-01],- [8.0000e-01, 3.5302e-01, 4.4127e-01]]]], device=device, dtype=dtype)- expected = expected.to(device)+ expected = torch.tensor([[[[0.0000, 0.0907, 0.2272],+ [0.5454, 0.5000, 0.4000],+ [0.6818, 0.8000, 1.0000]],+ [[1.0000, 0.4454, 0.5818],+ [0.5454, 0.2636, 0.1636],+ [0.8000, 0.0000, 0.0543]],+ [[0.5556, 0.8000, 0.7000],+ [0.9000, 0.2636, 0.1636],+ [0.8000, 0.3429, 0.4090]]],+ [[[0.0000, 0.0971, 0.2314],+ [0.5485, 0.5000, 0.4000],+ [0.6828, 0.8000, 1.0000]],+ [[1.0000, 0.4485, 0.5828],+ [0.5485, 0.2657, 0.1657],+ [0.8000, 0.0000, 0.0628]],+ [[0.5556, 0.8000, 0.7000],+ [0.9000, 0.2657, 0.1657],+ [0.8000, 0.3429, 0.4142]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -701,31 +660,24 @@ class TestColorJitter:[0.8, 0.4, .5]]]], device=device, dtype=dtype) # 1 x 1 x 3 x 3input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],- [6.2921e-01, 5.0000e-01, 4.0000e-01],- [7.0974e-01, 8.0000e-01, 1.0000e+00]],-- [[1.0000e+00, 5.2921e-01, 6.0974e-01],- [6.2921e-01, 3.1947e-01, 2.1947e-01],- [8.0000e-01, 1.6816e-01, 2.7790e-01]],-- [[6.3895e-01, 8.0000e-01, 7.0000e-01],- [9.0000e-01, 3.1947e-01, 2.1947e-01],- [8.0000e-01, 4.3895e-01, 5.4869e-01]]],--- [[[1.1921e-07, 1.2953e-01, 2.5302e-01],- [5.6476e-01, 5.0000e-01, 4.0000e-01],- [6.8825e-01, 8.0000e-01, 1.0000e+00]],-- [[1.0000e+00, 4.6476e-01, 5.8825e-01],- [5.6476e-01, 2.7651e-01, 1.7651e-01],- [8.0000e-01, 1.7781e-02, 1.0603e-01]],-- [[5.5556e-01, 8.0000e-01, 7.0000e-01],- [9.0000e-01, 2.7651e-01, 1.7651e-01],- [8.0000e-01, 3.5302e-01, 4.4127e-01]]]], device=device, dtype=dtype)- expected = expected.to(device)+ expected = torch.tensor([[[[0.0000, 0.0907, 0.2272],+ [0.5454, 0.5000, 0.4000],+ [0.6818, 0.8000, 1.0000]],+ [[1.0000, 0.4454, 0.5818],+ [0.5454, 0.2636, 0.1636],+ [0.8000, 0.0000, 0.0543]],+ [[0.5556, 0.8000, 0.7000],+ [0.9000, 0.2636, 0.1636],+ [0.8000, 0.3429, 0.4090]]],+ [[[0.0000, 0.0971, 0.2314],+ [0.5485, 0.5000, 0.4000],+ [0.6828, 0.8000, 1.0000]],+ [[1.0000, 0.4485, 0.5828],+ [0.5485, 0.2657, 0.1657],+ [0.8000, 0.0000, 0.0628]],+ [[0.5556, 0.8000, 0.7000],+ [0.9000, 0.2657, 0.1657],+ [0.8000, 0.3429, 0.4142]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -747,28 +699,23 @@ class TestColorJitter:input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],- [0.6000, 0.5000, 0.4000],+ [0.6030, 0.5000, 0.4000],[0.7000, 0.8000, 1.0000]],-- [[1.0000, 0.5251, 0.6167],- [0.6126, 0.3000, 0.2000],- [0.8000, 0.1000, 0.2000]],-- [[0.5623, 0.8000, 0.7000],- [0.9000, 0.3084, 0.2084],- [0.7958, 0.4293, 0.5335]]],-+ [[1.0000, 0.4940, 0.5960],+ [0.6000, 0.3020, 0.2020],+ [0.7990, 0.1000, 0.2000]],+ [[0.6090, 0.8000, 0.7000],+ [0.9000, 0.3000, 0.2000],+ [0.8000, 0.3930, 0.4920]]],[[[0.1000, 0.2000, 0.3000],- [0.6116, 0.5000, 0.4000],+ [0.6000, 0.5000, 0.4000],[0.7000, 0.8000, 1.0000]],-- [[1.0000, 0.4769, 0.5846],- [0.6000, 0.3077, 0.2077],- [0.7961, 0.1000, 0.2000]],-- [[0.6347, 0.8000, 0.7000],- [0.9000, 0.3000, 0.2000],- [0.8000, 0.3730, 0.4692]]]], device=device, dtype=dtype)+ [[1.0000, 0.5053, 0.6035],+ [0.6027, 0.3000, 0.2000],+ [0.8000, 0.1000, 0.2000]],+ [[0.5920, 0.8000, 0.7000],+ [0.9000, 0.3018, 0.2018],+ [0.7991, 0.4062, 0.5071]]]], device=device, dtype=dtype)expected = expected.to(device)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -791,30 +738,23 @@ class TestColorJitter:input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],- [0.6000, 0.5000, 0.4000],+ [0.6030, 0.5000, 0.4000],[0.7000, 0.8000, 1.0000]],-- [[1.0000, 0.5251, 0.6167],- [0.6126, 0.3000, 0.2000],- [0.8000, 0.1000, 0.2000]],-- [[0.5623, 0.8000, 0.7000],- [0.9000, 0.3084, 0.2084],- [0.7958, 0.4293, 0.5335]]],--+ [[1.0000, 0.4940, 0.5960],+ [0.6000, 0.3020, 0.2020],+ [0.7990, 0.1000, 0.2000]],+ [[0.6090, 0.8000, 0.7000],+ [0.9000, 0.3000, 0.2000],+ [0.8000, 0.3930, 0.4920]]],[[[0.1000, 0.2000, 0.3000],- [0.6116, 0.5000, 0.4000],+ [0.6000, 0.5000, 0.4000],[0.7000, 0.8000, 1.0000]],-- [[1.0000, 0.4769, 0.5846],- [0.6000, 0.3077, 0.2077],- [0.7961, 0.1000, 0.2000]],-- [[0.6347, 0.8000, 0.7000],- [0.9000, 0.3000, 0.2000],- [0.8000, 0.3730, 0.4692]]]], device=device, dtype=dtype)- expected = expected.to(device)+ [[1.0000, 0.5053, 0.6035],+ [0.6027, 0.3000, 0.2000],+ [0.8000, 0.1000, 0.2000]],+ [[0.5920, 0.8000, 0.7000],+ [0.9000, 0.3018, 0.2018],+ [0.7991, 0.4062, 0.5071]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -836,30 +776,23 @@ class TestColorJitter:input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],- [0.6000, 0.5000, 0.4000],+ [0.6030, 0.5000, 0.4000],[0.7000, 0.8000, 1.0000]],-- [[1.0000, 0.5251, 0.6167],- [0.6126, 0.3000, 0.2000],- [0.8000, 0.1000, 0.2000]],-- [[0.5623, 0.8000, 0.7000],- [0.9000, 0.3084, 0.2084],- [0.7958, 0.4293, 0.5335]]],--+ [[1.0000, 0.4940, 0.5960],+ [0.6000, 0.3020, 0.2020],+ [0.7990, 0.1000, 0.2000]],+ [[0.6090, 0.8000, 0.7000],+ [0.9000, 0.3000, 0.2000],+ [0.8000, 0.3930, 0.4920]]],[[[0.1000, 0.2000, 0.3000],- [0.6116, 0.5000, 0.4000],+ [0.6000, 0.5000, 0.4000],[0.7000, 0.8000, 1.0000]],-- [[1.0000, 0.4769, 0.5846],- [0.6000, 0.3077, 0.2077],- [0.7961, 0.1000, 0.2000]],-- [[0.6347, 0.8000, 0.7000],- [0.9000, 0.3000, 0.2000],- [0.8000, 0.3730, 0.4692]]]], device=device, dtype=dtype)- expected = expected.to(device)+ [[1.0000, 0.5053, 0.6035],+ [0.6027, 0.3000, 0.2000],+ [0.8000, 0.1000, 0.2000]],+ [[0.5920, 0.8000, 0.7000],+ [0.9000, 0.3018, 0.2018],+ [0.7991, 0.4062, 0.5071]]]], device=device, dtype=dtype)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-4)@@ -877,7 +810,7 @@ class TestColorJitter:expected_transform = torch.eye(3, device=device, dtype=dtype).unsqueeze(0) # 3 x 3assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-5)- assert_allclose(f(input)[1], expected_transform)+ assert_allclose(f(input)[1], expected_transform, atol=1e-4, rtol=1e-5)def test_color_jitter_batch_sequential(self, device, dtype):f = nn.Sequential(@@ -891,8 +824,8 @@ class TestColorJitter:expected_transform = torch.eye(3, device=device, dtype=dtype).unsqueeze(0).expand((2, 3, 3)) # 2 x 3 x 3assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-5)- assert_allclose(f(input)[0], expected)- assert_allclose(f(input)[1], expected_transform)+ assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-5)+ assert_allclose(f(input)[1], expected_transform, atol=1e-4, rtol=1e-5)def test_gradcheck(self, device, dtype):input = torch.rand((3, 5, 5), device=device, dtype=dtype) # 3 x 3@@ -1005,8 +938,7 @@ class TestRandomGrayscale:[0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],[0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],[0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])- data = data.to(device)+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]], device=device, dtype=dtype)# Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)expected = torch.tensor([[[0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],@@ -1025,8 +957,7 @@ class TestRandomGrayscale:[0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],[0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],[0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],- [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]]])- expected = expected.to(device)+ [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]]], device=device, dtype=dtype)img_gray = kornia.augmentation.RandomGrayscale(p=1.)(data)assert_allclose(img_gray, expected)@@ -1048,8 +979,7 @@ class TestRandomGrayscale:[0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],[0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],[0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])- data = data.to(device)+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]], device=device, dtype=dtype)expected = data@@ -1073,8 +1003,7 @@ class TestRandomGrayscale:[0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],[0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],[0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])- data = data.to(device)+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]], device=device, dtype=dtype)data = data.unsqueeze(0).repeat(4, 1, 1, 1)# Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)@@ -1094,8 +1023,7 @@ class TestRandomGrayscale:[0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],[0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],[0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],- [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]]])- expected = expected.to(device)+ [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]]], device=device, dtype=dtype)expected = expected.unsqueeze(0).repeat(4, 1, 1, 1)img_gray = kornia.augmentation.RandomGrayscale(p=1.)(data)@@ -1118,8 +1046,7 @@ class TestRandomGrayscale:[0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],[0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],[0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])- data = data.to(device)+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]], device=device, dtype=dtype)data = data.unsqueeze(0).repeat(4, 1, 1, 1)expected = data@@ -1197,25 +1124,21 @@ class TestRandomRotation:input = torch.tensor([[1., 0., 0., 2.],[0., 0., 0., 0.],[0., 1., 2., 0.],- [0., 0., 1., 2.]]) # 4 x 4- input = input.to(device)+ [0., 0., 1., 2.]], device=device, dtype=dtype) # 4 x 4- expected = torch.tensor([[[0.9824, 0.0088, 0.0000, 1.9649],- [0.0000, 0.0029, 0.0000, 0.0176],- [0.0029, 1.0000, 1.9883, 0.0000],- [0.0000, 0.0088, 1.0117, 1.9649]]]) # 1 x 4 x 4- expected = expected.to(device)+ expected = torch.tensor([[[[0.0000, 0.2645, 0.4798, 0.0000],+ [0.2399, 0.0000, 0.7409, 0.3652],+ [0.1323, 0.4232, 1.3269, 1.2947],+ [0.0000, 0.0877, 0.0465, 0.2285]]]], device=device, dtype=dtype) # 1 x 4 x 4- expected_transform = torch.tensor([[[1.0000, -0.0059, 0.0088],- [0.0059, 1.0000, -0.0088],- [0.0000, 0.0000, 1.0000]]]) # 1 x 3 x 3- expected_transform = expected_transform.to(device)+ expected_transform = torch.tensor([[[0.7396, 0.6731, -0.6190],+ [-0.6731, 0.7396, 1.4002],+ [0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype) # 1 x 3 x 3- expected_2 = torch.tensor([[[[0.1322, 0.0000, 0.7570, 0.2644],- [0.3785, 0.0000, 0.4166, 0.0000],- [0.0000, 0.6309, 1.5910, 1.2371],- [0.0000, 0.1444, 0.3177, 0.6499]]]]) # 1 x 4 x 4- expected_2 = expected_2.to(device)+ expected_2 = torch.tensor([[[[0.2631, 0.0000, 0.6914, 0.5261],+ [0.3457, 0.0000, 0.3235, 0.0000],+ [0.0000, 0.7043, 1.6793, 1.0616],+ [0.0000, 0.1307, 0.4526, 0.8613]]]], device=device, dtype=dtype) # 1 x 4 x 4out, mat = f(input)assert_allclose(out, expected, rtol=1e-6, atol=1e-4)@@ -1231,33 +1154,29 @@ class TestRandomRotation:input = torch.tensor([[[[1., 0., 0., 2.],[0., 0., 0., 0.],[0., 1., 2., 0.],- [0., 0., 1., 2.]]]]) # 1 x 1 x 4 x 4- input = input.to(device)-- expected = torch.tensor([[[[0.9824, 0.0088, 0.0000, 1.9649],- [0.0000, 0.0029, 0.0000, 0.0176],- [0.0029, 1.0000, 1.9883, 0.0000],- [0.0000, 0.0088, 1.0117, 1.9649]]],- [[[0.1322, 0.0000, 0.7570, 0.2644],- [0.3785, 0.0000, 0.4166, 0.0000],- [0.0000, 0.6309, 1.5910, 1.2371],- [0.0000, 0.1444, 0.3177, 0.6499]]]]) # 2 x 1 x 4 x 4- expected = expected.to(device)-- expected_transform = torch.tensor([[[1.0000, -0.0059, 0.0088],- [0.0059, 1.0000, -0.0088],+ [0., 0., 1., 2.]]]], device=device, dtype=dtype) # 1 x 1 x 4 x 4++ expected = torch.tensor([[[[0.0000, 0.2645, 0.4798, 0.0000],+ [0.2399, 0.0000, 0.7409, 0.3652],+ [0.1323, 0.4232, 1.3269, 1.2947],+ [0.0000, 0.0877, 0.0465, 0.2285]]],+ [[[0.2631, 0.0000, 0.6914, 0.5261],+ [0.3457, 0.0000, 0.3235, 0.0000],+ [0.0000, 0.7043, 1.6793, 1.0616],+ [0.0000, 0.1307, 0.4526, 0.8613]]]], device=device, dtype=dtype) # 2 x 1 x 4 x 4++ expected_transform = torch.tensor([[[0.7396, 0.6731, -0.6190],+ [-0.6731, 0.7396, 1.4002],[0.0000, 0.0000, 1.0000]],-- [[0.9125, 0.4090, -0.4823],- [-0.4090, 0.9125, 0.7446],- [0.0000, 0.0000, 1.0000]]]) # 2 x 3 x 3- expected_transform = expected_transform.to(device)+ [[0.9472, 0.3207, -0.4018],+ [-0.3207, 0.9472, 0.5602],+ [0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype) # 2 x 3 x 3input = input.repeat(2, 1, 1, 1) # 5 x 3 x 3 x 3out, mat = f(input)- assert_allclose(out, expected, rtol=1e-6, atol=1e-4)- assert_allclose(mat, expected_transform, rtol=1e-6, atol=1e-4)+ assert_allclose(out, expected, rtol=1e-4, atol=1e-4)+ assert_allclose(mat, expected_transform, rtol=1e-4, atol=1e-4)def test_same_on_batch(self, device, dtype):f = RandomRotation(degrees=40, same_on_batch=True)@@ -1281,30 +1200,26 @@ class TestRandomRotation:input = torch.tensor([[1., 0., 0., 2.],[0., 0., 0., 0.],[0., 1., 2., 0.],- [0., 0., 1., 2.]]) # 4 x 4- input = input.to(device)+ [0., 0., 1., 2.]], device=device, dtype=dtype) # 4 x 4- expected = torch.tensor([[[[0.1314, 0.1050, 0.6649, 0.2628],- [0.3234, 0.0202, 0.4256, 0.1671],- [0.0525, 0.5976, 1.5199, 1.1306],- [0.0000, 0.1453, 0.3224, 0.5796]]]]) # 1 x 4 x 4- expected = expected.to(device)+ expected = torch.tensor([[[[1.2791, 0.1719, 0.2457, 1.3764],+ [0.1720, 0.0772, 1.8012, 0.9797],+ [0.0860, 0.0361, 0.9309, 0.1257],+ [0.6396, 0.0873, 0.0299, 0.0037]]]], device=device, dtype=dtype) # 1 x 4 x 4- expected_transform = torch.tensor([[[0.8864, 0.4629, -0.5240],- [-0.4629, 0.8864, 0.8647],- [0.0000, 0.0000, 1.0000]]]) # 1 x 3 x 3- expected_transform = expected_transform.to(device)+ expected_transform = torch.tensor([[[-0.0049, 1.0000, 0.0073],+ [-1.0000, -0.0049, 3.0073],+ [0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype) # 1 x 3 x 3- expected_transform_2 = torch.tensor([[[0.8381, -0.5455, 1.0610],- [0.5455, 0.8381, -0.5754],- [0.0000, 0.0000, 1.0000]]]) # 1 x 3 x 3- expected_transform_2 = expected_transform_2.to(device)+ expected_transform_2 = torch.tensor([[[0.9562, 0.2927, -0.3733],+ [-0.2927, 0.9562, 0.5046],+ [0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype) # 1 x 3 x 3out, mat = f(input)_, mat_2 = f1(input)- assert_allclose(out, expected, rtol=1e-6, atol=1e-4)- assert_allclose(mat, expected_transform, rtol=1e-6, atol=1e-4)- assert_allclose(mat_2, expected_transform_2, rtol=1e-6, atol=1e-4)+ assert_allclose(out, expected, rtol=1e-4, atol=1e-4)+ assert_allclose(mat, expected_transform, rtol=1e-4, atol=1e-4)+ assert_allclose(mat_2, expected_transform_2, rtol=1e-4, atol=1e-4)@pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):@@ -1379,10 +1294,11 @@ class TestRandomCrop:[3., 4., 5.],[6., 7., 8.]]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)- expected = torch.tensor([[- [0., 1., 2.],- [3., 4., 5.],- ]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)+ expected = torch.tensor([+ [[[0., 1., 2.],+ [3., 4., 5.]]],+ [[[3., 4., 5.],+ [6., 7., 8.]]]], device=device, dtype=dtype)rc = RandomCrop(size=(2, 3), padding=None, align_corners=True, p=1.)out = rc(inp)@@ -1403,11 +1319,11 @@ class TestRandomCrop:[6., 7., 8.]]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)expected = torch.tensor([[[- [0., 0., 0.],- [0., 1., 2.]+ [1., 2., 0.],+ [4., 5., 0.]]], [[- [0., 0., 0.],- [1., 2., 0.]+ [7., 8., 0.],+ [0., 0., 0.]]]], device=device, dtype=dtype)rc = RandomCrop(size=(2, 3), padding=1, align_corners=True, p=1.)out = rc(inp)@@ -1423,11 +1339,11 @@ class TestRandomCrop:[6., 7., 8.]]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)expected = torch.tensor([[[- [0., 1., 2.],- [3., 4., 5.]- ]], [[[1., 2., 10.],[4., 5., 10.]+ ]], [[+ [4., 5., 10.],+ [7., 8., 10.],]]], device=device, dtype=dtype)rc = RandomCrop(size=(2, 3), padding=(0, 1), fill=10, align_corners=True, p=1.)out = rc(inp)@@ -1443,11 +1359,11 @@ class TestRandomCrop:[6., 7., 8.]]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)expected = torch.tensor([[[- [8., 8., 8.],- [8., 0., 1.]+ [2., 8., 8.],+ [5., 8., 8.]]], [[- [8., 8., 8.],- [1., 2., 8.]+ [4., 5., 8.],+ [7., 8., 8.]]]], device=device, dtype=dtype)rc = RandomCrop(size=(2, 3), padding=(0, 1, 2, 3), fill=8, align_corners=True, p=1.)out = rc(inp)@@ -1460,10 +1376,11 @@ class TestRandomCrop:inp = torch.tensor([[[0., 1., 2.],]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)- expected = torch.tensor([[- [9., 9., 9.],- [0., 1., 2.]- ]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)+ expected = torch.tensor([+ [[[9., 9., 9.],+ [0., 1., 2.]]],+ [[[0., 1., 2.],+ [9., 9., 9.]]]], device=device, dtype=dtype)rc = RandomCrop(size=(2, 3), pad_if_needed=True, fill=9, align_corners=True, p=1.)out = rc(inp)@@ -1524,8 +1441,8 @@ class TestRandomResizedCrop:]], device=device, dtype=dtype)expected = torch.tensor(- [[[[5.3750, 5.8750, 4.5938],- [6.3437, 6.7812, 5.2500]]]], device=device, dtype=dtype)+ [[[[3.0937, 4.3750, 4.8750],+ [3.9375, 5.4688, 5.9062]]]], device=device, dtype=dtype)rrc = RandomResizedCrop(size=(2, 3), scale=(1., 1.), ratio=(1.0, 1.0))# It will crop a size of (2, 2) from the aspect ratio implementation of torch@@ -1553,9 +1470,9 @@ class TestRandomResizedCrop:]], device=device, dtype=dtype)expected = torch.tensor(- [[[[1.2500, 1.7500, 1.5000],- [4.2500, 4.7500, 3.7500],- [7.2500, 7.7500, 6.0000]]]], device=device, dtype=dtype)+ [[[[0.0000, 0.2500, 0.7500],+ [2.2500, 3.2500, 3.7500],+ [4.5000, 6.2500, 6.7500]]]], device=device, dtype=dtype)rrc = RandomResizedCrop(size=(3, 3), scale=(3., 3.), ratio=(2., 2.))# It will crop a size of (2, 2) from the aspect ratio implementation of torchout = rrc(inp)@@ -1571,9 +1488,9 @@ class TestRandomResizedCrop:]], device=device, dtype=dtype).repeat(batch_size, 1, 1, 1)expected = torch. tensor(- [[[[1.2500, 1.7500, 1.5000],- [4.2500, 4.7500, 3.7500],- [7.2500, 7.7500, 6.0000]]],+ [[[[0.0000, 0.2500, 0.7500],+ [2.2500, 3.2500, 3.7500],+ [4.5000, 6.2500, 6.7500]]],[[[0.0000, 0.2500, 0.7500],[2.2500, 3.2500, 3.7500],[4.5000, 6.2500, 6.7500]]]], device=device, dtype=dtype)diff --git a/test/augmentation/test_augmentation_3d.py b/test/augmentation/test_augmentation_3d.pyindex 440f78f38..6b7d62abe 100644--- a/test/augmentation/test_augmentation_3d.py+++ b/test/augmentation/test_augmentation_3d.py@@ -533,7 +533,7 @@ class TestRandomRotation3D:"""p_batch=1.0, same_on_batch=False, return_transform=False)"""assert str(f) == repr- def test_random_rotation(self, device):+ def test_random_rotation(self, device, dtype):# This is included in doctesttorch.manual_seed(0) # for random reproductibility@@ -551,49 +551,45 @@ class TestRandomRotation3D:[[1., 0., 0., 2.],[0., 0., 0., 0.],[0., 1., 2., 0.],- [0., 0., 1., 2.]]]) # 3 x 4 x 4- input = input.to(device)-- expected = torch.tensor([[[[[0.0000, 0.0000, 0.6810, 0.5250],- [0.5052, 0.0000, 0.0000, 0.0613],- [0.1159, 0.1072, 0.5324, 0.0870],- [0.0000, 0.0000, 0.1927, 0.0000]],- [[0.0000, 0.1683, 0.6963, 0.1131],- [0.0566, 0.0000, 0.5215, 0.2796],- [0.0694, 0.6039, 1.4519, 1.1240],- [0.0000, 0.1325, 0.1542, 0.2510]],- [[0.0000, 0.2054, 0.0000, 0.0000],- [0.0026, 0.6088, 0.7358, 0.2319],- [0.1262, 1.0830, 1.3687, 1.4940],- [0.0000, 0.0416, 0.2012, 0.3124]]]]])- expected = expected.to(device)-- expected_transform = torch.tensor([[[0.6523, 0.3666, -0.6635, 0.6352],- [-0.6185, 0.7634, -0.1862, 1.4689],- [0.4382, 0.5318, 0.7247, -1.1797],- [0.0000, 0.0000, 0.0000, 1.0000]]])- expected_transform = expected_transform.to(device)-- expected_2 = torch.tensor([[[[[0.0000, 0.4771, 0.0243, 0.0000],- [0.0000, 0.1652, 0.0000, 0.6771],- [0.1668, 1.1430, 0.7131, 0.2692],- [0.0285, 0.7100, 0.6012, 0.0000]],- [[0.0000, 0.3068, 0.0000, 0.0000],- [0.0000, 0.3175, 0.0000, 0.6602],- [0.1330, 1.1962, 0.9750, 0.0000],- [0.0648, 0.9818, 0.9785, 0.0000]],- [[0.0000, 0.1136, 0.0000, 0.0000],- [0.0518, 0.4617, 0.0000, 0.4928],- [0.0407, 1.0954, 1.1413, 0.0000],- [0.0587, 0.8768, 1.1815, 0.0000]]]]])- expected_2 = expected_2.to(device)+ [0., 0., 1., 2.]]], device=device, dtype=dtype) # 3 x 4 x 4++ expected = torch.tensor([[[[[0.2771, 0.0000, 0.0036, 0.0000],+ [0.5751, 0.0183, 0.7505, 0.4702],+ [0.0262, 0.2591, 0.5776, 0.4764],+ [0.0000, 0.0093, 0.0000, 0.0393]],+ [[0.0000, 0.0000, 0.0583, 0.0222],+ [0.1665, 0.0000, 1.0424, 1.0224],+ [0.1296, 0.4846, 1.4200, 1.2287],+ [0.0078, 0.3851, 0.3965, 0.3612]],+ [[0.0000, 0.7704, 0.6704, 0.0000],+ [0.0000, 0.0332, 0.2414, 0.0524],+ [0.0000, 0.3349, 1.4545, 1.3689],+ [0.0000, 0.0312, 0.5874, 0.8702]]]]], device=device, dtype=dtype)++ expected_transform = torch.tensor([[[0.5784, 0.7149, -0.3929, -0.0471],+ [-0.3657, 0.6577, 0.6585, 0.4035],+ [0.7292, -0.2372, 0.6419, -0.3799],+ [0.0000, 0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype)++ expected_2 = torch.tensor([[[[[1., 0., 0., 2.],+ [0., 0., 0., 0.],+ [0., 1., 2., 0.],+ [0., 0., 1., 2.]],+ [[1., 0., 0., 2.],+ [0., 0., 0., 0.],+ [0., 1., 2., 0.],+ [0., 0., 1., 2.]],+ [[1., 0., 0., 2.],+ [0., 0., 0., 0.],+ [0., 1., 2., 0.],+ [0., 0., 1., 2.]]]]], device=device, dtype=dtype)out, mat = f(input)assert_allclose(out, expected, rtol=1e-6, atol=1e-4)assert_allclose(mat, expected_transform, rtol=1e-6, atol=1e-4)assert_allclose(f1(input), expected_2, rtol=1e-6, atol=1e-4)- def test_batch_random_rotation(self, device):+ def test_batch_random_rotation(self, device, dtype):torch.manual_seed(0) # for random reproductibility@@ -610,46 +606,41 @@ class TestRandomRotation3D:[[1., 0., 0., 2.],[0., 0., 0., 0.],[0., 1., 2., 0.],- [0., 0., 1., 2.]]]]) # 1 x 1 x 4 x 4- input = input.to(device)-- expected = torch.tensor([[[[[7.5651e-01, 6.4340e-02, 0.0000e+00, 0.0000e+00],- [8.9954e-02, 2.0099e-01, 8.2089e-01, 4.4695e-01],- [0.0000e+00, 4.8303e-01, 8.0751e-01, 1.1574e+00],- [0.0000e+00, 6.5891e-02, 1.7392e-01, 2.9013e-01]],- [[4.0104e-01, 0.0000e+00, 1.9018e-02, 7.2668e-04],- [3.5247e-01, 0.0000e+00, 6.5445e-01, 3.7179e-01],- [5.4804e-02, 7.0015e-01, 1.7578e+00, 1.2048e+00],- [3.7536e-02, 3.8235e-01, 9.0737e-01, 1.1033e+00]],- [[1.2648e-02, 0.0000e+00, 9.4951e-01, 4.6696e-01],- [1.2791e-01, 0.0000e+00, 8.2977e-02, 0.0000e+00],- [1.3047e-02, 2.8671e-01, 8.3294e-01, 1.7991e-01],- [0.0000e+00, 5.8076e-02, 6.7866e-01, 1.5130e+00]]]],-- [[[[1.0000e+00, 0.0000e+00, 0.0000e+00, 2.0000e+00],- [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],- [0.0000e+00, 1.0000e+00, 2.0000e+00, 0.0000e+00],- [0.0000e+00, 0.0000e+00, 1.0000e+00, 2.0000e+00]],- [[1.0000e+00, 0.0000e+00, 0.0000e+00, 2.0000e+00],- [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],- [0.0000e+00, 1.0000e+00, 2.0000e+00, 0.0000e+00],- [0.0000e+00, 0.0000e+00, 1.0000e+00, 2.0000e+00]],- [[1.0000e+00, 0.0000e+00, 0.0000e+00, 2.0000e+00],- [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],- [0.0000e+00, 1.0000e+00, 2.0000e+00, 0.0000e+00],- [0.0000e+00, 0.0000e+00, 1.0000e+00, 2.0000e+00]]]]])- expected = expected.to(device)-- expected_transform = torch.tensor([[[0.8017, 0.4358, -0.4090, 0.0527],- [-0.0877, 0.7627, 0.6408, -0.1533],- [0.5912, -0.4779, 0.6497, 0.1803],+ [0., 0., 1., 2.]]]], device=device, dtype=dtype) # 1 x 1 x 4 x 4++ expected = torch.tensor([[[[[0.0000, 0.5106, 0.1146, 0.0000],+ [0.0000, 0.1261, 0.0000, 0.4723],+ [0.1714, 0.9931, 0.5442, 0.4684],+ [0.0193, 0.5802, 0.4195, 0.0000]],+ [[0.0000, 0.2386, 0.0000, 0.0000],+ [0.0187, 0.3527, 0.0000, 0.6119],+ [0.1294, 1.2251, 0.9130, 0.0942],+ [0.0962, 1.0769, 0.8448, 0.0000]],+ [[0.0000, 0.0202, 0.0000, 0.0000],+ [0.1092, 0.5845, 0.1038, 0.4598],+ [0.0000, 1.1218, 1.0796, 0.0000],+ [0.0780, 0.9513, 1.1278, 0.0000]]]],+ [[[[1.0000, 0.0000, 0.0000, 2.0000],+ [0.0000, 0.0000, 0.0000, 0.0000],+ [0.0000, 1.0000, 2.0000, 0.0000],+ [0.0000, 0.0000, 1.0000, 2.0000]],+ [[1.0000, 0.0000, 0.0000, 2.0000],+ [0.0000, 0.0000, 0.0000, 0.0000],+ [0.0000, 1.0000, 2.0000, 0.0000],+ [0.0000, 0.0000, 1.0000, 2.0000]],+ [[1.0000, 0.0000, 0.0000, 2.0000],+ [0.0000, 0.0000, 0.0000, 0.0000],+ [0.0000, 1.0000, 2.0000, 0.0000],+ [0.0000, 0.0000, 1.0000, 2.0000]]]]], device=device, dtype=dtype)++ expected_transform = torch.tensor([[[0.7894, -0.6122, 0.0449, 1.1892],+ [0.5923, 0.7405, -0.3176, -0.1816],+ [0.1612, 0.2773, 0.9472, -0.6049],[0.0000, 0.0000, 0.0000, 1.0000]],-[[1.0000, 0.0000, 0.0000, 0.0000],[0.0000, 1.0000, 0.0000, 0.0000],[0.0000, 0.0000, 1.0000, 0.0000],- [0.0000, 0.0000, 0.0000, 1.0000]]])- expected_transform = expected_transform.to(device)+ [0.0000, 0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype)input = input.repeat(2, 1, 1, 1, 1) # 5 x 4 x 4 x 3@@ -657,13 +648,13 @@ class TestRandomRotation3D:assert_allclose(out, expected, rtol=1e-6, atol=1e-4)assert_allclose(mat, expected_transform, rtol=1e-6, atol=1e-4)- def test_same_on_batch(self, device):+ def test_same_on_batch(self, device, dtype):f = RandomRotation3D(degrees=40, same_on_batch=True)- input = torch.eye(6).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1, 1)+ input = torch.eye(6, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 1, 1, 1)res = f(input)assert (res[0] == res[1]).all()- def test_sequential(self, device):+ def test_sequential(self, device, dtype):torch.manual_seed(0) # for random reproductibility@@ -687,37 +678,30 @@ class TestRandomRotation3D:[[1., 0., 0., 2.],[0., 0., 0., 0.],[0., 1., 2., 0.],- [0., 0., 1., 2.]]]) # 3 x 4 x 4- input = input.to(device)-- expected = torch.tensor([[[[[4.5604e-02, 4.6441e-03, 7.1205e-01, 7.3379e-01],- [2.7580e-01, 3.3129e-02, 1.1986e-01, 5.7227e-01],- [1.4329e-01, 4.0604e-02, 4.4003e-03, 1.3030e-01],- [8.0267e-05, 9.0396e-03, 2.8991e-02, 4.0690e-03]],-- [[9.8822e-03, 4.4220e-02, 1.2963e-01, 3.9873e-02],- [4.3757e-02, 3.4982e-01, 5.1378e-01, 8.6131e-02],- [6.6809e-02, 5.5708e-01, 1.0904e+00, 4.6732e-01],- [2.9877e-02, 1.9682e-01, 3.5764e-01, 1.0877e-01]],-- [[2.3905e-02, 2.1605e-01, 2.5145e-02, 3.3507e-04],- [1.1453e-01, 1.1965e+00, 1.2008e+00, 3.6272e-01],- [1.3368e-01, 5.4211e-01, 1.2059e+00, 1.0104e+00],- [8.6762e-03, 6.7149e-02, 2.0946e-01, 2.7900e-01]]]]])-- expected = expected.to(device)-- expected_transform = torch.tensor([[[0.8369, 0.0343, -0.5463, 0.7395],- [-0.5104, 0.4091, -0.7563, 2.4083],- [0.1976, 0.9118, 0.3599, -1.0240],- [0.0000, 0.0000, 0.0000, 1.0000]]])- expected_transform = expected_transform.to(device)-- expected_transform_2 = torch.tensor([[[0.9869, -0.1351, 0.0879, 0.1343],- [0.1598, 0.7501, -0.6417, 0.7769],- [0.0208, 0.6474, 0.7619, -0.7641],- [0.0000, 0.0000, 0.0000, 1.0000]]])- expected_transform_2 = expected_transform_2.to(device)+ [0., 0., 1., 2.]]], device=device, dtype=dtype) # 3 x 4 x 4++ expected = torch.tensor([[[[[0.2752, 0.0000, 0.0000, 0.0000],+ [0.5767, 0.0059, 0.6440, 0.4307],+ [0.0000, 0.2793, 0.6638, 0.5716],+ [0.0000, 0.0049, 0.0000, 0.0685]],+ [[0.0000, 0.0000, 0.1806, 0.0000],+ [0.2138, 0.0000, 0.9061, 0.7966],+ [0.0657, 0.5395, 1.4299, 1.2912],+ [0.0000, 0.3600, 0.3088, 0.3655]],+ [[0.0000, 0.6515, 0.8861, 0.0000],+ [0.0000, 0.0000, 0.2278, 0.0000],+ [0.0027, 0.4403, 1.5462, 1.3480],+ [0.0000, 0.1182, 0.6297, 0.8623]]]]], device=device, dtype=dtype)++ expected_transform = torch.tensor([[[0.6306, 0.6496, -0.4247, 0.0044],+ [-0.3843, 0.7367, 0.5563, 0.4151],+ [0.6743, -0.1876, 0.7142, -0.4443],+ [0.0000, 0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype)++ expected_transform_2 = torch.tensor([[[0.9611, 0.0495, -0.2717, 0.2557],+ [0.1255, 0.7980, 0.5894, -0.4747],+ [0.2460, -0.6006, 0.7608, 0.7710],+ [0.0000, 0.0000, 0.0000, 1.0000]]], device=device, dtype=dtype)out, mat = f(input)_, mat_2 = f1(input)@@ -803,26 +787,24 @@ class TestRandomCrop3D:out = f(inp)if batch_size == 1:expected = torch.tensor([[[[- [10, 11, 12, 13],- [15, 16, 17, 18],- [20, 21, 22, 23]+ [11, 12, 13, 14],+ [16, 17, 18, 19],+ [21, 22, 23, 24]]]]], device=device, dtype=dtype).repeat(batch_size, 1, 2, 1, 1)if batch_size == 2:- expected = torch.tensor([[[- [[0., 1., 2., 3.],- [5., 6., 7., 8.],- [10, 11, 12, 13]],- [[0., 1., 2., 3.],- [5., 6., 7., 8.],- [10, 11, 12, 13]],- ]], [[- [[1., 2., 3., 4.],- [6., 7., 8., 9.],- [11, 12, 13, 14]],- [[1., 2., 3., 4.],- [6., 7., 8., 9.],- [11, 12, 13, 14]],- ]]], device=device, dtype=dtype)+ expected = torch.tensor([+ [[[[6.0000, 7.0000, 8.0000, 9.0000],+ [11.0000, 12.0000, 13.0000, 14.0000],+ [16.0000, 17.0000, 18.0000, 19.0000]],+ [[6.0000, 7.0000, 8.0000, 9.0000],+ [11.0000, 12.0000, 13.0000, 14.0000],+ [16.0000, 17.0000, 18.0000, 19.0000]]]],+ [[[[11.0000, 12.0000, 13.0000, 14.0000],+ [16.0000, 17.0000, 18.0000, 19.0000],+ [21.0000, 22.0000, 23.0000, 24.0000]],+ [[11.0000, 12.0000, 13.0000, 14.0000],+ [16.0000, 17.0000, 18.0000, 19.0000],+ [21.0000, 22.0000, 23.0000, 24.0000]]]]], device=device, dtype=dtype)assert_allclose(out, expected, atol=1e-4, rtol=1e-4)@@ -842,19 +824,19 @@ class TestRandomCrop3D:[6., 7., 8.]]]], device=device, dtype=dtype).repeat(batch_size, 1, 3, 1, 1)expected = torch.tensor([[[- [[10, 10, 10, 10],- [10, 0., 1., 2.],- [10, 3., 4., 5.]],- [[10, 10, 10, 10],- [10, 0., 1., 2.],- [10, 3., 4., 5.]],+ [[0., 1., 2., 10.],+ [3., 4., 5., 10.],+ [6., 7., 8., 10.]],+ [[0., 1., 2., 10.],+ [3., 4., 5., 10.],+ [6., 7., 8., 10.]],]], [[- [[10, 10, 10, 10],- [0., 1., 2., 10],- [3., 4., 5., 10]],- [[10, 10, 10, 10],- [0., 1., 2., 10],- [3., 4., 5., 10]],+ [[3., 4., 5., 10.],+ [6., 7., 8., 10.],+ [10, 10, 10, 10.]],+ [[10, 10, 10, 10.],+ [10, 10, 10, 10.],+ [10, 10, 10, 10.]],]]], device=device, dtype=dtype)f = RandomCrop3D(size=(2, 3, 4), fill=10., padding=padding, align_corners=True, p=1.)out = f(inp)@@ -870,7 +852,7 @@ class TestRandomCrop3D:[[9., 9., 9., 9.],[9., 9., 9., 9.],[9., 9., 9., 9.]],- [[9., 0., 1., 2.],+ [[0., 1., 2., 9.],[9., 9., 9., 9.],[9., 9., 9., 9.]],]]], device=device, dtype=dtype)diff --git a/test/augmentation/test_augmentation_mix.py b/test/augmentation/test_augmentation_mix.pyindex d83a5cdf4..91431f037 100644--- a/test/augmentation/test_augmentation_mix.py+++ b/test/augmentation/test_augmentation_mix.py@@ -28,7 +28,7 @@ class TestRandomMixUp:torch.zeros(1, 3, 4, device=device, dtype=dtype)])label = torch.tensor([1, 0], device=device)- lam = torch.tensor([0.1320, 0.3074], device=device, dtype=dtype)+ lam = torch.tensor([0.0562, 0.0726], device=device, dtype=dtype)expected = torch.stack([torch.ones(1, 3, 4, device=device, dtype=dtype) * (1 - lam[0]),@@ -89,7 +89,7 @@ class TestRandomMixUp:torch.zeros(1, 3, 4, device=device, dtype=dtype)])label = torch.tensor([1, 0], device=device)- lam = torch.tensor([0.0885, 0.0885], device=device, dtype=dtype)+ lam = torch.tensor([0.7078, 0.7078], device=device, dtype=dtype)expected = torch.stack([torch.ones(1, 3, 4, device=device, dtype=dtype) * (1 - lam[0]),@@ -166,8 +166,8 @@ class TestRandomCutMix:expected = torch.tensor([[[[1., 0., 0., 1.],[1., 0., 0., 1.],[1., 1., 1., 1.]]],- [[[1., 1., 0., 0.],- [1., 1., 0., 0.],+ [[[0., 1., 1., 0.],+ [0., 1., 1., 0.],[0., 0., 0., 0.]]]], device=device, dtype=dtype)out_image, out_label = f(input, label)diff --git a/test/augmentation/test_perspective.py b/test/augmentation/test_perspective.pyindex d1bb93417..5fa54f17e 100644--- a/test/augmentation/test_perspective.py+++ b/test/augmentation/test_perspective.py@@ -102,20 +102,19 @@ class TestRandomPerspective:torch.manual_seed(0)x_data = torch.rand(1, 2, 4, 5).to(device)- expected_output = torch.tensor([[[[0.0000000000, 0.0000000000, 0.0000000000, 0.0197417457, 0.0429493971],- [0.0000000000, 0.5632190704, 0.5321710110, 0.3676981330, 0.1430126727],- [0.0000000000, 0.3082636893, 0.4031507671, 0.1760708243, 0.0000000000],- [0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000]],-- [[0.0000000000, 0.0000000000, 0.0000000000, 0.1189093292, 0.0585946590],- [0.0000000000, 0.7087295055, 0.5419756770, 0.3995491862, 0.0863459259],- [0.0000000000, 0.2694899142, 0.5981453061, 0.5887590051, 0.0000000000],- [0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000]]]],+ expected_output = torch.tensor([[[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],+ [0.0000, 0.2770, 0.6475, 0.0335, 0.0000],+ [0.0000, 0.4842, 0.5532, 0.0445, 0.0000],+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],+ [0.0000, 0.2417, 0.1325, 0.0193, 0.0000],+ [0.0000, 0.0296, 0.6933, 0.1170, 0.0000],+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]],device=device, dtype=x_data.dtype)- expected_transform = torch.tensor([[[1.0522739887, 0.3492536247, 0.3045728207],- [-0.1066057906, 1.0426188707, 0.5845923424],- [0.0350575559, 0.1213315651, 1.0000000000]]],+ expected_transform = torch.tensor([[[0.5445, -0.2336, 1.1866],+ [0.0364, 0.4761, 0.2652],+ [0.0475, -0.0936, 1.0000]]],device=device, dtype=x_data.dtype)out_perspective = kornia.augmentation.RandomPerspective(p=.99999999, # step one the random state@@ -125,8 +124,8 @@ class TestRandomPerspective:assert len(out_perspective) == 2assert out_perspective[0].shape == x_data.shapeassert out_perspective[1].shape == (1, 3, 3)- assert_allclose(out_perspective[0], expected_output)- assert_allclose(out_perspective[1], expected_transform)+ assert_allclose(out_perspective[0], expected_output, atol=1e-4, rtol=1e-4)+ assert_allclose(out_perspective[1], expected_transform, atol=1e-4, rtol=1e-4)def test_gradcheck(self, device):input = torch.rand(1, 2, 5, 7).to(device)diff --git a/test/augmentation/test_random_generator.py b/test/augmentation/test_random_generator.pyindex 949fa5474..60ae10c3f 100644--- a/test/augmentation/test_random_generator.py+++ b/test/augmentation/test_random_generator.py@@ -1,128 +1,1061 @@++import pytestimport torch+from torch.testing import assert_allclose++from kornia.augmentation.random_generator import (+ random_prob_generator,+ random_color_jitter_generator,+ random_perspective_generator,+ random_affine_generator,+ random_rotation_generator,+ random_crop_generator,+ random_crop_size_generator,+ random_rectangles_params_generator,+ center_crop_generator,+ random_motion_blur_generator,+ random_solarize_generator,+ random_posterize_generator,+ random_sharpness_generator,+ random_mixup_generator,+ random_cutmix_generator,+)+++class RandomGeneratorBaseTests():++ def test_valid_param_combinations(self, device, dtype):+ raise NotImplementedError++ def test_invalid_param_combinations(self, device, dtype):+ raise NotImplementedError++ def test_random_gen(self, device, dtype):+ raise NotImplementedError++ def test_same_on_batch(self, device, dtype):+ raise NotImplementedError+-from kornia.augmentation.random_generator import random_prob_generator, random_color_jitter_generator+class TestRandomProbGen(RandomGeneratorBaseTests):+ @pytest.mark.parametrize('p', [0., 0.5, 1.])+ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, p, batch_size, same_on_batch, device, dtype):+ random_prob_generator(batch_size=batch_size, p=p, same_on_batch=same_on_batch)-class TestRandomProbGen:+ @pytest.mark.parametrize('p', [+ # Should be failed if p > 1. or p < 0.+ pytest.param(-1., marks=pytest.mark.xfail),+ pytest.param(2., marks=pytest.mark.xfail)+ ])+ def test_invalid_param_combinations(self, p, device, dtype):+ random_prob_generator(batch_size=8, p=p)- def test_random_prob_gen(self):+ @pytest.mark.parametrize('p,expected', [+ (0., [False] * 8),+ (0.5, [False, False, True, False, True, False, True, False]),+ (1., [True] * 8)+ ])+ def test_random_gen(self, p, expected, device, dtype):torch.manual_seed(42)batch_size = 8+ res = random_prob_generator(batch_size=batch_size, p=p)+ assert (res == torch.tensor(expected)).long().sum() == batch_size- halfs = random_prob_generator(batch_size=batch_size, p=.5)- expected_halfs = [False, False, True, False, True, False, True, False]- zeros = random_prob_generator(batch_size=batch_size, p=0.)- ones = random_prob_generator(batch_size=batch_size, p=1.)+ @pytest.mark.parametrize("seed,expected", [+ (42, [False] * 8),+ (0, [True] * 8),+ ])+ def test_same_on_batch(self, seed, expected, device, dtype):+ torch.manual_seed(seed)+ batch_size = 8+ res = random_prob_generator(batch_size=batch_size, p=.5, same_on_batch=True)+ assert (res == torch.tensor(expected)).long().sum() == batch_size- assert (halfs == torch.tensor(expected_halfs)).long().sum() == batch_size- assert (zeros == torch.tensor([False] * batch_size)).long().sum() == batch_size- assert (ones == torch.tensor([True] * batch_size)).long().sum() == batch_size- def test_random_prob_gen_same_on_batch(self):- batch_size = 8+class TestColorJitterGen(RandomGeneratorBaseTests):- torch.manual_seed(42)- falses = random_prob_generator(batch_size=batch_size, p=.5, same_on_batch=True)- assert (falses == torch.tensor([False] * batch_size)).long().sum() == batch_size+ @pytest.mark.parametrize('brightness', [None, torch.tensor([0.8, 1.2])])+ @pytest.mark.parametrize('contrast', [None, torch.tensor([0.8, 1.2])])+ @pytest.mark.parametrize('saturation', [None, torch.tensor([0.8, 1.2])])+ @pytest.mark.parametrize('hue', [None, torch.tensor([-0.1, 0.1])])+ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, brightness, contrast, saturation, hue, batch_size, same_on_batch, device, dtype+ ):+ random_color_jitter_generator(+ batch_size,+ brightness.to(device=device, dtype=dtype) if brightness is not None else None,+ contrast.to(device=device, dtype=dtype) if contrast is not None else None,+ saturation.to(device=device, dtype=dtype) if saturation is not None else None,+ hue.to(device=device, dtype=dtype) if hue is not None else None,+ same_on_batch+ )++ @pytest.mark.parametrize('brightness,contrast,saturation,hue', [+ # Should be failed if value out of bounds or tensor.shape != [1, 2]+ pytest.param(torch.tensor([-1., 2.]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0., 3.]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(torch.tensor(0.), None, None, None, marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0.]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0., 1., 2.]), None, None, None, marks=pytest.mark.xfail),- torch.manual_seed(0)- trues = random_prob_generator(batch_size=batch_size, p=.5, same_on_batch=True)- assert (trues == torch.tensor([True] * batch_size)).long().sum() == batch_size+ pytest.param(None, torch.tensor([-1., 2.]), None, None, marks=pytest.mark.xfail),+ pytest.param(None, torch.tensor(0.), None, None, marks=pytest.mark.xfail),+ pytest.param(None, torch.tensor([0.]), None, None, marks=pytest.mark.xfail),+ pytest.param(None, torch.tensor([0., 1., 2.]), None, None, marks=pytest.mark.xfail),+ pytest.param(None, None, torch.tensor([-1., 2.]), None, marks=pytest.mark.xfail),+ pytest.param(None, None, torch.tensor(0.), None, marks=pytest.mark.xfail),+ pytest.param(None, None, torch.tensor([0.]), None, marks=pytest.mark.xfail),+ pytest.param(None, None, torch.tensor([0., 1., 2.]), None, marks=pytest.mark.xfail),-class TestColorJitterGen:+ pytest.param(None, None, None, torch.tensor([-1., 0.]), marks=pytest.mark.xfail),+ pytest.param(None, None, None, torch.tensor([0, 1.]), marks=pytest.mark.xfail),+ pytest.param(None, None, None, torch.tensor(0.), marks=pytest.mark.xfail),+ pytest.param(None, None, None, torch.tensor([0.]), marks=pytest.mark.xfail),+ pytest.param(None, None, None, torch.tensor([0., 1., 2.]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(+ self, brightness, contrast, saturation, hue, device, dtype+ ):+ random_color_jitter_generator(+ 8,+ brightness.to(device=device, dtype=dtype) if brightness is not None else None,+ contrast.to(device=device, dtype=dtype) if contrast is not None else None,+ saturation.to(device=device, dtype=dtype) if saturation is not None else None,+ hue.to(device=device, dtype=dtype) if hue is not None else None+ )- def test_color_jitter_gen(self):+ def test_random_gen(self, device, dtype):torch.manual_seed(42)batch_size = 8jitter_params = random_color_jitter_generator(- batch_size, brightness=torch.tensor([0.8, 1.2]), contrast=torch.tensor([0.7, 1.3]),- saturation=torch.tensor([0.6, 1.4]), hue=torch.tensor([-0.1, 0.1]))+ batch_size, brightness=torch.tensor([0.8, 1.2], device=device, dtype=dtype),+ contrast=torch.tensor([0.7, 1.3], device=device, dtype=dtype),+ saturation=torch.tensor([0.6, 1.4], device=device, dtype=dtype),+ hue=torch.tensor([-0.1, 0.1], device=device, dtype=dtype))expected_jitter_params = {- 'brightness_factor': torch.tensor([- 1.15290772914886474609375, 1.16600155830383300781250, 0.95314550399780273437500,- 1.18372225761413574218750, 0.956179320812225341796875, 1.04035818576812744140625,- 0.90262901782989501953125, 1.11745655536651611328125]),- 'contrast_factor': torch.tensor([- 1.2644628286361694, 0.7799115180969238, 1.260758876800537, 1.056147813796997,- 1.2216426134109497, 1.0406291484832764, 1.1446564197540283, 0.957642674446106]),- 'hue_factor': torch.tensor([- 0.07708858698606491, 0.014780893921852112, -0.04668399319052696, 0.02548982948064804,- -0.04607366397976875, -0.011727288365364075, -0.040615834295749664, 0.06633710116147995]),- 'saturation_factor': torch.tensor([- 0.6842519640922546, 0.8155958652496338, 0.8870501518249512, 0.7594910264015198,- 1.0377532243728638, 0.6049283742904663, 1.3612436056137085, 0.6602127552032471]),- 'order': torch.tensor([3, 2, 0, 1])+ 'brightness_factor': torch.tensor(+ [0.8233, 0.8252, 0.8494, 0.8210, 1.0105, 0.9907, 1.1821, 1.1715], device=device, dtype=dtype),+ 'contrast_factor': torch.tensor(+ [0.7501, 0.7796, 0.7942, 0.9252, 1.2055, 1.2223, 0.9265, 1.0675], device=device, dtype=dtype),+ 'hue_factor': torch.tensor(+ [-0.0824, 0.0402, 0.0247, -0.0125, -0.0851, 0.0367, -0.0376, -0.0382], device=device, dtype=dtype),+ 'saturation_factor': torch.tensor(+ [0.6251, 0.6323, 1.3455, 0.7217, 0.8120, 0.7043, 0.8015, 0.7867], device=device, dtype=dtype),+ 'order': torch.tensor([2, 3, 1, 0], device=device, dtype=dtype)}assert set(list(jitter_params.keys())) == set(['brightness_factor', 'contrast_factor', 'hue_factor', 'saturation_factor', 'order']), \"Redundant keys found apart from \'brightness_factor', 'contrast_factor', 'hue_factor', 'saturation_factor', 'order'"- assert (jitter_params['brightness_factor'] == expected_jitter_params['brightness_factor']) \- .long().sum() == batch_size- assert (jitter_params['contrast_factor'] == expected_jitter_params['contrast_factor']) \- .long().sum() == batch_size- assert (jitter_params['hue_factor'] == expected_jitter_params['hue_factor']) \- .long().sum() == batch_size- assert (jitter_params['saturation_factor'] == expected_jitter_params['saturation_factor']) \- .long().sum() == batch_size- assert (jitter_params['order'] == expected_jitter_params['order']).long().sum() == 4-- def test_color_jitter_tuple_gen(self):- torch.manual_seed(42)- batch_size = 8- jitter_params_tuple = random_color_jitter_generator(- batch_size, brightness=torch.tensor([0.8, 1.2]), contrast=torch.tensor([0.7, 1.3]),- saturation=torch.tensor([0.6, 1.4]), hue=torch.tensor([-0.1, 0.1]))-- expected_jitter_params_tuple = {- 'brightness_factor': torch.tensor([- 1.15290772914886474609375, 1.16600155830383300781250, 0.95314550399780273437500,- 1.18372225761413574218750, 0.956179320812225341796875, 1.04035818576812744140625,- 0.90262901782989501953125, 1.11745655536651611328125]),- 'contrast_factor': torch.tensor([- 1.2644628286361694, 0.7799115180969238, 1.260758876800537, 1.056147813796997,- 1.2216426134109497, 1.0406291484832764, 1.1446564197540283, 0.957642674446106]),- 'hue_factor': torch.tensor([- 0.07708858698606491, 0.014780893921852112, -0.04668399319052696, 0.02548982948064804,- -0.04607366397976875, -0.011727288365364075, -0.040615834295749664, 0.06633710116147995]),- 'saturation_factor': torch.tensor([- 0.6842519640922546, 0.8155958652496338, 0.8870501518249512, 0.7594910264015198,- 1.0377532243728638, 0.6049283742904663, 1.3612436056137085, 0.6602127552032471]),- 'order': torch.tensor([3, 2, 0, 1])- }- assert set(list(jitter_params_tuple.keys())) == set([- 'brightness_factor', 'contrast_factor', 'hue_factor', 'saturation_factor', 'order']), \- "Redundant keys found apart from \- 'brightness_factor', 'contrast_factor', 'hue_factor', 'saturation_factor', 'order'"- assert (jitter_params_tuple['brightness_factor'] == expected_jitter_params_tuple['brightness_factor']) \- .long().sum() == batch_size- assert (jitter_params_tuple['contrast_factor'] == expected_jitter_params_tuple['contrast_factor']) \- .long().sum() == batch_size- assert (jitter_params_tuple['hue_factor'] == expected_jitter_params_tuple['hue_factor']) \- .long().sum() == batch_size- assert (jitter_params_tuple['saturation_factor'] == expected_jitter_params_tuple['saturation_factor']) \- .long().sum() == batch_size- assert (jitter_params_tuple['order'] == expected_jitter_params_tuple['order']).long().sum() == 4-- def test_random_prob_gen_same_on_batch(self):++ assert_allclose(+ jitter_params['brightness_factor'], expected_jitter_params['brightness_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['contrast_factor'], expected_jitter_params['contrast_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['hue_factor'], expected_jitter_params['hue_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['saturation_factor'], expected_jitter_params['saturation_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['order'].to(dtype), expected_jitter_params['order'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):torch.manual_seed(42)batch_size = 8jitter_params = random_color_jitter_generator(- batch_size, brightness=torch.tensor([0.8, 1.2]), contrast=torch.tensor([0.7, 1.3]),- saturation=torch.tensor([0.6, 1.4]), hue=torch.tensor([-0.1, 0.1]), same_on_batch=True)+ batch_size, brightness=torch.tensor([0.8, 1.2], device=device, dtype=dtype),+ contrast=torch.tensor([0.7, 1.3], device=device, dtype=dtype),+ saturation=torch.tensor([0.6, 1.4], device=device, dtype=dtype),+ hue=torch.tensor([-0.1, 0.1], device=device, dtype=dtype), same_on_batch=True)expected_res = {- 'brightness_factor': torch.tensor([1.15290772914886474609375] * batch_size),- 'contrast_factor': torch.tensor([1.24900233745574951171875] * batch_size),- 'hue_factor': torch.tensor([-0.0234272480010986328125] * batch_size),- 'saturation_factor': torch.tensor([1.367444515228271484375] * batch_size),- 'order': torch.tensor([2, 3, 0, 1])+ 'brightness_factor': torch.tensor([0.8233] * batch_size, device=device, dtype=dtype),+ 'contrast_factor': torch.tensor([0.7377] * batch_size, device=device, dtype=dtype),+ 'hue_factor': torch.tensor([-0.0753] * batch_size, device=device, dtype=dtype),+ 'saturation_factor': torch.tensor([0.6421] * batch_size, device=device, dtype=dtype),+ 'order': torch.tensor([2, 3, 0, 1], device=device, dtype=dtype)}- assert (jitter_params['brightness_factor'] == expected_res['brightness_factor']).long().sum() == batch_size- assert (jitter_params['contrast_factor'] == expected_res['contrast_factor']).long().sum() == batch_size- assert (jitter_params['saturation_factor'] == expected_res['saturation_factor']).long().sum() == batch_size- assert (jitter_params['hue_factor'] == expected_res['hue_factor']).long().sum() == batch_size- assert (jitter_params['order'] == expected_res['order']).long().sum() == 4++ assert_allclose(+ jitter_params['brightness_factor'], expected_res['brightness_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['contrast_factor'], expected_res['contrast_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['hue_factor'], expected_res['hue_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['saturation_factor'], expected_res['saturation_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(+ jitter_params['order'].to(dtype), expected_res['order'], rtol=1e-4, atol=1e-4)+++class TestRandomPerspectiveGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('height,width', [(200, 200)])+ @pytest.mark.parametrize('distortion_scale', [torch.tensor(0.), torch.tensor(0.5), torch.tensor(1.)])+ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, height, width, distortion_scale, batch_size, same_on_batch, device, dtype+ ):+ random_perspective_generator(+ batch_size=8, height=height, width=width,+ distortion_scale=distortion_scale.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('height,width,distortion_scale', [+ # Should be failed if distortion_scale > 1. or distortion_scale < 0.+ pytest.param(-100, 100, torch.tensor(0.5), marks=pytest.mark.xfail),+ pytest.param(100, -100, torch.tensor(0.5), marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor(-0.5), marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor(1.5), marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([0., 0.5]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, height, width, distortion_scale, device, dtype):+ random_perspective_generator(+ batch_size=8, height=height, width=width,+ distortion_scale=distortion_scale.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 2+ res = random_perspective_generator(batch_size, 200, 200, torch.tensor(0.5, device=device, dtype=dtype))+ expected = dict(+ start_points=torch.tensor([+ [[0., 0.],+ [199., 0.],+ [199., 199.],+ [0., 199.]],+ [[0., 0.],+ [199., 0.],+ [199., 199.],+ [0., 199.]]], device=device, dtype=dtype),+ end_points=torch.tensor([+ [[2.9077, 3.1455],+ [192.8207, 2.6290],+ [172.6914, 175.1608],+ [47.7618, 152.5624]],+ [[4.1772, 6.6320],+ [191.1473, 18.7684],+ [156.8740, 155.4749],+ [18.8766, 168.3782]]], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['start_points'], expected['start_points'])+ assert_allclose(res['end_points'], expected['end_points'])++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 2+ res = random_perspective_generator(+ batch_size, 200, 200, torch.tensor(0.5, device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ start_points=torch.tensor([+ [[0., 0.],+ [199., 0.],+ [199., 199.],+ [0., 199.]]], device=device, dtype=dtype).repeat(2, 1, 1),+ end_points=torch.tensor([+ [[2.9077, 3.1455],+ [192.8207, 2.6290],+ [172.6914, 175.1608],+ [47.7618, 152.5624]]], device=device, dtype=dtype).repeat(2, 1, 1),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['start_points'], expected['start_points'])+ assert_allclose(res['end_points'], expected['end_points'])+++class TestRandomAffineGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('height', [200])+ @pytest.mark.parametrize('width', [300])+ @pytest.mark.parametrize('degrees', [torch.tensor([0, 30])])+ @pytest.mark.parametrize('translate', [None, torch.tensor([0.1, 0.1])])+ @pytest.mark.parametrize('scale', [None, torch.tensor([0.7, 1.2])])+ @pytest.mark.parametrize('shear', [None, torch.tensor([[0, 20], [0, 20]])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, height, width, degrees, translate, scale, shear, same_on_batch, device, dtype+ ):+ random_affine_generator(+ batch_size=batch_size, height=height, width=width, degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shear=shear.to(device=device, dtype=dtype) if shear is not None else None,+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('height,width,degrees,translate,scale,shear', [+ pytest.param(-100, 100, torch.tensor([10, 20]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, -100, torch.tensor([10, 20]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 0.5, None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([10, 20, 30]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([10, 20]), torch.tensor([0.1]), None, None, marks=pytest.mark.xfail),+ pytest.param(10, 10, torch.tensor([1, 2]), torch.tensor([0.1, 0.2, 0.3]), None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([10, 20]), None, torch.tensor([1]), None, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([10, 20]), None, torch.tensor([1, 2, 3]), None, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([10, 20]), None, None, torch.tensor([1]), marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([10, 20]), None, None, torch.tensor([1, 2]), marks=pytest.mark.xfail),+ pytest.param(10, 10, torch.tensor([1, 2]), None, None, torch.tensor([1, 2, 3]), marks=pytest.mark.xfail),+ pytest.param(10, 10, torch.tensor([1, 2]), None, None, torch.tensor([1, 2, 3, 4]), marks=pytest.mark.xfail),+ pytest.param(10, 10, torch.tensor([1, 2]), None, None, torch.tensor([1, 2, 3, 4, 5]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(+ self, batch_size, height, width, degrees, translate, scale, shear, device, dtype+ ):+ batch_size = 8+ random_affine_generator(+ batch_size=batch_size, height=height, width=width, degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shear=shear.to(device=device, dtype=dtype) if shear is not None else None)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ translate = torch.tensor([0.1, 0.1])+ scale = torch.tensor([0.7, 1.2])+ shear = torch.tensor([[10, 20], [10, 20]])+ res = random_affine_generator(+ batch_size=2, height=200, width=200, degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shear=shear.to(device=device, dtype=dtype) if shear is not None else None,+ same_on_batch=False)+ expected = dict(+ translations=torch.tensor([[1.0469, 18.2094], [-0.9286, 17.1501]], device=device, dtype=dtype),+ center=torch.tensor([[99.5000, 99.5000], [99.5000, 99.5000]], device=device, dtype=dtype),+ scale=torch.tensor([[0.7618, 0.7618], [0.7263, 0.7263]], device=device, dtype=dtype),+ angle=torch.tensor([10.5815, 10.6291], device=device, dtype=dtype),+ sx=torch.tensor([10.8354, 11.3264], device=device, dtype=dtype),+ sy=torch.tensor([11.5705, 13.7537], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['translations'], expected['translations'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['center'], expected['center'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['scale'], expected['scale'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angle'], expected['angle'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sx'], expected['sx'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sy'], expected['sy'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ translate = torch.tensor([0.1, 0.1])+ scale = torch.tensor([0.7, 1.2])+ shear = torch.tensor([[10, 20], [10, 20]])+ res = random_affine_generator(+ batch_size=2, height=200, width=200, degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shear=shear.to(device=device, dtype=dtype) if shear is not None else None,+ same_on_batch=True)+ expected = dict(+ translations=torch.tensor([[-15.0566, -17.8968], [-15.0566, -17.8968]], device=device, dtype=dtype),+ center=torch.tensor([[99.5000, 99.5000], [99.5000, 99.5000]], device=device, dtype=dtype),+ scale=torch.tensor([[0.7315, 0.7315], [0.7315, 0.7315]], device=device, dtype=dtype),+ angle=torch.tensor([10.5815, 10.5815], device=device, dtype=dtype),+ sx=torch.tensor([15.2617, 15.2617], device=device, dtype=dtype),+ sy=torch.tensor([14.7678, 14.7678], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['translations'], expected['translations'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['center'], expected['center'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['scale'], expected['scale'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angle'], expected['angle'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sx'], expected['sx'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sy'], expected['sy'], rtol=1e-4, atol=1e-4)+++class TestRandomRotationGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('degrees', [torch.tensor([0, 30])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, degrees, same_on_batch, device, dtype):+ random_rotation_generator(+ batch_size=batch_size, degrees=degrees.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('degrees', [+ pytest.param(torch.tensor(10), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([10]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([10, 20, 30]), marks=pytest.mark.xfail)+ ])+ def test_invalid_param_combinations(self, degrees, device, dtype):+ batch_size = 8+ random_rotation_generator(+ batch_size=batch_size, degrees=degrees.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ res = random_rotation_generator(+ batch_size=2, degrees=degrees.to(device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ degrees=torch.tensor([10.5815, 10.6291], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['degrees'], expected['degrees'])++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ res = random_rotation_generator(+ batch_size=2, degrees=degrees.to(device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ degrees=torch.tensor([10.5815, 10.5815], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['degrees'], expected['degrees'])+++class TestRandomCropGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [2])+ @pytest.mark.parametrize('input_size', [(200, 200)])+ @pytest.mark.parametrize('size', [(100, 100), torch.tensor([[50, 50], [60, 60]])])+ @pytest.mark.parametrize('resize_to', [None, (100, 100)])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, input_size, size, resize_to, same_on_batch, device, dtype+ ):+ random_crop_generator(+ batch_size=batch_size, input_size=input_size,+ size=size.to(device=device, dtype=dtype) if isinstance(size, torch.Tensor) else size,+ resize_to=resize_to,+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('input_size,size,resize_to', [+ pytest.param((-300, 300), (200, 200), (100, 100), marks=pytest.mark.xfail),+ pytest.param((100, 100), (200, 200), (100, 100), marks=pytest.mark.xfail),+ pytest.param((200, 200), torch.tensor([50, 50]), (100, 100), marks=pytest.mark.xfail),+ pytest.param((100, 100), torch.tensor([[200, 200], [200, 200]]), (100, 100), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, input_size, size, resize_to, device, dtype):+ batch_size = 2+ random_crop_generator(+ batch_size=batch_size, input_size=input_size,+ size=size.to(device=device, dtype=dtype) if isinstance(size, torch.Tensor) else size,+ resize_to=resize_to)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ res = random_crop_generator(+ batch_size=2, input_size=(100, 100),+ size=torch.tensor([[50, 60], [70, 80]], device=device, dtype=dtype),+ resize_to=(200, 200))+ expected = dict(+ src=torch.tensor([+ [[2, 6],+ [61, 6],+ [61, 55],+ [2, 55]],+ [[1, 1],+ [80, 1],+ [80, 70],+ [1, 70]]], device=device, dtype=torch.long),+ dst=torch.tensor([+ [[0, 0],+ [199, 0],+ [199, 199],+ [0, 199]],+ [[0, 0],+ [199, 0],+ [199, 199],+ [0, 199]]], device=device, dtype=torch.long),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['src'], expected['src'])+ assert_allclose(res['dst'], expected['dst'])++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ res = random_crop_generator(+ batch_size=2, input_size=(100, 100),+ size=torch.tensor([[50, 60], [70, 80]], device=device, dtype=dtype),+ resize_to=(200, 200), same_on_batch=True)+ expected = dict(+ src=torch.tensor([+ [[2, 3],+ [61, 3],+ [61, 52],+ [2, 52]],+ [[2, 3],+ [81, 3],+ [81, 72],+ [2, 72]]], device=device, dtype=torch.long),+ dst=torch.tensor([+ [[0, 0],+ [199, 0],+ [199, 199],+ [0, 199]],+ [[0, 0],+ [199, 0],+ [199, 199],+ [0, 199]]], device=device, dtype=torch.long),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['src'], expected['src'])+ assert_allclose(res['dst'], expected['dst'])+++class TestRandomCropSizeGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('size', [(200, 200)])+ @pytest.mark.parametrize('scale', [torch.tensor([.7, 1.3])])+ @pytest.mark.parametrize('ratio', [torch.tensor([.9, 1.1])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, size, scale, ratio, same_on_batch, device, dtype+ ):+ random_crop_size_generator(+ batch_size=batch_size, size=size,+ scale=scale.to(device=device, dtype=dtype),+ ratio=ratio.to(device=device, dtype=dtype),+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('size,scale,ratio', [+ pytest.param((100), torch.tensor([.7, 1.3]), torch.tensor([.9, 1.1]), marks=pytest.mark.xfail),+ pytest.param((100, 100, 100), torch.tensor([.7, 1.3]), torch.tensor([.9, 1.1]), marks=pytest.mark.xfail),+ pytest.param((100, 100), torch.tensor([.7]), torch.tensor([.9, 1.1]), marks=pytest.mark.xfail),+ pytest.param((100, 100), torch.tensor([.7, 1.3, 1.5]), torch.tensor([.9, 1.1]), marks=pytest.mark.xfail),+ pytest.param((100, 100), torch.tensor([.7, 1.3]), torch.tensor([.9]), marks=pytest.mark.xfail),+ pytest.param((100, 100), torch.tensor([.7, 1.3]), torch.tensor([.9, 1.1, 1.3]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, size, scale, ratio, device, dtype):+ batch_size = 2+ random_crop_size_generator(+ batch_size=batch_size, size=size,+ scale=scale.to(device=device, dtype=dtype),+ ratio=ratio.to(device=device, dtype=dtype),+ same_on_batch=same_on_batch)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ res = random_crop_size_generator(+ batch_size=8, size=(100, 100),+ scale=torch.tensor([0.7, 1.3], device=device, dtype=dtype),+ ratio=torch.tensor([0.9, 1.1], device=device, dtype=dtype),+ same_on_batch=False)+ expected = dict(+ size=torch.tensor([+ [84, 92],+ [87, 86],+ [89, 87],+ [98, 97],+ [89, 84],+ [90, 96],+ [89, 87],+ [92, 95]], device=device, dtype=torch.long)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['size'], expected['size'])++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([10, 20])+ res = random_crop_size_generator(+ batch_size=8, size=(100, 100),+ scale=torch.tensor([0.7, 1.3], device=device, dtype=dtype),+ ratio=torch.tensor([0.9, 1.1], device=device, dtype=dtype),+ same_on_batch=True)+ expected = dict(+ size=torch.tensor([+ [87, 89],+ [87, 89],+ [87, 89],+ [87, 89],+ [87, 89],+ [87, 89],+ [87, 89],+ [87, 89]], device=device, dtype=torch.long),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['size'], expected['size'])+++class TestRandomRectangleGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('height', [200])+ @pytest.mark.parametrize('width', [300])+ @pytest.mark.parametrize('scale', [torch.tensor([.7, 1.1])])+ @pytest.mark.parametrize('ratio', [torch.tensor([.7, 1.1])])+ @pytest.mark.parametrize('value', [0])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, height, width, scale, ratio, value, same_on_batch, device, dtype+ ):+ random_rectangles_params_generator(+ batch_size=batch_size, height=height, width=width, scale=scale.to(device=device, dtype=dtype),+ ratio=ratio.to(device=device, dtype=dtype), value=value, same_on_batch=same_on_batch)++ @pytest.mark.parametrize('height,width,scale,ratio,value', [+ pytest.param(-100, 100, torch.tensor([0.7, 1.3]), torch.tensor([0.7, 1.3]), 0, marks=pytest.mark.xfail),+ pytest.param(100, -100, torch.tensor([0.7, 1.3]), torch.tensor([0.7, 1.3]), 0, marks=pytest.mark.xfail),+ pytest.param(100, -100, torch.tensor([0.7]), torch.tensor([0.7, 1.3]), 0, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([0.7, 1.3, 1.5]), torch.tensor([0.7, 1.3]), 0, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([0.7, 1.3]), torch.tensor([0.7]), 0, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([0.7, 1.3]), torch.tensor([0.7, 1.3, 1.5]), 0, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([0.7, 1.3]), torch.tensor([0.7, 1.3]), -1, marks=pytest.mark.xfail),+ pytest.param(100, 100, torch.tensor([0.7, 1.3]), torch.tensor([0.7, 1.3]), 2, marks=pytest.mark.xfail),+ pytest.param(+ 100, 100, torch.tensor([.5, .7]), torch.tensor([.7, .9]), torch.tensor(0.5), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(+ self, height, width, scale, ratio, value, device, dtype+ ):+ batch_size = 8+ random_rectangles_params_generator(+ batch_size=batch_size, height=height, width=width, scale=scale.to(device=device, dtype=dtype),+ ratio=ratio.to(device=device, dtype=dtype), value=value, same_on_batch=same_on_batch)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ width, height = 100, 150+ scale = torch.tensor([0.7, 1.3], device=device, dtype=dtype)+ ratio = torch.tensor([0.7, 1.3], device=device, dtype=dtype)+ value = 0.5+ res = random_rectangles_params_generator(+ batch_size=2, height=height, width=width, scale=scale,+ ratio=ratio, value=value, same_on_batch=False)+ expected = dict(+ widths=torch.tensor([100, 100], device=device, dtype=torch.int32),+ heights=torch.tensor([0, 0], device=device, dtype=torch.int32),+ xs=torch.tensor([0, 0], device=device, dtype=torch.int32),+ ys=torch.tensor([9, 23], device=device, dtype=torch.int32),+ values=torch.tensor([0.5000, 0.5000], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['widths'], expected['widths'])+ assert_allclose(res['widths'], expected['widths'])+ assert_allclose(res['xs'], expected['xs'])+ assert_allclose(res['ys'], expected['ys'])+ assert_allclose(res['values'], expected['values'])++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ width, height = 100, 150+ scale = torch.tensor([0.7, 1.3], device=device, dtype=dtype)+ ratio = torch.tensor([0.7, 1.3], device=device, dtype=dtype)+ value = 0.5+ res = random_rectangles_params_generator(+ batch_size=2, height=height, width=width, scale=scale,+ ratio=ratio, value=value, same_on_batch=True)+ expected = dict(+ widths=torch.tensor([100, 100], device=device, dtype=torch.int32),+ heights=torch.tensor([0, 0], device=device, dtype=torch.int32),+ xs=torch.tensor([0, 0], device=device, dtype=torch.int32),+ ys=torch.tensor([20, 20], device=device, dtype=torch.int32),+ values=torch.tensor([0.5000, 0.5000], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['widths'], expected['widths'])+ assert_allclose(res['widths'], expected['widths'])+ assert_allclose(res['xs'], expected['xs'])+ assert_allclose(res['ys'], expected['ys'])+ assert_allclose(res['values'], expected['values'])+++class TestCenterCropGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [2])+ @pytest.mark.parametrize('height', [200])+ @pytest.mark.parametrize('width', [200])+ @pytest.mark.parametrize('size', [(100, 100)])+ def test_valid_param_combinations(+ self, batch_size, height, width, size, device, dtype+ ):+ center_crop_generator(batch_size=batch_size, height=height, width=width, size=size)++ @pytest.mark.parametrize('height,width,size', [+ pytest.param(200, -200, (100, 100), marks=pytest.mark.xfail),+ pytest.param(-200, 200, (100, 100), marks=pytest.mark.xfail),+ pytest.param(100, 100, (120, 120), marks=pytest.mark.xfail),+ pytest.param(150, 100, (120, 120), marks=pytest.mark.xfail),+ pytest.param(100, 150, (120, 120), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, height, width, size, device, dtype):+ batch_size = 2+ center_crop_generator(batch_size=batch_size, height=height, width=width, size=size)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ res = center_crop_generator(batch_size=2, height=200, width=200, size=(120, 150))+ expected = dict(+ src=torch.tensor([+ [[25, 40],+ [174, 40],+ [174, 159],+ [25, 159]],+ [[25, 40],+ [174, 40],+ [174, 159],+ [25, 159]]], device=device, dtype=torch.long),+ dst=torch.tensor([+ [[0, 0],+ [149, 0],+ [149, 119],+ [0, 119]],+ [[0, 0],+ [149, 0],+ [149, 119],+ [0, 119]]], device=device, dtype=torch.long),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['src'], expected['src'])+ assert_allclose(res['dst'], expected['dst'])++ def test_same_on_batch(self, device, dtype):+ pass+++class TestRandomMotionBlur(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('kernel_size', [1, (3, 5)])+ @pytest.mark.parametrize('angle', [torch.tensor([10, 30])])+ @pytest.mark.parametrize('direction', [torch.tensor([-1, -1]), torch.tensor([1, 1])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, kernel_size, angle, direction, same_on_batch, device, dtype):+ random_motion_blur_generator(+ batch_size=batch_size, kernel_size=kernel_size,+ angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype),+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('kernel_size,angle,direction', [+ pytest.param(4, torch.tensor([30, 100]), torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param(1, torch.tensor([30, 100]), torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param((1, 2, 3), torch.tensor([30, 100]), torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param(3, torch.tensor([30, 100]), torch.tensor([-2, 1]), marks=pytest.mark.xfail),+ pytest.param(3, torch.tensor([30, 100]), torch.tensor([-1, 2]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, kernel_size, angle, direction, device, dtype):+ random_motion_blur_generator(+ batch_size=8, kernel_size=kernel_size, angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ angle = torch.tensor([30, 90])+ direction = torch.tensor([-1, 1])+ res = random_motion_blur_generator(+ batch_size=2, kernel_size=3, angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ ksize_factor=torch.tensor([3., 3.], device=device, dtype=torch.int32),+ angle_factor=torch.tensor([33.4893, 33.7746], device=device, dtype=dtype),+ direction_factor=torch.tensor([-0.7528, -0.8948], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['ksize_factor'], expected['ksize_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angle_factor'], expected['angle_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['direction_factor'], expected['direction_factor'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ angle = torch.tensor([30, 90])+ direction = torch.tensor([-1, 1])+ res = random_motion_blur_generator(+ batch_size=2, kernel_size=3, angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ ksize_factor=torch.tensor([3., 3.], device=device, dtype=torch.int32),+ angle_factor=torch.tensor([33.4893, 33.4893], device=device, dtype=dtype),+ direction_factor=torch.tensor([-0.8742, -0.8742], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['ksize_factor'], expected['ksize_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angle_factor'], expected['angle_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['direction_factor'], expected['direction_factor'], rtol=1e-4, atol=1e-4)+++class TestRandomSolarizeGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('thresholds', [torch.tensor([0, 1]), torch.tensor([0.4, 0.6])])+ @pytest.mark.parametrize('additions', [torch.tensor([-0.5, 0.5])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, thresholds, additions, same_on_batch, device, dtype):+ random_solarize_generator(+ batch_size=batch_size, thresholds=thresholds.to(device=device, dtype=dtype),+ additions=additions.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('thresholds,additions', [+ pytest.param(torch.tensor([0, 2]), torch.tensor([-0.5, 0.5]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([-1, 1]), torch.tensor([-0.5, 0.5]), marks=pytest.mark.xfail),+ pytest.param([0, 1], torch.tensor([-0.5, 0.5]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0, 1]), torch.tensor([-0.5, 1]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0, 1]), torch.tensor([-1, 0.5]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0, 1]), [-0.5, 0.5], marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, thresholds, additions, device, dtype):+ random_solarize_generator(+ batch_size=batch_size, thresholds=thresholds.to(device=device, dtype=dtype),+ additions=additions.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 8+ res = random_solarize_generator(+ batch_size=batch_size, thresholds=torch.tensor([0, 1], device=device, dtype=dtype),+ additions=torch.tensor([-0.5, 0.5], device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ thresholds_factor=torch.tensor(+ [0.0582, 0.0629, 0.1236, 0.0526, 0.5262, 0.4768, 0.9552, 0.9288], device=device, dtype=dtype),+ additions_factor=torch.tensor(+ [-0.4165, -0.3674, -0.3429, -0.1246, 0.3425, 0.3705, -0.1225, 0.1124], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['thresholds_factor'], expected['thresholds_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['additions_factor'], expected['additions_factor'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 8+ res = random_solarize_generator(+ batch_size=batch_size, thresholds=torch.tensor([0, 1], device=device, dtype=dtype),+ additions=torch.tensor([-0.5, 0.5], device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ thresholds_factor=torch.tensor(+ [0.0582, 0.0582, 0.0582, 0.0582, 0.0582, 0.0582, 0.0582, 0.0582], device=device, dtype=dtype),+ additions_factor=torch.tensor(+ [-0.4371, -0.4371, -0.4371, -0.4371, -0.4371, -0.4371, -0.4371, -0.4371], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['thresholds_factor'], expected['thresholds_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['additions_factor'], expected['additions_factor'], rtol=1e-4, atol=1e-4)+++class TestRandomPosterizeGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('bits', [torch.tensor([0, 8])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, bits, same_on_batch, device, dtype):+ random_posterize_generator(+ batch_size=batch_size, bits=bits.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('bits', [+ pytest.param(torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0, 9]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([3]), marks=pytest.mark.xfail),+ pytest.param([0, 8], marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, bits, device, dtype):+ random_posterize_generator(+ batch_size=batch_size, bits=bits.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(9)+ batch_size = 8+ res = random_posterize_generator(+ batch_size=batch_size, bits=torch.tensor([0, 8], device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ bits_factor=torch.tensor(+ [1, 6, 2, 0, 0, 4, 0, 0], device=device, dtype=torch.int32)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['bits_factor'], expected['bits_factor'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(9)+ batch_size = 8+ res = random_posterize_generator(+ batch_size=batch_size, bits=torch.tensor([0, 8], device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ bits_factor=torch.tensor(+ [1, 1, 1, 1, 1, 1, 1, 1], device=device, dtype=torch.int32)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['bits_factor'], expected['bits_factor'], rtol=1e-4, atol=1e-4)+++class TestRandomSharpnessGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('sharpness', [torch.tensor([0., 1.])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, sharpness, same_on_batch, device, dtype):+ random_sharpness_generator(+ batch_size=batch_size, sharpness=sharpness.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('sharpness', [+ pytest.param(torch.tensor([-1, 5]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([3]), marks=pytest.mark.xfail),+ pytest.param([0, 1.], marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, sharpness, device, dtype):+ random_sharpness_generator(+ batch_size=batch_size, sharpness=sharpness.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 8+ res = random_sharpness_generator(+ batch_size=batch_size, sharpness=torch.tensor([0., 1.], device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ sharpness_factor=torch.tensor(+ [0.0582, 0.0629, 0.1236, 0.0526, 0.5262, 0.4768, 0.9552, 0.9288], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['sharpness_factor'], expected['sharpness_factor'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 8+ res = random_sharpness_generator(+ batch_size=batch_size, sharpness=torch.tensor([0., 1.], device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ sharpness_factor=torch.tensor(+ [0.0582, 0.0582, 0.0582, 0.0582, 0.0582, 0.0582, 0.0582, 0.0582], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['sharpness_factor'], expected['sharpness_factor'], rtol=1e-4, atol=1e-4)+++class TestRandomMixUpGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('p', [0., 0.5, 1.])+ @pytest.mark.parametrize('lambda_val', [None, torch.tensor([0., 1.])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, p, lambda_val, same_on_batch, device, dtype):+ random_mixup_generator(+ batch_size=batch_size, p=p,+ lambda_val=lambda_val.to(+ device=device, dtype=dtype) if isinstance(lambda_val, (torch.Tensor)) else lambda_val,+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('lambda_val', [+ pytest.param(torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0, 2]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([0, 0.5, 1]), marks=pytest.mark.xfail),+ pytest.param([0., 1.], marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, lambda_val, device, dtype):+ random_mixup_generator(+ batch_size=8, lambda_val=lambda_val.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 8+ res = random_mixup_generator(+ batch_size=batch_size, p=0.5,+ lambda_val=torch.tensor([0., 1.], device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ mixup_pairs=torch.tensor([6, 1, 0, 7, 2, 5, 3, 4], device=device, dtype=torch.long),+ mixup_lambdas=torch.tensor(+ [0.0000, 0.0000, 0.0196, 0.0000, 0.6535, 0.0000, 0.5949, 0.0000], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['mixup_pairs'], expected['mixup_pairs'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['mixup_lambdas'], expected['mixup_lambdas'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(9)+ batch_size = 8+ res = random_mixup_generator(+ batch_size=batch_size, p=.9999,+ lambda_val=torch.tensor([0., 1.], device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ mixup_pairs=torch.tensor([4, 6, 7, 5, 0, 1, 3, 2], device=device, dtype=torch.long),+ mixup_lambdas=torch.tensor(+ [0.0435, 0.0435, 0.0435, 0.0435, 0.0435, 0.0435, 0.0435, 0.0435], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['mixup_pairs'], expected['mixup_pairs'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['mixup_lambdas'], expected['mixup_lambdas'], rtol=1e-4, atol=1e-4)+++class TestRandomCutMixGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('p', [0, 0.5, 1.])+ @pytest.mark.parametrize('width,height', [(200, 200)])+ @pytest.mark.parametrize('num_mix', [1, 3])+ @pytest.mark.parametrize('beta', [None, torch.tensor(0.), torch.tensor(1.)])+ @pytest.mark.parametrize('cut_size', [None, torch.tensor([0., 1.]), torch.tensor([0.3, 0.6])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, p, width, height, num_mix, beta, cut_size, same_on_batch, device, dtype+ ):+ random_cutmix_generator(+ batch_size=batch_size, p=p, width=width, height=height,+ beta=beta.to(device=device, dtype=dtype) if isinstance(beta, (torch.Tensor)) else beta,+ cut_size=cut_size.to(device=device, dtype=dtype) if isinstance(cut_size, (torch.Tensor)) else cut_size,+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('width,height,num_mix,beta,cut_size', [+ pytest.param(200, -200, 1, None, None, marks=pytest.mark.xfail),+ pytest.param(-200, 200, 1, None, None, marks=pytest.mark.xfail),+ pytest.param(200, 200, 0, None, None, marks=pytest.mark.xfail),+ pytest.param(200, 200, 1.5, None, None, marks=pytest.mark.xfail),+ pytest.param(200, 200, 1, torch.tensor([0., 1.]), None, marks=pytest.mark.xfail),+ pytest.param(200, 200, 1, None, torch.tensor([-1., 1.]), marks=pytest.mark.xfail),+ pytest.param(200, 200, 1, None, torch.tensor([0., 2.]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, width, height, num_mix, beta, cut_size, device, dtype):+ random_cutmix_generator(+ batch_size=8, p=p, width=width, height=height,+ beta=beta.to(device=device, dtype=dtype) if isinstance(beta, (torch.Tensor)) else beta,+ cut_size=beta.to(device=device, dtype=dtype) if isinstance(cut_size, (torch.Tensor)) else cut_size,+ same_on_batch=same_on_batch)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 2+ res = random_cutmix_generator(+ batch_size=batch_size, width=200, height=200, p=0.5, num_mix=1,+ beta=torch.tensor(1., device=device, dtype=dtype),+ cut_size=torch.tensor([0., 1.], device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ mix_pairs=torch.tensor([[0., 1.]], device=device, dtype=torch.long),+ crop_src=torch.tensor([[+ [[174, 122],+ [173, 122],+ [173, 121],+ [174, 121]],+ [[75, 17],+ [74, 17],+ [74, 16],+ [75, 16]]]], device=device, dtype=torch.long)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['mix_pairs'], expected['mix_pairs'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['crop_src'], expected['crop_src'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 2+ res = random_cutmix_generator(+ batch_size=batch_size, width=200, height=200, p=0.5, num_mix=1,+ beta=torch.tensor(1., device=device, dtype=dtype),+ cut_size=torch.tensor([0., 1.], device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ mix_pairs=torch.tensor([[1., 0.]], device=device, dtype=torch.long),+ crop_src=torch.tensor([[+ [[118, 3],+ [117, 3],+ [117, 2],+ [118, 2]],+ [[118, 3],+ [117, 3],+ [117, 2],+ [118, 2]]]], device=device, dtype=torch.long)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['mix_pairs'], expected['mix_pairs'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['crop_src'], expected['crop_src'], rtol=1e-4, atol=1e-4)diff --git a/test/augmentation/test_random_generator_3d.py b/test/augmentation/test_random_generator_3d.pynew file mode 100644index 000000000..d69c1c097--- /dev/null+++ b/test/augmentation/test_random_generator_3d.py@@ -0,0 +1,561 @@+import pytest+import torch+from torch.testing import assert_allclose++from kornia.augmentation.random_generator import (+ random_rotation_generator3d,+ random_affine_generator3d,+ random_motion_blur_generator3d,+ center_crop_generator3d,+ random_crop_generator3d,+ random_perspective_generator3d,+)+++class RandomGeneratorBaseTests():++ def test_valid_param_combinations(self, device, dtype):+ raise NotImplementedError++ def test_invalid_param_combinations(self, device, dtype):+ raise NotImplementedError++ def test_random_gen(self, device, dtype):+ raise NotImplementedError++ def test_same_on_batch(self, device, dtype):+ raise NotImplementedError+++class TestRandomPerspectiveGen3D(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('depth,height,width', [(200, 200, 200)])+ @pytest.mark.parametrize('distortion_scale', [torch.tensor(0.), torch.tensor(0.5), torch.tensor(1.)])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, depth, height, width, distortion_scale, batch_size, same_on_batch, device, dtype+ ):+ random_perspective_generator3d(+ batch_size=batch_size, depth=depth, height=height, width=width,+ distortion_scale=distortion_scale.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('depth,height,width,distortion_scale', [+ # Should be failed if distortion_scale > 1. or distortion_scale < 0.+ pytest.param(-100, 100, 100, torch.tensor(0.5), marks=pytest.mark.xfail),+ pytest.param(100, -100, 100, torch.tensor(0.5), marks=pytest.mark.xfail),+ pytest.param(100, 100, -100, torch.tensor(-0.5), marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor(1.5), marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([0., 0.5]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, depth, height, width, distortion_scale, device, dtype):+ random_perspective_generator3d(+ batch_size=8, height=height, width=width,+ distortion_scale=distortion_scale.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 2+ res = random_perspective_generator3d(+ batch_size, 200, 200, 200, torch.tensor(0.5, device=device, dtype=dtype))+ expected = dict(+ start_points=torch.tensor([[[0., 0., 0.],+ [199., 0., 0.],+ [199., 199., 0.],+ [0., 199., 0.],+ [0., 0., 199.],+ [199., 0., 199.],+ [199., 199., 199.],+ [0., 199., 199.]],+ [[0., 0., 0.],+ [199., 0., 0.],+ [199., 199., 0.],+ [0., 199., 0.],+ [0., 0., 199.],+ [199., 0., 199.],+ [199., 199., 199.],+ [0., 199., 199.]]], device=device, dtype=dtype),+ end_points=torch.tensor([[[2.9077, 3.1455, 6.1793],+ [196.3710, 26.3086, 23.8392],+ [151.2382, 152.5624, 4.1772],+ [6.6320, 191.1473, 18.7684],+ [42.1260, 43.5251, 180.1234],+ [168.3782, 4.4055, 163.9409],+ [167.8298, 177.1361, 195.2633],+ [34.1715, 183.3881, 183.5471]],+ [[1.5670, 2.0183, 46.5954],+ [191.3937, 13.2522, 6.5217],+ [186.4072, 187.3296, 11.2833],+ [15.8784, 168.2796, 29.8886],+ [42.4501, 1.6248, 183.5378],+ [177.3848, 46.7149, 192.8844],+ [177.6152, 184.5908, 149.9299],+ [42.2815, 179.7861, 194.0347]]], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['start_points'], expected['start_points'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['end_points'], expected['end_points'], atol=1e-4, rtol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ batch_size = 2+ res = random_perspective_generator3d(+ batch_size, 200, 200, 200, torch.tensor(0.5, device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ start_points=torch.tensor([[[0., 0., 0.],+ [199., 0., 0.],+ [199., 199., 0.],+ [0., 199., 0.],+ [0., 0., 199.],+ [199., 0., 199.],+ [199., 199., 199.],+ [0., 199., 199.]],+ [[0., 0., 0.],+ [199., 0., 0.],+ [199., 199., 0.],+ [0., 199., 0.],+ [0., 0., 199.],+ [199., 0., 199.],+ [199., 199., 199.],+ [0., 199., 199.]]], device=device, dtype=dtype),+ end_points=torch.tensor([[[2.9077, 3.1455, 6.1793],+ [196.3710, 26.3086, 23.8392],+ [151.2382, 152.5624, 4.1772],+ [6.6320, 191.1473, 18.7684],+ [42.1260, 43.5251, 180.1234],+ [168.3782, 4.4055, 163.9409],+ [167.8298, 177.1361, 195.2633],+ [34.1715, 183.3881, 183.5471]],+ [[2.9077, 3.1455, 6.1793],+ [196.3710, 26.3086, 23.8392],+ [151.2382, 152.5624, 4.1772],+ [6.6320, 191.1473, 18.7684],+ [42.1260, 43.5251, 180.1234],+ [168.3782, 4.4055, 163.9409],+ [167.8298, 177.1361, 195.2633],+ [34.1715, 183.3881, 183.5471]]], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['start_points'], expected['start_points'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['end_points'], expected['end_points'], atol=1e-4, rtol=1e-4)+++class TestRandomAffineGen3D(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('depth,height,width', [(200, 300, 400)])+ @pytest.mark.parametrize('degrees', [torch.tensor([(0, 30), (0, 30), (0, 30)])])+ @pytest.mark.parametrize('translate', [None, torch.tensor([0.1, 0.1, 0.1])])+ @pytest.mark.parametrize('scale', [None, torch.tensor([[0.7, 1.2], [0.7, 1.2], [0.7, 1.2]])])+ @pytest.mark.parametrize('shear', [None, torch.tensor([[0, 20], [0, 20], [0, 20], [0, 20], [0, 20], [0, 20]])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, depth, height, width, degrees, translate, scale, shear, same_on_batch, device, dtype+ ):+ random_affine_generator3d(+ batch_size=batch_size, depth=depth, height=height, width=width,+ degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shears=shear.to(device=device, dtype=dtype) if shear is not None else None,+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('depth,height,width,degrees,translate,scale,shear', [+ pytest.param(-100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, -100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, -100, torch.tensor([[0, 9], [0, 9], [0, 9]]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([0, 9]), None, None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ torch.tensor([0.1, 0.2]), None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ torch.tensor([0.1, 0.2]), None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ torch.tensor([0.1]), None, None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ None, torch.tensor([[0.2, 0.2, 0.2]]), None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ None, torch.tensor([0.2]), None, marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ None, None, torch.tensor([[20, 20, 30]]), marks=pytest.mark.xfail),+ pytest.param(100, 100, 100, torch.tensor([[0, 9], [0, 9], [0, 9]]),+ None, None, torch.tensor([20]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(+ self, depth, height, width, degrees, translate, scale, shear, device, dtype+ ):+ batch_size = 8+ random_affine_generator3d(+ batch_size=batch_size, depth=depth, height=height, width=width,+ degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shears=shear.to(device=device, dtype=dtype) if shear is not None else None)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([[10, 20], [10, 20], [10, 20]])+ translate = torch.tensor([0.1, 0.1, 0.1])+ scale = torch.tensor([[0.7, 1.2], [0.7, 1.2], [0.7, 1.2]])+ shear = torch.tensor([[0, 20], [0, 20], [0, 20], [0, 20], [0, 20], [0, 20]])+ res = random_affine_generator3d(+ batch_size=2, depth=200, height=200, width=200,+ degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shears=shear.to(device=device, dtype=dtype) if shear is not None else None)+ expected = dict(+ translations=torch.tensor(+ [[13.7008, -4.8987, -16.4756], [14.8200, 4.4975, 8.0473]], device=device, dtype=dtype),+ center=torch.tensor(+ [[99.5000, 99.5000, 99.5000], [99.5000, 99.5000, 99.5000]], device=device, dtype=dtype),+ scale=torch.tensor(+ [[1.1776, 0.7418, 0.7785], [1.1644, 0.7663, 0.8877]], device=device, dtype=dtype),+ angles=torch.tensor(+ [[10.5815, 11.2359, 15.2617], [10.6291, 10.5258, 14.7678]], device=device, dtype=dtype),+ sxy=torch.tensor([12.4681, 8.7456], device=device, dtype=dtype),+ sxz=torch.tensor([1.4947, 13.6686], device=device, dtype=dtype),+ syx=torch.tensor([6.2448, 6.1812], device=device, dtype=dtype),+ syz=torch.tensor([0.6268, 0.8073], device=device, dtype=dtype),+ szx=torch.tensor([18.6382, 3.0425], device=device, dtype=dtype),+ szy=torch.tensor([5.3009, 2.6087], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['translations'], expected['translations'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['center'], expected['center'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['scale'], expected['scale'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angles'], expected['angles'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sxy'], expected['sxy'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sxz'], expected['sxz'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['syx'], expected['syx'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['syz'], expected['syz'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['szx'], expected['szx'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['szy'], expected['szy'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([[10, 20], [10, 20], [10, 20]])+ translate = torch.tensor([0.1, 0.1, 0.1])+ scale = torch.tensor([[0.7, 1.2], [0.7, 1.2], [0.7, 1.2]])+ shear = torch.tensor([[0, 20], [0, 20], [0, 20], [0, 20], [0, 20], [0, 20]])+ res = random_affine_generator3d(+ batch_size=2, depth=200, height=200, width=200,+ degrees=degrees.to(device=device, dtype=dtype),+ translate=translate.to(device=device, dtype=dtype) if translate is not None else None,+ scale=scale.to(device=device, dtype=dtype) if scale is not None else None,+ shears=shear.to(device=device, dtype=dtype) if shear is not None else None,+ same_on_batch=True)+ expected = dict(+ translations=torch.tensor(+ [[18.2094, 17.1501, -16.6583], [18.2094, 17.1501, -16.6583]], device=device, dtype=dtype),+ center=torch.tensor(+ [[99.5000, 99.5000, 99.5000], [99.5000, 99.5000, 99.5000]], device=device, dtype=dtype),+ scale=torch.tensor(+ [[0.7263, 0.9631, 0.9384], [0.7263, 0.9631, 0.9384]], device=device, dtype=dtype),+ angles=torch.tensor(+ [[10.5815, 10.6291, 11.2359], [10.5815, 10.6291, 11.2359]], device=device, dtype=dtype),+ sxy=torch.tensor([2.6528, 2.6528], device=device, dtype=dtype),+ sxz=torch.tensor([3.1411, 3.1411], device=device, dtype=dtype),+ syx=torch.tensor([7.5073, 7.5073], device=device, dtype=dtype),+ syz=torch.tensor([16.8504, 16.8504], device=device, dtype=dtype),+ szx=torch.tensor([17.4100, 17.4100], device=device, dtype=dtype),+ szy=torch.tensor([7.5507, 7.5507], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['translations'], expected['translations'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['center'], expected['center'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['scale'], expected['scale'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angles'], expected['angles'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sxy'], expected['sxy'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['sxz'], expected['sxz'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['syx'], expected['syx'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['syz'], expected['syz'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['szx'], expected['szx'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['szy'], expected['szy'], rtol=1e-4, atol=1e-4)+++class TestRandomRotationGen3D(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('degrees', [torch.tensor([[0, 30], [0, 30], [0, 30]])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, degrees, same_on_batch, device, dtype):+ random_rotation_generator3d(+ batch_size=batch_size, degrees=degrees.to(device=device, dtype=dtype), same_on_batch=same_on_batch)++ @pytest.mark.parametrize('degrees', [+ pytest.param(torch.tensor(10), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([10]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([[0, 30]]), marks=pytest.mark.xfail),+ pytest.param(torch.tensor([[0, 30], [0, 30]]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, degrees, device, dtype):+ batch_size = 8+ random_rotation_generator3d(+ batch_size=batch_size, degrees=degrees.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([[0, 30], [0, 30], [0, 30]])+ res = random_rotation_generator3d(+ batch_size=2, degrees=degrees.to(device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ yaw=torch.tensor([1.7446, 1.8873], device=device, dtype=dtype),+ pitch=torch.tensor([3.7076, 1.5774], device=device, dtype=dtype),+ roll=torch.tensor([15.7852, 14.3035], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['yaw'], expected['yaw'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['pitch'], expected['pitch'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['roll'], expected['roll'], atol=1e-4, rtol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ degrees = torch.tensor([[0, 30], [0, 30], [0, 30]])+ res = random_rotation_generator3d(+ batch_size=2, degrees=degrees.to(device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ yaw=torch.tensor([1.7446, 1.7446], device=device, dtype=dtype),+ pitch=torch.tensor([1.8873, 1.8873], device=device, dtype=dtype),+ roll=torch.tensor([3.7076, 3.7076], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['yaw'], expected['yaw'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['pitch'], expected['pitch'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['roll'], expected['roll'], atol=1e-4, rtol=1e-4)+++class TestRandomCropGen3D(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [2])+ @pytest.mark.parametrize('input_size', [(200, 200, 200)])+ @pytest.mark.parametrize('size', [(100, 100, 100), torch.tensor([[50, 60, 70], [50, 60, 70]])])+ @pytest.mark.parametrize('resize_to', [None, (100, 100, 100)])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(+ self, batch_size, input_size, size, resize_to, same_on_batch, device, dtype+ ):+ random_crop_generator3d(+ batch_size=batch_size, input_size=input_size,+ size=size.to(device=device, dtype=dtype) if isinstance(size, torch.Tensor) else size,+ resize_to=resize_to,+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('input_size,size,resize_to', [+ pytest.param((-300, 300, 300), (200, 200, 200), (100, 100, 100), marks=pytest.mark.xfail),+ pytest.param((100, 100, 100), (200, 200, 200), (100, 100, 100), marks=pytest.mark.xfail),+ pytest.param((200, 200, 200), torch.tensor([50, 50, 50]), (100, 100, 100), marks=pytest.mark.xfail),+ pytest.param((100, 100, 100), torch.tensor([[50, 60, 70], [50, 60, 70]]),+ (100, 100), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, input_size, size, resize_to, device, dtype):+ batch_size = 2+ random_crop_generator3d(+ batch_size=batch_size, input_size=input_size,+ size=size.to(device=device, dtype=dtype) if isinstance(size, torch.Tensor) else size,+ resize_to=resize_to)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ res = random_crop_generator3d(+ batch_size=2, input_size=(200, 200, 200),+ size=torch.tensor([[50, 60, 70], [50, 60, 70]], device=device, dtype=dtype),+ resize_to=(100, 100, 100))+ expected = dict(+ src=torch.tensor([[[7., 17., 79.],+ [76., 17., 79.],+ [76., 76., 79.],+ [7., 76., 79.],+ [7., 17., 128.],+ [76., 17., 128.],+ [76., 76., 128.],+ [7., 76., 128.]],+ [[8., 7., 71.],+ [77., 7., 71.],+ [77., 66., 71.],+ [8., 66., 71.],+ [8., 7., 120.],+ [77., 7., 120.],+ [77., 66., 120.],+ [8., 66., 120.]]], device=device, dtype=dtype),+ dst=torch.tensor([[[0., 0., 0.],+ [99., 0., 0.],+ [99., 99., 0.],+ [0., 99., 0.],+ [0., 0., 99.],+ [99., 0., 99.],+ [99., 99., 99.],+ [0., 99., 99.]],+ [[0., 0., 0.],+ [99., 0., 0.],+ [99., 99., 0.],+ [0., 99., 0.],+ [0., 0., 99.],+ [99., 0., 99.],+ [99., 99., 99.],+ [0., 99., 99.]]], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['src'], expected['src'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['dst'], expected['dst'], atol=1e-4, rtol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ res = random_crop_generator3d(+ batch_size=2, input_size=(200, 200, 200),+ size=torch.tensor([[50, 60, 70], [50, 60, 70]], device=device, dtype=dtype),+ resize_to=(100, 100, 100), same_on_batch=True)+ expected = dict(+ src=torch.tensor([[[7., 8., 18.],+ [76., 8., 18.],+ [76., 67., 18.],+ [7., 67., 18.],+ [7., 8., 67.],+ [76., 8., 67.],+ [76., 67., 67.],+ [7., 67., 67.]],+ [[7., 8., 18.],+ [76., 8., 18.],+ [76., 67., 18.],+ [7., 67., 18.],+ [7., 8., 67.],+ [76., 8., 67.],+ [76., 67., 67.],+ [7., 67., 67.]]], device=device, dtype=dtype),+ dst=torch.tensor([[[0., 0., 0.],+ [99., 0., 0.],+ [99., 99., 0.],+ [0., 99., 0.],+ [0., 0., 99.],+ [99., 0., 99.],+ [99., 99., 99.],+ [0., 99., 99.]],+ [[0., 0., 0.],+ [99., 0., 0.],+ [99., 99., 0.],+ [0., 99., 0.],+ [0., 0., 99.],+ [99., 0., 99.],+ [99., 99., 99.],+ [0., 99., 99.]]], device=device, dtype=dtype),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['src'], expected['src'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['dst'], expected['dst'], atol=1e-4, rtol=1e-4)+++class TestCenterCropGen(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [2])+ @pytest.mark.parametrize('depth,height,width', [(200, 200, 200)])+ @pytest.mark.parametrize('size', [(100, 100, 100)])+ def test_valid_param_combinations(+ self, batch_size, depth, height, width, size, device, dtype+ ):+ center_crop_generator3d(batch_size=batch_size, depth=depth, height=height, width=width, size=size)++ @pytest.mark.parametrize('depth,height,width,size', [+ pytest.param(200, 200, -200, (100, 100, 100), marks=pytest.mark.xfail),+ pytest.param(200, -200, 200, (100, 100), marks=pytest.mark.xfail),+ pytest.param(200, 100, 100, (300, 120, 100), marks=pytest.mark.xfail),+ pytest.param(200, 150, 100, (120, 180, 100), marks=pytest.mark.xfail),+ pytest.param(200, 100, 150, (120, 80, 200), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, depth, height, width, size, device, dtype):+ batch_size = 2+ center_crop_generator3d(batch_size=batch_size, depth=depth, height=height, width=width, size=size)++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ res = center_crop_generator3d(batch_size=2, depth=200, height=200, width=200, size=(120, 150, 100))+ expected = dict(+ src=torch.tensor([+ [[50, 25, 40],+ [149, 25, 40],+ [149, 174, 40],+ [50, 174, 40],+ [50, 25, 159],+ [149, 25, 159],+ [149, 174, 159],+ [50, 174, 159]]], device=device, dtype=torch.long),+ dst=torch.tensor([+ [[0, 0, 0],+ [99, 0, 0],+ [99, 149, 0],+ [0, 149, 0],+ [0, 0, 119],+ [99, 0, 119],+ [99, 149, 119],+ [0, 149, 119]]], device=device, dtype=torch.long),+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['src'], expected['src'], atol=1e-4, rtol=1e-4)+ assert_allclose(res['dst'], expected['dst'], atol=1e-4, rtol=1e-4)++ def test_same_on_batch(self, device, dtype):+ pass+++class TestRandomMotionBlur3D(RandomGeneratorBaseTests):++ @pytest.mark.parametrize('batch_size', [1, 8])+ @pytest.mark.parametrize('kernel_size', [1, (3, 5)])+ @pytest.mark.parametrize('angle', [torch.tensor([(10, 30), (30, 60), (60, 90)])])+ @pytest.mark.parametrize('direction', [torch.tensor([-1, -1]), torch.tensor([-1, 1]), torch.tensor([1, 1])])+ @pytest.mark.parametrize('same_on_batch', [True, False])+ def test_valid_param_combinations(self, batch_size, kernel_size, angle, direction, same_on_batch, device, dtype):+ random_motion_blur_generator3d(+ batch_size=batch_size, kernel_size=kernel_size,+ angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype),+ same_on_batch=same_on_batch)++ @pytest.mark.parametrize('kernel_size,angle,direction', [+ pytest.param(4, torch.tensor([(10, 30), (30, 60), (60, 90)]), torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param(1, torch.tensor([(10, 30), (30, 60), (60, 90)]), torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param((1, 2, 3), torch.tensor([(10, 30), (30, 60), (60, 90)]),+ torch.tensor([-1, 1]), marks=pytest.mark.xfail),+ pytest.param(3, torch.tensor([(10, 30), (30, 60), (60, 90)]), torch.tensor([-2, 1]), marks=pytest.mark.xfail),+ pytest.param(3, torch.tensor([(10, 30), (30, 60), (60, 90)]), torch.tensor([-1, 2]), marks=pytest.mark.xfail),+ ])+ def test_invalid_param_combinations(self, kernel_size, angle, direction, device, dtype):+ random_motion_blur_generator3d(+ batch_size=8, kernel_size=kernel_size, angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype))++ def test_random_gen(self, device, dtype):+ torch.manual_seed(42)+ angle = torch.tensor([(10, 30), (30, 60), (60, 90)])+ direction = torch.tensor([-1, 1])+ res = random_motion_blur_generator3d(+ batch_size=2, kernel_size=3, angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype), same_on_batch=False)+ expected = dict(+ ksize_factor=torch.tensor([3., 3.], device=device, dtype=torch.int32),+ angle_factor=torch.tensor([+ [11.1631, 33.7076, 75.7852],+ [11.2582, 31.5774, 74.3035]], device=device, dtype=dtype),+ direction_factor=torch.tensor([0.9105, 0.8575], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['ksize_factor'], expected['ksize_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angle_factor'], expected['angle_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['direction_factor'], expected['direction_factor'], rtol=1e-4, atol=1e-4)++ def test_same_on_batch(self, device, dtype):+ torch.manual_seed(42)+ angle = torch.tensor([(10, 30), (30, 60), (60, 90)])+ direction = torch.tensor([-1, 1])+ res = random_motion_blur_generator3d(+ batch_size=2, kernel_size=3, angle=angle.to(device=device, dtype=dtype),+ direction=direction.to(device=device, dtype=dtype), same_on_batch=True)+ expected = dict(+ ksize_factor=torch.tensor([3., 3.], device=device, dtype=torch.int32),+ angle_factor=torch.tensor([+ [11.1631, 31.8873, 63.7076],+ [11.1631, 31.8873, 63.7076]], device=device, dtype=dtype),+ direction_factor=torch.tensor([-0.8948, -0.8948], device=device, dtype=dtype)+ )+ assert res.keys() == expected.keys()+ assert_allclose(res['ksize_factor'], expected['ksize_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['angle_factor'], expected['angle_factor'], rtol=1e-4, atol=1e-4)+ assert_allclose(res['direction_factor'], expected['direction_factor'], rtol=1e-4, atol=1e-4)