def test_fn_multiple_input_sets(): pipe = Pipeline(batch_size = 1, num_threads = 1, device_id = 0) image1 = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uint8)[:,:,np.newaxis] image2 = np.array([ [10, 20], [30, 40], [50, 60]], dtype=np.uint8)[:,:,np.newaxis] batches = [[image1], [image2]] inputs = fn.external_source(lambda: batches, 2, layout = "HWC") rotated = fn.rotate(inputs, angle = 90) pipe.set_outputs(*rotated) pipe.build() outs = pipe.run() arr1 = outs[0].at(0) arr2 = outs[1].at(0) ref1 = np.array([ [4, 8, 12], [3, 7, 11], [2, 6, 10], [1, 5, 9]])[:,:,np.newaxis] ref2 = np.array([ [20, 40, 60], [10, 30, 50]], dtype=np.uint8)[:,:,np.newaxis] assert(np.array_equal(arr1, ref1)) assert(np.array_equal(arr2, ref2))
def get_pipeline(folder="train", custom_reader=None): pipe = Pipeline(batch_size=64, num_threads=1, device_id=1) if custom_reader: raw_files, labels = custom_reader else: raw_files, labels = fn.file_reader(file_root="%s" % folder, random_shuffle=True) decode = fn.image_decoder(raw_files, device="mixed", output_type=types.GRAY) resize = fn.resize(decode, device="gpu", image_type=types.RGB, interp_type=types.INTERP_LINEAR, resize_x=WIDTH, resize_y=HEIGHT) hsv = fn.hsv(resize, hue=fn.uniform(range=(-10, 10)), saturation=fn.uniform(range=(-.5, .5)), value=fn.uniform(range=(0.9, 1.2)), device="gpu", dtype=types.UINT8) bc = fn.brightness_contrast(hsv, device="gpu", brightness=fn.uniform(range=(.9, 1.1))) cmn = fn.crop_mirror_normalize(bc, device="gpu", output_dtype=types.FLOAT, output_layout=types.NHWC, image_type=types.GRAY, mean=[255 // 2], std=[255 // 2]) rot = fn.rotate(cmn, angle=fn.uniform(range=(-40, 40)), device="gpu", keep_size=True) tpose = fn.transpose(rot, perm=(2, 0, 1), device="gpu") # Reshaping to a format PyTorch likes pipe.set_outputs(tpose, labels) pipe.build() dali_iter = DALIClassificationIterator([pipe], -1) return dali_iter
def test_move_to_device_middle(): 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) data = fn.external_source(source=get_data) outs = fn.rotate(data.gpu(), angle=25) pipe.set_outputs(outs) assert_raises(RuntimeError, pipe.build)
def _test_fn_rotate(device): pipe = Pipeline(batch_size=1, num_threads=1, device_id=0) image = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uint8)[:, :, np.newaxis] batch = [image] input = fn.external_source([batch], layout="HWC") rotated = fn.rotate(input.gpu() if device == "gpu" else input, angle=90) pipe.set_outputs(rotated) pipe.build() outs = pipe.run() out = outs[0] if device == "cpu" else outs[0].as_cpu() arr = out.at(0) ref = np.array([[4, 8, 12], [3, 7, 11], [2, 6, 10], [1, 5, 9]])[:, :, np.newaxis] assert (np.array_equal(arr, ref))
def test_move_to_device_middle(): 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) data = fn.external_source(source=get_data) outs = fn.rotate(data.gpu(), angle=25) pipe.set_outputs(outs) assert_raises( RuntimeError, pipe.build, glob= "Cannot add a GPU operator Rotate, device_id should not be equal CPU_ONLY_DEVICE_ID." )