def __getitem__(self, idx): image_path = self._image_paths[idx] mask_path = self._mask_paths[idx] image = Image.open(image_path) image = np.array(image).astype(np.uint8) mask = np.array(Image.open(mask_path)).astype(np.uint8) if self.category_type == BinaryCategory: mask[mask == 1] = 1 mask[mask != 1] = 0 else: raise NotImplementedError data = {'image': image, 'mask': mask} augmented = self._transform(**data) image, mask = augmented['image'], augmented['mask'] # Imagenet params image = normalize( image, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), ) X = to_tensor(image) if self.category_type == BinaryCategory: Y = torch.from_numpy(mask).float().unsqueeze(0) else: #Y = torch.from_numpy(mask).long() raise NotImplementedError return dict( X=X, Y=Y, image_path=str(image_path), mask_path=str(mask_path), )
def apply(self, image, **params): return F.normalize(image, self.mean, self.std, self.max_pixel_value)
def test_normalize_float(): img = np.ones((100, 100, 3), dtype=np.float32) * 0.4 normalized = F.normalize(img, mean=50, std=3, max_pixel_value=1.0) expected = (np.ones((100, 100, 3), dtype=np.float32) * 0.4 - 50) / 3 assert_array_almost_equal_nulp(normalized, expected)
def test_normalize(): img = np.ones((100, 100, 3), dtype=np.uint8) * 127 normalized = F.normalize(img, mean=50, std=3) expected = (np.ones((100, 100, 3), dtype=np.float32) * 127 / 255 - 50) / 3 assert_array_almost_equal_nulp(normalized, expected)
def apply(self, image, **params): image = image[:, :, [2, 1, 0]] * 255. return af.normalize(image, self.mean, self.std, self.max_pixel_value)
def test_normalize(): img = np.ones((100, 100, 3), dtype=np.uint8) * 127 normalized = F.normalize(img, mean=50, std=3) expected = (np.ones((100, 100, 3), dtype=np.float32) * 127 / 255 - 50) / 3 assert np.allclose(normalized, expected)
def preprocessing_fn(x): return to_tensor(normalize(x, MEAN, STD, max_pixel_value=1.0))
def norm(img, **params): return F.normalize(img, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_pixel_value=255.0)
def apply(self, image, **params): mean = image.mean(axis=(0, 1), keepdims=True) std = image.mean(axis=(0, 1), keepdims=True) std[std == 0] = 1 return F.normalize(image, mean, std, 1)
def imagenet_normalize(images: np.array): return normalize( images, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), )
def test_normalize(): img = np.ones((100, 100, 3), dtype=np.uint8) * 127 normalized = F.normalize(img, mean=50, std=3) expected = (np.ones((100, 100, 3), dtype=np.float32) * 127 / 255 - 50) / 3 assert np.allclose(normalized, expected)