def get_makeup_transform(cfg, pic="image"):
    if pic == "image":
        transform = T.Compose([
            T.Resize(size=cfg.trans_size),
            T.Permute(to_rgb=False),
        ])
    else:
        transform = T.Resize(size=cfg.trans_size,
                             interpolation=cv2.INTER_NEAREST)

    return transform
Esempio n. 2
0
 def __init__(self, mode="train"):
     super(HumanClasDataset, self).__init__()
     self.data_path = data_dir
     ps = os.listdir(osp.join(self.data_path, "p"))
     ns = os.listdir(osp.join(self.data_path, "n"))
     ps.sort()
     ns.sort()
     ps = [osp.join("p", x) for x in ps]
     ns = [osp.join("n", x) for x in ns]
     ns = random.sample(ns, len(ps))
     data = []
     if mode == "train":
         for idx in range(int(len(ps) * 0.8)):
             data.append([ps[idx], 1])
         for idx in range(int(len(ns) * 0.8)):
             data.append([ns[idx], 0])
     else:
         for idx in range(int(len(ps) * 0.8), len(ps)):
             data.append([ps[idx], 1])
         for idx in range(int(len(ns) * 0.8), len(ns)):
             data.append([ns[idx], 0])
     self.data = data
     self.transform = vt.Compose([
         vt.ColorJitter(0.1, 0.1, 0.1, 0.1),
         # # vt.RandomRotation(10),
         vt.RandomHorizontalFlip(),
         vt.Resize(64),
         vt.ToTensor(),
     ])  # TODO: 研究合适的数据增强策略
    def __init__(self, mode='train'):
        """
        初始化函数
        """
        assert mode in ['train', 'test',
                        'valid'], 'mode is one of train, test, valid.'

        self.data = []

        with open('signs/{}.txt'.format(mode)) as f:
            for line in f.readlines():
                info = line.strip().split('\t')

                if len(info) > 0:
                    self.data.append([info[0].strip(), info[1].strip()])

        if mode == 'train':
            self.transforms = T.Compose([
                T.RandomResizedCrop(IMAGE_SIZE),  # 随机裁剪大小
                T.RandomHorizontalFlip(0.5),  # 随机水平翻转
                T.ToTensor(),  # 数据的格式转换和标准化 HWC => CHW
                T.Normalize(mean=[0.485, 0.456, 0.406],
                            std=[0.229, 0.224, 0.225])  # 图像归一化
            ])
        else:
            self.transforms = T.Compose([
                T.Resize(256),  # 图像大小修改
                T.RandomCrop(IMAGE_SIZE),  # 随机裁剪
                T.ToTensor(),  # 数据的格式转换和标准化 HWC => CHW
                T.Normalize(mean=[0.485, 0.456, 0.406],
                            std=[0.229, 0.224, 0.225])  # 图像归一化
            ])
def prepare_input():
    transforms = [
        T.Resize(size=(target_height, target_width)),
        T.Normalize(mean=(0, 0, 0),
                    std=(1, 1, 1),
                    data_format='HWC',
                    to_rgb=True),
        T.Transpose()
    ]

    img_file = root_path / "street.jpeg"
    img = cv2.imread(str(img_file))
    normalized_img = T.Compose(transforms)(img)
    normalized_img = normalized_img.astype(np.float32, copy=False) / 255.0

    # add an new axis in front
    img_input = normalized_img[np.newaxis, :]
    # scale_factor is calculated as: im_shape / original_im_shape
    h_scale = target_height / img.shape[0]
    w_scale = target_width / img.shape[1]
    input = {
        "image": img_input,
        "im_shape": [target_height, target_width],
        "scale_factor": [h_scale, w_scale]
    }
    return input, img
