Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Code/deep_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def validate_manifest(manifest):


def pet_dataset(manifest, split, training=False, image_size=(64, 64)):
from PIL import Image
from PIL import Image, ImageOps
records = [r for r in manifest["records"] if r["split"] == split]
if not records:
raise ValueError(f"Empty split: {split}")
Expand All @@ -148,7 +148,7 @@ def pet_dataset(manifest, split, training=False, image_size=(64, 64)):
def decode(index):
record = records[int(index.numpy())]
with Image.open(root / record["path"]) as image:
pixels = np.asarray(image.convert("RGB").resize(
pixels = np.asarray(ImageOps.exif_transpose(image).convert("RGB").resize(
(width, height), resample=Image.Resampling.BILINEAR), dtype=np.float32) / 255.0
return pixels, np.float32(record["label"])

Expand Down
19 changes: 19 additions & 0 deletions tests/test_pet_splits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@

@unittest.skipUnless(importlib.util.find_spec("tensorflow"), "optional deep-learning dependencies")
class PetSplits(unittest.TestCase):
def test_pet_dataset_applies_exif_orientation(self):
from deep_utils import pet_dataset
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
pixels = np.zeros((8, 12, 3), dtype=np.uint8)
pixels[:, :6] = [255, 0, 0]
pixels[:, 6:] = [0, 0, 255]
orientation = Image.Exif()
orientation[274] = 6
Image.fromarray(pixels).save(root / "portrait.jpg", quality=100,
subsampling=0, exif=orientation)
manifest = {"root": str(root), "records": [
{"path": "portrait.jpg", "label": 0, "split": "train"}]}
actual = next(iter(pet_dataset(manifest, "train", image_size=(12, 8))))[0][0].numpy()
self.assertGreater(actual[1, 1, 0], 0.9) # red at the top
self.assertLess(actual[1, 1, 2], 0.1)
self.assertGreater(actual[-2, 1, 2], 0.9) # blue at the bottom
self.assertLess(actual[-2, 1, 0], 0.1)

def test_reproducible_disjoint_splits_and_bad_image_reporting(self):
from deep_utils import prepare_pets
with tempfile.TemporaryDirectory() as temporary:
Expand Down
Loading