Функция нормализации зрения факела не работает должным образом. Нормализованное изображение имеет только 0, 1 и -1. Вот код:
class Cifar10(Dataset):
def __init__(self, cropped, ground_truth, transform=None):
"""
Args:
cropped (string): File location of cropped numpy
ground_truth (string): File location of ground_truth numpy
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.transform = transform
self.noisy = np.load(cropped)[0:8]
self.ground_truth = np.load(ground_truth)[0:8]
self.data = np.array([self.noisy, self.ground_truth])
def __len__(self):
return len(self.ground_truth)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
noisy = self.noisy[idx].transpose(2, 0, 1)
ground_truth = self.ground_truth[idx].transpose(2, 0, 1)
sample = {'noisy': noisy, 'ground_truth': ground_truth}
if self.transform:
sample = self.transform(sample)
return sample
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
noisy, ground_truth = sample['noisy'], sample['ground_truth']
return {'noisy': torch.from_numpy(noisy),
'ground_truth': torch.from_numpy(ground_truth)}
class Normalize(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample, transform_=transforms.Normalize((127.5, 127.5, 127.5), (127.5, 127.5, 127.5))):
noisy, ground_truth = transform_(sample['noisy']), transform_(sample['ground_truth'])
return {'noisy': noisy,
'ground_truth': ground_truth}
if __name__ == '__main__':
compose = transforms.Compose(
[ToTensor(),
Normalize()
])
transformed_dataset = Cifar10(cropped=root + 'cropped.npy',
ground_truth=root + 'original.npy',
transform=compose)
dataloader = DataLoader(transformed_dataset, batch_size=1,
shuffle=True)
for x in dataloader:
pass
print(x['noisy'])
print(x['ground_truth'])
print(torch.unique(x['noisy']))
Вот часть вывода:
tensor([[[[-1, -1, 0, ..., 0, -1, 0],
[-1, -1, 0, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, 0, -1],
...,
[-1, 0, 0, ..., -1, -1, 0],
[-1, 0, -1, ..., -1, -1, 0],
[ 0, -1, -1, ..., 0, -1, -1]],
[[-1, -1, 0, ..., 0, -1, 0],
[-1, -1, 0, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, 0, -1],
...,
[-1, 0, 0, ..., -1, -1, 0],
[-1, 0, -1, ..., -1, -1, 0],
[ 0, -1, -1, ..., 0, -1, -1]],
[[-1, -1, 0, ..., 0, -1, 0],
[-1, -1, 0, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, 0, -1],
...,
[-1, 0, 0, ..., -1, -1, 0],
[-1, 0, -1, ..., -1, -1, 0],
[ 0, -1, -1, ..., 0, -1, -1]]]])
tensor([[[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]],
[[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]]]])
tensor([-1, 0, 1])
Я попытался распечатать данные, не применяя преобразование Normalize, и пиксели присутствуют в диапазоне ( 0, 255), тогда как нормализованное изображение имеет только 3 уникальных значения - 0, 1 и -1