Repository: lightly-ai/lightly

Issue:
data.LightlyDataset.from_torch_dataset  does not work properly
Hello,

I am trying to use Lightly with a dataset of images in `.npz` format. I have created a torch dataset class that loads the sample images and use `data.LightlyDataset.from_torch_dataset ` to create a Lightly dataset from it. However, it seems that the transformations that are passed to the `data.LightlyDataset.from_torch_dataset ` function are not properly performed. and subsequently the `Dataloader` does not work as expected. Can you help me figure out how to handle this? thanks

```
dataset_test_pl = data.LightlyDataset.from_torch_dataset(dataset_test, transform=test_transform)
dataloader_test = torch.utils.data.DataLoader(
    dataset_test_pl,
    batch_size=batch_size,
    shuffle=False,
    drop_last=False,
    num_workers=num_workers
)
```


Discussion:
Hi @Sadegh-Saberian and thanks for reporting this bug. We will fix it ASAP.

In the meantime, you can try the following two alternatives:
1. Set the transform for the torch dataset, i.e. something like:
    ```python
    dataset_test = YourTorchDataset(..., transform=test_transform)
    dataset_test_pl = data.LightlyDataset.from_torch_dataset(dataset_test)
    ```
2. Set the transform after the fact:
    ```python
    dataset_test_pl = data.LightlyDataset.from_torch_dataset(dataset_test)
    dataset_test_pl.transform = test_transform
    ```
Thanks @philippmwirth ! setting the transformation for the torch dataset worked fine; but the other approach did not. Thanks!
Thanks for letting us know!

Resolving patch:
diff --git a/lightly/data/dataset.py b/lightly/data/dataset.py
--- a/lightly/data/dataset.py
+++ b/lightly/data/dataset.py
@@ -10,6 +10,7 @@
 
 from PIL import Image
 from typing import List, Union, Callable
+from torch._C import Value
 
 import torchvision.datasets as datasets
 from torchvision import transforms
@@ -153,8 +154,7 @@ def __init__(self,
                  transform: transforms.Compose = None,
                  index_to_filename:
                  Callable[[datasets.VisionDataset, int], str] = None,
-                 filenames: List[str] = None,
-                 ):
+                 filenames: List[str] = None):
 
         # can pass input_dir=None to create an "empty" dataset
         self.input_dir = input_dir
@@ -169,10 +169,16 @@ def is_valid_file(filepath: str):
                 return filepath in filepaths
         else:
             is_valid_file = None
+
         if self.input_dir is not None:
             self.dataset = _load_dataset_from_folder(
                 self.input_dir, transform, is_valid_file=is_valid_file
             )
+        elif transform is not None:
+            raise ValueError(
+                'transform must be None when input_dir is None but is '
+                f'{transform}',
+            )
 
         # initialize function to get filename of image
         self.index_to_filename = _get_filename_by_index
@@ -214,12 +220,12 @@ def from_torch_dataset(cls,
         # create an "empty" dataset object
         dataset_obj = cls(
             None,
-            transform=transform,
-            index_to_filename=index_to_filename
+            index_to_filename=index_to_filename,
         )
 
         # populate it with the torch dataset
         dataset_obj.dataset = dataset
+        dataset_obj.transform = transform
         return dataset_obj
 
     def __getitem__(self, index: int):


Test patch:
diff --git a/tests/data/test_LightlyDataset.py b/tests/data/test_LightlyDataset.py
--- a/tests/data/test_LightlyDataset.py
+++ b/tests/data/test_LightlyDataset.py
@@ -189,6 +189,15 @@ def test_from_torch_dataset(self):
         self.assertEqual(len(_dataset), len(dataset))
         self.assertEqual(len(dataset.get_filenames()), len(dataset))
 
+    def test_from_torch_dataset_with_transform(self):
+        dataset_ = torchvision.datasets.FakeData(size=1, image_size=(3, 32, 32))
+        dataset = LightlyDataset.from_torch_dataset(
+            dataset_,
+            transform=torchvision.transforms.ToTensor()
+        )
+        self.assertIsNotNone(dataset.transform)
+        self.assertIsNotNone(dataset.dataset.transform)
+
     def test_filenames_dataset_no_samples(self):
         tmp_dir, folder_names, sample_names = self.create_dataset()
         with self.assertRaises((RuntimeError, FileNotFoundError)):
@@ -326,3 +335,7 @@ def test_transform_setter(self, dataset: LightlyDataset = None):
         # assert that the transform is set in the nested dataset
         self.assertIsNotNone(dataset.transform)
         self.assertIsNotNone(dataset.dataset.transform)
+
+    def test_no_dir_no_transform_fails(self):
+        with self.assertRaises(ValueError):
+            LightlyDataset(None, transform=torchvision.transforms.ToTensor())