-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add Rendered sst2 dataset #5220
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
31fadbe
Adding multiweight support for shufflenetv2 prototype models
jdsgomes 1e578b7
Revert "Adding multiweight support for shufflenetv2 prototype models"
jdsgomes 85e4429
Merge branch 'pytorch:main' into main
jdsgomes 4e3d900
Adding multiweight support for shufflenetv2 prototype models
jdsgomes 615b612
Revert "Adding multiweight support for shufflenetv2 prototype models"
jdsgomes a0bbece
Merge branch 'pytorch:main' into main
jdsgomes ba966f4
Merge branch 'pytorch:main' into main
jdsgomes 6cdd49b
Merge branch 'pytorch:main' into main
jdsgomes d4f1638
Merge branch 'pytorch:main' into main
jdsgomes 069bba4
Add RenderedSST2 dataset
jdsgomes 409dcad
Merge branch 'main' into rendered-sst2-dataset
jdsgomes 78f5e45
Address PR comments
jdsgomes e6c95ad
Fix bug in dataset verification
jdsgomes 39b2441
Merge branch 'main' into rendered-sst2-dataset
NicolasHug 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,90 @@ | ||
from pathlib import Path | ||
from typing import Any, Tuple, Callable, Optional | ||
|
||
import PIL.Image | ||
|
||
from .utils import verify_str_arg, download_and_extract_archive | ||
from .vision import VisionDataset | ||
|
||
|
||
class RenderedSST2(VisionDataset): | ||
"""`The Rendered SST2 Dataset <https://github.com/openai/CLIP/blob/main/data/rendered-sst2.md>`_. | ||
|
||
Rendered SST2 is an image classification dataset used to evaluate the models capability on optical | ||
character recognition. This dataset was generated by rendering sentences in the Standford Sentiment | ||
Treebank v2 dataset. | ||
|
||
This dataset contains two classes (positive and negative) and is divided in three splits: a train | ||
split containing 6920 images (3610 positive and 3310 negative), a validation split containing 872 images | ||
(444 positive and 428 negative), and a test split containing 1821 images (909 positive and 912 negative). | ||
|
||
Args: | ||
root (string): Root directory of the dataset. | ||
split (string, optional): The dataset split, supports ``"train"`` (default), `"val"` and ``"test"``. | ||
download (bool, optional): If True, downloads the dataset from the internet and | ||
puts it in root directory. If dataset is already downloaded, it is not | ||
downloaded again. Default is False. | ||
transform (callable, optional): A function/transform that takes in an PIL image and returns a transformed | ||
version. E.g, ``transforms.RandomCrop``. | ||
target_transform (callable, optional): A function/transform that takes in the target and transforms it. | ||
""" | ||
|
||
_URL = "https://openaipublic.azureedge.net/clip/data/rendered-sst2.tgz" | ||
_MD5 = "2384d08e9dcfa4bd55b324e610496ee5" | ||
|
||
def __init__( | ||
self, | ||
root: str, | ||
split: str = "train", | ||
download: bool = False, | ||
transform: Optional[Callable] = None, | ||
target_transform: Optional[Callable] = None, | ||
) -> None: | ||
super().__init__(root, transform=transform, target_transform=target_transform) | ||
self._split = verify_str_arg(split, "split", ("train", "val", "test")) | ||
self._split_to_folder = {"train": "train", "val": "valid", "test": "test"} | ||
self._base_folder = Path(self.root) / "rendered-sst2" | ||
self.classes = ["negative", "positive"] | ||
self.class_to_idx = {"negative": 0, "positive": 1} | ||
|
||
if download: | ||
self._download() | ||
|
||
if not self._check_exists(): | ||
raise RuntimeError("Dataset not found. You can use download=True to download it") | ||
|
||
self._labels = [] | ||
self._image_files = [] | ||
|
||
for p in (self._base_folder / self._split_to_folder[self._split]).glob("**/*.png"): | ||
self._labels.append(self.class_to_idx[p.parent.name]) | ||
self._image_files.append(p) | ||
|
||
def __len__(self) -> int: | ||
return len(self._image_files) | ||
|
||
def __getitem__(self, idx) -> Tuple[Any, Any]: | ||
image_file, label = self._image_files[idx], self._labels[idx] | ||
image = PIL.Image.open(image_file).convert("RGB") | ||
|
||
if self.transform: | ||
image = self.transform(image) | ||
|
||
if self.target_transform: | ||
label = self.target_transform(label) | ||
|
||
return image, label | ||
|
||
def extra_repr(self) -> str: | ||
return f"split={self._split}" | ||
|
||
def _check_exists(self) -> bool: | ||
for class_label in set(self.classes): | ||
if not (self._base_folder / self._split_to_folder[self._split] / class_label).is_dir(): | ||
return False | ||
return True | ||
|
||
def _download(self) -> None: | ||
if self._check_exists(): | ||
return | ||
download_and_extract_archive(self._URL, download_root=self.root, md5=self._MD5) |
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.
Uh oh!
There was an error while loading. Please reload this page.