Skip to content
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

Migrate Semeion prototype dataset #5761

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/builtin_dataset_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,10 @@ def sbd(info, root, config):
return SBDMockData.generate(root)[config.split]


# @register_mock
def semeion(info, root, config):
@register_mock(configs=[dict()])
def semeion(root, config):
num_samples = 3
num_categories = len(info.categories)
num_categories = 10

images = torch.rand(num_samples, 256)
labels = one_hot(torch.randint(num_categories, size=(num_samples,)), num_classes=num_categories)
Expand Down
50 changes: 30 additions & 20 deletions torchvision/prototype/datasets/_builtin/semeion.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
from typing import Any, Dict, List, Tuple
import pathlib
from typing import Any, Dict, List, Tuple, Union

import torch
from pytest import skip
from torchdata.datapipes.iter import (
IterDataPipe,
Mapper,
CSVParser,
)
from torchvision.prototype.datasets.utils import (
Dataset,
DatasetConfig,
DatasetInfo,
Dataset2,
HttpResource,
OnlineResource,
)
from torchvision.prototype.datasets.utils._internal import hint_sharding, hint_shuffling
from torchvision.prototype.features import Image, OneHotLabel

from .._api import register_dataset, register_info

NAME = "semeion"


@register_info(NAME)
def _info() -> Dict[str, Any]:
return dict(categories=[str(i) for i in range(10)])

class SEMEION(Dataset):
def _make_info(self) -> DatasetInfo:
return DatasetInfo(
"semeion",
categories=10,
homepage="https://archive.ics.uci.edu/ml/datasets/Semeion+Handwritten+Digit",
)

def resources(self, config: DatasetConfig) -> List[OnlineResource]:
@register_dataset(NAME)
class SEMEION(Dataset2):
"""Semeion dataset
homepage="https://archive.ics.uci.edu/ml/datasets/Semeion+Handwritten+Digit",
"""

def __init__(self, root: Union[str, pathlib.Path], *, skip_integrity_check: bool = False) -> None:

self._categories = _info()["categories"]
super().__init__(root, skip_integrity_check=skip_integrity_check)

def _resources(self) -> List[OnlineResource]:
data = HttpResource(
"http://archive.ics.uci.edu/ml/machine-learning-databases/semeion/semeion.data",
sha256="f43228ae3da5ea6a3c95069d53450b86166770e3b719dcc333182128fe08d4b1",
Expand All @@ -36,18 +48,16 @@ def _prepare_sample(self, data: Tuple[str, ...]) -> Dict[str, Any]:
image_data, label_data = data[:256], data[256:-1]

return dict(
image=Image(torch.tensor([float(pixel) for pixel in image_data], dtype=torch.uint8).reshape(16, 16)),
label=OneHotLabel([int(label) for label in label_data], categories=self.categories),
image=Image(torch.tensor([float(pixel) for pixel in image_data], dtype=torch.float).reshape(16, 16)),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmeier the original PR used uint8 here, but these are float values in [0, 1]. Do you know if the use of dtype=uint8 was inteded for something specific?

Also, the manual conversion of every single value is fairly inefficient for all of the 16 * 16 pixels. Do you know if there's a more efficient conversion that could be applied at the CSVParser level?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original PR used uint8 here, but these are float values in [0, 1]. Do you know if the use of dtype=uint8 was inteded for something specific?

Nope, that seems like a bug. Good catch!

Also, the manual conversion of every single value is fairly inefficient for all of the 16 * 16 pixels. Do you know if there's a more efficient conversion that could be applied at the CSVParser level?

I'm not aware of anything. Without a user request for a better performance, I wouldn't sweat it though. I'm guessing this dataset is not in widespread use and the code will probably get more complicated. If you have a simple solution, go for it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw that this dataset has only 1.5k samples. If someone wants more speed, the best way out is probably to just load the whole dataset into memory.

label=OneHotLabel([int(label) for label in label_data], categories=self._categories),
)

def _make_datapipe(
self,
resource_dps: List[IterDataPipe],
*,
config: DatasetConfig,
) -> IterDataPipe[Dict[str, Any]]:
def _datapipe(self, resource_dps: List[IterDataPipe]) -> IterDataPipe[Dict[str, Any]]:
dp = resource_dps[0]
dp = CSVParser(dp, delimiter=" ")
dp = hint_shuffling(dp)
dp = hint_sharding(dp)
return Mapper(dp, self._prepare_sample)

def __len__(self) -> int:
return 1_593