def dali_pipeline(source, image_size, training, cpu=False): images, labels = fn.external_source(source=source, num_outputs=2) if cpu: device = "cpu" images = fn.decoders.image(images, device=device) else: device = "gpu" images = fn.decoders.image( images, device="mixed", device_memory_padding=211025920, host_memory_padding=140544512, ) if training: images = fn.random_resized_crop( images, device=device, size=image_size, interp_type=types.DALIInterpType.INTERP_CUBIC, ) coin = fn.random.coin_flip(0.5) images = fn.flip(images, horizontal=coin) else: pass return images, labels
def create_decoder_random_crop_pipeline(data_path, device): seed = 1234 jpegs, _ = fn.readers.file(file_root=data_path, shard_id=0, num_shards=1, name="Reader") w = 242 h = 230 images_random_crop_1 = fn.decoders.image_random_crop(jpegs, device=device, output_type=types.RGB, hw_decoder_load=0.7, seed=seed) images_random_crop_1 = fn.resize(images_random_crop_1, size=(w, h)) images = fn.decoders.image(jpegs, device=device, hw_decoder_load=0.7, output_type=types.RGB) images_random_crop_2 = fn.random_resized_crop(images, size=(w, h), seed=seed) return images_random_crop_1, images_random_crop_2
def _test_rrc(device, max_frames, layout, aspect_ratio_range, area_range, output_size, input_type, output_type): batch_size = 4 pipe = dali.pipeline.Pipeline(batch_size, 4, 0) channel_dim = layout.find('C') value_range = type_range(test_utils.dali_type_to_np(input_type)) if channel_dim == len(layout) - 1: channel_dim = -1 input = fn.external_source(source=generator(batch_size, max_frames, channel_dim, input_type), layout=layout) shape = fn.shapes(input) if device == "gpu": input = input.gpu() out = fn.random_resized_crop(input, random_aspect_ratio=aspect_ratio_range, random_area=area_range, size=output_size, interp_type=dali.types.INTERP_LINEAR, seed=12321, dtype=output_type) pipe.set_outputs(out, shape) pipe.build() for iter in range(3): outputs, input_shapes = pipe.run() if device == "gpu": outputs = outputs.as_cpu() assert outputs.layout() == layout for i in range(batch_size): out = outputs.at(i) input_shape = input_shapes.at(i).tolist() check_output(out, channel_dim, input_shape, aspect_ratio_range, area_range, value_range)
def order_change_pipeline(): if order_change_pipeline.change: rng = 0 else: order_change_pipeline.change = True rng = fn.random.coin_flip(probability=0.5, seed=47) jpegs, labels = fn.readers.file(file_root=file_root, shard_id=0, num_shards=2) images = fn.decoders.image(jpegs, device='mixed', output_type=types.RGB) resized_images = fn.random_resized_crop(images, device="gpu", size=(224, 224), seed=27) out_type = types.FLOAT16 output = fn.crop_mirror_normalize( resized_images.gpu(), mirror=rng, device="gpu", dtype=out_type, crop=(224, 224), mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) return rng, jpegs, labels, images, resized_images, output
def cpu_after_gpu_pipeline(): jpegs, labels = fn.readers.file(file_root=file_root, shard_id=0, num_shards=2, random_shuffle=True) images = fn.decoders.image(jpegs, output_type=types.RGB, device='mixed') output = fn.random_resized_crop(images, size=(224, 224), device='cpu') return labels, output
def multiple_input_sets_pipeline(): jpegs = [ fn.readers.file(file_root=file_root, seed=42, random_shuffle=True)[0] for _ in range(6) ] images = fn.decoders.image(jpegs, seed=42) cropped_images = fn.random_resized_crop(images, size=(224, 224), seed=42) output = fn.cat(cropped_images[:3], cropped_images[3:]) return tuple(output)
def input_sets_stateful_op_pipeline(): set_size = 5 jpegs = [ fn.readers.file(file_root=file_root, seed=42, random_shuffle=True)[0] for _ in range(set_size) ] images = fn.decoders.image(jpegs, seed=42) output = fn.random_resized_crop(images, size=(224, 224), seed=42) assert len(output) == set_size return tuple(output)
def device_change_rn50_pipeline_base(): jpegs, labels = fn.readers.file(file_root=file_root, shard_id=0, num_shards=2, random_shuffle=True) images = fn.decoders.image(jpegs, output_type=types.RGB) if device_change_rn50_pipeline_base.change: images = images.gpu() output = fn.random_resized_crop(images, size=(224, 224)) return labels, output
def es_pipeline_debug(): images = fn.external_source(name='input') labels = fn.external_source(name='labels') rng = fn.random.coin_flip(probability=0.5, seed=47) images = fn.random_resized_crop(images, size=(224, 224), seed=27) out_type = types.FLOAT16 output = fn.crop_mirror_normalize( images.gpu(), mirror=rng, device="gpu", dtype=out_type, crop=(224, 224), mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) return rng, images, output, labels
def injection_pipeline(callback, device='cpu'): rng = fn.random.coin_flip(probability=0.5, seed=47) images = fn.random_resized_crop(callback(), device=device, size=(224, 224), seed=27) out_type = types.FLOAT16 output = fn.crop_mirror_normalize( images.gpu(), mirror=rng, device="gpu", dtype=out_type, crop=(224, 224), mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) return rng, images, output
def es_pipeline_standard(): jpegs, labels = fn.readers.file(file_root=file_root, shard_id=0, num_shards=2) images = fn.decoders.image(jpegs, output_type=types.RGB) rng = fn.random.coin_flip(probability=0.5, seed=47) images = fn.random_resized_crop(images, size=(224, 224), seed=27) out_type = types.FLOAT16 output = fn.crop_mirror_normalize( images.gpu(), mirror=rng, device="gpu", dtype=out_type, crop=(224, 224), mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) return rng, images, output, labels
def rn50_pipeline(): rng = fn.random.coin_flip(probability=0.5, seed=47) print(f'rng: {rng.get().as_array()}') tmp = rng ^ 1 print(f'rng xor: {tmp.get().as_array()}') jpegs, labels = fn.readers.file(file_root=file_root, shard_id=0, num_shards=2) if jpegs.get().is_dense_tensor(): print(f'jpegs: {jpegs.get().as_array()}') else: print('jpegs shapes:') for j in jpegs.get(): print(j.shape()) print(f'labels: {labels.get().as_array()}') images = fn.decoders.image(jpegs, device='mixed', output_type=types.RGB) for i in images.get().as_cpu(): print(i) for i in images.get(): print(i.shape()) images = fn.random_resized_crop(images, device="gpu", size=(224, 224), seed=27) for i in images.get(): print(i.shape()) print(np.array(images.get().as_cpu()[0])) images += 1 print(np.array(images.get().as_cpu()[0])) out_type = types.FLOAT16 output = fn.crop_mirror_normalize( images.gpu(), mirror=rng, device="gpu", dtype=out_type, crop=(224, 224), mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) return (output, labels.gpu())
def create_image_pipeline( batch_size, num_threads, device_id, image0_list, image1_list, flow_list, valBool, ): pipeline = Pipeline(batch_size, num_threads, device_id, seed=2) with pipeline: if valBool: shuffleBool = False else: shuffleBool = True """ READ FILES """ image0, _ = fn.readers.file( file_root=args.data, files=image0_list, random_shuffle=shuffleBool, name="Reader", seed=1, ) image1, _ = fn.readers.file( file_root=args.data, files=image1_list, random_shuffle=shuffleBool, seed=1, ) flo = fn.readers.numpy( file_root=args.data, files=flow_list, random_shuffle=shuffleBool, seed=1, ) """ DECODE AND RESHAPE """ image0 = fn.decoders.image(image0, device="cpu") image0 = fn.reshape(image0, layout="HWC") image1 = fn.decoders.image(image1, device="cpu") image1 = fn.reshape(image1, layout="HWC") images = fn.cat(image0, image1, axis=2) flo = fn.reshape(flo, layout="HWC") if valBool: images = fn.resize(images, resize_x=162, resize_y=122) else: """ CO-TRANSFORM """ # random translate # angle_rng = fn.random.uniform(range=(-90, 90)) # images = fn.rotate(images, angle=angle_rng, fill_value=0) # flo = fn.rotate(flo, angle=angle_rng, fill_value=0) images = fn.random_resized_crop( images, size=[122, 162], # 122, 162 random_aspect_ratio=[1.3, 1.4], random_area=[0.8, 0.9], seed=1, ) flo = fn.random_resized_crop( flo, size=[122, 162], random_aspect_ratio=[1.3, 1.4], random_area=[0.8, 0.9], seed=1, ) # coin1 = fn.random.coin_flip(dtype=types.DALIDataType.BOOL, seed=10) # coin1_n = coin1 ^ True # coin2 = fn.random.coin_flip(dtype=types.DALIDataType.BOOL, seed=20) # coin2_n = coin2 ^ True # images = ( # fn.flip(images, horizontal=1, vertical=1) * coin1 * coin2 # + fn.flip(images, horizontal=1) * coin1 * coin2_n # + fn.flip(images, vertical=1) * coin1_n * coin2 # + images * coin1_n * coin2_n # ) # flo = ( # fn.flip(flo, horizontal=1, vertical=1) * coin1 * coin2 # + fn.flip(flo, horizontal=1) * coin1 * coin2_n # + fn.flip(flo, vertical=1) * coin1_n * coin2 # + flo * coin1_n * coin2_n # ) # _flo = flo # flo_0 = fn.slice(_flo, axis_names="C", start=0, shape=1) # flo_1 = fn.slice(_flo, axis_names="C", start=1, shape=1) # flo_0 = flo_0 * coin1 * -1 + flo_0 * coin1_n # flo_1 = flo_1 * coin2 * -1 + flo_1 * coin2_n # # flo = noflip + vertical flip + horizontal flip + both_flip # # A horizontal flip is around the vertical axis (switch left and right) # # So for a vertical flip coin1 is activated and needs to give +1, coin2 is activated needs to give -1 # # for a horizontal flip coin1 is activated and needs to be -1, coin2_n needs +1 # # no flip coin coin1_n +1, coin2_n +1 # flo = fn.cat(flo_0, flo_1, axis_name="C") """ NORMALIZE """ images = fn.crop_mirror_normalize( images, mean=[0, 0, 0, 0, 0, 0], std=[255, 255, 255, 255, 255, 255]) images = fn.crop_mirror_normalize( images, mean=[0.45, 0.432, 0.411, 0.45, 0.432, 0.411], std=[1, 1, 1, 1, 1, 1], ) flo = fn.crop_mirror_normalize( flo, mean=[0, 0], std=[args.div_flow, args.div_flow]) pipeline.set_outputs(images, flo) return pipeline
def incorrect_variable_batch_size_pipeline(): jpegs, labels = fn.readers.file(file_root=file_root) images = fn.decoders.image(jpegs) images = [images.get()[i] for i in range(6)] output = fn.random_resized_crop(images, size=(224, 224)) return labels, output
def variable_batch_size_from_external_source_pipeline(src_data): images = fn.external_source(src_data) output = fn.random_resized_crop(images, size=(32, 32)) return output,