MCPcopy Create free account
hub / github.com/CausalLearning/robust-unlearnable-examples / get_poisoned_loader

Function get_poisoned_loader

utils/generic.py:142–186  ·  view source on GitHub ↗
(
        dataset, batch_size, root='./data', train=True,
        noise_path=None, noise_rate=1.0, poisoned_indices_path=None, fitr=None)

Source from the content-addressed store, hash-verified

140
141
142def get_poisoned_loader(
143 dataset, batch_size, root='./data', train=True,
144 noise_path=None, noise_rate=1.0, poisoned_indices_path=None, fitr=None):
145
146 if dataset == 'imagenet' or dataset == 'imagenet-mini':
147 return imagenet_utils.get_poisoned_loader(
148 dataset, batch_size, root, train, noise_path, noise_rate, poisoned_indices_path, fitr)
149
150 target_set = get_dataset(dataset, root=root, train=train, fitr=fitr)
151
152 if noise_path is not None:
153 with open(noise_path, 'rb') as f:
154 raw_noise = pickle.load(f)
155
156 assert isinstance(raw_noise, np.ndarray)
157 assert raw_noise.dtype == np.int8
158
159 raw_noise = raw_noise.astype(np.int16)
160
161 noise = np.zeros_like(raw_noise)
162
163 if poisoned_indices_path is not None:
164 with open(poisoned_indices_path, 'rb') as f:
165 indices = pickle.load(f)
166 else:
167 indices = np.random.permutation(len(noise))[:int(len(noise)*noise_rate)]
168
169 noise[indices] += raw_noise[indices]
170
171 ''' restore noise (NCWH) for raw images (NHWC) '''
172 noise = np.transpose(noise, [0,2,3,1])
173
174 ''' add noise to images (uint8, 0~255) '''
175 imgs = target_set.x.astype(np.int16) + noise
176 imgs = imgs.clip(0,255).astype(np.uint8)
177 target_set.x = imgs
178
179 target_set = data.Dataset(x=target_set.x, y=target_set.y, transform=target_set.transform, fitr=target_set.fitr)
180
181 if train:
182 loader = data.Loader(target_set, batch_size=batch_size, shuffle=True, drop_last=True)
183 else:
184 loader = data.Loader(target_set, batch_size=batch_size, shuffle=False, drop_last=False)
185
186 return loader
187
188
189def get_clear_loader(

Callers

nothing calls this directly

Calls 1

get_datasetFunction · 0.70

Tested by

no test coverage detected