def power_iteration(W, u_, update=True, eps=1e-12): # Lists holding singular vectors and values Wt = torch.Tensor(W).t() us, vs, svs = [], [], [] for i, u in enumerate(u_): # Run one step of the power iteration with torch.no_grad(): if W.shape[1] == 27: a = 1 v = torch.matmul(u, W) # if (W.shape[0]==u.shape[1]) : # v = torch.matmul(u, W) # else: # v = torch.matmul(u, Wt) # Run Gram-Schmidt to subtract components of all other singular vectors v = F.normalize(gram_schmidt(v, vs), eps=eps) # Add to the list vs += [v] # Update the other singular vector u = torch.matmul(v, Wt) # if (W.shape[0]!=v.shape[1]): # u = torch.matmul(v, Wt ) # else: # u = torch.matmul(v, W) # Run Gram-Schmidt to subtract components of all other singular vectors u = F.normalize(gram_schmidt(u, us), eps=eps) # Add to the list us += [u] if update: torch.copy(u, u_[i]) # u_[i][:] = u # Compute this singular value and add it to the list svs += [torch.squeeze(torch.matmul(torch.matmul(v, Wt), u.t()))] # if (W.shape[0]!=v.shape[1]): # svs += [torch.squeeze(torch.matmul(torch.matmul(v, Wt ), u.t() ))] # else: # svs += [torch.squeeze(torch.matmul(torch.matmul(v, W), u.t()))] #svs += [torch.sum(F.linear(u, W.transpose(0, 1)) * v)] return svs, us, vs
def W_(self): self.training = True if isinstance(self, SNLinear): W_mat = torch.Tensor(self.weight).t( ) ##linear layer weight is different from pytorch weight, need to transpose else: W_mat = torch.Tensor(self.weight).view(self.weight.shape[0], -1) if self.transpose: W_mat = W_mat.t() # Apply num_itrs power iterations for _ in range(self.num_itrs): svs, us, vs = power_iteration(W_mat, self.u, update=self.training, eps=self.eps) # Update the svs if self.training: with torch.no_grad( ): # Make sure to do this in a no_grad() context or you'll get memory leaks! for i, sv in enumerate(svs): torch.copy(sv, self.sv[i]) # self.sv[i][:] = sv return self.weight / svs[0]
def copy_(self, src): torch.copy(src, self) return self
def __div__(self, other_var): paddorch.copy(self.values / other_var, self.values) # self.values.set_value(self.values/other_var) return self
def __mul__(self, other): paddorch.copy(self.values * other, self.values) # self.values.set_value(self.values*other) return self
def moving_average(model, model_test, beta=0.999): for param, param_test in zip(model.parameters(), model_test.parameters()): porch.copy(porch.lerp(param, param_test, beta), param_test)
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)