def __init__(self, data_paths, num_shards, batch_size, num_threads, device_id, prefetch, fp16, random_shuffle, nhwc, dont_use_mmap, decoder_type, decoder_cache_params, reader_queue_depth, shard_id): super(CommonPipeline, self).__init__(batch_size, num_threads, device_id, random_shuffle, prefetch_queue_depth=prefetch) if decoder_type == 'roi': print('Using nvJPEG with ROI decoding') self.decode_gpu = ops.decoders.ImageRandomCrop(device = "mixed", output_type = types.RGB) self.res = ops.Resize(device="gpu", resize_x=224, resize_y=224) elif decoder_type == 'cached': assert decoder_cache_params['cache_enabled'] == True cache_size = decoder_cache_params['cache_size'] cache_threshold = decoder_cache_params['cache_threshold'] cache_type = decoder_cache_params['cache_type'] print('Using nvJPEG with cache (size : {} threshold: {}, type: {})'.format(cache_size, cache_threshold, cache_type)) self.decode_gpu = ops.decoders.Image(device = "mixed", output_type = types.RGB, cache_size=cache_size, cache_threshold=cache_threshold, cache_type=cache_type, cache_debug=False) self.res = ops.RandomResizedCrop(device="gpu", size =(224,224)) else: print('Using nvJPEG') self.decode_gpu = ops.decoders.Image(device = "mixed", output_type = types.RGB) self.res = ops.RandomResizedCrop(device="gpu", size =(224,224)) layout = types.NHWC if nhwc else types.NCHW out_type = types.FLOAT16 if fp16 else types.FLOAT self.cmnp = ops.CropMirrorNormalize(device="gpu", dtype=out_type, output_layout=layout, crop=(224, 224), mean=[0.485 * 255,0.456 * 255,0.406 * 255], std=[0.229 * 255,0.224 * 255,0.225 * 255]) self.coin = ops.random.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, dali_cpu=False, local_rank=0, world_size=1): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) dali_device = "gpu" self.input = ops.FileReader(file_root=data_dir, shard_id=local_rank, num_shards=world_size, random_shuffle=True) self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB) self.res = ops.RandomResizedCrop(device="gpu", size=crop, random_area=[0.08, 1.25]) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) print('DALI "{0}" variant'.format(dali_device))
def __init__(self, batch_size, num_threads, device_id, image_dir, file_list, seed=1, num_gpu=1, random_area=[0.08, 1.0]): super(TrainPipeline, self).__init__(batch_size, num_threads, device_id, seed=seed) self.input = ops.FileReader(file_root=image_dir, file_list=file_list, random_shuffle=True, num_shards=num_gpu, shard_id=device_id) self.decode = ops.nvJPEGDecoder(device="mixed", output_type=types.RGB) self.rrc = ops.RandomResizedCrop(device="gpu", size=(224, 224), random_area=random_area) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, crop=(224, 224), image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, file_list, file_root, crop_size, batch_size, num_threads, device_id, random_shuffle=True, seed=-1, mean=None, std=None, num_samples=None): super(DaliPipelineTrain, self).__init__(batch_size, num_threads, device_id, seed=seed) crop_size = _pair(crop_size) if mean is None: mean = [0.485 * 255, 0.456 * 255, 0.406 * 255] if std is None: std = [0.229 * 255, 0.224 * 255, 0.225 * 255] if num_samples is None: initial_fill = 4096 else: initial_fill = min(4096, num_samples) self.loader = ops.FileReader(file_root=file_root, file_list=file_list, random_shuffle=random_shuffle, initial_fill=initial_fill) self.decode = ops.HostDecoder() self.resize = ops.Resize(device="gpu", resize_x=256, resize_y=256) # self.hue = ops.Hue(device="gpu") # self.bright = ops.Brightness(device="gpu") # self.cntrst = ops.Contrast(device="gpu") # self.rotate = ops.Rotate(device="gpu") # self.jitter = ops.Jitter(device="gpu") random_area = (crop_size[0] / 256) * (crop_size[1] / 256) random_area = _pair(random_area) random_aspect_ratio = _pair(1.0) self.rrcrop = ops.RandomResizedCrop( device="gpu", size=crop_size, random_area=random_area, random_aspect_ratio=random_aspect_ratio) self.cmnorm = ops.CropMirrorNormalize( device="gpu", crop=list(crop_size), mean=mean, std=std) self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, data_dir, crop): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, data_dir, crop) self.pad = ops.Paste(device="gpu", fill_value=0, ratio=1.1, min_canvas_size=crop) self.res = ops.RandomResizedCrop(device="gpu", size=crop, random_area=[0.9, 1.1], random_aspect_ratio=1.33333) self.cutmix = ops.PythonFunction(function=cut_mixe_image, num_outputs=2, device='gpu') self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) self.rotated = ops.Rotate(device="gpu", keep_size=True) self.rotated_rng = ops.Uniform(range=(-5.0, 5.0)) self.brightness = ops.Brightness(device="gpu") self.brightness_rng = ops.Uniform(range=(0.8, 1.2)) self.reshape = ops.Reshape(device="gpu", layout="HWC") self.one_hot = ops.OneHot(num_classes=3, dtype=types.INT32, device="cpu") self.jitter_rng = ops.CoinFlip(probability=0.3) self.jittered = ops.Jitter(device="gpu")
def __init__(self, DATA_PATH, input_height, batch_size, num_threads, device_id): super(SimCLRTrainDataTransform, self).__init__(batch_size, num_threads, device_id, seed=12) self.COPIES = 3 self.input_height = input_height self.input = ops.FileReader(file_root=DATA_PATH, random_shuffle=True, seed=12) self.coin = ops.CoinFlip(probability=0.5) self.uniform = ops.Uniform(range=[0.7, 1.3]) #-1 to 1 #read image (I think that has to be cpu, do a mixed operation to decode into gpu) self.decode = ops.ImageDecoder(device='mixed', output_type=types.RGB) self.crop = ops.RandomResizedCrop(size=self.input_height, device="gpu") self.flip = ops.Flip(vertical=self.coin(), horizontal=self.coin(), device="gpu") self.colorjit_gray = ops.ColorTwist(brightness=self.uniform(), contrast=self.uniform(), hue=self.uniform(), saturation=self.uniform(), device="gpu") self.blur = ops.GaussianBlur(window_size=int(0.1 * self.input_height), device="gpu") self.swapaxes = ops.Transpose(perm=[2, 0, 1], device="gpu") self.to_int64 = ops.Cast(dtype=types.INT64, device="gpu")
def __init__(self, DATA_PATH, input_height, batch_size, copies, stage, num_threads, device_id, seed=1729): super(SimCLRTransform, self).__init__(batch_size, num_threads, device_id, seed=seed) #this lets our pytorch compat function find the length of our dataset self.num_samples = len(ImageFolder(DATA_PATH)) self.copies = copies self.input_height = input_height self.stage = stage self.input = ops.FileReader(file_root=DATA_PATH, random_shuffle=True, seed=seed) self.to_int64 = ops.Cast(dtype=types.INT64, device="gpu") self.to_int32_cpu = ops.Cast(dtype=types.INT32, device="cpu") self.coin = ops.random.CoinFlip(probability=0.5) self.uniform = ops.random.Uniform(range=[0.6, 0.9]) self.blur_amt = ops.random.Uniform(values=[ float(i) for i in range(1, int(0.1 * self.input_height), 2) ]) self.angles = ops.random.Uniform(range=[0, 360]) self.cast = ops.Cast(dtype=types.FLOAT, device='gpu') self.decode = ops.ImageDecoder(device='mixed', output_type=types.RGB) self.crop = ops.RandomResizedCrop(size=self.input_height, minibatch_size=batch_size, random_area=[0.75, 1.0], device="gpu") self.resize = ops.Resize(resize_x=self.input_height, resize_y=self.input_height, device="gpu") self.flip = ops.Flip(vertical=self.coin(), horizontal=self.coin(), device="gpu") self.colorjit_gray = ops.ColorTwist(brightness=self.uniform(), contrast=self.uniform(), hue=self.uniform(), saturation=self.uniform(), device="gpu") self.blur = ops.GaussianBlur(window_size=self.to_int32_cpu( self.blur_amt()), device="gpu") self.rotate = ops.Rotate( angle=self.angles(), keep_size=True, interp_type=types.DALIInterpType.INTERP_LINEAR, device="gpu") self.swapaxes = ops.Transpose(perm=[2, 0, 1], device="gpu")
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, dali_cpu=False): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) self.input = ops.TFRecordReader( path=data_dir + ".tfrecord", index_path=data_dir + ".index", features={ "image": tfrec.FixedLenFeature([], tfrec.string, ""), 'label': tfrec.FixedLenFeature([], tfrec.float32, 0.0), 'index': tfrec.FixedLenFeature([], tfrec.int64, 0), }) self.decode = ops.ImageDecoder( device="cpu", output_type=types.RGB, split_stages=True, ) self.res = ops.RandomResizedCrop(device="gpu", size=(224, 224)) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, crop=(crop, crop), image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) print('DALI "{0}" variant'.format('gpu'))
def __init__(self, data_root, data_list, sampler, crop, colorjitter=None): super(ImageNetTrainPipeV2, self).__init__() # print('data root: {}, data list: {}, len(sampler_index): {}'.format( # data_root, data_list, len(sampler))) self.mc_input = ops.McReader(file_root=data_root, file_list=data_list, sampler_index=list(sampler)) self.colorjitter = colorjitter dali_device = "gpu" # This padding sets the size of the internal nvJPEG buffers to be able to handle all # images from full-sized ImageNet without additional reallocations self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB, device_memory_padding=211025920, host_memory_padding=140544512) self.res = ops.RandomResizedCrop(device=dali_device, size=(crop, crop)) self.cmnp = ops.CropMirrorNormalize(device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, crop=(crop, crop), image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) if self.colorjitter is not None: self.colorjit = ops.ColorTwist(device="gpu") self.rng_brightness = ops.Uniform(range=(1.0 - self.colorjitter[0], 1.0 + self.colorjitter[0])) self.rng_contrast = ops.Uniform(range=(1.0 - self.colorjitter[1], 1.0 + self.colorjitter[1])) self.rng_saturation = ops.Uniform(range=(1.0 - self.colorjitter[2], 1.0 + self.colorjitter[2])) self.rng_hue = ops.Uniform(range=(-self.colorjitter[3], self.colorjitter[3]))
def __init__(self, batch_size, num_threads, device_id, rec_path, idx_path, shard_id, num_shards, crop_shape, min_random_area, max_random_area, min_random_aspect_ratio, max_random_aspect_ratio, nvjpeg_padding, prefetch_queue=3, seed=12, output_layout=types.NCHW, pad_output=True, dtype='float16', mlperf_print=True): super(HybridTrainPipe, self).__init__( batch_size, num_threads, device_id, seed = seed + device_id, prefetch_queue_depth = prefetch_queue) if mlperf_print: # Shuffiling is done inside ops.MXNetReader mx_resnet_print(key=mlperf_log.INPUT_ORDER) self.input = ops.MXNetReader(path = [rec_path], index_path=[idx_path], random_shuffle=True, shard_id=shard_id, num_shards=num_shards) self.decode = ops.nvJPEGDecoder(device = "mixed", output_type = types.RGB, device_memory_padding = nvjpeg_padding, host_memory_padding = nvjpeg_padding) self.rrc = ops.RandomResizedCrop(device = "gpu", random_area = [ min_random_area, max_random_area], random_aspect_ratio = [ min_random_aspect_ratio, max_random_aspect_ratio], size = crop_shape) self.cmnp = ops.CropMirrorNormalize(device = "gpu", output_dtype = types.FLOAT16 if dtype == 'float16' else types.FLOAT, output_layout = output_layout, crop = crop_shape, pad_output = pad_output, image_type = types.RGB, mean = _mean_pixel, std = _std_pixel) self.coin = ops.CoinFlip(probability = 0.5) if mlperf_print: mx_resnet_print( key=mlperf_log.INPUT_CROP_USES_BBOXES, val=False) mx_resnet_print( key=mlperf_log.INPUT_DISTORTED_CROP_RATIO_RANGE, val=(min_random_aspect_ratio, max_random_aspect_ratio)) mx_resnet_print( key=mlperf_log.INPUT_DISTORTED_CROP_AREA_RANGE, val=(min_random_area, max_random_area)) mx_resnet_print( key=mlperf_log.INPUT_MEAN_SUBTRACTION, val=_mean_pixel) mx_resnet_print( key=mlperf_log.INPUT_RANDOM_FLIP)
def __init__(self, path_imgrec, batch_size, num_threads, device_id, num_gpus, initial_fill): ''' initial_fill: 太大会占用内存,太小导致单个 batch id 重复率高而 loss 下降太慢,测试了下 batch_size*1000 基本不影响到训练 num_threads: 经测试,单核3.5GHz的U,hhd设置为3~4,ssd设置为5~6 ''' super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) logging.info('loading recordio %s...', path_imgrec) path_imgidx = path_imgrec[0:-4] + ".idx" self.input = ops.MXNetReader(path=[path_imgrec], index_path=[path_imgidx], random_shuffle=True, shard_id=device_id, num_shards=num_gpus, prefetch_queue_depth=5, initial_fill=initial_fill) self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB) self.res = ops.Resize(device="gpu", resize_x=112, resize_y=112) self.rrc = ops.RandomResizedCrop(device="gpu", size=(112, 112)) # self.cmnp = ops.CropMirrorNormalize(device = "gpu", # dtype = types.FLOAT, # output_layout = types.NCHW, # mean = [0.485 * 255,0.456 * 255,0.406 * 255], # std = [0.229 * 255,0.224 * 255,0.225 * 255]) self.cmnp = ops.CropMirrorNormalize(device="gpu", dtype=types.FLOAT, output_layout=types.NCHW) self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, seed=12, local_rank=0, world_size=1, spos_pre=False): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=seed + device_id) color_space_type = types.BGR if spos_pre else types.RGB self.input = ops.FileReader(file_root=data_dir, shard_id=local_rank, num_shards=world_size, random_shuffle=True) self.decode = ops.ImageDecoder(device="mixed", output_type=color_space_type) self.res = ops.RandomResizedCrop(device="gpu", size=crop, interp_type=types.INTERP_LINEAR if spos_pre else types.INTERP_TRIANGULAR) self.twist = ops.ColorTwist(device="gpu") self.jitter_rng = ops.Uniform(range=[0.6, 1.4]) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, image_type=color_space_type, mean=0. if spos_pre else [0.485 * 255, 0.456 * 255, 0.406 * 255], std=1. if spos_pre else [0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, data_dir, crop, shuffle, scale, ratio, world_size=1, seed=10, da=False): super(HybridTrainPipe, self).__init__(batch_size, num_threads, 0, seed=seed) # , RANDOM_SEED_TRAINING self.input = ops.FileReader(file_root=data_dir, num_shards=world_size, pad_last_batch=False, prefetch_queue_depth=2, random_shuffle=shuffle) self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB) self.res = ops.RandomResizedCrop(device="gpu", size=crop, random_area=scale, random_aspect_ratio=ratio) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, image_type=types.RGB, mean=[0.5 * 255, 0.5 * 255, 0.5 * 255], std=[0.5 * 255, 0.5 * 255, 0.5 * 255]) self.da = da if self.da: self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, half=False): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=0, exec_async=True) out_type = types.FLOAT if half: out_type = types.FLOAT16 print('Reading from {}'.format(data_dir)) file_name = make_dali_cached_file_list_which_is_also_a_file(data_dir) self.input = ops.FileReader( file_root=data_dir, file_list=file_name, shard_id=0, num_shards=1, random_shuffle=True) self.decode = ops.nvJPEGDecoder(device="mixed", output_type=types.RGB) self.rrc = ops.RandomResizedCrop(device="gpu", size =(crop, crop)) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=out_type, output_layout=types.NCHW, crop=(crop, crop), image_type=types.RGB, mean=[0.485 * 255,0.456 * 255,0.406 * 255], std=[0.229 * 255,0.224 * 255,0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) self.jpegs = None self.labels = None
def __init__(self, batch_size, num_threads, device_id, num_gpus, data_paths, prefetch, fp16, nhwc): super(RN50Pipeline, self).__init__(batch_size, num_threads, device_id, prefetch_queue_depth=prefetch) self.input = ops.FileReader(file_root=data_paths[0], shard_id=device_id, num_shards=num_gpus) self.decode_gpu = ops.nvJPEGDecoder(device="mixed", output_type=types.RGB) self.res = ops.RandomResizedCrop(device="gpu", size=(224, 224)) layout = types.args.nhwc if nhwc else types.NCHW out_type = types.FLOAT16 if fp16 else types.FLOAT self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=out_type, output_layout=layout, crop=(224, 224), image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, img_dir, img_size, batch_size, num_threads, device_id, num_gpus=1, seed=1): super(DataPipeline, self).__init__(batch_size, num_threads, device_id, seed=seed) self.input = ops.FileReader(file_root=img_dir, random_shuffle=True, num_shards=num_gpus, shard_id=device_id) self.decode = ops.ImageDecoder(device='mixed', output_type=types.RGB) self.res = ops.Resize(device="gpu", resize_x=img_size) self.cmn = ops.CropMirrorNormalize( device='gpu', image_type=types.RGB, mean=[0.5 * 255, 0.5 * 255, 0.5 * 255], std=[0.5 * 255, 0.5 * 255, 0.5 * 255], ) self.rrc = ops.RandomResizedCrop(device='gpu', size=(img_size, img_size))
def __init__(self, root_dir, batch_size, num_threads, device_id, use_shift_scale=False, num_shards=None, shard_id=None): super().__init__(batch_size, num_threads, device_id, seed=12) self.random_angle = ops.Uniform(range=(0, 360.0)) self.random = ops.Uniform(range=(0.5, 1.5)) self.random_coin = ops.CoinFlip() self.input = ops.FileReader( file_root=root_dir, random_shuffle=True, num_shards=num_shards, shard_id=shard_id, ) self.decode = ops.ImageDecoder(device='mixed') self.rotate = ops.Rotate(device='gpu', interp_type=types.INTERP_LINEAR) self.crop = ops.Crop(device='gpu', crop=(224, 224)) self.use_shift_scale = use_shift_scale if self.use_shift_scale: self.shift_scale = ops.RandomResizedCrop( device='gpu', size=(224, 224), interp_type=types.INTERP_LINEAR, random_area=(0.3, 1.0), ) self.flip = ops.Flip(device='gpu') self.color_twist = ops.ColorTwist(device='gpu')
def __init__(self, batch_size, num_threads, device_id, data_dir, crop_size, local_rank=0, world_size=1): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) dali_device = "gpu" self.input = ops.FileReader(file_root=data_dir, shard_id=local_rank, num_shards=world_size, random_shuffle=True) self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB) self.rand_resize_crop = ops.RandomResizedCrop(device=dali_device, size=crop_size, random_area=[0.08, 1.25]) self.crop_mirror_norm = ops.CropMirrorNormalize( device=dali_device, output_dtype=types.FLOAT, output_layout=types.NCHW, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) self.coin_flip = ops.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, prefetch, fp16, nhwc): super(CommonPipeline, self).__init__(batch_size, num_threads, device_id, prefetch_queue_depth=prefetch) self.decode_gpu = ops.nvJPEGDecoder(device="mixed", output_type=types.RGB) self.res = ops.RandomResizedCrop(device="gpu", size=(224, 224)) if nhwc: layout = types.NHWC else: layout = types.NCHW if fp16: out_type = types.FLOAT16 else: out_type = types.FLOAT self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=out_type, output_layout=layout, crop=(224, 224), image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5)
def build_train_and_test_transforms(): """Returns torchvision OR nvidia-dali transforms. :returns: train_transforms, test_transforms :rtype: list, list """ resize_shape = (args.image_size_override, args.image_size_override) if 'dali' in args.task: # Lazy import DALI dependencies because debug cpu nodes might not have DALI. import nvidia.dali.ops as ops import nvidia.dali.types as types from datasets.dali_imagefolder import ColorJitter, RandomHorizontalFlip, RandomGrayScale train_transform = [ ops.RandomResizedCrop(device="gpu" if args.cuda else "cpu", size=resize_shape, random_area=(0.08, 1.0), random_aspect_ratio=(3. / 4, 4. / 3)), RandomHorizontalFlip(prob=0.2, cuda=args.cuda), ColorJitter(brightness=0.8 * args.color_jitter_strength, contrast=0.8 * args.color_jitter_strength, saturation=0.2 * args.color_jitter_strength, hue=0.2 * args.color_jitter_strength, prob=0.8, cuda=args.cuda), RandomGrayScale(prob=0.2, cuda=args.cuda) # TODO: Gaussian-blur ] test_transform = [ ops.Resize(resize_x=resize_shape[0], resize_y=resize_shape[1], device="gpu" if args.cuda else "cpu", image_type=types.RGB, interp_type=types.INTERP_LINEAR) ] else: from datasets.utils import GaussianBlur train_transform = [ # transforms.CenterCrop(first_center_crop_size), transforms.RandomResizedCrop( (args.image_size_override, args.image_size_override)), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomApply([ transforms.ColorJitter( brightness=0.8 * args.color_jitter_strength, contrast=0.8 * args.color_jitter_strength, saturation=0.8 * args.color_jitter_strength, hue=0.2 * args.color_jitter_strength) ], p=0.8), transforms.RandomGrayscale(p=0.2), GaussianBlur(kernel_size=int(0.1 * args.image_size_override), p=0.5) ] test_transform = [transforms.Resize(resize_shape)] return train_transform, test_transform
def __init__(self, batch_size, num_threads, device_id, data_dir="./", dali_cpu=False): super(DecodeAndCropPipeline, self).__init__(batch_size, num_threads, device_id, seed=12) self.input = ops.FileReader(file_root=data_dir, random_shuffle=True) if dali_cpu: decoder_device = "cpu" else: decoder_device = "mixed" device_memory_padding = 211025920 if decoder_device == 'mixed' else 0 host_memory_padding = 140544512 if decoder_device == 'mixed' else 0 self.decode = ops.ImageDecoder( device=decoder_device, output_type=types.RGB, device_memory_padding=device_memory_padding, host_memory_padding=host_memory_padding) dali_device = "cpu" if dali_cpu else "gpu" self.res = ops.RandomResizedCrop(device=dali_device, size=224, interp_type=types.INTERP_TRIANGULAR, random_aspect_ratio=[0.8, 1.25], random_area=[0.1, 1.0], num_attempts=100)
def __init__(self, args, batch_size, num_threads, device_id, rec_path, idx_path, shard_id, num_shards, crop_shape, nvjpeg_padding, prefetch_queue=3, output_layout=types.NCHW, pad_output=True, dtype='float16', dali_cpu=False): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id, prefetch_queue_depth=prefetch_queue) self.input = ops.MXNetReader(path=[rec_path], index_path=[idx_path], random_shuffle=True, shard_id=shard_id, num_shards=num_shards) dali_device, decoder_device = get_device_names(dali_cpu) if args.dali_fuse_decoder: self.decode = ops.ImageDecoderRandomCrop( device=decoder_device, output_type=types.RGB, device_memory_padding=nvjpeg_padding, host_memory_padding=nvjpeg_padding) else: self.decode = ops.ImageDecoder( device=decoder_device, output_type=types.RGB, device_memory_padding=nvjpeg_padding, host_memory_padding=nvjpeg_padding) if args.dali_fuse_decoder: self.resize = ops.Resize(device=dali_device, resize_x=crop_shape[1], resize_y=crop_shape[0]) else: self.resize = ops.RandomResizedCrop(device=dali_device, size=crop_shape) self.cmnp = ops.CropMirrorNormalize( device="gpu", dtype=types.FLOAT16 if dtype == 'float16' else types.FLOAT, output_layout=output_layout, crop=crop_shape, pad_output=pad_output, mean=args.rgb_mean, std=args.rgb_std) self.coin = ops.random.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, size=256, dali_cpu=False, local_rank=0, world_size=1): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) dali_device = "gpu" self.input = ops.FileReader(file_root=data_dir, shard_id=local_rank, num_shards=world_size, random_shuffle=True) self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB) # self.res = ops.Resize(device="gpu", resize_x=size, resize_y=size, interp_type=types.INTERP_LINEAR) self.res = ops.Resize(device="gpu", resize_shorter=size, interp_type=types.INTERP_LINEAR) self.rescrop = ops.RandomResizedCrop(device="gpu", size=crop, random_area=[0.08, 1.25]) self.bc = ops.BrightnessContrast(device="gpu", brightness=0.5, contrast=0.6) # Will flip vertically with prob of 0.1 self.vert_flip = ops.Flip(device='gpu', horizontal=0) self.vert_coin = ops.CoinFlip(probability=0.4) self.transform_source = ops.ExternalSource() self.warp_keep_size = ops.WarpAffine( device="gpu", # size # keep original canvas size interp_type=types.INTERP_LINEAR # use linear interpolation ) # My workaround for Dali not supporting random affine transforms: # a "synthetic random" warp affine transform. # Rotate within a narrow range with probability of 0.075 self.rotate = ops.Rotate(device='gpu') self.rotate_range = ops.Uniform(range=(-20.0, 20.0)) self.rotate_coin = ops.CoinFlip(probability=0.075) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) print('DALI "{0}" variant'.format(dali_device))
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, dali_cpu=False): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) if torch.distributed.is_initialized(): rank = torch.distributed.get_rank() world_size = torch.distributed.get_world_size() else: rank = 0 world_size = 1 self.input = ops.FileReader( file_root=data_dir, shard_id=rank, num_shards=world_size, random_shuffle=True, pad_last_batch=True, ) if dali_cpu: dali_device = "cpu" self.decode = ops.ImageDecoder(device=dali_device, output_type=types.RGB) else: dali_device = "gpu" # This padding sets the size of the internal nvJPEG buffers to be able to handle all images from full-sized ImageNet # without additional reallocations self.decode = ops.ImageDecoder( device="mixed", output_type=types.RGB, device_memory_padding=211025920, host_memory_padding=140544512, ) self.res = ops.RandomResizedCrop( device=dali_device, size=[crop, crop], interp_type=types.INTERP_LINEAR, random_aspect_ratio=[0.75, 4.0 / 3.0], random_area=[0.08, 1.0], num_attempts=100, ) self.cmnp = ops.CropMirrorNormalize( device="gpu", dtype=types.FLOAT, output_layout=types.NCHW, crop=(crop, crop), mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255], ) self.coin = ops.CoinFlip(probability=0.5)
def __init__(self, batch_size, num_threads, device_id, crop, colorjitter=None, dali_cpu=False): super(ImageNetTrainPipe, self).__init__(batch_size, num_threads, device_id, seed=12 + device_id) self.data_input = ops.ExternalSource() self.label_input = ops.ExternalSource() self.colorjitter = colorjitter # let user decide which pipeline works him bets for RN version he runs if dali_cpu: dali_device = "cpu" self.decode = ops.HostDecoderRandomCrop(device=dali_device, output_type=types.RGB) self.res = ops.Resize(resize_x=crop, resize_y=crop) else: dali_device = "gpu" # This padding sets the size of the internal nvJPEG buffers to be able to # handle all images from full-sized ImageNet without additional reallocations self.decode = ops.nvJPEGDecoder(device="mixed", output_type=types.RGB, device_memory_padding=211025920, host_memory_padding=140544512) self.res = ops.RandomResizedCrop(device=dali_device, size=(crop, crop)) self.cmnp = ops.CropMirrorNormalize( device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, crop=(crop, crop), image_type=types.RGB, mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]) self.coin = ops.CoinFlip(probability=0.5) if self.colorjitter is not None: self.colorjit = ops.ColorTwist(device="gpu") self.rng_brightness = ops.Uniform(range=(1.0 - self.colorjitter[0], 1.0 + self.colorjitter[0])) self.rng_contrast = ops.Uniform(range=(1.0 - self.colorjitter[1], 1.0 + self.colorjitter[1])) self.rng_saturation = ops.Uniform(range=(1.0 - self.colorjitter[2], 1.0 + self.colorjitter[2])) self.rng_hue = ops.Uniform(range=(-self.colorjitter[3], self.colorjitter[3]))
def __new__( cls, size, interp_type=None, mag_filter=None, min_filter=None, random_area=(0.08, 1.), random_aspect_ratio=(0.75, 1.33), num_attempts=10, **kwargs ): """Create a ``ImageDecoderRandomCrop`` operator. Parameters ---------- size : Union[int, Sequence[int]] The output image size. interp_type : str, optional The interpolation for both up and down sampling. mag_filter : str, optional, default='LINEAR' The interpolation for up sampling. min_filter : str, optional, default='TRIANGULAR' The interpolation for down sampling. random_area : Sequence[float], optional, default=(0.08, 1.) The range of scale for sampling. random_aspect_ratio : Sequence[float], optional, default=(0.75, 1.33) The range of aspect ratio for sampling. num_attempts : int, optional, default=10 The max number of sampling trails. Returns ------- nvidia.dali.ops.ImageDecoderRandomCrop The operator. """ if isinstance(interp_type, six.string_types): interp_type = getattr(types, 'INTERP_' + interp_type.upper()) if isinstance(mag_filter, six.string_types): mag_filter = getattr(types, 'INTERP_' + mag_filter.upper()) if isinstance(min_filter, six.string_types): min_filter = getattr(types, 'INTERP_' + min_filter.upper()) return ops.RandomResizedCrop( size=size, interp_type=interp_type, mag_filter=mag_filter, min_filter=min_filter, random_area=random_area, random_aspect_ratio=random_aspect_ratio, num_attempts=num_attempts, device=context.get_device_type(), **kwargs )
def __init__(self, batch_size, num_threads, device_id, num_gpus, data_paths): super(CustomPipe, self).__init__(batch_size, num_threads, device_id,) self.input = ops.FileReader(file_root = data_paths, shard_id = device_id, num_shards = num_gpus) self.decode_gpu = ops.ImageDecoder(device = "mixed", output_type = types.RGB) self.res = ops.RandomResizedCrop(device="gpu", size =(224, 224)) self.cmnp = ops.CropMirrorNormalize(device="gpu", output_dtype=types.FLOAT, output_layout=types.NCHW, crop=(224, 224), image_type=types.RGB, mean=[0.485 * 255,0.456 * 255,0.406 * 255], std=[0.229 * 255,0.224 * 255,0.225 * 255])
def __init__(self, batch_size, num_threads, device_id, data_dir, crop): super(HybridTrainPipe, self).__init__(batch_size, num_threads, device_id, seed = 12 + device_id) self.input = ops.FileReader(file_root = data_dir, shard_id = args.local_rank, num_shards = args.world_size, random_shuffle = True) self.decode = ops.nvJPEGDecoder(device = "mixed", output_type = types.RGB) self.rrc = ops.RandomResizedCrop(device = "gpu", size = (crop, crop)) self.cmnp = ops.CropMirrorNormalize(device = "gpu", output_dtype = types.FLOAT, output_layout = types.NCHW, crop = (crop, crop), image_type = types.RGB, mean = [0.485 * 255,0.456 * 255,0.406 * 255], std = [0.229 * 255,0.224 * 255,0.225 * 255]) self.coin = ops.CoinFlip(probability = 0.5)
def __init__(self, data_iter, batch_size, x_dim, num_threads, device_id): super(MiniPipeline, self).__init__(batch_size, num_threads, device_id, seed=12) data_iter = iter(data_iter) self.source = ops.ExternalSource(source=data_iter, num_outputs=2) self.decode = ops.ImageDecoder(device='mixed', output_type=types.RGB) self.rrc = ops.RandomResizedCrop(device='gpu', size=x_dim[:2], random_area=[0.95, 1.0]) self.flip = ops.Flip(device='gpu') # self.colortwist = ops.ColorTwist(device='gpu', brightness=.1, contrast=.1, saturation=.1, hue=.1) self.norm = ops.CropMirrorNormalize(device='gpu', output_layout=types.NCHW, # mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], # std=[0.229 * 255, 0.224 * 255, 0.225 * 255] )
def __init__(self, image_dir, batch_size, num_threads, device_id, num_gpus=1, seed=1, train=True): super(DataPipeline, self).__init__( batch_size, num_threads, device_id, seed=seed) self.input = ops.FileReader(file_root=image_dir, random_shuffle=True, num_shards=num_gpus, shard_id=device_id) self.decode = ops.ImageDecoder(device='mixed', output_type=types.RGB) self.rrc = ops.RandomResizedCrop(device='gpu', size=(128, 128)) self.cmn = ops.CropMirrorNormalize(device='gpu', crop=(128, 128), image_type=types.RGB, mean=[0.5*256, 0.5*256, 0.5*256], std=[0.5*256, 0.5*256, 0.5*256], ) self.coin = ops.CoinFlip(probability=0.5) self.res = ops.Resize(device="gpu", resize_shorter=256) self.train = train