Пример #1
0
    def __init__(self, dataset, data, labels, is_train=True):
        super(Cifar, self).__init__()
        self.data, self.labels = data, labels
        self.is_train = is_train
        assert len(self.data) == len(self.labels)
        if dataset == 'CIFAR10':
            mean, std = (0.4914, 0.4822, 0.4465), (0.2471, 0.2435, 0.2616)
        elif dataset == 'CIFAR100':
            mean, std = (0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)

        if is_train:
            self.trans_weak = T.Compose([
                T.Resize((32, 32)),
                T.PadandRandomCrop(border=4, cropsize=(32, 32)),
                T.RandomHorizontalFlip(p=0.5),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
            self.trans_strong = T.Compose([
                T.Resize((32, 32)),
                T.PadandRandomCrop(border=4, cropsize=(32, 32)),
                T.RandomHorizontalFlip(p=0.5),
                RandomAugment(2, 10),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
        else:
            self.trans = T.Compose([
                T.Resize((32, 32)),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
Пример #2
0
    def __init__(self, im_folder, label_file, is_train=True):
        super(DeepWeeds, self).__init__(im_folder, label_file)
        self.is_train = is_train
        mean, std = (0.485, 0.456, 0.406), (0.229, 0.224, 0.225)

        if is_train:
            self.trans_weak = T.Compose([
                T.Resize((224, 224)),
                T.PadandRandomCrop(border=4, cropsize=(224, 224)),
                T.RandomHorizontalFlip(p=0.5),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
            self.trans_strong = T.Compose([
                T.Resize((224, 224)),
                T.PadandRandomCrop(border=4, cropsize=(224, 224)),
                T.RandomHorizontalFlip(p=0.5),
                RandomAugment(2, 10),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
        else:
            self.trans = T.Compose([
                T.Resize((224, 224)),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
Пример #3
0
    def __init__(self,
                 dataset,
                 data,
                 labels,
                 is_train=True,
                 use_fixmatch_transform=True):
        super(Cifar, self).__init__()
        self.data, self.labels = data, labels
        self.is_train = is_train
        self.use_fixmatch_transform = use_fixmatch_transform
        s = 1
        color_jitter = torchvision.transforms.ColorJitter(
            0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s)
        assert len(self.data) == len(self.labels)
        if dataset == 'CIFAR10':
            mean, std = (0.4914, 0.4822, 0.4465), (0.2471, 0.2435, 0.2616)
        elif dataset == 'CIFAR100':
            mean, std = (0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)

        if is_train:
            self.trans_weak = T.Compose([
                T.Resize((32, 32)),
                T.PadandRandomCrop(border=4, cropsize=(32, 32)),
                T.RandomHorizontalFlip(p=0.5),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
            if use_fixmatch_transform == 0:
                # usa transformaciones de fixmatch
                self.trans_strong = T.Compose([
                    T.Resize((32, 32)),
                    T.PadandRandomCrop(border=4, cropsize=(32, 32)),
                    T.RandomHorizontalFlip(p=0.5),
                    RandomAugment(2, 10),
                    T.Normalize(mean, std),
                    T.ToTensor(),
                ])
            else:
                # usar transformaciones de simclr
                self.trans_strong = torchvision.transforms.Compose([
                    torchvision.transforms.RandomResizedCrop(
                        size=32),  # hardcoded 32 as the other transformation
                    torchvision.transforms.RandomHorizontalFlip(
                    ),  # with 0.5 probability
                    torchvision.transforms.RandomApply([color_jitter], p=0.8),
                    torchvision.transforms.RandomGrayscale(p=0.2),
                    torchvision.transforms.ToTensor(),
                ])

        else:
            self.trans = T.Compose([
                T.Resize((32, 32)),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
Пример #4
0
    def __init__(self, dataset, data, labels, mode):
        super(Cifar, self).__init__()
        self.data, self.labels = data, labels
        self.mode = mode
        assert len(self.data) == len(self.labels)
        if dataset == 'CIFAR10':
            mean, std = (0.4914, 0.4822, 0.4465), (0.2471, 0.2435, 0.2616)
        elif dataset == 'CIFAR100':
            mean, std = (0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)

        trans_weak = T.Compose([
            T.Resize((32, 32)),
            T.PadandRandomCrop(border=4, cropsize=(32, 32)),
            T.RandomHorizontalFlip(p=0.5),
            T.Normalize(mean, std),
            T.ToTensor(),
        ])
        trans_strong0 = T.Compose([
            T.Resize((32, 32)),
            T.PadandRandomCrop(border=4, cropsize=(32, 32)),
            T.RandomHorizontalFlip(p=0.5),
            RandomAugment(2, 10),
            T.Normalize(mean, std),
            T.ToTensor(),
        ])        
        trans_strong1 = transforms.Compose([
            transforms.ToPILImage(),
            transforms.RandomResizedCrop(32, scale=(0.2, 1.)),     
            transforms.RandomHorizontalFlip(p=0.5),
            transforms.RandomApply([
                transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)  
            ], p=0.8),
            transforms.RandomGrayscale(p=0.2),        
            transforms.ToTensor(),
            transforms.Normalize(mean, std),
        ])                    
        if self.mode == 'train_x':
            self.trans = trans_weak
        elif self.mode == 'train_u_comatch':
            self.trans = ThreeCropsTransform(trans_weak, trans_strong0, trans_strong1)               
        elif self.mode == 'train_u_fixmatch':
            self.trans = TwoCropsTransform(trans_weak, trans_strong0)    
        else:  
            self.trans = T.Compose([
                T.Resize((32, 32)),
                T.Normalize(mean, std),
                T.ToTensor(),
            ])
Пример #5
0
    def __init__(self, dataset_path, type, size, cropsize, classes, labelled_data=None, mean=None, std=None):
        assert type in ['labelled', 'unlabelled', 'validation','test']
        super().__init__()
        self.dataset_path = dataset_path
        self.type = type
        self.size = size
        self.cropsize = cropsize
        self.classes= classes
        self.n_classes = len(classes)
        self.label_dict= {k:v for v,k in enumerate(classes)}
        self.inverse_label_dict = {v:k for v,k in enumerate(classes)}

        if mean is None or std is None:
            self.mean, self.std = 0., 1.
        else:
            self.mean = mean
            self.std = std

        if type in ['labelled', 'unlabelled']:
            assert labelled_data is not None
            if type =='labelled':
                self.data, self.label = self._load_data(labelled_data)
            elif type=='unlabelled':
                self.data = self._load_data(labelled_data)
            # common for both labelled and unlabelled
            self.trans_weak = T.Compose([
                TTV.Resize(self.size),
                TTV.Pad(padding=4, padding_mode='reflect'),
                TTV.RandomCrop(self.cropsize),
                TTV.RandomHorizontalFlip(p=0.5),
                TTV.Lambda(lambda x: np.array(x)),
                T.Normalize(self.mean, self.std),
                T.ToTensor(),
            ])
            self.trans_strong = T.Compose([
                TTV.Resize(self.size),
                TTV.Pad(padding=4, padding_mode='reflect'),
                TTV.RandomCrop(self.cropsize),
                TTV.RandomHorizontalFlip(p=0.5),
                TTV.Lambda(lambda x: np.array(x)),
                RandomAugment(2, 10),
                T.Normalize(self.mean, self.std),
                T.ToTensor(),
            ])
        elif type=='validation':
            assert labelled_data is not None
            self.data, self.label = self._load_data(labelled_data)
            self.trans = T.Compose([
                TTV.Resize(self.size),
                TTV.CenterCrop(self.cropsize),
                TTV.Lambda(lambda x: np.array(x)),
                T.Normalize(self.mean, self.std),
                T.ToTensor(),
            ])

        elif type=='test':
            self.data = self._load_data()
            self.trans = T.Compose([
                TTV.Resize(self.size),
                TTV.CenterCrop(self.cropsize),
                TTV.Lambda(lambda x: np.array(x)),
                T.Normalize(self.mean, self.std),
                T.ToTensor(),
            ])