|
- import mindspore.dataset as ds
- import mindspore.dataset.transforms.c_transforms as C
- import mindspore.dataset.vision.c_transforms as CV
- import mindspore.common.dtype as mstype
- import os
- import numpy
-
- from src.config import alexnet_imagenet_cfg
-
- #DATA_DIR = "./prepare_data/VOCdevkit/VOC2012"
-
- num_parallel_workers = 6
-
-
- def crop_image(image, bbox,label):
- img = numpy.array(image)
- xmin = bbox[0][0]
- ymin = bbox[0][1]
- xmax = bbox[0][2]
- ymax = bbox[0][3]
- sample_img = img[xmin:xmax, ymin:ymax, :]
- return sample_img, bbox
-
- def create_dataset_voc(dataset_path, batch_size=32, repeat_num=1, training=True,
- num_parallel_workers=None, shuffle=None, sampler=None, class_indexing=None):
-
- device_num = int(os.getenv("RANK_SIZE"))
- rank_id = int(os.getenv("DEVICE_ID"))
- #print("dfsdddddddddddddddddddddddddddddeviece num:",device_num)
-
- cfg = alexnet_imagenet_cfg
-
- num_parallel_workers = 16
- if device_num == 1:
- num_parallel_workers = 48
- ds.config.set_prefetch_size(8)
- else:
- ds.config.set_numa_enable(False)
-
- data_set = ds.VOCDataset(dataset_path, task="Detection", usage="train", decode=True, num_parallel_workers=4,
- shuffle=shuffle, sampler=sampler, class_indexing=class_indexing,num_shards=device_num, shard_id=rank_id)
-
- type_cast = C.TypeCast(mstype.int32)
- data_set = data_set.map(operations=type_cast, input_columns="bbox")
-
- data_set = data_set.map(input_columns=["image", "bbox"],
- output_columns=["image", "bbox"],
- column_order=["image", "bbox"],
- operations=crop_image, python_multiprocessing=False,
- num_parallel_workers=num_parallel_workers)
-
- #data_set = data_set.batch(batch_size, drop_remainder=True, pad_info={"bbox": ([2, None], 0),"label":([1],0)})
-
- assert cfg.image_height == cfg.image_width, "imagenet_cfg.image_height not equal imagenet_cfg.image_width"
- image_size = cfg.image_height
- mean = [0.485 * 255, 0.456 * 255, 0.406 * 255]
- std = [0.229 * 255, 0.224 * 255, 0.225 * 255]
-
- # define map operations
- if training:
- transform_img = [
- CV.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)),
- CV.RandomHorizontalFlip(prob=0.5),
- CV.Normalize(mean=mean, std=std),
- CV.HWC2CHW()
- ]
- else:
- transform_img = [
- CV.Decode(),
- CV.Resize((256, 256)),
- CV.CenterCrop(image_size),
- CV.Normalize(mean=mean, std=std),
- CV.HWC2CHW()
- ]
-
- #data_set=data_set.map(input_columns="image,bbox_labels,bbox",operations=crop_image)
- data_set = data_set.map(input_columns="image", num_parallel_workers=num_parallel_workers,
- operations=transform_img)
-
- data_set = data_set.batch(batch_size, drop_remainder=True)
-
- # apply dataset repeat operation
- if repeat_num > 1:
- data_set = data_set.repeat(repeat_num)
-
- return data_set
-
-
-
-
- '''
- dataset = ds.VOCDataset(DATA_DIR, task="Detection", usage="train", decode=True, shuffle=False)
- type_cast = C.TypeCast(mstype.int32)
- dataset = dataset.map(operations=type_cast, input_columns="bbox")
- def crop_image(image,bbox,label,difficult,truncate):
- img=numpy.array(image)
- xmin=bbox[0]
- ymin=bbox[1]
- xmax=bbox[2]
- ymax=bbox[3]
- sample_img = img[xmin:xmax, ymin:ymax, :]
- return sample_img,bbox,label,difficult,truncate
-
- dataset = dataset.map(input_columns=["image", "bbox","label","difficult","truncate"],
- output_columns=["image", "bbox","label","difficult","truncate"],
- column_order=["image", "bbox","label","difficult","truncate"],
- operations=crop_image, python_multiprocessing=False,
- num_parallel_workers=num_parallel_workers)
- ds = ds.batch(batch_size, drop_remainder=True, pad_info={"mask": ([2, None, None], 0)})
- print("[Detection]:")
- '''
-
- #for data in dataset.create_dict_iterator(output_numpy=True):
- #for data in dataset.create_dict_iterator():
- # print("image shape:", data["image"])
- # print("bbox shape:", data["bbox"])
- # print("bbox shape:", data["bbox"].shape)
- # xmin = (data["bbox"][0])
- # ymin = (data["bbox"][0][1])
- # xmax = (data["bbox"][0][2])
- # ymax = (data["bbox"][0][3])
- # print(ymax.dtype)
- # sample_img = data["image"][xmin:xmax, ymin:ymax,:]
|