Skip to content

Commit 9969ce5

Browse files
author
The TensorFlow Datasets Authors
committed
Merge pull request #10991 from SanjaySG:master
PiperOrigin-RevId: 720485890
2 parents b68aa45 + 80c4c48 commit 9969ce5

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

tensorflow_datasets/image_classification/cats_vs_dogs.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Cats vs Dogs dataset."""
1717

1818
import io
19+
import os
1920
import re
2021
import zipfile
2122

@@ -43,7 +44,7 @@
4344
_NUM_CORRUPT_IMAGES = 1738
4445
_DESCRIPTION = (
4546
"A large set of images of cats and dogs. "
46-
"There are %d corrupted images that are dropped." % _NUM_CORRUPT_IMAGES
47+
f"There are {_NUM_CORRUPT_IMAGES} corrupted images that are dropped."
4748
)
4849

4950
_NAME_RE = re.compile(r"^PetImages[\\/](Cat|Dog)[\\/]\d+\.jpg$")
@@ -94,7 +95,8 @@ def _generate_examples(self, archive):
9495
"""Generate Cats vs Dogs images and labels given a directory path."""
9596
num_skipped = 0
9697
for fname, fobj in archive:
97-
res = _NAME_RE.match(fname)
98+
norm_fname = os.path.normpath(fname)
99+
res = _NAME_RE.match(norm_fname)
98100
if not res: # README file, ...
99101
continue
100102
label = res.group(1).lower()
@@ -113,19 +115,19 @@ def _generate_examples(self, archive):
113115
# Converting the recoded image back into a zip file container.
114116
buffer = io.BytesIO()
115117
with zipfile.ZipFile(buffer, "w") as new_zip:
116-
new_zip.writestr(fname, img_recoded.numpy())
117-
new_fobj = zipfile.ZipFile(buffer).open(fname)
118+
new_zip.writestr(norm_fname, img_recoded.numpy())
119+
new_fobj = zipfile.ZipFile(buffer).open(norm_fname)
118120

119121
record = {
120122
"image": new_fobj,
121-
"image/filename": fname,
123+
"image/filename": norm_fname,
122124
"label": label,
123125
}
124-
yield fname, record
126+
yield norm_fname, record
125127

126128
if num_skipped != _NUM_CORRUPT_IMAGES:
127129
raise ValueError(
128-
"Expected %d corrupt images, but found %d"
129-
% (_NUM_CORRUPT_IMAGES, num_skipped)
130+
f"Expected {_NUM_CORRUPT_IMAGES} corrupt images, but found"
131+
f" {num_skipped}."
130132
)
131133
logging.warning("%d images were corrupted and were skipped", num_skipped)

0 commit comments

Comments
 (0)