Skip to content

Commit

Permalink
catch and skip images that fail to load (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
heyalexchoi authored Jan 9, 2024
1 parent b469e27 commit 37e482b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions clip_retrieval/clip_inference/reader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Reader module provides files and webdataset readers"""

from pathlib import Path
from PIL import Image
from PIL import Image, UnidentifiedImageError
from torch.utils.data import DataLoader
from torch.utils.data.dataloader import default_collate
import io
Expand Down Expand Up @@ -94,7 +94,11 @@ def __getitem__(self, ind):

if self.enable_image:
image_file = self.image_files[key]
image_tensor = self.image_transform(Image.open(image_file))
try:
image_tensor = self.image_transform(Image.open(image_file))
except (UnidentifiedImageError, OSError) as e:
print(f"Failed to load image {image_file}. Error: {e}. Skipping.")
return None # return None to be filtered in the batch collate_fn
output["image_filename"] = str(image_file)
output["image_tensor"] = image_tensor

Expand Down

0 comments on commit 37e482b

Please sign in to comment.