Repository: facebookresearch/DomainBed
Issue: Running a sweep with my Custom Dataset
Root cause: Documentation Deficiency

Hello, I have the data from multiple domains(3-4) for each class(around 30 classes). I am able to create a json file with the path to each image file for each class. My problem is how do we approach to apply these algorithms on custom dataset. For my experiments, the difficult part was to create a dataset, please highlight the steps to replicate these experiments onto my own dataset. 

Is there any update?

Hi @RohitSingh1226, sorry for the delay. You can follow how the datasets are implemented in : domainbed/datasets.py

Let me know if you have issues! 




I will give it a look and will let you know soon!

> Hi [@RohitSingh1226](https://github.com/RohitSingh1226), sorry for the delay. You can follow how the datasets are implemented in : domainbed/datasets.py
> 
> Let me know if you have issues!



I was able to modify the code, I will close this issue for now 

@piotr-teterwak 
I want to create a dataset with a json file, which contains the path of the sample and the class name inside the json file and also the environment(domain) name.

The structure looks like this:

{
    "Env1": {
        "Class1": [
            "1.png",
            "2.png"
        ],
        "Class2": [
            "1.png",
            "2.png"
        ],
  "Env2": {
        "Class1": [
            "1.png",
            "2.png"
        ],
        "Class2": [
            "1.png",
            "2.png"
        ],


But since there are multiple environments(domains), it is not possible to inherit from the default classes mentioned.

`class MultipleDomainDataset:
    N_STEPS = 5001
    CHECKPOINT_FREQ = 100
    N_WORKERS = 8
    ENVIRONMENTS = None
    INPUT_SHAPE = None

    def __getitem__(self, index):
        env, env_samples = self.datasets[index]
        #print(env)
        #print("env_samplesss: ",env_samples)
        for sample, class_name in env_samples:
            print(f"Environment: {env}, Sample: {sample.size}, Class Name: {class_name}")
            return sample, class_name

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



class CustomEnvDataset(MultipleDomainDataset):
    # N_STEPS = 2500
    CHECKPOINT_FREQ = 300
    ENVIRONMENTS = []

    def __init__(self, root, augment, hparams, test_env=None):
        self.dir = os.path.join(root, "Train_UK_cleaned")
        self.sample_file = "/home/rohit/wise-ft-clip/utils/data_process/sample_files/overall_balanced2.json"
        self.ENVIRONMENTS, self.sample_paths_list = self.load_environments(json_file=self.sample_file)
        self.transform = build_transform(n_px=224, is_train=True)
        self.target_transform = None
        self.datasets = []
        self.images = []
        self.classes_sample_file = list(self.sample_paths_list[0].keys())
        super().__init__()

        self.datasets = []


        for env, file_paths in zip(self.ENVIRONMENTS, self.sample_paths_list):
            env_samples = []
            for class_name, file_path in file_paths.items():
                for single_file in file_path:
                    sample = Image.open(single_file)
                    sample = self.transform(sample)
                    env_samples.append((sample, class_name))

            # Append the environment samples for all classes and paths
            self.datasets.append((env, env_samples))

        self.input_shape = (3, 224, 224,)
        self.num_classes = len(self.classes_sample_file)
        #print("num_ classes: ",self.num_classes)

    def load_environments(self, json_file):

        self.ENVIRONMENTS = []
        classes_img_path_list = []

        with open(json_file, 'r') as file:
            data = json.load(file)
            for key, value in data.items():
                self.ENVIRONMENTS.append(key)
                classes_img_path = {}
                for classes, paths in value.items():
                    classes_img_path[classes] = paths
                classes_img_path_list.append(classes_img_path)
        return self.ENVIRONMENTS, classes_img_path_list`


This is the code I am using in order to load data, the CustomEnvDataset reads the json file and appends all the sample images in a list. 
I cannot directly use ImageFolder() method from pytorch, can you suggest what I should do

If you want any information I will be happy to provide it!


Instead of a list of samples, I would construct a helper dataset class. Something like:

 ```
class EnvDataset(Dataset):
    def __init__(self, env_name, classes_img_paths, root_dir, transform=None):
        self.env = env_name
        self.transform = transform
        self.samples = []
        for class_name, img_list in classes_img_paths.items():
            for img_file in img_list:
                full_path = os.path.join(root_dir, env_name, class_name, img_file)
                self.samples.append((full_path, class_name))
    
    def __len__(self):
        return len(self.samples)
    
    def __getitem__(self, idx):
        img_path, class_name = self.samples[idx]
        image = Image.open(img_path).convert("RGB")
        if self.transform:
            image = self.transform(image)
        return image, class_name


```

Then in your loop:

```        
            env_dataset = EnvDataset(env, classes_img_paths, root, transform)
            self.datasets.append((env, env_dataset))

```

Thanks for the solution!