There is a segmentation data loader that does this for you, the mask is an image with int values each value represents a class. In that reposotory are linked some videos that explain how to use it!!!
You are welcome !