-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_aug.py
116 lines (103 loc) · 4.49 KB
/
data_aug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import copy
import torch
import numpy as np
from torchvision import transforms
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.dataloader import default_collate
from randaugment import *
data_stats = {'MNIST': ((0.1307,), (0.3081,)), 'fmnist': ((0.2860,), (0.3530,)),
'cifar10': ((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
'cifar100': ((0.5071, 0.4865, 0.4409), (0.2673, 0.2564, 0.2762)),
'SVHN': ((0.4377, 0.4438, 0.4728), (0.1980, 0.2010, 0.1970)),
'STL10': ((0.4409, 0.4279, 0.3868), (0.2683, 0.2610, 0.2687))}
def transform_pseudo_label(dataset):
if dataset in ['cifar10','cifar100']:
weak_trans = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
strong_trans = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
RandAugment(n=2, m=10, dataset=dataset),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
if dataset in ['SVHN']:
weak_trans = transforms.Compose([
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
strong_trans = transforms.Compose([
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
RandAugment(n=2, m=10, dataset=dataset),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
if dataset in ['fmnist']:
weak_trans = transforms.Compose([
transforms.Resize(32),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
strong_trans = transforms.Compose([
transforms.Resize(32),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
RandAugment(n=2, m=10, dataset=dataset),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
return weak_trans, strong_trans
class MixDataset(Dataset):
def __init__(self, size, dataset):
self.size = size
self.dataset = dataset
def __getitem__(self, index):
index = torch.randint(0, len(self.dataset), (1,)).item()
input = self.dataset[index]
input = {'data': input['data'], 'target': input['target']}
return input
def __len__(self):
return self.size
def transform_pseudo_label_fedConsis(dataset):
if dataset in ['cifar10','cifar100']:
weak_trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
strong_trans = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
RandAugment(n=2, m=10, dataset=dataset),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
if dataset in ['SVHN']:
weak_trans = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
strong_trans = transforms.Compose([
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
RandAugment(n=2, m=10, dataset=dataset),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
if dataset in ['fmnist']:
weak_trans = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
strong_trans = transforms.Compose([
transforms.Resize(32),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
RandAugment(n=2, m=10, dataset=dataset),
transforms.ToTensor(),
transforms.Normalize(*data_stats[dataset])
])
return weak_trans, strong_trans