Esempio n. 5
0
def get_transforms(resize, crop):
    transforms = T.Compose([
        T.Resize(resize, interpolation="bicubic"),
        T.CenterCrop(crop),
        T.ToTensor(),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    return transforms
Esempio n. 6
0
def get_transforms(resize, crop):
    transforms = [T.Resize(resize, interpolation="bicubic")]
    if crop:
        transforms.append(T.CenterCrop(crop))
    transforms.append(T.ToTensor())
    transforms.append(
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    )
    transforms = T.Compose(transforms)
    return transforms
Esempio n. 7
0
def get_transforms(interpolation):
    transforms = T.Compose(
        [
            T.Resize(256, interpolation=interpolation),
            T.CenterCrop(224),
            T.ToTensor(),
            T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
        ]
    )
    return transforms
Esempio n. 8
0
 def __init__(self, config, need_parser=True):
     self.img_size = 256
     self.transform = transform = T.Compose([
         T.Resize(size=256),
         T.ToTensor(),
     ])
     self.norm = T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
     if need_parser:
         self.face_parser = futils.mask.FaceParser()
     self.up_ratio = 0.6 / 0.85
     self.down_ratio = 0.2 / 0.85
     self.width_ratio = 0.2 / 0.85
Esempio n. 9
0
 def __init__(self, config, need_parser=True):
     self.img_size = 256
     self.transform = transform = T.Compose([
         T.Resize(size=256),
         T.Permute(to_rgb=False),
     ])
     self.norm = T.Normalize([127.5, 127.5, 127.5], [127.5, 127.5, 127.5])
     if need_parser:
         self.face_parser = futils.mask.FaceParser()
     self.up_ratio = 0.6 / 0.85
     self.down_ratio = 0.2 / 0.85
     self.width_ratio = 0.2 / 0.85
Esempio n. 10
0
    def __init__(self, opt=opt):
        super(DataGenerater, self).__init__()
        self.dir = opt.imgs_path

        self.datalist = os.listdir(
            self.dir) if opt.test == False else os.listdir(self.dir)[:100]
        self.batch_size = opt.batch_size

        img = Image.open(self.dir + self.datalist[0])
        self.image_size = img.size
        img.close()

        self.transform = T.Compose([
            T.Resize(opt.img_size),
            T.CenterCrop(opt.img_size),
            T.ToTensor(),
        ])
        self.num_path_dict = {}
def load_train_test_datasets(dataset_root):
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    resize = transforms.Resize(256)
    rcrop = transforms.RandomCrop((224, 224))
    ccrop = transforms.CenterCrop((224, 224))
    tot = transforms.ToTensor()
    normalize = transforms.Normalize(mean, std)

    train_transforms = transforms.Compose([resize, rcrop, tot, normalize])
    test_transforms = transforms.Compose([resize, ccrop, tot, normalize])

    train_set = DatasetFolder(osp.join(dataset_root, 'train'),
                              transform=train_transforms)
    test_set = DatasetFolder(osp.join(dataset_root, 'test'),
                             transform=test_transforms)

    return train_set, test_set
Esempio n. 12
0
def get_transform(cfg,
                  params=None,
                  grayscale=False,
                  method=cv2.INTER_CUBIC,
                  convert=True):
    transform_list = []
    if grayscale:
        print('grayscale not support for now!!!')
        pass
    if 'resize' in cfg.preprocess:
        osize = (cfg.load_size, cfg.load_size)
        transform_list.append(transforms.Resize(osize, method))
    elif 'scale_width' in cfg.preprocess:
        print('scale_width not support for now!!!')
        pass

    if 'crop' in cfg.preprocess:

        if params is None:
            transform_list.append(T.RandomCrop(cfg.crop_size))
        else:
            transform_list.append(T.Crop(params['crop_pos'], cfg.crop_size))

    if cfg.preprocess == 'none':
        print('preprocess not support for now!!!')
        pass

    if not cfg.no_flip:
        if params is None:
            transform_list.append(transforms.RandomHorizontalFlip())
        elif params['flip']:
            transform_list.append(transforms.RandomHorizontalFlip(1.0))

    if convert:
        transform_list += [transforms.Permute(to_rgb=True)]
        if cfg.get('normalize', None):
            transform_list += [
                transforms.Normalize(cfg.normalize.mean, cfg.normalize.std)
            ]

    return transforms.Compose(transform_list)
Esempio n. 13
0
import math

import paddle
import paddle.nn as nn
import paddle.nn.functional as F
import paddle.vision.transforms as T

from paddle.nn.initializer import Normal

from ppim.models.common import Identity
from ppim.models.common import load_model
from ppim.models.common import zeros_, ones_

transforms = T.Compose([
    T.Resize(256, interpolation="bilinear"),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

urls = {
    "dla_34":
    r"https://bj.bcebos.com/v1/ai-studio-online/a4e08c790f0247c8ab44cfa9ec6264720a3fab64b51d4ee88d0e7d3511e6348a?responseContentDisposition=attachment%3B%20filename%3Ddla34%2Btricks.pdparams",
    "dla_46_c":
    r"https://bj.bcebos.com/v1/ai-studio-online/245e16ae6b284b368798a6f8e3cf068e55eea96e22724ec5bff8d146c64da990?responseContentDisposition=attachment%3B%20filename%3Ddla46_c.pdparams",
    "dla_46x_c":
    r"https://bj.bcebos.com/v1/ai-studio-online/b295201d245247fb8cd601b60919cabf5df51a8997d04380bd07eac71e4152dd?responseContentDisposition=attachment%3B%20filename%3Ddla46x_c.pdparams",
    "dla_60":
    r"https://bj.bcebos.com/v1/ai-studio-online/e545d431a9f84bb4aecd2c75e34e6169503be2d2e8d246cb9cff393559409f7b?responseContentDisposition=attachment%3B%20filename%3Ddla60.pdparams",
    "dla_60x":
    r"https://bj.bcebos.com/v1/ai-studio-online/a07ea1cec75a460ebf6dcace4ab0c8c28e923af88dd74573baaaa6db8738168d?responseContentDisposition=attachment%3B%20filename%3Ddla60x.pdparams",
Esempio n. 14
0
        for im, la in zip(fn, label):
            img = Image.open(im)
            img = img.convert("RGB")
            img = np.array(img)
            label = np.array([la]).astype(dtype='int64')
        #按照路径读取图片
        if self.trainsform is not None:
            img = self.trainsform(img)
            # 数据标签转化为tensor
        return img, label

    def __len__(self):
        # 返回数据集的长度,多少张图片。
        return len(self.imgs)


transform = T.Compose([
    T.Resize([224, 224]),
    T.ToTensor(),
    T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])

train_dataset = MyDataset(image=image_path_list,
                          label=train_label_list,
                          transform=transform)

train_loader = paddle.io.DataLoader(train_dataset,
                                    places=paddle.CPUPlace(),
                                    batch_size=8,
                                    shuffle=True)
Esempio n. 15
0
import paddle
import paddle.nn as nn
import paddle.vision.transforms as T

from math import ceil

from ppim.models.common import load_model

transforms = T.Compose([
    T.Resize(256, interpolation="bicubic"),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

urls = {
    "rexnet_1_0":
    r"https://bj.bcebos.com/v1/ai-studio-online/6c890dd95dfc4e388335adfa298163d3ab413cca558e4abe966d52cb5c3aee31?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_1.0x.pdparams",
    "rexnet_1_3":
    r"https://bj.bcebos.com/v1/ai-studio-online/41a4cc3e6d9545b9b69b4782cafa01147eb7661ec6af4f43841adc734149b3a7?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_1.3x.pdparams",
    "rexnet_1_5":
    r"https://bj.bcebos.com/v1/ai-studio-online/20b131a7cb1840b5aed37c512b2665fb20c72eebe4344da5a3c6f0ab0592a323?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_1.5x.pdparams",
    "rexnet_2_0":
    r"https://bj.bcebos.com/v1/ai-studio-online/b4df9f7be43446b0952a25ee6e83f2e443e3b879a00046f6bb33278319cb5fd0?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_2.0x.pdparams",
    "rexnet_3_0":
    r"https://bj.bcebos.com/v1/ai-studio-online/9663f0570f0a4e4a8dde0b9799c539f5e22f46917d3d4e5a9d566cd213032d25?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_3.0x.pdparams",
}


def ConvBN(out,
           in_channels,
Esempio n. 16
0
    GPU_ID = cfg['GPU_ID']  # specify your GPU ids
    PIN_MEMORY = cfg['PIN_MEMORY']
    NUM_WORKERS = cfg['NUM_WORKERS']
    USE_PRETRAINED = cfg['USE_PRETRAINED']
    logger.info("=" * 60)
    logger.info("Overall Configurations:")
    for cfg_, value in cfg.items():
        logger.info(cfg_ + ":" + str(value))
    logger.info("=" * 60)

    fleet.init(is_collective=True)

    train_transform = transforms.Compose([
        # refer to https://pytorch.org/docs/stable/torchvision/transforms.html for more build-in online data augmentation
        transforms.Resize(
            [int(128 * INPUT_SIZE[0] / 112),
             int(128 * INPUT_SIZE[0] / 112)]),  # smaller side resized
        transforms.RandomCrop([INPUT_SIZE[0], INPUT_SIZE[1]]),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=RGB_MEAN, std=RGB_STD),
    ])
    logger.info('loading data form file:{}'.format(DATA_ROOT))
    train_dataset = LFWDataset(os.path.join(DATA_ROOT), train_transform)
    num_classes = train_dataset.num_classes
    train_loader = paddle.io.DataLoader(train_dataset,
                                        places=[paddle.CPUPlace()],
                                        batch_size=BATCH_SIZE,
                                        shuffle=True,
                                        drop_last=DROP_LAST,
                                        num_workers=NUM_WORKERS)
Esempio n. 17
0
        help='diffusion/diffusion-m/diffusion-s/improved/improved-s')

    args, _ = parser.parse_known_args()

    print(args)

    conf = config[args.config]

    full_save_dir = f'{conf.save_dir}/{dirname}/{conf.name}'
    conf.full_save_dir = full_save_dir

    os.makedirs(f'{full_save_dir}/sample', exist_ok=True)
    os.makedirs(f'{full_save_dir}/checkpoint', exist_ok=True)

    transform = transforms.Compose([
        transforms.Resize([conf.dataset.resolution] * 2),
        transforms.Transpose(),
        transforms.Normalize((127.5, 127.5, 127.5), (127.5, 127.5, 127.5)),
    ])

    train_dataset = datasets.ImageFolder(conf.dataset.path,
                                         transform=transform)
    train_loader = DataLoader(train_dataset,
                              batch_size=conf.training.dataloader.batch_size,
                              shuffle=True,
                              num_workers=conf.training.dataloader.num_workers,
                              drop_last=conf.training.dataloader.drop_last,
                              use_shared_memory=False)

    model = UNet(**conf.model)
    ema = UNet(**conf.model)
Esempio n. 18
0
import paddle
from paddle.fluid.reader import DataLoader
from paddle.optimizer.lr import StepDecay
from paddle.vision import transforms

from reid.dataset import ReIDDataset
from reid.modeling import PCB

eval_transforms = transforms.Compose([
    transforms.Resize((384, 192), interpolation='bicubic'),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
train_ds = ReIDDataset(
    './data/archive/Market-1501-v15.09.15/bounding_box_train',
    transform=eval_transforms)
test_ds = ReIDDataset('./data/archive/Market-1501-v15.09.15/bounding_box_test',
                      transform=eval_transforms)
model = PCB(1500)
# model = paddle.Model(PCB(1500))
scheduler = StepDecay(learning_rate=0.1, step_size=40, verbose=1, gamma=0.130)
train_loader = DataLoader(train_ds, batch_size=32, shuffle=True)
# scheduler = ReduceOnPlateau(learning_rate=0.1,verbose=1,patience=40)
sgd = paddle.optimizer.SGD(parameters=model.parameters(),
                           weight_decay=5e-4,
                           learning_rate=scheduler)
m = paddle.metric.Accuracy()
ce_loss = paddle.nn.CrossEntropyLoss()
for epoch in range(100):
    m.reset()
Esempio n. 19
0
import paddle
import paddle.nn as nn
import paddle.vision.transforms as T

from math import ceil
from ..units import load_model

transforms = T.Compose([
    T.Resize(256, interpolation='bicubic'),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

urls = {
    'rexnet_100':
    r'https://bj.bcebos.com/v1/ai-studio-online/6c890dd95dfc4e388335adfa298163d3ab413cca558e4abe966d52cb5c3aee31?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_1.0x.pdparams',
    'rexnet_130':
    r'https://bj.bcebos.com/v1/ai-studio-online/41a4cc3e6d9545b9b69b4782cafa01147eb7661ec6af4f43841adc734149b3a7?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_1.3x.pdparams',
    'rexnet_150':
    r'https://bj.bcebos.com/v1/ai-studio-online/20b131a7cb1840b5aed37c512b2665fb20c72eebe4344da5a3c6f0ab0592a323?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_1.5x.pdparams',
    'rexnet_200':
    r'https://bj.bcebos.com/v1/ai-studio-online/b4df9f7be43446b0952a25ee6e83f2e443e3b879a00046f6bb33278319cb5fd0?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_2.0x.pdparams',
    'rexnet_300':
    r'https://bj.bcebos.com/v1/ai-studio-online/9663f0570f0a4e4a8dde0b9799c539f5e22f46917d3d4e5a9d566cd213032d25?responseContentDisposition=attachment%3B%20filename%3Drexnetv1_3.0x.pdparams'
}


def ConvBN(out,
           in_channels,
           channels,
    def run(
            self,
            image,
            need_align=False,
            start_lr=0.1,
            final_lr=0.025,
            latent_level=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                          11],  # for ffhq (0~17)
            step=100,
            mse_weight=1,
            pre_latent=None):

        if need_align:
            src_img = run_alignment(image)
        else:
            src_img = Image.open(image).convert("RGB")

        generator = self.generator
        generator.train()

        percept = LPIPS(net='vgg')
        # on PaddlePaddle, lpips's default eval mode means no gradients.
        percept.train()

        n_mean_latent = 4096

        transform = transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(256),
            transforms.Transpose(),
            transforms.Normalize([127.5, 127.5, 127.5], [127.5, 127.5, 127.5]),
        ])

        imgs = paddle.to_tensor(transform(src_img)).unsqueeze(0)

        if pre_latent is None:
            with paddle.no_grad():
                noise_sample = paddle.randn(
                    (n_mean_latent, generator.style_dim))
                latent_out = generator.style(noise_sample)

                latent_mean = latent_out.mean(0)

            latent_in = latent_mean.detach().clone().unsqueeze(0).tile(
                (imgs.shape[0], 1))
            latent_in = latent_in.unsqueeze(1).tile(
                (1, generator.n_latent, 1)).detach()

        else:
            latent_in = paddle.to_tensor(np.load(pre_latent)).unsqueeze(0)

        var_levels = list(latent_level)
        const_levels = [
            i for i in range(generator.n_latent) if i not in var_levels
        ]
        assert len(var_levels) > 0
        if len(const_levels) > 0:
            latent_fix = latent_in.index_select(paddle.to_tensor(const_levels),
                                                1).detach().clone()
            latent_in = latent_in.index_select(paddle.to_tensor(var_levels),
                                               1).detach().clone()

        latent_in.stop_gradient = False

        optimizer = optim.Adam(parameters=[latent_in], learning_rate=start_lr)

        pbar = tqdm(range(step))

        for i in pbar:
            t = i / step
            lr = get_lr(t, step, start_lr, final_lr)
            optimizer.set_lr(lr)

            if len(const_levels) > 0:
                latent_dict = {}
                for idx, idx2 in enumerate(var_levels):
                    latent_dict[idx2] = latent_in[:, idx:idx + 1]
                for idx, idx2 in enumerate(const_levels):
                    latent_dict[idx2] = (latent_fix[:, idx:idx + 1]).detach()
                latent_list = []
                for idx in range(generator.n_latent):
                    latent_list.append(latent_dict[idx])
                latent_n = paddle.concat(latent_list, 1)
            else:
                latent_n = latent_in

            img_gen, _ = generator([latent_n],
                                   input_is_latent=True,
                                   randomize_noise=False)

            batch, channel, height, width = img_gen.shape

            if height > 256:
                factor = height // 256

                img_gen = img_gen.reshape((batch, channel, height // factor,
                                           factor, width // factor, factor))
                img_gen = img_gen.mean([3, 5])

            p_loss = percept(img_gen, imgs).sum()
            mse_loss = F.mse_loss(img_gen, imgs)
            loss = p_loss + mse_weight * mse_loss

            optimizer.clear_grad()
            loss.backward()
            optimizer.step()

            pbar.set_description(
                (f"perceptual: {p_loss.numpy()[0]:.4f}; "
                 f"mse: {mse_loss.numpy()[0]:.4f}; lr: {lr:.4f}"))

        img_gen, _ = generator([latent_n],
                               input_is_latent=True,
                               randomize_noise=False)
        dst_img = make_image(img_gen)[0]
        dst_latent = latent_n.numpy()[0]

        os.makedirs(self.output_path, exist_ok=True)
        save_src_path = os.path.join(self.output_path, 'src.fitting.png')
        cv2.imwrite(save_src_path,
                    cv2.cvtColor(np.asarray(src_img), cv2.COLOR_RGB2BGR))
        save_dst_path = os.path.join(self.output_path, 'dst.fitting.png')
        cv2.imwrite(save_dst_path, cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR))
        save_npy_path = os.path.join(self.output_path, 'dst.fitting.npy')
        np.save(save_npy_path, dst_latent)

        return np.asarray(src_img), dst_img, dst_latent
Esempio n. 21
0
def get_dataset(args, config):
    if config.data.random_flip is False:
        tran_transform = test_transform = transforms.Compose([
            transforms.Resize([config.data.image_size] * 2),
            transforms.Transpose(), lambda x: x
            if x.dtype != np.uint8 else x.astype('float32') / 255.0
        ])
    else:
        tran_transform = transforms.Compose([
            transforms.Resize([config.data.image_size] * 2),
            transforms.RandomHorizontalFlip(prob=0.5),
            transforms.Transpose(),
            lambda x: x
            if x.dtype != np.uint8 else x.astype('float32') / 255.0,
        ])
        test_transform = transforms.Compose([
            transforms.Resize([config.data.image_size] * 2),
            transforms.Transpose(), lambda x: x
            if x.dtype != np.uint8 else x.astype('float32') / 255.0
        ])

    if config.data.dataset == "CIFAR10":
        dataset = Cifar10(
            # os.path.join(args.exp, "datasets", "cifar10"),
            mode="train",
            download=True,
            transform=tran_transform,
        )
        test_dataset = Cifar10(
            # os.path.join(args.exp, "datasets", "cifar10_test"),
            mode="test",
            download=True,
            transform=test_transform,
        )

    elif config.data.dataset == "CELEBA":
        cx = 89
        cy = 121
        x1 = cy - 64
        x2 = cy + 64
        y1 = cx - 64
        y2 = cx + 64
        if config.data.random_flip:
            dataset = CelebA(
                root=os.path.join(args.exp, "datasets", "celeba"),
                split="train",
                transform=transforms.Compose([
                    Crop(x1, x2, y1, y2),
                    transforms.Resize([config.data.image_size] * 2),
                    transforms.RandomHorizontalFlip(),
                    transforms.Transpose(),
                    lambda x: x
                    if x.dtype != np.uint8 else x.astype('float32') / 255.0,
                ]),
                download=True,
            )
        else:
            dataset = CelebA(
                root=os.path.join(args.exp, "datasets", "celeba"),
                split="train",
                transform=transforms.Compose([
                    Crop(x1, x2, y1, y2),
                    transforms.Resize([config.data.image_size] * 2),
                    transforms.Transpose(),
                    lambda x: x
                    if x.dtype != np.uint8 else x.astype('float32') / 255.0,
                ]),
                download=True,
            )

        test_dataset = CelebA(
            root=os.path.join(args.exp, "datasets", "celeba"),
            split="test",
            transform=transforms.Compose([
                Crop(x1, x2, y1, y2),
                transforms.Resize([config.data.image_size] * 2),
                transforms.Transpose(),
                lambda x: x
                if x.dtype != np.uint8 else x.astype('float32') / 255.0,
            ]),
            download=True,
        )

    elif config.data.dataset == "LSUN":
        train_folder = "{}_train".format(config.data.category)
        val_folder = "{}_val".format(config.data.category)
        if config.data.random_flip:
            dataset = LSUN(
                root=os.path.join(args.exp, "datasets", "lsun"),
                classes=[train_folder],
                transform=transforms.Compose([
                    transforms.Resize([config.data.image_size] * 2),
                    transforms.CenterCrop((config.data.image_size, ) * 2),
                    transforms.RandomHorizontalFlip(prob=0.5),
                    transforms.Transpose(),
                    lambda x: x
                    if x.dtype != np.uint8 else x.astype('float32') / 255.0,
                ]),
            )
        else:
            dataset = LSUN(
                root=os.path.join(args.exp, "datasets", "lsun"),
                classes=[train_folder],
                transform=transforms.Compose([
                    transforms.Resize([config.data.image_size] * 2),
                    transforms.CenterCrop((config.data.image_size, ) * 2),
                    transforms.Transpose(),
                    lambda x: x
                    if x.dtype != np.uint8 else x.astype('float32') / 255.0,
                ]),
            )

        test_dataset = LSUN(
            root=os.path.join(args.exp, "datasets", "lsun"),
            classes=[val_folder],
            transform=transforms.Compose([
                transforms.Resize([config.data.image_size] * 2),
                transforms.CenterCrop((config.data.image_size, ) * 2),
                transforms.Transpose(),
                lambda x: x
                if x.dtype != np.uint8 else x.astype('float32') / 255.0,
            ]),
        )

    elif config.data.dataset == "FFHQ":
        if config.data.random_flip:
            dataset = FFHQ(
                path=os.path.join(args.exp, "datasets", "FFHQ"),
                transform=transforms.Compose([
                    transforms.RandomHorizontalFlip(prob=0.5),
                    transforms.Transpose(), lambda x: x
                    if x.dtype != np.uint8 else x.astype('float32') / 255.0
                ]),
                resolution=config.data.image_size,
            )
        else:
            dataset = FFHQ(
                path=os.path.join(args.exp, "datasets", "FFHQ"),
                transform=transforms.Compose(
                    transforms.Transpose(), lambda x: x
                    if x.dtype != np.uint8 else x.astype('float32') / 255.0),
                resolution=config.data.image_size,
            )

        num_items = len(dataset)
        indices = list(range(num_items))
        random_state = np.random.get_state()
        np.random.seed(2019)
        np.random.shuffle(indices)
        np.random.set_state(random_state)
        train_indices, test_indices = (
            indices[:int(num_items * 0.9)],
            indices[int(num_items * 0.9):],
        )
        test_dataset = Subset(dataset, test_indices)
        dataset = Subset(dataset, train_indices)
    else:
        dataset, test_dataset = None, None

    return dataset, test_dataset
Esempio n. 22
0
import os
import random
import paddle
import paddle.nn as nn
import paddle.optimizer as optim
import paddle.vision.datasets as dset
import paddle.vision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

dataset = paddle.vision.datasets.MNIST(
    mode='train',
    transform=transforms.Compose([
        # resize ->(32,32)
        transforms.Resize((32, 32)),
        # 归一化到-1~1
        transforms.Normalize([127.5], [127.5])
    ]))

dataloader = paddle.io.DataLoader(dataset,
                                  batch_size=32,
                                  shuffle=True,
                                  num_workers=4)


#参数初始化的模块
@paddle.no_grad()
def normal_(x, mean=0., std=1.):
    temp_value = paddle.normal(mean, std, shape=x.shape)
    x.set_value(temp_value)
Esempio n. 23
0
import math

import paddle
import paddle.vision.transforms as T

from paddle import nn
from paddle.nn.initializer import Assign

from ..units import load_model
from .vit import trunc_normal_, zeros_, ones_, Block

transforms = T.Compose([
    T.Resize(248, interpolation='bicubic'),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

urls = {
    'pit_ti':
    'https://bj.bcebos.com/v1/ai-studio-online/3d0fe9a33bb74abaa0648f6200b37e5b49ca9a4f15a04afbab7a885da64dfa62?responseContentDisposition=attachment%3B%20filename%3Dpit_ti.pdparams',
    'pit_xs':
    'https://bj.bcebos.com/v1/ai-studio-online/4bee539cc81a477a8bae4795f91d583c810ea4832e6d4ed983b37883669e6a6d?responseContentDisposition=attachment%3B%20filename%3Dpit_xs.pdparams',
    'pit_s':
    'https://bj.bcebos.com/v1/ai-studio-online/232c216331d04fb58f77839673b34652ea229a9ab84044a493e08cd802ab4fe3?responseContentDisposition=attachment%3B%20filename%3Dpit_s.pdparams',
    'pit_b':
    'https://bj.bcebos.com/v1/ai-studio-online/26f33b44d9424626b74eb7cfad2041582afabdebd6474afa976cc0a55c226791?responseContentDisposition=attachment%3B%20filename%3Dpit_b.pdparams',
    'pit_ti_distilled':
    'https://bj.bcebos.com/v1/ai-studio-online/9707c73717274b5e880e8401b85dcf9ad12b0d7e47944af68b3d6a2236b70567?responseContentDisposition=attachment%3B%20filename%3Dpit_ti_distill.pdparams',
    'pit_xs_distilled':
    'https://bj.bcebos.com/v1/ai-studio-online/61aa3339366d4315854bf67a8df1cea20f4a2402b2d94d7688d995423a197df1?responseContentDisposition=attachment%3B%20filename%3Dpit_xs_distill.pdparams',
Esempio n. 24
0
# 
# 采用Cifar10数据集,无过多的数据增强

# ### 数据准备

# In[7]:


import paddle.vision.transforms as T
from paddle.vision.datasets import Cifar10

paddle.set_device('gpu')

#数据准备
transform = T.Compose([
    T.Resize(size=(224,224)),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225],data_format='HWC'),
    T.ToTensor()
])

train_dataset = Cifar10(mode='train', transform=transform)
val_dataset = Cifar10(mode='test',  transform=transform)


# ### 模型准备

# In[ ]:


Conformer_tiny = Conformer_tiny_patch16(num_classes=10)
Conformer_tiny.set_state_dict(paddle.load('/home/aistudio/data/data96103/Conformer_tiny_patch16.pdparams'))
Esempio n. 25
0
import paddle
from paddle.io import DataLoader
from paddle.vision import datasets
from paddle.vision import transforms

dataset = datasets.MNIST(
    mode='train',
    download=True,
    transform=transforms.Compose([
        transforms.Resize(28),
        transforms.ToTensor(),
        transforms.Normalize([0.5], [0.5])
    ]),
)

dataloader = DataLoader(
    dataset=dataset,
    batch_size=8,
    shuffle=True,
)
print(type(dataset))
print(type(dataloader))
for i, (img, _) in enumerate(dataloader()):
    print(img.shape)
Esempio n. 26
0
import paddle
import paddle.nn as nn
import paddle.vision.transforms as T
import numpy as np

from ..units import load_model

transforms = T.Compose([
    T.Resize(256),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

urls = {
    'repvgg_a0':
    'https://bj.bcebos.com/v1/ai-studio-online/26d1d26e0d0141deafeb7e9980ec8b5a555232b938e44fefa93da930422af42b?responseContentDisposition=attachment%3B%20filename%3DRepVGG_A0.pdparams',
    'repvgg_a1':
    'https://bj.bcebos.com/v1/ai-studio-online/afa4629fb917427a829bb278250b84b0380d580b40fc4e478eb5fdb75fe22096?responseContentDisposition=attachment%3B%20filename%3DRepVGG_A1.pdparams',
    'repvgg_a2':
    'https://bj.bcebos.com/v1/ai-studio-online/200f4d6038834fd49796941f5acf65308e6e096d2b8c496abb9d1c0204f44cb1?responseContentDisposition=attachment%3B%20filename%3DRepVGG_A2.pdparams',
    'repvgg_b0':
    'https://bj.bcebos.com/v1/ai-studio-online/93c345b4a76b4f88b3590fa703a270b009cc9c05481640a49e8654222459e79f?responseContentDisposition=attachment%3B%20filename%3DRepVGG_B0.pdparams',
    'repvgg_b1':
    'https://bj.bcebos.com/v1/ai-studio-online/b2f8171754bd4d3cb44739b675dc1f0b8cb77ebefdad47ec82ce98292726bf2c?responseContentDisposition=attachment%3B%20filename%3DRepVGG_B1.pdparams',
    'repvgg_b2':
    'https://bj.bcebos.com/v1/ai-studio-online/9fc65aab46b441dca194f974bdf420710b2144e941704330869d62a2ab9cb0b6?responseContentDisposition=attachment%3B%20filename%3DRepVGG_B2.pdparams',
    'repvgg_b3':
    'https://bj.bcebos.com/v1/ai-studio-online/8d902ba9ebf3441e896e8d7078544005a0715ca6867f4067989dcc533ace2435?responseContentDisposition=attachment%3B%20filename%3DRepVGG_B3_200epochs.pdparams',
    'repvgg_b1g2':
    'https://bj.bcebos.com/v1/ai-studio-online/da4931eff12142a290ce8d01a0cd3b777a81b53c971b4dd2a1a627c615466570?responseContentDisposition=attachment%3B%20filename%3DRepVGG_B1g2.pdparams',
Esempio n. 27
0
import itertools
import numpy as np

import paddle
import paddle.nn as nn
import paddle.vision.transforms as T

from paddle.nn.initializer import Constant

from ppim.models.common import zeros_
from ppim.models.common import load_model

transforms = T.Compose([
    T.Resize(248, interpolation="bicubic"),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

urls = {
    "levit_128s":
    r"https://bj.bcebos.com/v1/ai-studio-online/3d875a55328c487d833363574b3e6b3be41be4b44a8542dc972f089a6f9e7cc5?responseContentDisposition=attachment%3B%20filename%3DLeViT-128S.pdparams",
    "levit_128":
    r"https://bj.bcebos.com/v1/ai-studio-online/8cf41e96d369411dbc526177c040d68b041c2fd282674069b89a51b16431e3db?responseContentDisposition=attachment%3B%20filename%3DLeViT-128.pdparams",
    "levit_192":
    r"https://bj.bcebos.com/v1/ai-studio-online/26275887b63b4fe9bea2d5155229e49ba5ee6e49f3294e1c8c88eec08f19cd09?responseContentDisposition=attachment%3B%20filename%3DLeViT-192.pdparams",
    "levit_256":
    r"https://bj.bcebos.com/v1/ai-studio-online/9c869ecde73147b39726ba6a154e91ef326de0a04b2d4ad5809a7a50db7a6ea0?responseContentDisposition=attachment%3B%20filename%3DLeViT-256.pdparams",
    "levit_384":
    r"https://bj.bcebos.com/v1/ai-studio-online/1e39ab61d8a5408aa08cdb30a9de52f9e17b839bd3054be7b8b2c6de1e19742c?responseContentDisposition=attachment%3B%20filename%3DLeViT-384.pdparams",
}
Esempio n. 28
0
else:
    # Initialize weights
    generator.apply(weights_init_normal)
    discriminator.apply(weights_init_normal)

# Optimizers
optimizer_G = torch.optim.Adam(generator.parameters(),
                               lr=opt.lr,
                               betas=(opt.b1, opt.b2))
optimizer_D = torch.optim.Adam(discriminator.parameters(),
                               lr=opt.lr,
                               betas=(opt.b1, opt.b2))

# Configure dataloaders
transforms_ = [
    transforms.Resize((opt.img_height, opt.img_width), Image.BICUBIC),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]

dataloader = DataLoader(
    ImageDataset("../../data/%s" % opt.dataset_name, transforms_=transforms_),
    batch_size=opt.batch_size,
    shuffle=True,
    num_workers=opt.n_cpu,
)

val_dataloader = DataLoader(
    ImageDataset("../../data/%s" % opt.dataset_name,
                 transforms_=transforms_,
                 mode="val"),
Esempio n. 29
0
    names = tar.getnames()
    for name in names:
        tar.extract(name, "./")

model = paddle.jit.load("./paddle_resnet50/model")

######################################################################
# Load a test image
# ---------------------------------------------
# A single cat dominates the examples!

from PIL import Image
import paddle.vision.transforms as T

transforms = T.Compose([
    T.Resize((256, 256)),
    T.CenterCrop(224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true"
img_path = download_testdata(img_url, "cat.png", module="data")
img = Image.open(img_path).resize((224, 224))

img = transforms(img)
img = np.expand_dims(img, axis=0)

######################################################################
# Compile the model with relay
# ---------------------------------------------
Esempio n. 30
0
# Loss function
adversarial_loss = paddle.nn.BCELoss()

# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()
# print("-1")
# Configure data loader
os.makedirs("../../data/mnist", exist_ok=True)

dataset = datasets.MNIST(
    mode='train',
    download=True,
    transform=transforms.Compose([
        transforms.Resize(opt.img_size),
        transforms.ToTensor(),
        transforms.Normalize([0.5], [0.5])
    ]),
)
# print(len(dataset))
dataloader = DataLoader(
    dataset=dataset,
    batch_size=opt.batch_size,
    shuffle=True,
)
# print("0")
# Optimizers
optimizer_G = paddle.optimizer.Adam(parameters=generator.parameters(),
                                    learning_rate=opt.lr,
                                    beta1=opt.b1,