16
16
"""Cats vs Dogs dataset."""
17
17
18
18
import io
19
+ import os
19
20
import re
20
21
import zipfile
21
22
43
44
_NUM_CORRUPT_IMAGES = 1738
44
45
_DESCRIPTION = (
45
46
"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."
47
48
)
48
49
49
50
_NAME_RE = re .compile (r"^PetImages[\\/](Cat|Dog)[\\/]\d+\.jpg$" )
@@ -94,7 +95,8 @@ def _generate_examples(self, archive):
94
95
"""Generate Cats vs Dogs images and labels given a directory path."""
95
96
num_skipped = 0
96
97
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 )
98
100
if not res : # README file, ...
99
101
continue
100
102
label = res .group (1 ).lower ()
@@ -113,19 +115,19 @@ def _generate_examples(self, archive):
113
115
# Converting the recoded image back into a zip file container.
114
116
buffer = io .BytesIO ()
115
117
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 )
118
120
119
121
record = {
120
122
"image" : new_fobj ,
121
- "image/filename" : fname ,
123
+ "image/filename" : norm_fname ,
122
124
"label" : label ,
123
125
}
124
- yield fname , record
126
+ yield norm_fname , record
125
127
126
128
if num_skipped != _NUM_CORRUPT_IMAGES :
127
129
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 } ."
130
132
)
131
133
logging .warning ("%d images were corrupted and were skipped" , num_skipped )
0 commit comments