Train and Test a GAN model

for a GAN model, I have an image dataset folder splitted between train, test and validation sub-folders. In each sub-folder 4 classes (1 Normal and 3 abnormal patients). How can I split this dataset to train my model only on normal patients and test the model on unseen normal and abnormal patients?

Depending how you are currently loading the data, you could use the targets (assuming you are already creating them beforehand), use the target indices to filter out the desired classes, and pass them to different Subsets.

Below you can find my code. How can I create the targets for this dataset ?

data_root = "./data/OCT2017/"

train_transform = transforms.Compose([
                                  transforms.Normalize([0.5], [0.5]) 

test_transform = transforms.Compose([
                                  transforms.Normalize([0.5], [0.5])

train_data = dataset.ImageFolder(root = data_root+ 'train', transform= train_transform)
test_data = dataset.ImageFolder(root = data_root + 'test', transform= test_transform)

trainloader =, batch_size=batch_size, shuffle=True)                                
testloader =, batch_size=1,shuffle=True)  

You can access the .targets attribute in train_data.targets and test_data.targets.

is the below code correct? and then how can I load the data ?

x_train = train_data[train_data.targets == 1]
y_train = train_data.targets[train_data.targets==1]

x_test = test_data[test_data.targets != 1]
y_test = test_data.targets[test_data.targets!=1]

Not quite. Here is an example how to use the internal targets attribute to filter out samples and create a Subset with only one class:

class MyDataset(Dataset):
    def __init__(self): = torch.randn(100, 1)
        self.targets = torch.randint(0, 2, (100,))
    def __getitem__(self, index):
        x =[index]
        y = self.targets[index]
        return x, y
    def __len__(self):
        return len(

dataset = MyDataset()
> (tensor([0, 1]), tensor([55, 45]))

# Filter out class1 and get indices for all samples corresponding to class1
idx = (dataset.targets == 1).nonzero()
print('{} class1 samples found'.format(len(idx)))
> 45 class1 samples found

dataset_class1 = Subset(dataset, indices=idx)
loader = DataLoader(dataset_class1)

for data, target in loader:

> tensor([[1]])

And then target data how can be used during training?

You could use the data as in any other training script. I’m not familiar with your use case and thus don’t know what you are exactly trying to achieve, as “training” with a single class is usually not working.
Your model would in this case only predict the single class and I don’t think it can learn anything else (but I think you’ve explained your approach in your other topics in more details).