forked from AberHu/Knowledge-Distillation-Zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
127 lines (100 loc) · 4.01 KB
/
dataset.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
116
117
118
119
120
121
122
123
124
125
126
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import numpy as np
from PIL import Image
import torchvision.datasets as dst
'''
Modified from https://github.com/HobbitLong/RepDistiller/blob/master/dataset/cifar100.py
'''
class CIFAR10IdxSample(dst.CIFAR10):
def __init__(self, root, train=True,
transform=None, target_transform=None,
download=False, n=4096, mode='exact', percent=1.0):
super().__init__(root=root, train=train, download=download,
transform=transform, target_transform=target_transform)
self.n = n
self.mode = mode
num_classes = 10
num_samples = len(self.data)
labels = self.targets
self.cls_positive = [[] for _ in range(num_classes)]
for i in range(num_samples):
self.cls_positive[labels[i]].append(i)
self.cls_negative = [[] for _ in range(num_classes)]
for i in range(num_classes):
for j in range(num_classes):
if j == i:
continue
self.cls_negative[i].extend(self.cls_positive[j])
self.cls_positive = [np.asarray(self.cls_positive[i]) for i in range(num_classes)]
self.cls_negative = [np.asarray(self.cls_negative[i]) for i in range(num_classes)]
if 0 < percent < 1:
num = int(len(self.cls_negative[0]) * percent)
self.cls_negative = [np.random.permutation(self.cls_negative[i])[0:num]
for i in range(num_classes)]
self.cls_positive = np.asarray(self.cls_positive)
self.cls_negative = np.asarray(self.cls_negative)
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
if self.mode == 'exact':
pos_idx = index
elif self.mode == 'relax':
pos_idx = np.random.choice(self.cls_positive[target], 1)[0]
else:
raise NotImplementedError(self.mode)
replace = True if self.n > len(self.cls_negative[target]) else False
neg_idx = np.random.choice(self.cls_negative[target], self.n, replace=replace)
sample_idx = np.hstack((np.asarray([pos_idx]), neg_idx))
return img, target, index, sample_idx
class CIFAR100IdxSample(dst.CIFAR100):
def __init__(self, root, train=True,
transform=None, target_transform=None,
download=False, n=4096, mode='exact', percent=1.0):
super().__init__(root=root, train=train, download=download,
transform=transform, target_transform=target_transform)
self.n = n
self.mode = mode
num_classes = 100
num_samples = len(self.data)
labels = self.targets
self.cls_positive = [[] for _ in range(num_classes)]
for i in range(num_samples):
self.cls_positive[labels[i]].append(i)
self.cls_negative = [[] for _ in range(num_classes)]
for i in range(num_classes):
for j in range(num_classes):
if j == i:
continue
self.cls_negative[i].extend(self.cls_positive[j])
self.cls_positive = [np.asarray(self.cls_positive[i]) for i in range(num_classes)]
self.cls_negative = [np.asarray(self.cls_negative[i]) for i in range(num_classes)]
if 0 < percent < 1:
num = int(len(self.cls_negative[0]) * percent)
self.cls_negative = [np.random.permutation(self.cls_negative[i])[0:num]
for i in range(num_classes)]
self.cls_positive = np.asarray(self.cls_positive)
self.cls_negative = np.asarray(self.cls_negative)
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
if self.mode == 'exact':
pos_idx = index
elif self.mode == 'relax':
pos_idx = np.random.choice(self.cls_positive[target], 1)[0]
else:
raise NotImplementedError(self.mode)
replace = True if self.n > len(self.cls_negative[target]) else False
neg_idx = np.random.choice(self.cls_negative[target], self.n, replace=replace)
sample_idx = np.hstack((np.asarray([pos_idx]), neg_idx))
return img, target, index, sample_idx