コード例 #1
0
def set_loader(opt):
    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.485, 0.456, 0.406)
        std = (0.229, 0.224, 0.225)
    normalize = transforms.Normalize(mean=mean, std=std)
    # [0.485, 0.456, 0.406], [0.229, 0.224, 0.225] for Dogs vs Cats
    # TODO write train_dataset for cvd --check utils

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=256, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([
            transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)
        ], p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize])
        
    train_dir= opt.data_dir
    train_files= os.listdir(train_dir)
    cat_files = [tf for tf in train_files if 'cat' in tf]
    dog_files = [tf for tf in train_files if 'dog' in tf]

    cats = CatDogDataset(cat_files, train_dir, transform = TwoCropTransform(train_transform))
    dogs = CatDogDataset(train_files, train_dir, transform = TwoCropTransform(train_transform))

    train_dataset = ConcatDataset([cats, dogs])

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=opt.batch_size, shuffle=(train_sampler is None),
        num_workers=opt.num_workers, pin_memory=True, sampler=train_sampler)

    return train_loader
コード例 #2
0
ファイル: main_supcon.py プロジェクト: XG293/SupConLoss
def set_loader(opt):
    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    elif opt.dataset == 'SD198-20':
        mean = (0.485, 0.456, 0.406)
        std = (0.229, 0.224, 0.225)
    elif opt.dataset == 'path':
        mean = eval(opt.mean)
        std = eval(opt.std)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))
    normalize = transforms.Normalize(mean=mean, std=std)
    # ['RandomResizedCrop', 'ImageJitter', 'RandomRotation', 'RandomHorizontalFlip', 'ToTensor', 'Normalize']
    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=opt.size, scale=(0.8, 1.2)),
        transforms.RandomRotation(degrees=30),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])

    if opt.dataset == 'cifar10':
        train_dataset = datasets.CIFAR10(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = datasets.CIFAR100(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'SD198-20':
        ## opt.data_folder = '/home/slidm/SkinLesionData/SD-198-20/base.json'
        train_dataset = SimpleDataset(
            data_file=opt.data_folder,
            transform=TwoCropTransform(train_transform))
    elif opt.dataset == 'path':
        train_dataset = datasets.ImageFolder(
            root=opt.data_folder, transform=TwoCropTransform(train_transform))
    else:
        raise ValueError(opt.dataset)

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=opt.num_workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    return train_loader
コード例 #3
0
def set_loader(opt):
    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    elif opt.dataset == 'path':
        mean = eval(opt.mean)
        std = eval(opt.std)
    elif opt.dataset == 'cardio':
        mean = (0.485, 0.456, 0.406)
        std = (0.229, 0.224, 0.225)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))
    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        #transforms.RandomResizedCrop(size=opt.size, scale=(0.2, 1.)),
        transforms.Resize(size=opt.size),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])

    if opt.dataset == 'cifar10':
        train_dataset = datasets.CIFAR10(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = datasets.CIFAR100(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'path':
        train_dataset = datasets.ImageFolder(
            root=opt.data_folder, transform=TwoCropTransform(train_transform))

    elif opt.dataset == 'cardio':
        train_dataset = get_cardio_smclr(opt.data_folder)
        print('cardio dataset')
    else:
        raise ValueError(opt.dataset)

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=opt.num_workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    return train_loader
コード例 #4
0
def set_loader(opt):
    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    elif opt.dataset == 'shapenet':
        mean = (0.1575, 0.1497, 0.1429)
        std = (0.2985, 0.2891, 0.2827)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))
    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=32, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])

    if opt.dataset == 'cifar10':
        train_dataset = datasets.CIFAR10(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = datasets.CIFAR100(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'shapenet':
        train_dataset = ImageFolder(root=os.path.join(opt.data_folder,
                                                      'train'),
                                    transform=train_transform,
                                    views=opt.views,
                                    drop_objects_ratio=opt.drop_objects_ratio)
    else:
        raise ValueError(opt.dataset)

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=opt.num_workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    return train_loader
コード例 #5
0
def set_loader(opt):

    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    elif opt.dataset == 'path':
        mean = eval(opt.mean)
        std = eval(opt.std)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))

    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=32, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(), normalize
    ])

    if opt.dataset == 'cifar10':
        train_dataset = CIFAR10Instance(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = CIFAR100Instance(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'path':
        train_dataset = datasets.ImageFolder(
            root=opt.data_folder, transform=TwoCropTransform(train_transform))
    else:
        raise ValueError(opt.dataset)

    n_data = len(train_dataset)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.batch_size,
                                               num_workers=opt.num_workers,
                                               pin_memory=True)

    return train_loader, n_data
コード例 #6
0
def set_loader(opt):
    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))
    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=32, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])

    if opt.dataset == 'cifar10':
        train_dataset = datasets.CIFAR10(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = datasets.CIFAR100(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    else:
        raise ValueError(opt.dataset)
    from util import InfiniteSampler
    train_sampler = InfiniteSampler(train_dataset, shuffle=True)
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=opt.num_workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    return iter(train_loader)
コード例 #7
0
def set_dataset(opt):
    # construct dataset
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    elif opt.dataset == 'path':
        mean = eval(opt.mean)
        std = eval(opt.mean)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))
    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=opt.size, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])

    if opt.dataset == 'cifar10':
        train_dataset = datasets.CIFAR10(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = datasets.CIFAR100(
            root=opt.data_folder,
            transform=TwoCropTransform(train_transform),
            download=True)
    elif opt.dataset == 'path':
        train_dataset = datasets.ImageFolder(
            root=opt.data_folder, transform=TwoCropTransform(train_transform))
    else:
        raise ValueError(opt.dataset)

    return train_dataset
コード例 #8
0
def set_loader(opt, train=True):

    if opt.dataset == 'cifar10':
        dataset = datasets.CIFAR10(root=opt.data_folder,
                                         train=train,
                                         transform=TwoCropTransform(transforms.ToTensor()),
                                         download=True)
    elif opt.dataset == 'cifar100':
        dataset = datasets.CIFAR100(root=opt.data_folder,
                                          train=train,
                                          transform=TwoCropTransform(transforms.ToTensor()),
                                          download=True)
    elif opt.dataset == 'shapenet':
        dataset = ImageFolder(root=opt.data_folder, transform=transforms.ToTensor())
    else:
        raise ValueError(opt.dataset)

    data_loader = torch.utils.data.DataLoader(
        dataset, batch_size=opt.batch_size, shuffle=False,
        num_workers=opt.num_workers, pin_memory=True)

    return data_loader
コード例 #9
0
def set_loader(opt):
    # construct data loader
    # if opt.dataset == 'cifar10':
    #     mean = (0.4914, 0.4822, 0.4465)
    #     std = (0.2023, 0.1994, 0.2010)
    # elif opt.dataset == 'cifar100':
    #     mean = (0.5071, 0.4867, 0.4408)
    #     std = (0.2675, 0.2565, 0.2761)
    # # else:
    # #     raise ValueError('dataset not supported: {}'.format(opt.dataset))
    mean = [0.485, 0.456, 0.406]  # imagenet mean
    std = [0.229, 0.224, 0.225]  # imagenet std
    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=192, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)],
                               p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])
    train_transform = TwoCropTransform(train_transform)
    #
    # if opt.dataset == 'cifar10':
    #     train_dataset = datasets.CIFAR10(root=opt.data_folder,
    #                                      transform=TwoCropTransform(train_transform),
    #                                      download=True)
    # elif opt.dataset == 'cifar100':
    #     train_dataset = datasets.CIFAR100(root=opt.data_folder,
    #                                       transform=TwoCropTransform(train_transform),
    #                                       download=True)
    if opt.dataset == 'ali':
        train_dataset = Ali(train_transform)
    else:
        raise ValueError(opt.dataset)

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=opt.batch_size,
                                               shuffle=(train_sampler is None),
                                               num_workers=opt.num_workers,
                                               pin_memory=True,
                                               sampler=train_sampler)

    return train_loader
