Skip to content

Instantly share code, notes, and snippets.

@hushell
Created March 5, 2021 09:36
Show Gist options
  • Save hushell/77b584e94cd8a0722cc52acf189926b0 to your computer and use it in GitHub Desktop.
Save hushell/77b584e94cd8a0722cc52acf189926b0 to your computer and use it in GitHub Desktop.
Superpixel feature
import torch
F = torch.rand(5, 10, 10) # C, H, W
M = torch.randint(8, (10, 10)) # 8 superpixels
M1hot = torch.nn.functional.one_hot(M, 8) # H, W, K
counts = M1hot.sum(dim=(0, 1)) # counts of pixels per superpixel
Favg = M1hot.view(-1, 8).T.float() @ F0.view(5, -1).T # K, C
Favg = Favg.div(counts.unsqueeze(-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment