Assuming pipe is a DataLoader object, you could iterate it once and collect all targets via:
targets = []
for _, target in pipe:
    targets.append(target)
targets = torch.stack(targets)

and calculate the class distribution later.
I hope that the target tensors are not too big to fit into your RAM.
If that’s the case, you might need to add a break statement after a certain amount of targets.
As long as you shuffle the targets and grab “enough” of them, the calculated estimation of the class distribution could come close to the real distribution.