def set_loader(opt):
    # construct data loader

    # mean and std of imagenet dataset
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]

    operations = transforms.Compose([
        transforms.RandomHorizontalFlip(),
        transforms.RandomResizedCrop(size=224, scale=(0.2, 1)),
        transforms.ToTensor(),
        transforms.Normalize(mean=mean, std=std)
    ])

    transformed_dataset = PascalVOCDataset(
        csv_file='objects.csv', transform=TwoCropTransform(operations))

    train_loader = torch.utils.data.DataLoader(transformed_dataset,
                                               batch_size=4,
                                               shuffle=True,
                                               num_workers=2)

    return train_loader
コード例 #11
0
def set_loader(opt):
    # construct data loader
    if opt.dataset == 'cifar10':
        mean = (0.4914, 0.4822, 0.4465)
        std = (0.2023, 0.1994, 0.2010)
    elif opt.dataset == 'cifar100':
        mean = (0.5071, 0.4867, 0.4408)
        std = (0.2675, 0.2565, 0.2761)
    elif opt.dataset == 'path' or opt.dataset == 'cmnist' or opt.dataset == 'celeba':
        mean = eval(opt.mean)
        std = eval(opt.std)
    else:
        raise ValueError('dataset not supported: {}'.format(opt.dataset))
    normalize = transforms.Normalize(mean=mean, std=std)

    train_transform = transforms.Compose([
        transforms.RandomResizedCrop(size=opt.size, scale=(0.2, 1.)),
        transforms.RandomHorizontalFlip(),
        transforms.RandomApply([
            transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)
        ], p=0.8),
        transforms.RandomGrayscale(p=0.2),
        transforms.ToTensor(),
        normalize,
    ])

    if opt.dataset == 'cifar10':
        train_dataset = datasets.CIFAR10(root=opt.data_folder,
                                         transform=TwoCropTransform(
                                             train_transform),
                                         download=True)
    elif opt.dataset == 'cifar100':
        train_dataset = datasets.CIFAR100(root=opt.data_folder,
                                          transform=TwoCropTransform(
                                              train_transform),
                                          download=True)
    elif opt.dataset == 'path':
        train_dataset = datasets.ImageFolder(root=opt.data_folder,
                                             transform=TwoCropTransform(train_transform))
    elif opt.dataset == 'cmnist':
        train_dataset = CMNIST(
            opt.data_folder,
            train=True,
            variance=0.02,
            colorize='fg',
            # class_subset=[4, 9],
            same_distribution=True,
            transform=TwoCropTransform(train_transform))
    elif opt.dataset == 'celeba':
        train_dataset = CelebA(
            root=opt.data_folder,
            split='train',
            class_subset=['Arched_Eyebrows'],
            download=False,
            transform=TwoCropTransform(train_transform)
        )
    else:
        raise ValueError(opt.dataset)

    train_sampler = None
    train_loader = torch.utils.data.DataLoader(
        train_dataset, batch_size=opt.batch_size, shuffle=(
            train_sampler is None),
        num_workers=opt.num_workers, pin_memory=True, sampler=train_sampler)

    return train_loader