def __getitem__(self, idx): path, cid = self._data[idx] img = cv2.imread(path, 1) img = np.pad(img, ((self._pad, self._pad), (self._pad, self._pad), (0, 0)), mode='constant', constant_values=0) img = nd.array(img, dtype='float32') / 255 H, W, C = img.shape data_shape = (C, H - self._pad * 2, W - self._pad * 2) if self._fortrain: augs = image.CreateAugmenter(data_shape=data_shape, resize=0, rand_mirror=True, rand_crop=True, mean=self._mean, std=self._std) else: augs = image.CreateAugmenter(data_shape=data_shape, resize=0, rand_mirror=False, rand_crop=False, mean=self._mean, std=self._std) for aug in augs: img = aug(img) img = nd.transpose(img, (2, 0, 1)) return img, nd.array([cid]).astype("float32")
def transform_train(data, label): #将图像调整为不同的大小,分别进行数据扩增,因为是使用两个模型进行融合,所以也使用两类数据分别给两个网络进行训练 im1 = image.imresize(data.astype('float32') / 255, 224, 224) #将图像调整为224x224,data的每个像素点除以255,保证在[0-1] im2 = image.imresize(data.astype('float32') / 255, 299, 299) #数据增强参数1,给第一个网络 auglist1 = image.CreateAugmenter(data_shape=(3, 224, 224), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225]), brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) # 数据增强参数2,给第二个网络 auglist2 = image.CreateAugmenter(data_shape=(3, 299, 299), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225]), brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) #分别进行增强 for aug in auglist1: im1 = aug(im1) for aug in auglist2: im2 = aug(im2) # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 im1 = nd.transpose(im1, (2, 0, 1)) im2 = nd.transpose(im2, (2, 0, 1)) #返回给两个数据,分别给两个网络进行训练 return (im1, im2, nd.array([label]).asscalar().astype('float32'))
def transform_test(data, label): im = data.astype('float32') / 255 auglist = image.CreateAugmenter(data_shape=(3, 224, 224)) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label]).asscalar().astype('float32')
def transform_test(data, label): im = data.astype('float32') / 255.0 auglist = image.CreateAugmenter(data_shape=(3, 32, 32), mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010])) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label]).astype('float32')
def transform_test(data, label): im1 = image.imresize(data.astype('float32') / 255, 224, 224) im2 = image.imresize(data.astype('float32') / 255, 299, 299) auglist1 = image.CreateAugmenter(data_shape=(3, 224, 224), mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225])) auglist2 = image.CreateAugmenter(data_shape=(3, 299, 299), mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225])) for aug in auglist1: im1 = aug(im1) for aug in auglist2: im2 = aug(im2) # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 im1 = nd.transpose(im1, (2,0,1)) im2 = nd.transpose(im2, (2,0,1)) return (im1,im2, nd.array([label]).asscalar().astype('float32'))
def transform_train(data, label): im = data.astype('float32') / 255 auglist = image.CreateAugmenter(data_shape=(3, 32, 32)) for aug in auglist: im = aug(im) # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label]).asscalar().astype('float32')
def trans_test(data, label): im = data.astype(np.float32) / 255. auglist = image.CreateAugmenter(data_shape=(3, 32, 32), mean=mx.nd.array([0.485, 0.456, 0.406]), std=mx.nd.array([0.229, 0.224, 0.225])) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, label
def transform_test(data, label): im = data.astype('float32') / 255 auglist = image.CreateAugmenter(data_shape(3, 32, 32), resize=0, mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010])) for aug in auglist: im = aug(im) # change data from height*width*channel to channel*height*width im = nd.transpose(im, (2,0,1)) return (im, nd.array([label]).asscalar().astype('float32'))
def transform_test(data, label): im = data.astype("float32") / 255 auglist = image.CreateAugmenter(data_shape=(3, 32, 32), mean=np.array([0., 0., 0.]), std=np.array([1, 1, 1])) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label]).asscalar().astype("float32")
def transform_train(data, label): im = data.astype('float32') / 255 auglist = image.CreateAugmenter(data_shape=(3, 224, 224), resize=256, rand_crop=True, rand_mirror=True, mean = np.array([0.485, 0.456, 0.406]), std = np.array([0.229, 0.224, 0.225])) for aug in auglist: im = aug(im) im = nd.transpose(im, (2,0,1)) return (im, nd.array([label]).asscalar())
def trans_test(data, label): im = data auglist = image.CreateAugmenter(data_shape=(3, 224, 224), resize=256, mean=True, std=True) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, label
def transform_test(data): im1 = image.imresize(data.astype('float32') / 255, 288, 288) auglist1 = image.CreateAugmenter(data_shape=(3, 224, 224), resize=0, mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225])) im2 = image.imresize(data.astype('float32') / 255, 363, 363) auglist2 = image.CreateAugmenter(data_shape=(3, 299, 299), resize=0, mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225])) for aug in auglist1: im1 = aug(im1) im1 = nd.transpose(im1, (2, 0, 1)) for aug in auglist2: im2 = aug(im2) im2 = nd.transpose(im2, (2, 0, 1)) return im1.expand_dims(axis=0), im2.expand_dims(axis=0)
def transformer(data, label): im = data.asnumpy() im = np.pad(im, pad_width=((4, 4), (4, 4), (0, 0)), mode='constant') im = random_eraser(im) im = nd.array(im) / 255. auglist = image.CreateAugmenter(data_shape=(3, 32, 32), rand_crop=True, rand_mirror=True, mean=mx.nd.array([0.4914, 0.4824, 0.4467]), std=mx.nd.array([0.2471, 0.2435, 0.2616])) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, label
def transform_train(data, label): im = image.imresize(data.astype('float32') / 255, 96, 96) auglist = image.CreateAugmenter(data_shape=(3, 96, 96), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=None, std=None, brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) for aug in auglist: im = aug(im) # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 im = nd.transpose(im, (2,0,1)) return (im, nd.array([label]).asscalar().astype('float32'))
def transform_train(data, label): im1 = image.imresize(data.astype('float32') / 255, 224, 224) im2 = image.imresize(data.astype('float32') / 255, 299, 299) auglist1 = image.CreateAugmenter(data_shape=(3, 224, 224), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225]), brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) auglist2 = image.CreateAugmenter(data_shape=(3, 299, 299), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=np.array([0.485, 0.456, 0.406]), std=np.array([0.229, 0.224, 0.225]), brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) for aug in auglist1: im1 = aug(im1) for aug in auglist2: im2 = aug(im2) # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 im1 = nd.transpose(im1, (2, 0, 1)) im2 = nd.transpose(im2, (2, 0, 1)) return (im1, im2, nd.array([label]).asscalar().astype('float32'))
def transform_train(data, label): im = data.astype('float32') / 255 auglist = image.CreateAugmenter(data_shape=(3, 299, 299), resize=320, rand_crop=True, rand_mirror=True, mean = np.array([0.485, 0.456, 0.406]), std = np.array([0.229, 0.224, 0.225]), brightness=0.125, contrast=0.125, saturation = 0.125, pca_noise = 0.05, inter_method = 10 ) for aug in auglist: im = aug(im) im = nd.transpose(im, (2,0,1)) return (im, nd.array([label]).asscalar())
def transform_train(data, label): im = data.asnumpy() # im = np.pad(im, ((4, 4), (4, 4), (0, 0)), mode='constant', constant_values=0) im = np.pad(im, ((4, 4), (4, 4), (0, 0)), mode='reflect') im = nd.array(im, dtype='float32') / 255.0 auglist = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, rand_mirror=True, rand_crop=True, mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010])) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) # channel x height x weight return im, nd.array([label]).astype('float32')
def transform_train(data, label): im = data.astype('float32') / 255 aug_list = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010]), brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) for aug in aug_list: im = aug(im) # 将数据格式从"高*宽*通道"改为"通道*高*宽" im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label]).asscalar().astype('float32')
def transform_train(data, label): im = data.astype('float32') / 255 auglist = image.CreateAugmenter(data_shape(3, 32, 32), resize=0, rand_crop=True, rand_resize=True, rand_mirror=True, mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010]), brightness=0, contrast=0, saturation=0, hue=0, pca_noise=0, rand_gray=0, inter_method=2) for aug in auglist: im = aug(im) # change data from height*width*channel to channel*height*width im = nd.transpose(im, (2,0,1)) return (im, nd.array([label]).asscalar().astype('float32'))
def transform_train(data, label): im = data.astype("float32") / 255 auglist = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, rand_crop=True, rand_resize=True, rand_gray=0.5, rand_mirror=True, mean=np.array([0., 0., 0.]), std=np.array([1, 1, 1]), brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5, pca_noise=0, inter_method=2) for aug in auglist: im = aug(im) # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label]).asscalar().astype("float32")
def transform_train(data, label): im = data.asnumpy() im = np.pad(im, ((4, 4), (4, 4), (0, 0)), mode='constant') # 前半部分为np augment. 后半部分为nd augment,注意要求转换为float型 im = nd.array(im, dtype="float32") / 255 auglist = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, rand_mirror=True, rand_crop=True, mean=nd.array([0.4914, 0.4822, 0.4465]), std=nd.array([0.2023, 0.1994, 0.2010])) for aug in auglist: im = aug(im) im = im.transpose((2, 0, 1)) return im, label.astype('float32')
def transform_train_DA2(data, label): im = data.astype(np.float32) / 255 auglist = [image.RandomSizedCropAug(size=(32, 32), min_area=0.49, ratio=(0.5, 2))] _aug = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010]), brightness=0.3, contrast=0.3, saturation=0.3, hue=0.3, pca_noise=0.01, rand_gray=0, inter_method=2) auglist.append(image.RandomOrderAug(_aug)) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return (im, nd.array([label]).asscalar().astype('float32'))
def augment(data): aug_list = image.CreateAugmenter(data_shape=(3, 32, 32), rand_crop=True, rand_mirror=True) data = nd.pad(data, pad_width=(0, 0, 0, 0, 2, 2, 2, 2), mode='constant', constant_value=0) data = nd.transpose(data, (0, 2, 3, 1)) temp = [] for d in data: for aug in aug_list: d = aug(d) temp.append(d) data = nd.stack(*temp) data = nd.transpose(data, (0, 3, 1, 2)) return data
def transformer(data, label): jitter_param = 0.4 lighting_param = 0.1 im = data auglist = image.CreateAugmenter(data_shape=(3, 224, 224), rand_crop=True, rand_resize=True, rand_mirror=True, brightness=jitter_param, saturation=jitter_param, contrast=jitter_param, pca_noise=lighting_param, mean=True, std=True) for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return im, label
def __init__(self, batch_size, img_h, img_w, ctx): self.h = img_h self.w = img_w self.bs = batch_size self.ctx = ctx self.BIL = PILIMG.BILINEAR self.all_img = [] base_dir = os.path.dirname(os.path.abspath(__file__)) #png_img_path = os.path.join(base_dir, 'syn_images') png_img_path = '/media/nolan/9fc64877-3935-46df-9ad0-c601733f5888/insulator_raw_images' for img in os.listdir(png_img_path): img_path = os.path.join(png_img_path, img) self.all_img.append(img_path) self.pil_image_enhance = PILImageEnhance(G=0.1, noise_var=2) self.augs = image.CreateAugmenter(data_shape=(3, img_h, img_w), inter_method=10, brightness=0.1, contrast=0.3, saturation=0.3, hue=1.0, pca_noise=0.1) print('Loading background!')
def pre_processing(im_path, data_shape, resize=128, mean_rgb=(0, 0, 0), std_rgb=(1, 1, 1)): try: with open(im_path, 'rb') as f: im = image.imdecode(f.read()) augmenter = image.CreateAugmenter(data_shape, resize=resize, mean=np.array(mean_rgb), std=np.array(std_rgb)) for aug_func in augmenter: im = aug_func(im) im = im.transpose((2, 0, 1)).expand_dims(axis=0) return im except Exception as e: print(e) return None
def get_image_augs(shape, scope="train"): augs_params = train_augs_params if scope == 'train' else test_augs_params augs_params['data_shape'] = (0, shape, shape) augs = image.CreateAugmenter(**augs_params) return augs
type=str, help='symbol path') sgroup.add_argument('--output-blobs', default='', type=str, help='output blocks name') igroup = parser.add_argument_group() igroup.add_argument('--arch', default='resnet101_v2', help='architecture') igroup.add_argument('--num_classes', default=1000, type=int, help='num classes: WebFace: 10575, VGG-Face2: 9131') args = parser.parse_args() args.im_exts = [ext.strip() for ext in args.im_exts.split(',')] ctx = mx.gpu(args.gpu) if args.gpu >= 0 else mx.cpu() data_shape = tuple(map(int, args.data_shape.split(','))) mean_rgb = tuple(map(float, args.mean.split(','))) std_rgb = tuple(map(float, args.std.split(','))) augmenter = image.CreateAugmenter(data_shape, resize=args.resize, mean=np.array(mean_rgb), std=np.array(std_rgb)) extracting_features()
train_pic, train_label, test_pic, test_label = load_cifar(data_dir) # ------------------------------------ # train_pic = np.transpose(train_pic.astype('float32') / 255, (0, 2, 3, 1)) test_pic = np.transpose(test_pic.astype('float32') / 255, (0, 2, 3, 1)) mean = [0.4914, 0.4822, 0.4465] std = [0.2470, 0.2435, 0.2616] for i in range(3): train_pic[:, :, :, i] = (train_pic[:, :, :, i] - mean[i]) / std[i] test_pic[:, :, :, i] = (test_pic[:, :, :, i] - mean[i]) / std[i] train_pic = np.transpose(train_pic, (0, 3, 1, 2)) test_pic = np.transpose(test_pic, (0, 3, 1, 2)) aug_train = image.CreateAugmenter(data_shape=(3, 32, 32), rand_crop=True, rand_mirror=True) # ------------------------------------ # batch_size = 128 train_data = gluon.data.DataLoader(gluon.data.ArrayDataset( train_pic, train_label.astype('float32')), batch_size, shuffle=True) test_data = gluon.data.DataLoader(gluon.data.ArrayDataset( test_pic, test_label.astype('float32')), batch_size, shuffle=False) # ------------------------------------ #
help='hyper param of mix-up') parser.add_argument('--save-period', type=int, default=10, help='period in epoch of model saving.') _args = parser.parse_args() return _args os.environ[ 'MXNET_GLUON_REPO'] = 'https://apache-mxnet.s3.cn-north-1.amazonaws.com.cn/' os.environ['MXNET_ENABLE_GPU_P2P'] = '0' train_auglist = image.CreateAugmenter(data_shape=(3, 32, 32), rand_crop=True, rand_mirror=True, mean=nd.array([0.485, 0.456, 0.406]), std=nd.array([0.229, 0.224, 0.225])) val_auglist = image.CreateAugmenter(data_shape=(3, 32, 32), mean=nd.array([0.485, 0.456, 0.406]), std=nd.array([0.229, 0.224, 0.225])) args = parse_args() use_mix_up = True if args.mix_up else False def transform_train(data, label): im = data.astype('float32') / 255 im = image.copyMakeBorder(im, 4, 4, 4, 4) for aug in train_auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1))