How to get gender information as label from CelebA dataset


I’d like to get label which has only gender information from torchvision.datasets.CelebA

My code snippet is as follows:

trainset = torchvision.datasets.CelebA(..., download=True, split='train', target_type='attr', ...)
trainloader =, ...)

for idx, (imgs, labels) in enumerate(trainlader):
    imgs =
    labels =

What I really want to do is get labels only have gender information.

In this case, what should I do?

If I’m not mistaken, the default target_type="attr" should yield various attributes including the male attribute, so you could slice the target for this attribute only.