def check_multichannel_synth_data_vs_numpy(tested_operator, device, batch_size, shape): eii1 = RandomDataIterator(batch_size, shape=shape) eii2 = RandomDataIterator(batch_size, shape=shape) compare_pipelines(MultichannelSynthPipeline(device, batch_size, "HWC", iter(eii1), tested_operator=tested_operator), MultichannelSynthPythonOpPipeline(get_numpy_func(tested_operator), batch_size, "HWC", iter(eii2)), batch_size=batch_size, N_iterations=10, eps = 0.2)
def check_operator_erase_vs_python(device, batch_size, input_shape, anchor, shape, axis_names, axes, input_layout, fill_value): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines(ErasePipeline(device, batch_size, input_layout, iter(eii1), anchor=anchor, shape=shape, axis_names=axis_names, axes=axes, fill_value=fill_value), ErasePythonPipeline(device, batch_size, input_layout, iter(eii2), anchor=anchor, shape=shape, axis_names=axis_names, axes=axes, fill_value=fill_value), batch_size=batch_size, N_iterations=3, eps=1e-04, expected_layout=input_layout)
def check_vs_ref(device, inp_dtype, out_dtype): batch_size = 32 n_iters = 8 ri1 = RandomDataIterator(batch_size, shape=(128, 32, 3), dtype=dali_type_to_np(inp_dtype)) ri2 = RandomDataIterator(batch_size, shape=(128, 32, 3), dtype=dali_type_to_np(inp_dtype)) contrast_center = 0.4 * max_range(dali_type_to_np(inp_dtype)) pipe1 = bricon_ref_pipe(ri1, contrast_center, out_dtype, device, batch_size=batch_size) pipe2 = bricon_pipe(ri2, contrast_center, True, True, out_dtype, device, batch_size=batch_size) if out_dtype in [np.half, np.single, np.double]: eps = 1e-4 else: eps = 1 compare_pipelines(pipe1, pipe2, batch_size, n_iters, eps=eps)
def check_operator_mel_filter_bank_vs_python(device, batch_size, input_shape, nfilter, sample_rate, freq_low, freq_high, normalize, mel_formula): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines(MelFilterBankPipeline(device, batch_size, iter(eii1), nfilter=nfilter, sample_rate=sample_rate, freq_low=freq_low, freq_high=freq_high, normalize=normalize, mel_formula=mel_formula), MelFilterBankPythonPipeline(device, batch_size, iter(eii2), nfilter=nfilter, sample_rate=sample_rate, freq_low=freq_low, freq_high=freq_high, normalize=normalize, mel_formula=mel_formula), batch_size=batch_size, N_iterations=5, eps=1e-03)
def check_cmn_random_data_vs_numpy(device, batch_size, output_dtype, input_layout, input_shape, output_layout, mirror_probability, mean, std, should_pad): crop_y, crop_x, crop_h, crop_w = (0.2, 0.3, 224, 224) eii1 = RandomDataIterator(batch_size, shape=input_shape) eii2 = RandomDataIterator(batch_size, shape=input_shape) assert mirror_probability in cmn_coin_flip_samples global cmn_idx cmn_idx = 0 function = partial(crop_mirror_normalize_func, crop_y, crop_x, crop_h, crop_w, mirror_probability, should_pad, mean, std, input_layout, output_layout) compare_pipelines( CMNRandomDataPipeline(device, batch_size, input_layout, iter(eii1), output_dtype=output_dtype, output_layout=output_layout, mirror_probability=mirror_probability, mean=mean, std=std, pad_output=should_pad), CMNRandomDataPythonOpPipeline(function, batch_size, input_layout, iter(eii2)), batch_size=batch_size, N_iterations=1)
def check_lookup_table_vs_python_op(device, batch_size, layout, shape, dictionary_type, default_value): eii1 = RandomDataIterator(batch_size, shape=shape) eii2 = RandomDataIterator(batch_size, shape=shape) if dictionary_type == 'empty': dictionary = {} elif dictionary_type == 'random': dictionary = {k: random.random() for k in range(256)} elif dictionary_type == 'small': dictionary = {0: 0.1, 200: 0.99} else: assert (False) compare_pipelines(LookupTablePipeline(device, batch_size, iter(eii1), data_shape=shape, data_layout=layout, dictionary=dictionary, default_value=default_value), LookupTablePythonOpPipeline(lookup_func, batch_size, iter(eii2), data_shape=shape, data_layout=layout, dictionary=dictionary, default_value=default_value), batch_size=batch_size, N_iterations=3)
def check_operator_erase_with_normalized_coords(device, batch_size, input_shape, anchor, shape, axis_names, input_layout, anchor_norm, shape_norm, normalized_anchor, normalized_shape): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines(ErasePipeline(device, batch_size, input_layout, iter(eii1), anchor=anchor_norm, shape=shape_norm, normalized_anchor=normalized_anchor, normalized_shape=normalized_shape, axis_names=axis_names, axes=None, fill_value=0), ErasePipeline(device, batch_size, input_layout, iter(eii2), anchor=anchor, shape=shape, axis_names=axis_names, axes=None, fill_value=0), batch_size=batch_size, N_iterations=3, eps=1e-04)
def check_natural_logarithm(device, batch_size, input_shape): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines( NLDaliPipeline(device, iter(eii1), batch_size), NLPythonPipeline(iter(eii2), batch_size), batch_size=batch_size, N_iterations=5, eps=1e-04)
def check_equivalence(device, inp_dtype, out_dtype, op): batch_size = 32 n_iters = 16 ri1 = RandomDataIterator(batch_size, shape=(128, 32, 3), dtype=dali_type_to_np(inp_dtype)) ri2 = RandomDataIterator(batch_size, shape=(128, 32, 3), dtype=dali_type_to_np(inp_dtype)) contrast_center = 0.4 * max_range(dali_type_to_np(inp_dtype)) if op == 'brightness': pipe1 = bri_pipe(ri1, out_dtype, device, batch_size=batch_size) else: pipe1 = con_pipe(ri1, contrast_center, out_dtype, device, batch_size=batch_size) bri = op == 'brightness' con = op == 'contrast' pipe2 = bricon_pipe(ri2, contrast_center, bri, con, out_dtype, device, batch_size=batch_size) if out_dtype in [np.half, np.single, np.double]: eps = 1e-4 else: eps = 1 compare_pipelines(pipe1, pipe2, batch_size, n_iters, eps=eps)
def check_transpose_vs_numpy(device, batch_size, shape): eii1 = RandomDataIterator(batch_size, shape=shape) eii2 = RandomDataIterator(batch_size, shape=shape) compare_pipelines(TransposePipeline(device, batch_size, "HWC", iter(eii1)), PythonOpPipeline(transpose_func, batch_size, "HWC", iter(eii2)), batch_size=batch_size, N_iterations=10)
def test_color_twist_vs_cpu(): batch_size = 8 seed = 1919 rand_it1 = RandomDataIterator(batch_size, shape=(1024, 512, 3)) rand_it2 = RandomDataIterator(batch_size, shape=(1024, 512, 3)) compare_pipelines(ColorTwistPipeline(batch_size, seed, iter(rand_it1), kind="new"), ColorTwistPipeline(batch_size, seed, iter(rand_it2), kind="oldCpu"), batch_size=batch_size, N_iterations=16, eps=1)
def check_crop_NHWC_vs_python_op_crop(device, batch_size): eii1 = RandomDataIterator(batch_size, shape=(600, 800, 3)) eii2 = RandomDataIterator(batch_size, shape=(600, 800, 3)) compare_pipelines(CropSequencePipeline(device, batch_size, types.NHWC, iter(eii1)), CropSequencePythonOpPipeline(crop_NHWC_func, batch_size, types.NHWC, iter(eii2)), batch_size=batch_size, N_iterations=10)
def check_cast_operator_float16(device, batch_size): input_shape = (300, 400, 3) eii1 = RandomDataIterator(batch_size, shape=input_shape) eii2 = RandomDataIterator(batch_size, shape=input_shape) compare_pipelines(CastPipeline(device, batch_size, iter(eii1), [types.FLOAT16, types.FLOAT]), CastPipeline(device, batch_size, iter(eii2), [types.FLOAT]), batch_size=batch_size, N_iterations=5)
def check_operator_to_decibels_vs_python(device, batch_size, input_shape, multiplier, reference, cutoff_db): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines( ToDecibelsPipeline(device, batch_size, iter(eii1), multiplier=multiplier, reference=reference, cutoff_db=cutoff_db), ToDecibelsPythonPipeline(device, batch_size, iter(eii2), multiplier=multiplier, reference=reference, cutoff_db=cutoff_db), batch_size=batch_size, N_iterations=5, eps=1e-04)
def check_operator_spectrogram_vs_python(device, batch_size, input_shape, nfft, window_length, window_step): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines( SpectrogramPipeline(device, batch_size, iter(eii1), nfft=nfft, window=None, window_length=window_length, window_step=window_step), SpectrogramPythonPipeline(device, batch_size, iter(eii2), window=None, nfft=nfft, window_length=window_length, window_step=window_step), batch_size=batch_size, N_iterations=5, eps=1e-04)
def check_operator_mfcc_vs_python(device, batch_size, input_shape, axis, dct_type, lifter, n_mfcc, norm): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines( MFCCPipeline(device, batch_size, iter(eii1), axis=axis, dct_type=dct_type, lifter=lifter, n_mfcc=n_mfcc, norm=norm), MFCCPythonPipeline(device, batch_size, iter(eii2), axis=axis, dct_type=dct_type, lifter=lifter, n_mfcc=n_mfcc, norm=norm), batch_size=batch_size, N_iterations=5, eps=1e-03)
def check_crop_3d_vs_python_op_crop(device, batch_size, layout, shape): eii1 = RandomDataIterator(batch_size, shape=shape) eii2 = RandomDataIterator(batch_size, shape=shape) compare_pipelines(Crop3dPipeline(device, batch_size, iter(eii1), data_shape=shape, data_layout=layout), Crop3dPythonOpPipeline(crop_3d_func, batch_size, iter(eii2), data_shape=shape, data_layout=layout), batch_size=batch_size, N_iterations=3)
def check_slice_with_out_of_bounds_error(device, batch_size, input_shape=(100, 200, 3), normalized_anchor=False, normalized_shape=False): # This test case is written with HWC layout in mind and "HW" axes in slice arguments axis_names = "HW" naxes = len(axis_names) axes = None layout = "HWC" assert(len(input_shape) == 3) eii = RandomDataIterator(batch_size, shape=input_shape) eii_arg = SliceArgsIterator(batch_size, len(input_shape), image_shape=input_shape, image_layout=layout, axes=axes, axis_names=axis_names, normalized_anchor=normalized_anchor, normalized_shape=normalized_shape, min_norm_anchor=-0.5, max_norm_anchor=-0.1, min_norm_shape=1.1, max_norm_shape=3.6) pipe = SliceSynthDataPipeline(device, batch_size, layout, iter(eii), iter(eii_arg), axes=axes, axis_names=axis_names, normalized_anchor=normalized_anchor, normalized_shape=normalized_shape, out_of_bounds_policy="error") pipe.build() with assert_raises(RuntimeError): outs = pipe.run()
def check_crop_with_out_of_bounds_error(device, batch_size, input_shape=(100, 200, 3)): # This test case is written with HWC layout in mind and "HW" axes in slice arguments layout = "HWC" assert len(input_shape) == 3 eii = RandomDataIterator(batch_size, shape=input_shape) crop_shape = tuple(extent * 2 for extent in input_shape[:2]) crop_y = 0.5 crop_x = 0.5 pipe = CropSynthPipe(device, batch_size, iter(eii), layout=layout, crop_shape=crop_shape, crop_x=crop_x, crop_y=crop_y, out_of_bounds_policy="error") pipe.build() with assert_raises( RuntimeError, glob="Slice can't be placed out of bounds with current policy."): _ = pipe.run()
def check_crop_sequence_length(device, batch_size, dtype, input_layout, input_shape): if input_layout == "FHWC": D, H, W, C = input_shape elif input_layout == "FCHW": D, C, H, W = input_shape else: assert False crop_d = int(D * 0.91) crop_h = int(H * 0.85) crop_w = int(W * 0.75) if input_layout == "FHWC": crop_shape = (crop_d, crop_h, crop_w, C) elif input_layout == "FCHW": crop_shape = (crop_d, C, crop_h, crop_w) else: assert False eii1 = RandomDataIterator(batch_size, shape=input_shape) pipe = Crop3dPipeline(device, batch_size, iter(eii1), data_shape=input_shape, data_layout=input_layout, crop_seq_as_depth=True) pipe.build() out = pipe.run() out_data = out[0] for i in range(batch_size): assert out_data.at(i).shape == crop_shape, \ "Shape mismatch {} != {}".format(out_data.at(i).shape, crop_shape)
def check_operator_power_spectrum(device, batch_size, input_shape, nfft, axis): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) eii2 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) compare_pipelines(PowerSpectrumPipeline(device, batch_size, iter(eii1), axis=axis, nfft=nfft), PowerSpectrumNumpyPipeline(device, batch_size, iter(eii2), axis=axis, nfft=nfft), batch_size=batch_size, N_iterations=3, eps=1e-04)
def __init__(self, dims, epoch_size, dtype, exception_class=StopIteration, random_data=False, random_shape=False): self.dims = dims self.epoch_size = epoch_size self.dtype = dtype self.exception_class = exception_class self.ds = {} self.random_data = random_data self.random_shape = random_shape self.data_iterator = None self.iterator_data_samples = [] if random_data and not random_shape: self.data_iterator = iter( RandomDataIterator(1, shape=dims, dtype=dtype)) if random_data and random_shape: self.data_iterator = iter( RandomlyShapedDataIterator(1, max_shape=dims, dtype=dtype)) if not random_data and random_shape: raise ValueError( "If random_shape is required the random_data is required to be True." )
def check_slice_with_out_of_bounds_policy_support(device, batch_size, input_shape=(100, 200, 3), out_of_bounds_policy=None, fill_values=(0x76, 0xb9, 0x00), normalized_anchor=False, normalized_shape=False): # This test case is written with HWC layout in mind and "HW" axes in slice arguments axis_names = "HW" axes = None layout = "HWC" assert len(input_shape) == 3 if fill_values is not None and len(fill_values) > 1: assert input_shape[2] == len(fill_values) eii = RandomDataIterator(batch_size, shape=input_shape) eii_arg = SliceArgsIterator(batch_size, len(input_shape), image_shape=input_shape, image_layout=layout, axes=axes, axis_names=axis_names, normalized_anchor=normalized_anchor, normalized_shape=normalized_shape, min_norm_anchor=-0.5, max_norm_anchor=-0.1, min_norm_shape=1.1, max_norm_shape=3.6) pipe = SliceSynthDataPipeline(device, batch_size, layout, iter(eii), iter(eii_arg), axes=axes, axis_names=axis_names, normalized_anchor=normalized_anchor, normalized_shape=normalized_shape, out_of_bounds_policy=out_of_bounds_policy, fill_values=fill_values, extra_outputs=True) if fill_values is None: fill_values = 0 pipe.build() for _ in range(3): outs = pipe.run() out, in_data, anchor_data, shape_data = outs assert batch_size == len(out) for idx in range(batch_size): sample_in = as_array(in_data[idx]) sample_out = as_array(out[idx]) anchor = as_array(anchor_data[idx]) shape = as_array(shape_data[idx]) in_shape = sample_in.shape abs_start, abs_end, abs_slice_shape = abs_slice_start_and_end( in_shape[:2], anchor, shape, normalized_anchor, normalized_shape) check_slice_output(sample_in, sample_out, anchor, abs_slice_shape, abs_start, abs_end, out_of_bounds_policy, fill_values)
def check_operator_coord_flip(device, batch_size, layout, shape, center_x, center_y, center_z): eii1 = RandomDataIterator(batch_size, shape=shape, dtype=np.float32) pipe = CoordFlipPipeline(device, batch_size, iter(eii1), layout, center_x, center_y, center_z) pipe.build() for i in range(30): outputs = pipe.run() for sample in range(batch_size): in_coords = outputs[0].at(sample) out_coords = outputs[1].as_cpu().at(sample) if device == 'gpu' else outputs[1].at(sample) if in_coords.shape == () or in_coords.shape[0] == 0: assert(out_coords.shape == () or out_coords.shape[0] == 0) continue flip_x = outputs[2].at(sample) flip_y = outputs[3].at(sample) flip_z = None if len(layout) == 3: flip_z = outputs[4].at(sample) npoints, ndim = in_coords.shape flip_dim = [flip_x, flip_y] if ndim == 3: flip_dim.append(flip_z) center_dim = [center_x, center_y] if ndim == 3: center_dim.append(center_z) expected_out_coords = np.copy(in_coords) for d in range(ndim): if flip_dim[d]: expected_out_coords[:, d] = 2 * center_dim[d] - in_coords[:, d] np.testing.assert_allclose(out_coords[:, d], expected_out_coords[:, d])
def check_operator_mfcc_wrong_args(device, batch_size, input_shape, axis, dct_type, lifter, n_mfcc, norm): eii1 = RandomDataIterator(batch_size, shape=input_shape, dtype=np.float32) pipe = MFCCPipeline(device, batch_size, iter(eii1), axis=axis, dct_type=dct_type, lifter=lifter, n_mfcc=n_mfcc, norm=norm) pipe.build() pipe.run()
def check_cmn_with_out_of_bounds_error(device, batch_size, input_shape=(100, 200, 3)): # This test case is written with HWC layout in mind and "HW" axes in slice arguments layout = "HWC" assert (len(input_shape) == 3) eii = RandomDataIterator(batch_size, shape=input_shape) crop_y, crop_x = 0.5, 0.5 crop_h, crop_w = input_shape[0] * 2, input_shape[1] * 2 pipe = CMNRandomDataPipeline(device, batch_size, layout, iter(eii), dtype=types.FLOAT, output_layout=layout, mirror_probability=0.5, mean=[127.], std=[127.], pad_output=True, crop_w=crop_w, crop_h=crop_h, crop_pos_x=crop_x, crop_pos_y=crop_y, out_of_bounds_policy="error") pipe.build() with assert_raises(RuntimeError): outs = pipe.run()
def check_vs_ref(device, inp_dtype, out_dtype, has_3_dims, use_const_contr_center): batch_size = 32 n_iters = 8 shape = (128, 32, 3) if not has_3_dims else (random.randint(2, 5), 128, 32, 3) ri1 = RandomDataIterator(batch_size, shape=shape, dtype=dali_type_to_np(inp_dtype)) ri2 = RandomDataIterator(batch_size, shape=shape, dtype=dali_type_to_np(inp_dtype)) contrast_center = None if not use_const_contr_center else 0.4 * \ max_range(dali_type_to_np(inp_dtype)) pipe1 = bricon_ref_pipe(ri1, contrast_center, out_dtype, has_3_dims=has_3_dims, batch_size=batch_size) pipe2 = bricon_pipe(ri2, contrast_center, True, True, out_dtype, device, batch_size=batch_size) if out_dtype in [np.half, np.single, np.double]: eps = 1e-4 else: eps = 1 compare_pipelines(pipe1, pipe2, batch_size, n_iters, eps=eps)
def check_cmn_crop_sequence_length(device, batch_size, dtype, input_layout, input_shape, output_layout, mirror_probability, mean, std, should_pad): crop_z, crop_y, crop_x = (0.1, 0.2, 0.3) crop_d, crop_h, crop_w = (8, 16, 32) eii1 = RandomDataIterator(batch_size, shape=input_shape) pipe = CMNRandomDataPipeline(device, batch_size, input_layout, iter(eii1), dtype=dtype, output_layout=output_layout, mirror_probability=mirror_probability, mean=mean, std=std, pad_output=should_pad, crop_seq_as_depth=True) pipe.build() out = pipe.run() out_data = out[0] expected_out_shape = (crop_d, 3, crop_h, crop_w) if output_layout == "FCHW" else (crop_d, crop_h, crop_w, 3) for i in range(batch_size): assert(out_data.at(i).shape == expected_out_shape), \ "Shape mismatch {} != {}".format(out_data.at(i).shape, expected_out_shape)
def check_flip(batch_size, layout, shape, device): eiis = [RandomDataIterator(batch_size, shape=shape) for k in range(2)] compare_pipelines(SynthFlipPipeline(batch_size, layout, iter(eiis[0]), device), SynthPythonFlipPipeline(batch_size, layout, iter(eiis[1])), batch_size=batch_size, N_iterations=5)
def check_cmn_random_data_vs_numpy(device, batch_size, dtype, input_layout, input_shape, output_layout, mirror_probability, mean, std, scale, shift, should_pad): crop_z, crop_y, crop_x = (0.1, 0.2, 0.3) crop_d, crop_h, crop_w = (8, 16, 32) eii1 = RandomDataIterator(batch_size, shape=input_shape) eii2 = RandomDataIterator(batch_size, shape=input_shape) assert mirror_probability in cmn_coin_flip_samples global cmn_idx cmn_idx = 0 function = partial(crop_mirror_normalize_func, crop_z, crop_y, crop_x, crop_d, crop_h, crop_w, mirror_probability, should_pad, mean, std, scale, shift, input_layout, output_layout, dali_type_to_np(dtype)) cmn_pipe = CMNRandomDataPipeline(device, batch_size, input_layout, iter(eii1), dtype=dtype, output_layout=output_layout, mirror_probability=mirror_probability, mean=mean, std=std, scale=scale, shift=shift, pad_output=should_pad) ref_pipe = CMNRandomDataPythonOpPipeline(function, batch_size, input_layout, output_layout, iter(eii2)) eps, max_err = (1e-5, 1e-5) if dtype == types.FLOAT else (0.3, 0.6) compare_pipelines(cmn_pipe, ref_pipe, batch_size, 2, eps=eps, max_allowed_error=max_err)