This line of code should return all unique colors:
colors = torch.unique(target.view(-1, target.size(2)), dim=0).numpy()

and the length of this tensor would give you the number of classes for this target tensor.
Note that you would have to use multiple targets, if this particular target doesn’t contain all classes.