Supporting that I have a N × 4 tensor which is N rectangular ROIs of a mask(C  × H × W), and I want to sum up all value of each channel inside a ROI to get a tensor of size N × C, the only way I can figure out is using a for loop. But this method is very slow, so I want to ask if there are faster way to do this?
The for loop method is as below:
proposals # N * 4 tensor (ROIs)
mask # C * H * W tensor
total_numbers = []
for i in range(proposals.shape[1]):
    proposal = proposals[i]
    mask_region = mask[:, proposal[0]: proposal[2], proposal[1]: proposal[3]]
    total_number = mask_region.sum(dim=(1, 2))
    total_numbers.append(total_number)