def test_mxnet_reader_cpu(): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None) out, _ = fn.mxnet_reader(path=os.path.join(recordio_dir, "train.rec"), index_path=os.path.join(recordio_dir, "train.idx"), shard_id=0, num_shards=1) pipe.set_outputs(out) pipe.build() for _ in range(3): pipe.run()
def test_mfcc_cpu(): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None) data = fn.external_source(source=get_audio_data) spectrum = fn.spectrogram(data, nfft=60, window_length=50, window_step=25) mel = fn.mel_filter_bank(spectrum) dec = fn.to_decibels(mel) processed = fn.mfcc(dec) pipe.set_outputs(processed) pipe.build() for _ in range(3): pipe.run()
def check_no_input(op, get_data=get_data, **kwargs): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None) with pipe: processed = op(**kwargs) if isinstance(processed, Iterable): pipe.set_outputs(*processed) else: pipe.set_outputs(processed) pipe.build() for _ in range(3): pipe.run()
def test_image_decoder_cpu(): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None) with pipe: input, _ = fn.readers.file(file_root=images_dir, shard_id=0, num_shards=1) decoded = fn.decoders.image(input, output_type=types.RGB) pipe.set_outputs(decoded) pipe.build() for _ in range(3): pipe.run()
def test_combine_transforms_cpu(): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None) with pipe: t = fn.transforms.translation(offset=(1, 2)) r = fn.transforms.rotation(angle=30.0) s = fn.transforms.scale(scale=(2, 3)) out = fn.transforms.combine(t, r, s) pipe.set_outputs(out) pipe.build() for _ in range(3): pipe.run()
def audio_decoder_pipe(max_batch_size, input_data, device): pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0) encoded = fn.external_source(source=input_data, cycle=False, device='cpu') decoded, _ = fn.decoders.audio(encoded, downmix=True, sample_rate=12345, device=device) pipe.set_outputs(decoded) return pipe
def test_external_source_collection(): pipe = Pipeline(1, 3, 0) batches = [ [np.array([1.5,2.5], dtype= np.float32)], [np.array([-1, 3.5,4.5], dtype= np.float32)] ] pipe.set_outputs(fn.external_source(batches)) pipe.build() run_and_check(pipe, batches)
def test_operator_coco_reader_same_images(): file_root = os.path.join(test_data_root, 'db', 'coco_pixelwise', 'images') train_annotations = os.path.join(test_data_root, 'db', 'coco_pixelwise', 'instances.json') coco_dir = os.path.join(test_data_root, 'db', 'coco') coco_dir_imgs = os.path.join(coco_dir, 'images') coco_pixelwise_dir = os.path.join(test_data_root, 'db', 'coco_pixelwise') coco_pixelwise_dir_imgs = os.path.join(coco_pixelwise_dir, 'images') for file_root, annotations_file in [ \ (coco_dir_imgs, os.path.join(coco_dir, 'instances.json')), (coco_pixelwise_dir_imgs, os.path.join(coco_pixelwise_dir, 'instances.json')), (coco_pixelwise_dir_imgs, os.path.join(coco_pixelwise_dir, 'instances_rle_counts.json'))]: pipe = Pipeline(batch_size=1, num_threads=4, device_id=0) with pipe: inputs1, boxes1, labels1, *other = fn.coco_reader( file_root=file_root, annotations_file=train_annotations, name="reader1", seed=1234 ) inputs2, boxes2, labels2, *other = fn.coco_reader( file_root=file_root, annotations_file=train_annotations, polygon_masks=True, name="reader2" ) inputs3, boxes3, labels3, *other = fn.coco_reader( file_root=file_root, annotations_file=train_annotations, pixelwise_masks=True, name="reader3" ) pipe.set_outputs( inputs1, boxes1, labels1, inputs2, boxes2, labels2, inputs3, boxes3, labels3 ) pipe.build() epoch_sz = pipe.epoch_size("reader1") assert epoch_sz == pipe.epoch_size("reader2") assert epoch_sz == pipe.epoch_size("reader3") for i in range(epoch_sz): inputs1, boxes1, labels1, inputs2, boxes2, labels2, inputs3, boxes3, labels3 = \ pipe.run() np.testing.assert_array_equal(inputs1.at(0), inputs2.at(0)) np.testing.assert_array_equal(inputs1.at(0), inputs3.at(0)) np.testing.assert_array_equal(labels1.at(0), labels2.at(0)) np.testing.assert_array_equal(labels1.at(0), labels3.at(0)) np.testing.assert_array_equal(boxes1.at(0), boxes2.at(0)) np.testing.assert_array_equal(boxes1.at(0), boxes3.at(0))
def _test_permute_batch_out_of_range(device): batch_size = 10 pipe = Pipeline(batch_size, 4, 0) data = fn.external_source(source=lambda: gen_data(batch_size, np.int32), device=device, layout="abc") perm = fn.batch_permutation() pipe.set_outputs( data, fn.permute_batch(data, indices=[0, 1, 2, 3, 4, 5, 10, 7, 8, 9]), perm) pipe.build() pipe.run()
def check_per_sample_gaussian_blur(batch_size, sigma_dim, window_size_dim, shape, layout, axes, op_type="cpu"): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0) data = RandomlyShapedDataIterator(batch_size, max_shape=shape) with pipe: if sigma_dim is not None: sigma = fn.uniform(range=[0.5, 3], shape=[sigma_dim]) sigma_arg = sigma else: # placeholder, so we can return something sigma = fn.coin_flip(probability=0) sigma_arg = None if window_size_dim is not None: window_radius = fn.uniform(range=[5, 10], shape=[window_size_dim]) window_size = fn.cast(window_radius, dtype=types.INT32) * 2 + 1 window_arg = window_size else: window_size = fn.coin_flip(probability=0) window_arg = None input = fn.external_source(data, layout=layout) if op_type == "gpu": input = input.gpu() blurred = fn.gaussian_blur(input, device=op_type, sigma=sigma_arg, window_size=window_arg) pipe.set_outputs(blurred, input, sigma, window_size) pipe.build() for _ in range(test_iters): result, input, sigma, window_size = pipe.run() if op_type == "gpu": result = result.as_cpu() input = input.as_cpu() input = to_batch(input, batch_size) sigma = to_batch(sigma, batch_size) window_size = to_batch(window_size, batch_size) baseline = [] for i in range(batch_size): sigma_arg = sigma[i] if sigma is not None else None window_arg = window_size[i] if window_size_dim is not None else None skip_axes = count_skip_axes(layout) baseline.append( gaussian_baseline(input[i], sigma_arg, window_arg, axes, skip_axes)) check_batch(result, baseline, batch_size, max_allowed_error=1)
def check_coin_flip(device='cpu', batch_size=32, max_shape=[1e5], p=None, use_shape_like_input=False): pipe = Pipeline(batch_size=batch_size, device_id=0, num_threads=3, seed=123456) with pipe: shape_gen_f = lambda: random_shape(max_shape) shape_arg = None inputs = [] shape_out = None if max_shape is not None: if use_shape_like_input: shape_like_in = dali.fn.external_source( lambda: np.zeros(shape_gen_f()), device=device, batch=False) inputs += [shape_like_in] shape_out = dali.fn.shapes(shape_like_in) else: shape_arg = dali.fn.external_source(shape_gen_f, batch=False) shape_out = shape_arg outputs = [ dali.fn.random.coin_flip(*inputs, device=device, probability=p, shape=shape_arg) ] if shape_out is not None: outputs += [shape_out] pipe.set_outputs(*outputs) pipe.build() outputs = pipe.run() data_out = outputs[0].as_cpu() \ if isinstance(outputs[0], TensorListGPU) else outputs[0] shapes_out = None if max_shape is not None: shapes_out = outputs[1].as_cpu() \ if isinstance(outputs[1], TensorListGPU) else outputs[1] p = p if p is not None else 0.5 for i in range(batch_size): data = np.array(data_out[i]) assert np.logical_or(data == 0, data == 1).all() if max_shape is not None: sample_shape = np.array(shapes_out[i]) assert (data.shape == sample_shape).all() total = len(data) positive = np.count_nonzero(data) np.testing.assert_allclose(p, positive / total, atol=0.005) # +/- -.5%
def test_changing_dtype(): batch_size = 2 src_data = [ [np.ones((120, 120, 3), dtype=np.float32)]*batch_size, [np.ones((120, 120, 3), dtype=np.uint8)]*batch_size ] src_pipe = Pipeline(batch_size, 1, 0) src_ext = fn.external_source(source=src_data, device='cpu') src_pipe.set_outputs(src_ext) src_pipe.build() src_pipe.run() src_pipe.run()
def get_pipeline(batch_size, tile, ratio, angle): pipe = Pipeline(batch_size, 4, None) with pipe: input, _ = fn.file_reader(file_root=img_dir) decoded = fn.image_decoder(input, device='cpu', output_type=types.RGB) grided = fn.grid_mask(decoded, device='cpu', tile=tile, ratio=ratio, angle=angle) pipe.set_outputs(grided, decoded) return pipe
def check_generic_gaussian_blur(batch_size, sigma, window_size, shape, layout, axes, op_type="cpu", in_dtype=np.uint8, out_dtype=types.NO_TYPE): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0) data = RandomlyShapedDataIterator(batch_size, max_shape=shape, dtype=in_dtype) # Extract the numpy type from DALI, we can have float32 or the same as input if out_dtype == types.NO_TYPE: result_type = in_dtype elif dali_type(in_dtype) == out_dtype: result_type = in_dtype else: result_type = np.float32 with pipe: input = fn.external_source(data, layout=layout) if op_type == "gpu": input = input.gpu() blurred = fn.gaussian_blur(input, device=op_type, sigma=sigma, window_size=window_size, dtype=out_dtype) pipe.set_outputs(blurred, input) pipe.build() for _ in range(test_iters): result, input = pipe.run() if op_type == "gpu": result = result.as_cpu() input = input.as_cpu() input = to_batch(input, batch_size) skip_axes = count_skip_axes(layout) baseline = [ gaussian_baseline(img, sigma, window_size, axes, skip_axes, dtype=result_type) for img in input ] max_error = 1 if result_type != np.float32 else 1e-04 check_batch(result, baseline, batch_size, max_allowed_error=max_error, expected_layout=layout)
def test_dtype_arg(): batch_size = 2 src_data = [[np.ones((120, 120, 3), dtype=np.uint8)] * batch_size] src_pipe = Pipeline(batch_size, 1, 0) src_ext = fn.external_source(source=src_data, dtype=DALIDataType.UINT8) src_pipe.set_outputs(src_ext) src_pipe.build() out, = src_pipe.run() for i in range(batch_size): t = out.at(i) assert t.dtype == np.uint8 np.array_equal(t, np.ones((120, 120, 3), dtype=np.uint8))
def test_ndim_data_mismatch(): batch_size = 2 src_data = [[[np.ones((120, 120, 3), dtype=np.uint8)] * batch_size, [np.ones((120, 120), dtype=np.uint8)] * batch_size]] src_pipe = Pipeline(batch_size, 1, 0) src_ext1, src_ext2 = fn.external_source(source=src_data, num_outputs=2, dtype=DALIDataType.UINT8, ndim=3) src_pipe.set_outputs(src_ext1, src_ext2) src_pipe.build() src_pipe.run()
def test_reduce(): reduce_fns = [fn.reductions.std_dev, fn.reductions.variance] def pipe(max_batch_size, input_data, device, /, reduce_fn): pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0) data = fn.external_source(source=input_data, cycle=False, device=device) mean = fn.reductions.mean(data) reduced = reduce_fn(data, mean) pipe.set_outputs(reduced) return pipe
def test_external_source_generator(): pipe = Pipeline(1, 3, 0) def gen(): for i in range(5): yield [make_array([i + 1.5], dtype=datapy.float32)] pipe.set_outputs(fn.external_source(gen())) pipe.build() for i in range(5): check_output(pipe.run(), [np.array([i + 1.5], dtype=np.float32)])
def run_reduce_with_layout( batch_size, get_batch, reduction, axes, axis_names, batch_fn): pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0) with pipe: input = fn.external_source(source=get_batch, layout="ABC") reduced = reduction(input, keep_dims=False, axes=axes) reduced_by_name = reduction(input, keep_dims=False, axis_names=axis_names) pipe.set_outputs(reduced, reduced_by_name) pipe.build() run_and_compare_with_layout(batch_fn, pipe)
def test_external_source_collection_cycling(): pipe = Pipeline(1, 3, 0) batches = [[np.array([1.5, 2.5], dtype=np.float32)], [np.array([-1, 3.5, 4.5], dtype=np.float32)]] pipe.set_outputs(fn.external_source(batches, cycle=True)) pipe.build() # epochs are cycles over the source iterable for epoch in range(3): for batch in batches: check_output(pipe.run(), batch)
def get_pipeline(device, batch_size, tile, ratio, angle): pipe = Pipeline(batch_size, 4, 0) with pipe: input, _ = fn.readers.file(file_root=img_dir) decoded = fn.decoders.image(input, device='cpu', output_type=types.RGB) decoded = decoded.gpu() if device == 'gpu' else decoded grided = fn.grid_mask(decoded, device=device, tile=tile, ratio=ratio, angle=angle) pipe.set_outputs(grided, decoded) return pipe
def check_corrupted_videos(): corrupted_videos = [ corrupted_video_data_root + '/' + f for f in os.listdir(corrupted_video_data_root) ] for corrupted in corrupted_videos: pipe = Pipeline(batch_size=BATCH_SIZE, num_threads=4, device_id=0) with pipe: vid = fn.video_reader(device="gpu", filenames=corrupted, sequence_length=1) pipe.set_outputs(vid) pipe.build()
def pipe(max_batch_size, input_data, device): pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0) with pipe: data = fn.external_source(source=input_data, cycle=False, device=device) spectrum = fn.spectrogram(data, nfft=60, window_length=50, window_step=25) processed = fn.mel_filter_bank(spectrum) pipe.set_outputs(processed) return pipe
def test_external_source_gen_function_cycle(): pipe = Pipeline(1, 3, 0) def gen(): for i in range(5): yield [make_array([i + 1.5], dtype=datapy.float32)] pipe.set_outputs(fn.external_source(gen, cycle=True)) pipe.build() for _ in range(3): for i in range(5): check_output(pipe.run(), [np.array([i + 1.5], dtype=np.float32)])
def test_fn_python_function(): pipe = Pipeline(1, 1, 0, exec_pipelined = False, exec_async = False) batch1 = [np.array([1,2,3])] batch2 = [np.array([2,3,4])] # we need a context, because we use an operator with potential side-effects (python_function) with pipe: src = fn.external_source([batch1, batch2]) out = fn.python_function(src, function = lambda x: x+1) pipe.set_outputs(out) pipe.build() assert(np.array_equal(pipe.run()[0].at(0), batch1[0] + 1)) assert(np.array_equal(pipe.run()[0].at(0), batch2[0] + 1))
def image_decoder_slice_pipe(max_batch_size, input_data, device): pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0) encoded = fn.external_source(source=input_data, cycle=False, device='cpu') anch = fn.constant(fdata=.1) sh = fn.constant(fdata=.4) decoded = fn.image_decoder_slice(encoded, anch, sh, axes=0, device=device) pipe.set_outputs(decoded) return pipe
def test_move_to_device_end(): test_data_shape = [1, 3, 0, 4] def get_data(): out = [ np.empty(test_data_shape, dtype=np.uint8) for _ in range(batch_size) ] return out pipe = Pipeline(batch_size=batch_size, num_threads=3, device_id=None) outs = fn.external_source(source=get_data) pipe.set_outputs(outs.gpu()) assert_raises(RuntimeError, pipe.build)
def pipe(max_batch_size, input_data, device): pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0) data = fn.external_source(source=input_data, cycle=False, device=device) paste_posx = fn.uniform(range=(0, 1)) paste_posy = fn.uniform(range=(0, 1)) paste_ratio = fn.uniform(range=(1, 2)) processed = fn.bbox_paste(data, paste_x=paste_posx, paste_y=paste_posy, ratio=paste_ratio) pipe.set_outputs(processed) return pipe
def pipe(max_batch_size, input_data, device): pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0) depthwise = fn.coin_flip() horizontal = fn.coin_flip() vertical = fn.coin_flip() data = fn.external_source(source=input_data, cycle=False, device=device) processed = fn.flip(data, depthwise=depthwise, horizontal=horizontal, vertical=vertical) pipe.set_outputs(processed) return pipe
def check_bad_device(device_id): test_data_shape = [1, 3, 0, 4] def get_data(): out = [ np.empty(test_data_shape, dtype=np.uint8) for _ in range(batch_size) ] return out pipe = Pipeline(batch_size=batch_size, num_threads=3, device_id=device_id) outs = fn.external_source(source=get_data, device="gpu") pipe.set_outputs(outs) assert_raises(RuntimeError, pipe.build)