AntixK / PyTorch-VAE

A Collection of Variational Autoencoders (VAE) in PyTorch.
Apache License 2.0
6.44k stars 1.05k forks source link

Problems when using custom dataset #83

Open denisb411 opened 9 months ago

denisb411 commented 9 months ago

Hello,

I tried to code a custom dataset class for training but the images generated on samples and reconstruction are all messed.

To code the dataset class I was inspired by the Oxford Pets dataset class that was commented on dataset.py file, and it's looking like that:

# Add your custom dataset class here
class MyDataset(Dataset):
    def __init__(self, 
                 data_path: str, 
                 split: str,
                 transform: Callable,
                **kwargs):
        self.data_dir = Path(data_path)    
        self.transforms = transform
        imgs = sorted([f for f in self.data_dir.iterdir() if f.suffix == '.jpg'])

        self.imgs = imgs[:int(len(imgs) * 0.75)] if split == "train" else imgs[int(len(imgs) * 0.75):]

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

    def __getitem__(self, idx):
        img = default_loader(self.imgs[idx])

        if self.transforms is not None:
            img = self.transforms(img)

The samples generated: image

The reconstruction: image

In order to debug, I used the following code, and it seems that the images are fine:

        DEBUG_DIR = './debug'
        shutil.rmtree(DEBUG_DIR, ignore_errors=True)
        os.makedirs(DEBUG_DIR, exist_ok=True)
        for i, img in enumerate(self.val_dataset):
            img = img[0]
            img = img.permute(1, 2, 0)
            img = (img * 255).numpy().astype(np.uint8)
            img = Image.fromarray(img)
            img.save(os.path.join(DEBUG_DIR, uuid.uuid4().hex + '.jpg'))

            if i == 10:
                break

image

Can someone give me a light about what I am missing?

bserranoanton commented 5 months ago

Any solution about this?

kuailexiaohunzi commented 4 months ago

Any solution about this?

May I ask if you have resolved it? I have also encountered the same problem. Can you communicate with me

MisterBourbaki commented 4 months ago

Hi @denisb411 , Hi all! @denisb411 I think your post misses a few thinks, notably the change you have to perform on the run.py file, and the config file you use. I mean, I assume you just run the run.py file?

@kuailexiaohunzi Maybe you should open a dedicated issue with details on what issues you encoutered :)

P.S: Seeing the last updates on this repo, I sadly think it is not maintained anymore :(

kuailexiaohunzi commented 4 months ago

Hi @denisb411 , Hi all! @denisb411 I think your post misses a few thinks, notably the change you have to perform on the run.py file, and the config file you use. I mean, I assume you just run the run.py file?

@kuailexiaohunzi Maybe you should open a dedicated issue with details on what issues you encoutered :)

P.S: Seeing the last updates on this repo, I sadly think it is not maintained anymore :(

My current problem is that I want to use VAE to test on my own dataset, but the results are not satisfactory. The image below is my reconstructed image. recons_VanillaVAE_Epoch_0 I modified the hyperparameters in the configuration file, but the effect was minimal. I am not sure if it is because my dataset does not have attribute files. How do you think this problem should be solved? Can we exchange ideas

MisterBourbaki commented 4 months ago

I am sorry but could you be more precise? The best way would be to open a dedicated Issue, as your problems may not be the same as @denisb411 . Provide as much information as you can (where the data are, what they are, what Python file did you run, is it after training...). Then I may be able to help you :)

kuailexiaohunzi commented 4 months ago

I am sorry but could you be more precise? The best way would be to open a dedicated Issue, as your problems may not be the same as @denisb411 . Provide as much information as you can (where the data are, what they are, what Python file did you run, is it after training...). Then I may be able to help you :)

I have opened a special question and I hope to receive your help. If you need to provide any information, please feel free to contact me. Thank you

bserranoanton commented 4 months ago

Any solution about this?

May I ask if you have resolved it? I have also encountered the same problem. Can you communicate with me

Yes, I had to change dimensions in order to have a larger patch size. For instance, in models/vanilla_vae.py imagen imagen imagen

kuailexiaohunzi commented 4 months ago

change di

I have also modified these parameters before, but they did not solve my problem. Thank you for your reply