-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Port SBU dataset #5683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lezwon
wants to merge
12
commits into
pytorch:main
Choose a base branch
from
lezwon:5349_sbu_dataset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Port SBU dataset #5683
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
591a917
added sbu dataset to prototype
lezwon cc40b08
handle missing files
lezwon 0bf01b3
create sbu dataset
lezwon 3479fff
added tests
lezwon aab8dbb
Apply suggestions from code review
lezwon 5729c3f
handle missing images
lezwon 36c3af2
fixed type check
lezwon c04b56c
fixed type check in args
lezwon c1518ab
fix failing unit test
lezwon dae1727
Apply suggestions from code review
lezwon 91bd94f
imported warnings
lezwon 083b3ee
Apply suggestions from code review
lezwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import pathlib | ||
import warnings | ||
from typing import List, Any, Dict, Optional, Tuple, BinaryIO | ||
|
||
from torch.utils.model_zoo import tqdm | ||
from torchdata.datapipes.iter import IterDataPipe, Demultiplexer, LineReader, Zipper, Mapper, IterKeyZipper | ||
from torchvision.prototype.datasets.utils import Dataset, DatasetInfo, DatasetConfig, OnlineResource, HttpResource | ||
from torchvision.prototype.datasets.utils._internal import ( | ||
hint_sharding, | ||
hint_shuffling, | ||
INFINITE_BUFFER_SIZE, | ||
path_accessor, | ||
) | ||
from torchvision.prototype.features import EncodedImage | ||
|
||
|
||
class SBU(Dataset): | ||
|
||
def _make_info(self) -> DatasetInfo: | ||
return DatasetInfo( | ||
name="sbu", | ||
homepage="http://www.cs.virginia.edu/~vicente/sbucaptions/", | ||
) | ||
|
||
def _preprocess(self, path: pathlib.Path) -> pathlib.Path: | ||
folder = OnlineResource._extract(path) | ||
data_folder = folder / "dataset" | ||
image_folder = data_folder / "images" | ||
image_folder.mkdir() | ||
broken_urls = [] | ||
with open(data_folder / "SBU_captioned_photo_dataset_urls.txt") as fh: | ||
urls = fh.read().splitlines() | ||
|
||
# TODO: Use workers to download images | ||
for url in tqdm(urls): | ||
try: | ||
# TODO: suppress print statements within HttpResource.download() | ||
HttpResource(url).download(image_folder) | ||
except Exception: | ||
broken_urls.append(url) | ||
|
||
if broken_urls: | ||
broken_urls_file = folder.parent / "broken_urls.txt" | ||
warnings.warn( | ||
f"Failed to download {len(broken_urls)} ({len(broken_urls) / len(urls):.2%}) images. " | ||
f"They are logged in {broken_urls_file}." | ||
) | ||
with open(broken_urls_file, "w") as fh: | ||
fh.write("\n".join(broken_urls) + "\n") | ||
|
||
return folder | ||
|
||
def resources(self, config: DatasetConfig) -> List[OnlineResource]: | ||
return [ | ||
HttpResource( | ||
"http://www.cs.virginia.edu/~vicente/sbucaptions/SBUCaptionedPhotoDataset.tar.gz", | ||
sha256="2bf37d5e1c9e1c6eae7d5103030d58a7f2117fc5e8c6aa9620f0df165acebf09", | ||
preprocess=self._preprocess, | ||
) | ||
] | ||
|
||
def _classify_files(self, data: Tuple[str, Any]) -> Optional[int]: | ||
path = pathlib.Path(data[0]) | ||
if path.parent.name == "images": | ||
return 0 | ||
elif path.name == "SBU_captioned_photo_dataset_urls.txt": | ||
return 1 | ||
elif path.name == "SBU_captioned_photo_dataset_captions.txt": | ||
return 2 | ||
else: | ||
return None | ||
|
||
def _make_datapipe( | ||
self, | ||
resource_dps: List[IterDataPipe], | ||
*, | ||
config: DatasetConfig, | ||
) -> IterDataPipe[Dict[str, Any]]: | ||
|
||
images_dp, urls_dp, captions_dp = Demultiplexer( | ||
resource_dps[0], 3, self._classify_files, drop_none=True, buffer_size=INFINITE_BUFFER_SIZE | ||
) | ||
|
||
images_dp = hint_shuffling(images_dp) | ||
images_dp = hint_sharding(images_dp) | ||
|
||
urls_dp = LineReader(urls_dp, decode=True, return_path=False) | ||
captions_dp = LineReader(captions_dp, decode=True, return_path=False) | ||
anns_dp = Zipper(urls_dp, captions_dp) | ||
|
||
dp = IterKeyZipper(images_dp, anns_dp, path_accessor("name"), buffer_size=INFINITE_BUFFER_SIZE) | ||
return Mapper(dp, self._prepare_sample) | ||
|
||
def _prepare_sample(self, data: Tuple[Tuple[str, BinaryIO], Tuple[str, str]]) -> Dict[str, Any]: | ||
(path, buffer), (_, caption) = data | ||
return dict( | ||
path=path, | ||
image=EncodedImage.from_file(buffer), | ||
caption=caption.strip(), | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really cool idea and I'm definitely going to use this webiste for other things in the future 🚀 Unfortunately, we cannot have an actual download during mock data generation for two reasons:
I propose I send a patch for the test suite that allows us to also only generate the already preprocessed files. Thus, we only add a
SBUCaptionedPhotoDataset
that already includes test images. I'll ping you on the PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #5706.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pmeier I'll wait for the PR to get merged right? I can make the necessary changes after it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry for the delay. I'll try to get it merged soon.