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

Dataset from torch to torch does not write in storage #136

Merged
merged 4 commits into from
Apr 12, 2024
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
5 changes: 3 additions & 2 deletions flex/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def from_torchvision_dataset(cls, pytorch_dataset):
Dataset: a FlexDataObject which encapsulates the dataset.
"""

from flex.data.dataset_pt_utils import FeatureDataset, LabelDataset
from flex.data.pluggable_datasets import PluggableTorchvision

if pytorch_dataset.__class__.__name__ not in PluggableTorchvision:
Expand All @@ -134,8 +135,8 @@ def from_torchvision_dataset(cls, pytorch_dataset):

length = count(pytorch_dataset)

X_data = LazyIndexable((x for x, _ in pytorch_dataset), length=length)
y_data = LazyIndexable((y for _, y in pytorch_dataset), length=length)
X_data = LazyIndexable(FeatureDataset(pytorch_dataset), length=length)
y_data = LazyIndexable(LabelDataset(pytorch_dataset), length=length)

return cls(X_data=X_data, y_data=y_data)

Expand Down
23 changes: 23 additions & 0 deletions flex/data/dataset_pt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from torch.utils.data import Dataset
from torchvision.datasets import VisionDataset


Expand All @@ -37,3 +38,25 @@ def __getitem__(self, index: int):

def __len__(self):
return len(self.data)


class FeatureDataset(Dataset):
def __init__(self, dataset: Dataset):
self._dataset = dataset

def __len__(self):
return len(self._dataset)

def __getitem__(self, idx):
return self._dataset[idx][0]


class LabelDataset(Dataset):
def __init__(self, dataset: Dataset):
self._dataset = dataset

def __len__(self):
return len(self._dataset)

def __getitem__(self, idx):
return self._dataset[idx][1]
35 changes: 35 additions & 0 deletions tests/data/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ def fixture_simple_fex_data_object():
return Dataset.from_array(X_data, y_data)


def create_simple_torch_dataset():
from torch.utils.data import Dataset as TorchDataset

class SimpleTorchDataset(TorchDataset):
def __init__(self):
self.a = np.arange(10)

def __len__(self):
return len(self.a)

def __getitem__(self, idx):
return self.a[idx], self.a[idx] + 1

return SimpleTorchDataset()


class TestFlexDataObject(unittest.TestCase):
@pytest.fixture(autouse=True)
def _fixture_simple_flex_data_object(self, fcd):
Expand Down Expand Up @@ -236,3 +252,22 @@ def test_to_numpy_y_none(self):
X_data = fcd.to_numpy(x_dtype=np.int16)
assert isinstance(X_data, np.ndarray)
assert len(X_data) == len(self._fcd.X_data)

def test_from_torchvision_back_to_torchvision(self):
original_torch_dataset = create_simple_torch_dataset()
fcd = Dataset.from_torchvision_dataset(original_torch_dataset)
new_torch_dataset = fcd.to_torchvision_dataset()
for (x, y), (x_bis, y_bis) in zip(original_torch_dataset, new_torch_dataset):
assert np.array_equal(x, x_bis)
assert np.array_equal(y, y_bis)

def test_from_torchvision_back_to_torchvision_does_not_use_storage(self):
original_torch_dataset = create_simple_torch_dataset()
fcd = Dataset.from_torchvision_dataset(original_torch_dataset)
new_torch_dataset = fcd.to_torchvision_dataset()
# Consume the datasets
for (x, y), (x_bis, y_bis) in zip(original_torch_dataset, new_torch_dataset):
assert np.array_equal(x, x_bis)
assert np.array_equal(y, y_bis)
assert fcd.X_data._storage == {}
assert fcd.y_data._storage == {}
Loading