14
14
# limitations under the License.
15
15
16
16
"""Oxford-IIIT pet dataset."""
17
-
17
+ import io
18
18
import os
19
19
import xml .etree .ElementTree as ET
20
20
@@ -119,7 +119,7 @@ class Builder(tfds.core.GeneratorBasedBuilder):
119
119
def _info (self ):
120
120
return self .dataset_info_from_configs (
121
121
features = tfds .features .FeaturesDict ({
122
- "image" : tfds .features .Image (),
122
+ "image" : tfds .features .Image (encoding_format = "jpeg" ),
123
123
"label" : tfds .features .ClassLabel (names = _LABEL_CLASSES ),
124
124
"species" : tfds .features .ClassLabel (names = _SPECIES_CLASSES ),
125
125
"file_name" : tfds .features .Text (),
@@ -175,21 +175,19 @@ def _generate_examples(
175
175
image_name , label , species , _ = line .strip ().split (" " )
176
176
177
177
image_path = os .path .join (images_dir_path , image_name + ".jpg" )
178
+ with epath .Path (image_path ).open ("rb" ) as image_file :
179
+ img_data = image_file .read ()
178
180
179
181
if image_name in _CORRUPT_SAMPLES :
180
182
# some images caused 'Corrupt JPEG data...' messages during training
181
183
# or any other iteration recoding them once fixes the issue
182
184
# (discussion: https://github.com/tensorflow/datasets/issues/2188)
183
- with epath .Path (image_path ).open ("rb" ) as image_file :
184
- img_data = image_file .read ()
185
- img_tensor = tf .image .decode_image (img_data )
186
- if (
187
- tf .shape (img_tensor )[- 1 ] == 4
188
- ): # some files have an alpha channel -> remove
189
- img_tensor = img_tensor [:, :, :- 1 ]
190
- img_recoded = tf .io .encode_jpeg (img_tensor )
191
- with epath .Path (image_path ).open ("wb" ) as image_file :
192
- image_file .write (img_recoded .numpy ())
185
+ img_tensor = tf .image .decode_image (img_data )
186
+ if (
187
+ tf .shape (img_tensor )[- 1 ] == 4
188
+ ): # some files have an alpha channel -> remove
189
+ img_tensor = img_tensor [:, :, :- 1 ]
190
+ img_data = tf .io .encode_jpeg (img_tensor ).numpy ()
193
191
194
192
trimaps_dir_path = os .path .join (annotations_dir_path , "trimaps" )
195
193
xmls_dir_path = os .path .join (annotations_dir_path , "xmls" )
@@ -207,7 +205,7 @@ def _generate_examples(
207
205
head_bbox = _EMPTY_BBOX
208
206
209
207
record = {
210
- "image" : os . path . join ( images_dir_path , image_name ),
208
+ "image" : io . BytesIO ( img_data ),
211
209
"label" : int (label ),
212
210
"species" : species ,
213
211
"file_name" : image_name ,
0 commit comments