def __init__(self, height=64, width=64, with_r=False, with_boundary=False): super(AddCoordsTh, self).__init__() self.with_r = with_r self.with_boundary = with_boundary device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') with torch.no_grad(): x_coords = torch.arange(height).unsqueeze(1).expand( height, width).float() y_coords = torch.arange(width).unsqueeze(0).expand( height, width).float() x_coords = (x_coords / (height - 1)) * 2 - 1 y_coords = (y_coords / (width - 1)) * 2 - 1 coords = torch.stack([x_coords, y_coords], dim=0) # (2, height, width) if self.with_r: rr = torch.sqrt( torch.pow(x_coords, 2) + torch.pow(y_coords, 2)) # (height, width) rr = (rr / torch.max(rr)).unsqueeze(0) coords = torch.cat([coords, rr], dim=0) self.coords = coords.unsqueeze(0).to( device) # (1, 2 or 3, height, width) self.x_coords = x_coords.to(device) self.y_coords = y_coords.to(device)
def preprocess(x): """Preprocess 98-dimensional heatmaps.""" N, C, H, W = x.size() x = truncate(x) x = normalize(x) sw = H // 256 operations = Munch(chin=OPPAIR(0, 3), eyebrows=OPPAIR(-7 * sw, 2), nostrils=OPPAIR(8 * sw, 4), lipupper=OPPAIR(-8 * sw, 4), liplower=OPPAIR(8 * sw, 4), lipinner=OPPAIR(-2 * sw, 3)) for part, ops in operations.items(): start, end = index_map[part] x[:, start:end] = resize(shift(x[:, start:end], ops.shift), ops.resize) zero_out = porch.cat([ porch.arange(0, index_map.chin.start), porch.arange(index_map.chin.end, 33), porch.LongTensor([ index_map.eyebrowsedges.start, index_map.eyebrowsedges.end, index_map.lipedges.start, index_map.lipedges.end ]) ]) x[:, zero_out] = 0 start, end = index_map.nose x[:, start + 1:end] = shift(x[:, start + 1:end], 4 * sw) x[:, start:end] = resize(x[:, start:end], 1) start, end = index_map.eyes x[:, start:end] = resize(x[:, start:end], 1) x[:, start:end] = resize(shift(x[:, start:end], -8), 3) + \ shift(x[:, start:end], -24) # Second-level mask x2 = deepcopy(x) x2[:, index_map.chin.start:index_map.chin.end] = 0 # start:end was 0:33 x2[:, index_map.lipedges.start:index_map.lipinner. end] = 0 # start:end was 76:96 x2[:, index_map.eyebrows.start:index_map.eyebrows. end] = 0 # start:end was 33:51 x = porch.sum(x, dim=1, keepdim=True) # (N, 1, H, W) x2 = porch.sum(x2, dim=1, keepdim=True) # mask without faceline and mouth x[x != x] = 0 # set nan to zero x2[x != x] = 0 # set nan to zero return x.clamp_(0, 1), x2.clamp_(0, 1)
def forward(self, x): bsz = x.shape[0] x = x.squeeze() # positives on the diagonal label = torch.arange(bsz).cuda().long() loss = self.criterion(x, label) return loss
def shift(x, N): """Shift N pixels up or down.""" up = N >= 0 N = abs(N) _, _, H, W = x.shape if N == 0: return x if up: head = torch.arange(H - N) + N tail = torch.arange(N) else: head = torch.arange(N) + (H - N) tail = torch.arange(H - N) # permutation indices perm = torch.cat([head, tail]) out = torch.stack([x[:, :, int(a)] for a in perm.numpy()], dim=2) return out
def shift(x, N): """Shift N pixels up or down.""" up = N >= 0 N = abs(N) _, _, H, W = x.size() head = porch.arange(N) tail = porch.arange(H - N) if up: head = porch.arange(H - N) + N tail = porch.arange(N) else: head = porch.arange(N) + (H - N) tail = porch.arange(H - N) # permutation indices perm = porch.cat([head, tail]).to(x.device) out = x[:, :, perm, :] return out
import paddorch arr = paddorch.convertTensor(1.0 / paddorch.arange(1, 30)) print(arr) arr[arr > 0.1] = 0.0 print(arr) arr[paddorch.isfinite(arr)] = 1.0 print(arr) arr[paddorch.isinf(arr)] = 2.0 print(arr) arr = paddorch.convertTensor(1.0 / paddorch.arange(1, 31)).view(-1, 5, 2) print("before", arr) arr[:, 2, :] = 999 print("after", arr)
def preprocess(x): """Preprocess 98-dimensional heatmaps.""" N, C, H, W = x.shape x = truncate(x) x = normalize(x) sw = H // 256 operations = Munch(chin=OPPAIR(0, 3), eyebrows=OPPAIR(-7 * sw, 2), nostrils=OPPAIR(8 * sw, 4), lipupper=OPPAIR(-8 * sw, 4), liplower=OPPAIR(8 * sw, 4), lipinner=OPPAIR(-2 * sw, 3)) for part, ops in operations.items(): start, end = index_map[part] torch.copy(resize(shift(x[:, start:end], ops.shift), ops.resize), x[:, start:end]) # = zero_out = torch.cat([ torch.arange(0, index_map.chin.start), torch.arange(index_map.chin.end, 33), torch.LongTensor([ index_map.eyebrowsedges.start, index_map.eyebrowsedges.end, index_map.lipedges.start, index_map.lipedges.end ]) ]) x.fill_(val=0, dim=1, indices=zero_out.numpy()) # x_numpy=x.numpy() # x_numpy[:, zero_out.numpy()] = 0 # x.set_value(x_numpy) # torch.copy(target) # x[:, zero_out] = 0 start, end = index_map.nose torch.copy(shift(x[:, start + 1:end], 4 * sw), x[:, start + 1:end]) torch.copy(resize(x[:, start:end], 1), x[:, start:end]) # x[:, start+1:end] = shift(x[:, start+1:end], 4*sw) # x[:, start:end] = resize(x[:, start:end], 1) start, end = index_map.eyes torch.copy(resize(x[:, start:end], 1), x[:, start:end]) torch.copy(resize(shift(x[:, start:end], -8), 3) + \ shift(x[:, start:end], -24), x[:, start:end] ) # x[:, start:end] = resize(x[:, start:end], 1) # x[:, start:end] = resize(shift(x[:, start:end], -8), 3) + \ # shift(x[:, start:end], -24) # Second-level mask x2 = x.clone() #deepcopy(x) x2.fill_(0, 1, list(range(index_map.chin.start, index_map.chin.end))) x2.fill_(0, 1, list(range(index_map.lipedges.start, index_map.lipedges.end))) x2.fill_(0, 1, list(range(index_map.eyebrows.start, index_map.eyebrows.end))) # x2[:, index_map.chin.start:index_map.chin.end] = 0 # start:end was 0:33 # x2[:, index_map.lipedges.start:index_map.lipinner.end] = 0 # start:end was 76:96 # x2[:, index_map.eyebrows.start:index_map.eyebrows.end] = 0 # start:end was 33:51 x = torch.sum(x, dim=1, keepdim=True) # (N, 1, H, W) x2 = torch.sum(x2, dim=1, keepdim=True) # mask without faceline and mouth x_numpy = x.numpy zero_indices = np.where(x_numpy != x_numpy)[0] x.fill_(0, 0, zero_indices) x2.fill_(0, 0, zero_indices) # x[x != x] = 0 # set nan to zero # x2[x != x] = 0 # set nan to zero return x.clamp_(0, 1), x2.clamp_(0, 1)
import paddorch from paddorch import index_copy_inplace_nograd memory = paddorch.zeros((4, 3)) k = paddorch.arange(0, 6).view(2, 3) out_ids = paddorch.LongTensor([1, 3]) index_copy_inplace_nograd(memory, 0, out_ids, k) print("paddorch", memory) import torch memory = torch.zeros((4, 3)) k = torch.arange(0, 6).view(2, 3).float() out_ids = torch.LongTensor([1, 3]) memory.index_copy_(0, out_ids, k) print("pytorch", memory)