How to implement the grouped loss

Hello, every body, i want to implement a new loss that presented in recent paper in pytorch. this loss is used for instance segmentation. but i find it difficult to implement this loss because the number of instances in different picture are not the same. and i want to get some value of pixels of each instance.the formula iooks like that:20170815211010
any one can give me some advice? thank you very much!!