def create_dataset(dataset_path, config, do_train, repeat_num=1): """ create a train or eval dataset Args: dataset_path(string): the path of dataset. config(dict): config of dataset. do_train(bool): whether dataset is used for train or eval. repeat_num(int): the repeat times of dataset. Default: 1. Returns: dataset """ rank = config.rank group_size = config.group_size if group_size == 1: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=config.work_nums, shuffle=True) else: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=config.work_nums, shuffle=True, num_shards=group_size, shard_id=rank) # define map operations if do_train: trans = [ C.RandomCropDecodeResize(config.image_size), C.RandomHorizontalFlip(prob=0.5), C.RandomColorAdjust(brightness=0.4, saturation=0.5) # fast mode ] else: trans = [ C.Decode(), C.Resize(int(config.image_size / 0.875)), C.CenterCrop(config.image_size) ] trans += [ C.Rescale(1.0 / 255.0, 0.0), C.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), C.HWC2CHW() ] type_cast_op = C2.TypeCast(mstype.int32) ds = ds.map(operations=trans, input_columns="image", num_parallel_workers=config.work_nums) ds = ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=config.work_nums) # apply batch operations ds = ds.batch(config.batch_size, drop_remainder=True) # apply dataset repeat operation ds = ds.repeat(repeat_num) return ds
def create_dataset(data_path, batch_size=32, repeat_size=1, num_parallel_workers=1): """ create dataset for train or test Args: data_path (str): Data path batch_size (int): The number of data records in each group repeat_size (int): The number of replicated data records num_parallel_workers (int): The number of parallel workers """ # define dataset mnist_ds = ds.MnistDataset(data_path) # define some parameters needed for data enhancement and rough justification resize_height, resize_width = 32, 32 rescale = 1.0 / 255.0 shift = 0.0 rescale_nml = 1 / 0.3081 shift_nml = -1 * 0.1307 / 0.3081 # according to the parameters, generate the corresponding data enhancement method resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) rescale_nml_op = CV.Rescale(rescale_nml, shift_nml) rescale_op = CV.Rescale(rescale, shift) hwc2chw_op = CV.HWC2CHW() type_cast_op = C.TypeCast(mstype.int32) # using map to apply operations to a dataset mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=resize_op, input_columns="image", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=rescale_op, input_columns="image", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=rescale_nml_op, input_columns="image", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=hwc2chw_op, input_columns="image", num_parallel_workers=num_parallel_workers) # process the generated dataset buffer_size = 10000 mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size) mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True) mnist_ds = mnist_ds.repeat(repeat_size) return mnist_ds
def create_deeptext_dataset(mindrecord_file, batch_size=2, repeat_num=12, device_num=1, rank_id=0, is_training=True, num_parallel_workers=4): """Creatr deeptext dataset with MindDataset.""" ds = de.MindDataset(mindrecord_file, columns_list=["image", "annotation"], num_shards=device_num, shard_id=rank_id, num_parallel_workers=1, shuffle=is_training) decode = C.Decode() ds = ds.map(operations=decode, input_columns=["image"], num_parallel_workers=1) compose_map_func = (lambda image, annotation: preprocess_fn(image, annotation, is_training)) hwc_to_chw = C.HWC2CHW() normalize_op = C.Normalize((123.675, 116.28, 103.53), (58.395, 57.12, 57.375)) horizontally_op = C.RandomHorizontalFlip(1) type_cast0 = CC.TypeCast(mstype.float32) type_cast1 = CC.TypeCast(mstype.float32) type_cast2 = CC.TypeCast(mstype.int32) type_cast3 = CC.TypeCast(mstype.bool_) if is_training: ds = ds.map(operations=compose_map_func, input_columns=["image", "annotation"], output_columns=["image", "image_shape", "box", "label", "valid_num"], column_order=["image", "image_shape", "box", "label", "valid_num"], num_parallel_workers=num_parallel_workers) flip = (np.random.rand() < config.flip_ratio) if flip: ds = ds.map(operations=[normalize_op, type_cast0, horizontally_op], input_columns=["image"], num_parallel_workers=12) ds = ds.map(operations=flipped_generation, input_columns=["image", "image_shape", "box", "label", "valid_num"], num_parallel_workers=num_parallel_workers) else: ds = ds.map(operations=[normalize_op, type_cast0], input_columns=["image"], num_parallel_workers=12) ds = ds.map(operations=[hwc_to_chw, type_cast1], input_columns=["image"], num_parallel_workers=12) else: ds = ds.map(operations=compose_map_func, input_columns=["image", "annotation"], output_columns=["image", "image_shape", "box", "label", "valid_num"], column_order=["image", "image_shape", "box", "label", "valid_num"], num_parallel_workers=num_parallel_workers) ds = ds.map(operations=[normalize_op, hwc_to_chw, type_cast1], input_columns=["image"], num_parallel_workers=24) # transpose_column from python to c ds = ds.map(operations=[type_cast1], input_columns=["image_shape"]) ds = ds.map(operations=[type_cast1], input_columns=["box"]) ds = ds.map(operations=[type_cast2], input_columns=["label"]) ds = ds.map(operations=[type_cast3], input_columns=["valid_num"]) ds = ds.batch(batch_size, drop_remainder=True) ds = ds.repeat(repeat_num) return ds
def create_dataset(data_home, do_train, batch_size=32, repeat_num=1): """ create a train or evaluate cifar10 dataset for resnet50 Args: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. repeat_num(int): the repeat times of dataset. Default: 1 batch_size(int): the batch size of dataset. Default: 32 target(str): the device target. Default: Ascend Returns: dataset """ # define dataset cifar_ds = ds.Cifar10Dataset(data_home) resize_height = 224 resize_width = 224 rescale = 1.0 / 255.0 shift = 0.0 # define map operations random_crop_op = C.RandomCrop( (32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT random_horizontal_op = C.RandomHorizontalFlip() resize_op = C.Resize( (resize_height, resize_width)) # interpolation default BILINEAR rescale_op = C.Rescale(rescale, shift) normalize_op = C.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) changeswap_op = C.HWC2CHW() type_cast_op = C2.TypeCast(mstype.int32) c_trans = [] if do_train: c_trans = [random_crop_op, random_horizontal_op] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # apply map operations on images cifar_ds = cifar_ds.map(operations=type_cast_op, input_columns="label") cifar_ds = cifar_ds.map(operations=c_trans, input_columns="image") # apply DatasetOps # buffer_size = 10000 # apply shuffle operations cifar_ds = cifar_ds.shuffle(buffer_size=10) # apply batch operations #cifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True) #fix this cifar_ds = cifar_ds.batch(batch_size, drop_remainder=True) #fix this # apply repeat operations cifar_ds = cifar_ds.repeat(repeat_num) return cifar_ds
def create_dataset(repeat_num=1, training=True, batch_size=32, rank_id=0, rank_size=1, enable_hccl=False): data_dir = data_home + "/cifar-10-batches-bin" if not training: data_dir = data_home + "/cifar-10-verify-bin" data_set = ds.Cifar10Dataset(data_dir) if enable_hccl: rank_id = rank_id rank_size = rank_size data_set = ds.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id) resize_height = 224 resize_width = 224 rescale = 1.0 / 255.0 shift = 0.0 # define map operations random_crop_op = vision.RandomCrop( (32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT random_horizontal_op = vision.RandomHorizontalFlip() # interpolation default BILINEAR resize_op = vision.Resize((resize_height, resize_width)) rescale_op = vision.Rescale(rescale, shift) normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023)) changeswap_op = vision.HWC2CHW() type_cast_op = C.TypeCast(mstype.int32) c_trans = [] if training: c_trans = [random_crop_op, random_horizontal_op] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # apply map operations on images data_set = data_set.map(operations=type_cast_op, input_columns="label") data_set = data_set.map(operations=c_trans, input_columns="image") # apply shuffle operations data_set = data_set.shuffle(buffer_size=1000) # apply batch operations data_set = data_set.batch(batch_size=batch_size, drop_remainder=True) # apply repeat operations data_set = data_set.repeat(repeat_num) return data_set
def create_dataset_cifar10(data_home, repeat_num=1, training=True, cifar_cfg=None): """Data operations.""" data_dir = os.path.join(data_home, "cifar-10-batches-bin") if not training: data_dir = os.path.join(data_home, "cifar-10-verify-bin") rank_size, rank_id = _get_rank_info() if training: data_set = ds.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id, shuffle=True) else: data_set = ds.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id, shuffle=False) resize_height = cifar_cfg.image_height resize_width = cifar_cfg.image_width # define map operations random_crop_op = vision.RandomCrop( (32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT random_horizontal_op = vision.RandomHorizontalFlip() resize_op = vision.Resize( (resize_height, resize_width)) # interpolation default BILINEAR rescale_op = vision.Rescale(1.0 / 255.0, 0.0) #normalize_op = vision.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) normalize_op = vision.Normalize((0.4914, 0.4822, 0.4465), (0.24703233, 0.24348505, 0.26158768)) changeswap_op = vision.HWC2CHW() type_cast_op = c_transforms.TypeCast(mstype.int32) c_trans = [] if training: c_trans = [random_crop_op, random_horizontal_op] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # apply map operations on images data_set = data_set.map(operations=type_cast_op, input_columns="label") data_set = data_set.map(operations=c_trans, input_columns="image") # apply batch operations data_set = data_set.batch(batch_size=cifar_cfg.batch_size, drop_remainder=True) # apply repeat operations data_set = data_set.repeat(repeat_num) return data_set
def create_dataset(dataset_path, csv_path, batch_size, repeat_size, device_num, rank_id): """ 该函数用于创建训练或测试时所用的数据集 Args: data_path: 数据集所在的文件夹 csv_path: 描述数据集的txt文件。训练集还是测试机就是根据该txt文件进行区分的。 batch_size: 训练时的batch_size参数 repeat_size: 数据的重复次数 num_parallel_workers: 并行工作数量 """ # 创建数据集生成器。 dataset_generator = DatasetGenerator(dataset_path, csv_path) # 将创建的数据集生成器传入到GeneratorDataset类中,创建mindspore数据集。 # ["image", "label"]表示数据集中的数据名称用image标识,标签数据用label标识。 dataset = ds.GeneratorDataset(dataset_generator, ["image", "label"], num_shards=device_num, shard_id=rank_id, shuffle=True) # 确定对图像数据进行变换的一些参数 resize_height, resize_width = 224, 224 # 图片尺寸 rescale = 1.0 / 255.0 # 归一化缩放系数 shift = 0.0 # 偏移量 # 定义调整图片的尺寸大小的操作 resize_op = CV.Resize( (resize_height, resize_width), interpolation=Inter.LINEAR) # Resize images to (32, 32) # 根据rescale和shift,定义对图像中的像素值进行归一化处理的操作 rescale_op = CV.Rescale(rescale, shift) # 定义对图像中的像素进行标准化处理的操作。下面传入的6个参数,是该数据集所有图片的RGB三个通道的均值和方差。 normalize_op = CV.Normalize((0.46, 0.46, 0.46), (0.27, 0.27, 0.27)) # 为了适应网络,将数据由于(height, width, channel) 变换为(channel, height, width) changeswap_op = CV.HWC2CHW() # 将label的数据类型改成int32类型 type_cast_op = CV2.TypeCast(mstype.int32) c_trans = [] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # 将上述定义的操作应用到数据集上,对数据集中的图像进行一定的变换。 # 标签数据类型的变换 dataset = dataset.map(operations=type_cast_op, input_columns="label") # 图片的变换 dataset = dataset.map(operations=c_trans, input_columns="image") # 数据集相关参数设置 dataset = dataset.shuffle(buffer_size=10) # 设置缓存大小 dataset = dataset.batch(batch_size, drop_remainder=True) # 设置batch_size dataset = dataset.repeat(repeat_size) # 设置repeat_size return dataset
def test_HWC2CHW_callable(): """ Test HWC2CHW is callable """ logger.info("Test HWC2CHW callable") img = np.fromfile("../data/dataset/apple.jpg", dtype=np.uint8) logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape)) img = c_vision.Decode()(img) img = c_vision.HWC2CHW()(img) logger.info("Image.type: {}, Image.shape: {}".format(type(img), img.shape)) assert img.shape == (3, 2268, 4032)
def test_HWC2CHW_callable(): """ Test HWC2CHW is callable """ logger.info("Test HWC2CHW callable") img = np.zeros([50, 50, 3]) assert img.shape == (50, 50, 3) # test one tensor img1 = c_vision.HWC2CHW()(img) assert img1.shape == (3, 50, 50) # test input multiple tensors with pytest.raises(RuntimeError) as info: imgs = [img, img] _ = c_vision.HWC2CHW()(*imgs) assert "The op is OneToOne, can only accept one tensor as input." in str(info.value) with pytest.raises(RuntimeError) as info: _ = c_vision.HWC2CHW()(img, img) assert "The op is OneToOne, can only accept one tensor as input." in str(info.value)
def create_dataset(dataset_path, do_train, batch_size=16, device_num=1, rank=0): """ create a train or eval dataset Args: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. batch_size(int): the batch size of dataset. Default: 16. device_num (int): Number of shards that the dataset should be divided into (default=1). rank (int): The shard ID within num_shards (default=0). Returns: dataset """ if device_num == 1: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True) else: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True, num_shards=device_num, shard_id=rank) # define map operations if do_train: trans = [ C.RandomCropDecodeResize(299), C.RandomHorizontalFlip(prob=0.5), C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4) ] else: trans = [C.Decode(), C.Resize(320), C.CenterCrop(299)] trans += [ C.Normalize(mean=[127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5]), C.HWC2CHW(), C2.TypeCast(mstype.float32) ] type_cast_op = C2.TypeCast(mstype.int32) ds = ds.map(input_columns="image", operations=trans, num_parallel_workers=8) ds = ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=8) # apply batch operations ds = ds.batch(batch_size, drop_remainder=True) return ds
def create_dataset_cifar10(dataset_path, do_train, cfg, repeat_num=1): """ create a train or eval dataset Args: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. cfg (dict): the config for creating dataset. repeat_num(int): the repeat times of dataset. Default: 1. Returns: dataset """ dataset_path = os.path.join( dataset_path, "cifar-10-batches-bin" if do_train else "cifar-10-verify-bin") if cfg.group_size == 1: data_set = ds.Cifar10Dataset(dataset_path, num_parallel_workers=cfg.work_nums, shuffle=True) else: data_set = ds.Cifar10Dataset(dataset_path, num_parallel_workers=cfg.work_nums, shuffle=True, num_shards=cfg.group_size, shard_id=cfg.rank) # define map operations trans = [] if do_train: trans.append(C.RandomCrop((32, 32), (4, 4, 4, 4))) trans.append(C.RandomHorizontalFlip(prob=0.5)) trans.append(C.Resize((299, 299))) trans.append(C.Rescale(1.0 / 255.0, 0.0)) trans.append( C.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010])) trans.append(C.HWC2CHW()) type_cast_op = C2.TypeCast(mstype.int32) data_set = data_set.map(operations=trans, input_columns="image", num_parallel_workers=cfg.work_nums) data_set = data_set.map(operations=type_cast_op, input_columns="label", num_parallel_workers=cfg.work_nums) # apply batch operations data_set = data_set.batch(cfg.batch_size, drop_remainder=do_train) # apply dataset repeat operation data_set = data_set.repeat(repeat_num) return data_set
def create_lenet_dataset(data_path, batch_size=32, repeat_size=1, num_parallel_workers=1): """ create dataset for train or test """ # define dataset mnist_ds = ds.MnistDataset(data_path, num_parallel_workers=num_parallel_workers) resize_height, resize_width = 32, 32 rescale = 1.0 / 255.0 shift = 0.0 rescale_nml = 1 / 0.3081 shift_nml = -1 * 0.1307 / 0.3081 # define map operations resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # Bilinear mode rescale_nml_op = CV.Rescale(rescale_nml, shift_nml) rescale_op = CV.Rescale(rescale, shift) hwc2chw_op = CV.HWC2CHW() type_cast_op = C.TypeCast(mstype.int32) # apply map operations on images mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=resize_op, input_columns="image", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=rescale_op, input_columns="image", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=rescale_nml_op, input_columns="image", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=hwc2chw_op, input_columns="image", num_parallel_workers=num_parallel_workers) # apply DatasetOps buffer_size = 10000 mnist_ds = mnist_ds.shuffle( buffer_size=buffer_size) # 10000 as in LeNet train script mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True, num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.repeat(repeat_size) return mnist_ds
def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="GPU", dtype="fp16", device_num=1): if device_num == 1: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=4, shuffle=True) else: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=4, shuffle=True, num_shards=device_num, shard_id=get_rank()) image_size = 224 mean = [0.485 * 255, 0.456 * 255, 0.406 * 255] std = [0.229 * 255, 0.224 * 255, 0.225 * 255] # define map operations normalize_op = C.Normalize(mean=mean, std=std) if dtype == "fp16": normalize_op = C.NormalizePad(mean=mean, std=std, dtype="float16") if do_train: trans = [ C.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)), C.RandomHorizontalFlip(prob=0.5), normalize_op, ] else: trans = [ C.Decode(), C.Resize(256), C.CenterCrop(image_size), normalize_op, ] if dtype == "fp32": trans.append(C.HWC2CHW()) ds = ds.map(operations=trans, input_columns="image", num_parallel_workers=8) # apply batch operations ds = ds.batch(batch_size, drop_remainder=True) # apply dataset repeat operation if repeat_num > 1: ds = ds.repeat(repeat_num) return ds
def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32): """ Create a train or eval dataset. Args: dataset_path (str): The path of dataset. do_train (bool): Whether dataset is used for train or eval. repeat_num (int): The repeat times of dataset. Default: 1. batch_size (int): The batch size of dataset. Default: 32. Returns: Dataset. """ do_shuffle = bool(do_train) if device_num == 1 or not do_train: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=config.work_nums, shuffle=do_shuffle) else: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=config.work_nums, shuffle=do_shuffle, num_shards=device_num, shard_id=device_id) image_length = 299 if do_train: trans = [ C.RandomCropDecodeResize(image_length, scale=(0.08, 1.0), ratio=(0.75, 1.333)), C.RandomHorizontalFlip(prob=0.5), C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4) ] else: trans = [ C.Decode(), C.Resize(image_length), C.CenterCrop(image_length) ] trans += [ C.Rescale(1.0 / 255.0, 0.0), C.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), C.HWC2CHW() ] type_cast_op = C2.TypeCast(mstype.int32) ds = ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=config.work_nums) ds = ds.map(input_columns="image", operations=trans, num_parallel_workers=config.work_nums) # apply batch operations ds = ds.batch(batch_size, drop_remainder=True) # apply dataset repeat operation ds = ds.repeat(repeat_num) return ds
def create_dataset1(dataset_path, do_train, repeat_num=1, batch_size=32, target="Ascend"): """ create a train or evaluate cifar10 dataset for resnet50 Args: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. repeat_num(int): the repeat times of dataset. Default: 1 batch_size(int): the batch size of dataset. Default: 32 target(str): the device target. Default: Ascend Returns: dataset """ if target == "Ascend": device_num, rank_id = _get_rank_info() else: init() rank_id = get_rank() device_num = get_group_size() if device_num == 1: ds = de.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True) else: ds = de.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True, num_shards=device_num, shard_id=rank_id) # define map operations trans = [] if do_train: trans += [ C.RandomCrop((32, 32), (4, 4, 4, 4)), C.RandomHorizontalFlip(prob=0.5) ] trans += [ C.Resize((224, 224)), C.Rescale(1.0 / 255.0, 0.0), C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]), C.HWC2CHW() ] type_cast_op = C2.TypeCast(mstype.int32) ds = ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=8) ds = ds.map(operations=trans, input_columns="image", num_parallel_workers=8) # apply batch operations ds = ds.batch(batch_size, drop_remainder=True) # apply dataset repeat operation ds = ds.repeat(repeat_num) return ds
def create_dataset(dataset_path, batch_size=1, num_shards=1, shard_id=0, device_target='Ascend'): """ create train or evaluation dataset for warpctc Args: dataset_path(str): dataset path batch_size(int): batch size of generated dataset, default is 1 num_shards(int): number of devices shard_id(int): rank id device_target(str): platform of training, support Ascend and GPU """ dataset = _CaptchaDataset(dataset_path, cf.max_captcha_digits, device_target) data_set = ds.GeneratorDataset(dataset, ["image", "label"], shuffle=True, num_shards=num_shards, shard_id=shard_id) image_trans = [ vc.Rescale(1.0 / 255.0, 0.0), vc.Normalize([0.9010, 0.9049, 0.9025], std=[0.1521, 0.1347, 0.1458]), vc.Resize((m.ceil(cf.captcha_height / 16) * 16, cf.captcha_width)), c.TypeCast(mstype.float16) ] image_trans_gpu = [ vc.Rescale(1.0 / 255.0, 0.0), vc.Normalize([0.9010, 0.9049, 0.9025], std=[0.1521, 0.1347, 0.1458]), vc.Resize((m.ceil(cf.captcha_height / 16) * 16, cf.captcha_width)), vc.HWC2CHW() ] label_trans = [c.TypeCast(mstype.int32)] if device_target == 'Ascend': data_set = data_set.map(operations=image_trans, input_columns=["image"], num_parallel_workers=8) data_set = data_set.map(operations=transpose_hwc2whc, input_columns=["image"], num_parallel_workers=8) else: data_set = data_set.map(operations=image_trans_gpu, input_columns=["image"], num_parallel_workers=8) data_set = data_set.map(operations=label_trans, input_columns=["label"], num_parallel_workers=8) data_set = data_set.batch(batch_size, drop_remainder=True) return data_set
def create_dataset(dataset_path, do_train, rank, group_size, repeat_num=1): """ create a train or eval dataset Args: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. rank (int): The shard ID within num_shards (default=None). group_size (int): Number of shards that the dataset should be divided into (default=None). repeat_num(int): the repeat times of dataset. Default: 1. Returns: dataset """ if group_size == 1: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=cfg.work_nums, shuffle=True) else: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=cfg.work_nums, shuffle=True, num_shards=group_size, shard_id=rank) # define map operations if do_train: trans = [ C.RandomCropDecodeResize(299, scale=(0.08, 1.0), ratio=(0.75, 1.333)), C.RandomHorizontalFlip(prob=0.5), C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4) ] else: trans = [C.Decode(), C.Resize(299), C.CenterCrop(299)] trans += [ C.Rescale(1.0 / 255.0, 0.0), C.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), C.HWC2CHW() ] type_cast_op = C2.TypeCast(mstype.int32) ds = ds.map(operations=trans, input_columns="image", num_parallel_workers=cfg.work_nums) ds = ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=cfg.work_nums) # apply batch operations ds = ds.batch(cfg.batch_size, drop_remainder=True) # apply dataset repeat operation ds = ds.repeat(repeat_num) return ds
def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1, samples=None, num_parallel_workers=1, sparse=True): """ create dataset for training or testing """ # define dataset ds1 = ds.MnistDataset(data_path, num_samples=samples) # define operation parameters resize_height, resize_width = 32, 32 rescale = 1.0 / 255.0 shift = 0.0 # define map operations resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) rescale_op = CV.Rescale(rescale, shift) hwc2chw_op = CV.HWC2CHW() type_cast_op = C.TypeCast(mstype.int32) # apply map operations on images if not sparse: one_hot_enco = C.OneHot(10) ds1 = ds1.map(input_columns="label", operations=one_hot_enco, num_parallel_workers=num_parallel_workers) type_cast_op = C.TypeCast(mstype.float32) ds1 = ds1.map(input_columns="label", operations=type_cast_op, num_parallel_workers=num_parallel_workers) ds1 = ds1.map(input_columns="image", operations=resize_op, num_parallel_workers=num_parallel_workers) ds1 = ds1.map(input_columns="image", operations=rescale_op, num_parallel_workers=num_parallel_workers) ds1 = ds1.map(input_columns="image", operations=hwc2chw_op, num_parallel_workers=num_parallel_workers) # apply DatasetOps buffer_size = 10000 ds1 = ds1.shuffle(buffer_size=buffer_size) ds1 = ds1.batch(batch_size, drop_remainder=True) ds1 = ds1.repeat(repeat_size) return ds1
def create_dataset(dataset_path, do_train, device_num=1, rank=0): """ create a train or eval dataset Args: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. rank (int): The shard ID within num_shards (default=None). group_size (int): Number of shards that the dataset should be divided into (default=None). repeat_num(int): the repeat times of dataset. Default: 1. Returns: dataset """ if device_num == 1: data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True) else: data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True, num_shards=device_num, shard_id=rank) # define map operations if do_train: trans = [ C.RandomCropDecodeResize(224), C.RandomHorizontalFlip(prob=0.5), C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4) ] else: trans = [C.Decode(), C.Resize(239), C.CenterCrop(224)] trans += [ C.Normalize(mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], std=[0.229 * 255, 0.224 * 255, 0.225 * 255]), C.HWC2CHW(), ] type_cast_op = C2.TypeCast(mstype.int32) data_set = data_set.map(input_columns="image", operations=trans, num_parallel_workers=8) data_set = data_set.map(input_columns="label", operations=type_cast_op, num_parallel_workers=8) # apply batch operations data_set = data_set.batch(config.batch_size, drop_remainder=True) return data_set
def vgg_create_dataset(data_home, image_size, batch_size, rank_id=0, rank_size=1, repeat_num=1, training=True): """Data operations.""" de.config.set_seed(1) data_dir = os.path.join(data_home, "cifar-10-batches-bin") if not training: data_dir = os.path.join(data_home, "cifar-10-verify-bin") data_set = de.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id) rescale = 1.0 / 255.0 shift = 0.0 # define map operations random_crop_op = vision.RandomCrop( (32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT random_horizontal_op = vision.RandomHorizontalFlip() resize_op = vision.Resize(image_size) # interpolation default BILINEAR rescale_op = vision.Rescale(rescale, shift) normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023)) changeswap_op = vision.HWC2CHW() type_cast_op = C.TypeCast(mstype.int32) c_trans = [] if training: c_trans = [random_crop_op, random_horizontal_op] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # apply map operations on images data_set = data_set.map(operations=type_cast_op, input_columns="label") data_set = data_set.map(operations=c_trans, input_columns="image") # apply repeat operations data_set = data_set.repeat(repeat_num) # apply shuffle operations data_set = data_set.shuffle(buffer_size=10) # apply batch operations data_set = data_set.batch(batch_size=batch_size, drop_remainder=True) return data_set
def create_dataset(repeat_num=1, training=True): """ create data for next use such as training or infering """ cifar_ds = ds.Cifar10Dataset(data_home) if args_opt.run_distribute: rank_id = int(os.getenv('RANK_ID')) rank_size = int(os.getenv('RANK_SIZE')) cifar_ds = ds.Cifar10Dataset(data_home, num_shards=rank_size, shard_id=rank_id) resize_height = 224 resize_width = 224 rescale = 1.0 / 255.0 shift = 0.0 # define map operations random_crop_op = C.RandomCrop( (32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT random_horizontal_op = C.RandomHorizontalFlip() resize_op = C.Resize( (resize_height, resize_width)) # interpolation default BILINEAR rescale_op = C.Rescale(rescale, shift) normalize_op = C.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) changeswap_op = C.HWC2CHW() type_cast_op = C2.TypeCast(mstype.int32) c_trans = [] if training: c_trans = [random_crop_op, random_horizontal_op] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # apply map operations on images cifar_ds = cifar_ds.map(operations=type_cast_op, input_columns="label") cifar_ds = cifar_ds.map(operations=c_trans, input_columns="image") # apply shuffle operations cifar_ds = cifar_ds.shuffle(buffer_size=10) # apply batch operations cifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True) # apply repeat operations cifar_ds = cifar_ds.repeat(repeat_num) return cifar_ds
def vgg_create_dataset100(data_home, image_size, batch_size, rank_id=0, rank_size=1, repeat_num=1, training=True, num_samples=None, shuffle=True): """Data operations.""" ds.config.set_seed(1) data_dir = os.path.join(data_home, "train") if not training: data_dir = os.path.join(data_home, "test") if num_samples is not None: data_set = ds.Cifar100Dataset(data_dir, num_shards=rank_size, shard_id=rank_id, num_samples=num_samples, shuffle=shuffle) else: data_set = ds.Cifar100Dataset(data_dir, num_shards=rank_size, shard_id=rank_id) input_columns = ["fine_label"] output_columns = ["label"] data_set = data_set.rename(input_columns=input_columns, output_columns=output_columns) data_set = data_set.project(["image", "label"]) rescale = 1.0 / 255.0 shift = 0.0 # define map operations random_crop_op = CV.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT random_horizontal_op = CV.RandomHorizontalFlip() resize_op = CV.Resize(image_size) # interpolation default BILINEAR rescale_op = CV.Rescale(rescale, shift) normalize_op = CV.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023)) changeswap_op = CV.HWC2CHW() type_cast_op = C.TypeCast(mstype.int32) c_trans = [] if training: c_trans = [random_crop_op, random_horizontal_op] c_trans += [resize_op, rescale_op, normalize_op, changeswap_op] # apply map operations on images data_set = data_set.map(input_columns="label", operations=type_cast_op) data_set = data_set.map(input_columns="image", operations=c_trans) # apply shuffle operations data_set = data_set.shuffle(buffer_size=1000) # apply batch operations data_set = data_set.batch(batch_size=batch_size, drop_remainder=True) # apply repeat operations data_set = data_set.repeat(repeat_num) return data_set
def create_dataset(data_path, is_train=True, batch_size=32): # import import mindspore.common.dtype as mstype import mindspore.dataset.engine as de import mindspore.dataset.transforms.c_transforms as C2 import mindspore.dataset.vision.c_transforms as C from mindspore.common import set_seed set_seed(1) # shard num_shards = shard_id = None rand_size = os.getenv("RANK_SIZE") rand_id = os.getenv("RANK_ID") if rand_size is not None and rand_id is not None: num_shards = int(rand_size) shard_id = int(rand_id) # define dataset data_path = os.path.join(data_path, "train" if is_train else "val") ds = de.ImageFolderDataset(data_path, shuffle=True, num_parallel_workers=8, num_shards=num_shards, shard_id=shard_id, num_samples=None) # define ops comps_ops = list() # train or val if is_train: comps_ops.append(C.RandomCropDecodeResize(224, scale=(0.08, 1.0), ratio=(0.75, 1.333))) comps_ops.append(C.RandomHorizontalFlip(prob=0.5)) comps_ops.append(C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4)) else: comps_ops.append(C.Decode()) comps_ops.append(C.Resize(224)) comps_ops.append(C.CenterCrop(224)) comps_ops.append(C.Rescale(1 / 255.0, 0.)) comps_ops.append(C.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])) comps_ops.append(C.HWC2CHW()) # map ops ds = ds.map(input_columns=["image"], operations=comps_ops, num_parallel_workers=8) ds = ds.map(input_columns=["label"], operations=C2.TypeCast(mstype.int32), num_parallel_workers=8) # batch & repeat ds = ds.batch(batch_size=batch_size, drop_remainder=is_train) ds = ds.repeat(count=1) return ds
def create_dataset_val(batch_size=128, val_data_url='', workers=8, distributed=False): if not os.path.exists(val_data_url): raise ValueError('Path not exists') rank_id = get_rank() if distributed else 0 rank_size = get_group_size() if distributed else 1 dataset = ds.ImageFolderDataset(val_data_url, num_parallel_workers=workers, num_shards=rank_size, shard_id=rank_id, shuffle=False) scale_size = None interpolation = str2MsInter(inter_method) if isinstance(img_size, tuple): assert len(img_size) == 2 if img_size[-1] == img_size[-2]: scale_size = int(math.floor(img_size[0] / crop_pct)) else: scale_size = tuple([int(x / crop_pct) for x in img_size]) else: scale_size = int(math.floor(img_size / crop_pct)) type_cast_op = C2.TypeCast(mstype.int32) decode_op = C.Decode() resize_op = C.Resize(size=scale_size, interpolation=interpolation) center_crop = C.CenterCrop(size=224) rescale_op = C.Rescale(rescale, shift) normalize_op = C.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD) changeswap_op = C.HWC2CHW() ctrans = [ decode_op, resize_op, center_crop, rescale_op, normalize_op, changeswap_op ] dataset = dataset.map(input_columns=["label"], operations=type_cast_op, num_parallel_workers=workers) dataset = dataset.map(input_columns=["image"], operations=ctrans, num_parallel_workers=workers) dataset = dataset.batch(batch_size, drop_remainder=True, num_parallel_workers=workers) dataset = dataset.repeat(1) return dataset
def create_dataset(name, dataset_path, batch_size=1, num_shards=1, shard_id=0, is_training=True, config=config1): """ create train or evaluation dataset for crnn Args: dataset_path(int): dataset path batch_size(int): batch size of generated dataset, default is 1 num_shards(int): number of devices shard_id(int): rank id device_target(str): platform of training, support Ascend and GPU """ if name == 'synth': dataset = CaptchaDataset(dataset_path, is_training, config) elif name == 'ic03': dataset = IC03Dataset(dataset_path, "annotation.txt", config, True, 3) elif name == 'ic13': dataset = IC13Dataset(dataset_path, "Challenge2_Test_Task3_GT.txt", config) elif name == 'svt': dataset = SVTDataset(dataset_path, config) elif name == 'iiit5k': dataset = IIIT5KDataset(dataset_path, "annotation.txt", config) else: raise ValueError(f"unsupported dataset name: {name}") data_set = ds.GeneratorDataset(dataset, ["image", "label"], shuffle=True, num_shards=num_shards, shard_id=shard_id) image_trans = [ vc.Resize((config.image_height, config.image_width)), vc.Normalize([127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5]), vc.HWC2CHW() ] label_trans = [C.TypeCast(mstype.int32)] data_set = data_set.map(operations=image_trans, input_columns=["image"], num_parallel_workers=8) data_set = data_set.map(operations=label_trans, input_columns=["label"], num_parallel_workers=8) data_set = data_set.batch(batch_size, drop_remainder=True) return data_set
def test_HWC2CHW_md5(): """ Test HWC2CHW(md5) """ logger.info("Test HWC2CHW with md5 comparison") # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) decode_op = c_vision.Decode() hwc2chw_op = c_vision.HWC2CHW() data1 = data1.map(operations=decode_op, input_columns=["image"]) data1 = data1.map(operations=hwc2chw_op, input_columns=["image"]) # Compare with expected md5 from images filename = "HWC2CHW_01_result.npz" save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
def create_yolo_dataset(image_dir, anno_path, batch_size, max_epoch, device_num, rank, config=None, is_training=True, shuffle=True): """Create dataset for YOLOV4.""" cv2.setNumThreads(0) if is_training: filter_crowd = True remove_empty_anno = True else: filter_crowd = False remove_empty_anno = False yolo_dataset = COCOYoloDataset(root=image_dir, ann_file=anno_path, filter_crowd_anno=filter_crowd, remove_images_without_annotations=remove_empty_anno, is_training=is_training) distributed_sampler = DistributedSampler(len(yolo_dataset), device_num, rank, shuffle=shuffle) hwc_to_chw = CV.HWC2CHW() config.dataset_size = len(yolo_dataset) cores = multiprocessing.cpu_count() num_parallel_workers = int(cores / device_num) if is_training: multi_scale_trans = MultiScaleTrans(config, device_num) dataset_column_names = ["image", "annotation", "bbox1", "bbox2", "bbox3", "gt_box1", "gt_box2", "gt_box3"] if device_num != 8: ds = de.GeneratorDataset(yolo_dataset, column_names=dataset_column_names, num_parallel_workers=min(32, num_parallel_workers), sampler=distributed_sampler) ds = ds.batch(batch_size, per_batch_map=multi_scale_trans, input_columns=dataset_column_names, num_parallel_workers=min(32, num_parallel_workers), drop_remainder=True) else: ds = de.GeneratorDataset(yolo_dataset, column_names=dataset_column_names, sampler=distributed_sampler) ds = ds.batch(batch_size, per_batch_map=multi_scale_trans, input_columns=dataset_column_names, num_parallel_workers=min(8, num_parallel_workers), drop_remainder=True) else: ds = de.GeneratorDataset(yolo_dataset, column_names=["image", "img_id"], sampler=distributed_sampler) compose_map_func = (lambda image, img_id: reshape_fn(image, img_id, config)) ds = ds.map(operations=compose_map_func, input_columns=["image", "img_id"], output_columns=["image", "image_shape", "img_id"], column_order=["image", "image_shape", "img_id"], num_parallel_workers=8) ds = ds.map(operations=hwc_to_chw, input_columns=["image"], num_parallel_workers=8) ds = ds.batch(batch_size, drop_remainder=True) ds = ds.repeat(max_epoch) return ds, len(yolo_dataset)
def create_dataset(args, data_path, batch_size): # if args.use_kungfu: # rank = kfops.kungfu_current_rank() # size = kfops.kungfu_current_cluster_size() rank = 0 size = 4 ds = de.Cifar10Dataset( data_path, num_parallel_workers=8, shuffle=False, num_shards=size, shard_id=rank, ) print('using shard %d of %d' % (rank, size)) # else: # ds = de.Cifar10Dataset( # data_path, # num_parallel_workers=8, # shuffle=False, # ) trans = [] # if do_train: # trans += [ # # C.RandomCrop((32, 32), (4, 4, 4, 4)), # # C.RandomHorizontalFlip(prob=0.5) # ] trans += [ C.Rescale(1.0 / 255.0, 0.0), C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]), C.HWC2CHW() ] type_cast_op = C2.TypeCast(mstype.int32) ds = ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=8) ds = ds.map(operations=trans, input_columns="image", num_parallel_workers=8) # apply batch operations ds = ds.batch(batch_size, drop_remainder=True) return ds
def test_HWC2CHW_comp(plot=False): """ Test HWC2CHW between python and c image augmentation """ logger.info("Test HWC2CHW with c_transform and py_transform comparison") # First dataset data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) decode_op = c_vision.Decode() hwc2chw_op = c_vision.HWC2CHW() data1 = data1.map(operations=decode_op, input_columns=["image"]) data1 = data1.map(operations=hwc2chw_op, input_columns=["image"]) # Second dataset data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False) transforms = [ py_vision.Decode(), py_vision.ToTensor(), py_vision.HWC2CHW() ] transform = mindspore.dataset.transforms.py_transforms.Compose(transforms) data2 = data2.map(operations=transform, input_columns=["image"]) image_c_transposed = [] image_py_transposed = [] for item1, item2 in zip( data1.create_dict_iterator(num_epochs=1, output_numpy=True), data2.create_dict_iterator(num_epochs=1, output_numpy=True)): c_image = item1["image"] py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8) # Compare images between that applying c_transform and py_transform mse = diff_mse(py_image, c_image) # Note: The images aren't exactly the same due to rounding error assert mse < 0.001 image_c_transposed.append(c_image.transpose(1, 2, 0)) image_py_transposed.append(py_image.transpose(1, 2, 0)) if plot: visualize_list(image_c_transposed, image_py_transposed, visualize_mode=2)
def create_dataset(data_path, batch_size=32, repeat_size=1, num_parallel_workers=1): """ create dataset for train or test Args: data_path (str): Data path batch_size (int): The number of data records in each group repeat_size (int): The number of replicated data records num_parallel_workers (int): The number of parallel workers """ # define dataset mnist_ds = ds.MnistDataset(data_path) # define operation parameters resize_height, resize_width = 32, 32 rescale = 1.0 / 255.0 shift = 0.0 rescale_nml = 1 / 0.3081 shift_nml = -1 * 0.1307 / 0.3081 # define map operations type_cast_op = C.TypeCast(mstype.int32) c_trans = [ CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR), CV.Rescale(rescale_nml, shift_nml), CV.Rescale(rescale, shift), CV.HWC2CHW() ] # apply map operations on images mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=num_parallel_workers) mnist_ds = mnist_ds.map(operations=c_trans, input_columns="image", num_parallel_workers=num_parallel_workers) # apply DatasetOps buffer_size = 10000 mnist_ds = mnist_ds.shuffle( buffer_size=buffer_size) # 10000 as in LeNet train script mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True) mnist_ds = mnist_ds.repeat(repeat_size) return mnist_ds