def forward(self, x): in_mean, in_var = porch.mean(x, dim=(2, 3), keepdim=True), porch.var(x, dim=(2, 3), keepdim=True) out_in = (x - in_mean) / porch.sqrt(in_var + self.eps) ln_mean, ln_var = porch.mean(x, dim=(1, 2, 3), keepdim=True), porch.var(x, dim=(1, 2, 3), keepdim=True) out_ln = (x - ln_mean) / porch.sqrt(ln_var + self.eps) out = porch.Tensor(self.rho).expand(x.shape[0], -1, -1, -1) * out_in + (1 - porch.Tensor(self.rho).expand(x.shape[0], -1, -1, -1)) * out_ln out = out * porch.Tensor(self.gamma).expand(x.shape[0], -1, -1, -1) + porch.Tensor(self.beta).expand(x.shape[0], -1, -1, -1) return out
def forward(self, x, gamma, beta): in_mean, in_var = porch.mean(x, dim=[2, 3], keepdim=True), porch.var(x, dim=[2, 3], keepdim=True) out_in = (x - in_mean) / porch.sqrt(in_var + self.eps) ln_mean, ln_var = porch.mean(x, dim=[1, 2, 3], keepdim=True), porch.var(x, dim=[1, 2, 3], keepdim=True) out_ln = (x - ln_mean) / porch.sqrt(ln_var + self.eps) out = porch.Tensor(self.rho).expand(x.shape[0], -1, -1, -1) * out_in + (1 - porch.Tensor(self.rho).expand(x.shape[0], -1, -1, -1)) * out_ln out = out * porch.Tensor(gamma).unsqueeze(2).unsqueeze(3) + porch.Tensor(beta).unsqueeze(2).unsqueeze(3) return out
def compute_g_loss(nets, args, x_real, y_org, y_trg, z_trgs=None, x_refs=None, masks=None): assert (z_trgs is None) != (x_refs is None) if z_trgs is not None: z_trg, z_trg2 = z_trgs if x_refs is not None: x_ref, x_ref2 = x_refs # adversarial loss if z_trgs is not None: s_trg = nets.mapping_network(z_trg, y_trg) else: s_trg = nets.style_encoder(x_ref, y_trg) x_fake = nets.generator(x_real, s_trg, masks=masks) out = nets.discriminator(x_fake, y_trg) loss_adv = adv_loss(out, 1) # style reconstruction loss s_pred = nets.style_encoder(x_fake, y_trg) loss_sty = porch.mean(porch.abs(s_pred - s_trg)) # diversity sensitive loss if z_trgs is not None: s_trg2 = nets.mapping_network(z_trg2, y_trg) else: s_trg2 = nets.style_encoder(x_ref2, y_trg) x_fake2 = nets.generator(x_real, s_trg2, masks=masks) x_fake2 = x_fake2.detach() loss_ds = porch.mean(porch.abs(x_fake - x_fake2)) # cycle-consistency loss masks = nets.fan.get_heatmap(x_fake) if args.w_hpf > 0 else None s_org = nets.style_encoder(x_real, y_org) x_rec = nets.generator(x_fake, s_org, masks=masks) loss_cyc = porch.mean(porch.abs(x_rec - x_real)) loss = loss_adv + args.lambda_sty * loss_sty \ - args.lambda_ds * loss_ds + args.lambda_cyc * loss_cyc return loss, Munch(adv=loss_adv.numpy().flatten()[0], sty=loss_sty.numpy().flatten()[0], ds=loss_ds.numpy().flatten()[0], cyc=loss_cyc.numpy().flatten()[0]), x_fake[0]
def get_inception_metrics(sample, num_inception_images, num_splits=10, prints=True, use_torch=True): if prints: print('Gathering activations...') pool, logits, labels = accumulate_inception_activations( sample, net, num_inception_images) if prints: print('Calculating Inception Score...') IS_mean, IS_std = calculate_inception_score(logits.numpy(), num_splits) if no_fid: FID = 9999.0 else: if prints: print('Calculating means and covariances...') if use_torch: mu, sigma = torch.mean(pool, 0), torch_cov(pool, rowvar=False) else: mu, sigma = np.mean(pool.numpy(), axis=0), np.cov(pool.numpy(), rowvar=False) if prints: print('Covariances calculated, getting FID...') if use_torch: FID = torch_calculate_frechet_distance( mu, sigma, torch.tensor(data_mu), torch.tensor(data_sigma)) FID = float(FID.numpy()) else: FID = numpy_calculate_frechet_distance(mu, sigma, data_mu, data_sigma) # Delete mu, sigma, pool, logits, and labels, just in case del mu, sigma, pool, logits, labels return IS_mean, IS_std, FID
def translate_using_latent(nets, args, x_src, y_trg_list, z_trg_list, psi, filename): n_images = 100 x_src.stop_gradient = True N, C, H, W = x_src.shape latent_dim = z_trg_list[0].shape[1] x_concat = [x_src] masks = nets.fan.get_heatmap(x_src) if args.w_hpf > 0 else None for i, y_trg in enumerate(y_trg_list): z_many = porch.randn(n_images, latent_dim) # y_many = porch.LongTensor(10000).fill_(y_trg[0]) y_many = np.empty([n_images]) y_many.fill(y_trg[0].numpy()[0]) y_many = to_variable(y_many) s_many = nets.mapping_network(z_many, y_many) s_avg = porch.mean(s_many, dim=0, keepdim=True) s_avg = s_avg.repeat(N, 1) for z_trg in z_trg_list: s_trg = nets.mapping_network(z_trg, y_trg) s_trg = porch.lerp(s_avg, s_trg, psi) x_fake = nets.generator(x_src, s_trg, masks=masks) x_concat += [x_fake] x_concat = porch.cat(x_concat, dim=0) save_image(x_concat, N, filename)
def video_latent(nets, args, x_src, y_list, z_list, psi, fname): x_src.stop_gradient = True latent_dim = z_list[0].size(1) s_list = [] for i, y_trg in enumerate(y_list): z_many = porch.randn(10000, latent_dim) y_many = porch.LongTensor(10000).fill_(y_trg[0]) s_many = nets.mapping_network(z_many, y_many) s_avg = porch.mean(s_many, dim=0, keepdim=True) s_avg = s_avg.repeat(x_src.size(0), 1) for z_trg in z_list: s_trg = nets.mapping_network(z_trg, y_trg) s_trg = porch.lerp(s_avg, s_trg, psi) s_list.append(s_trg) s_prev = None video = [] # fetch reference images for idx_ref, s_next in enumerate(tqdm(s_list, 'video_latent', len(s_list))): if s_prev is None: s_prev = s_next continue if idx_ref % len(z_list) == 0: s_prev = s_next continue frames = interpolate(nets, args, x_src, s_prev, s_next).cpu() video.append(frames) s_prev = s_next for _ in range(10): video.append(frames[-1:]) video = tensor2ndarray255(porch.cat(video)) save_video(fname, video)
def torch_cov(m, rowvar=False): '''Estimate a covariance matrix given data. Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, `X = [x_1, x_2, ... x_N]^T`, then the covariance matrix element `C_{ij}` is the covariance of `x_i` and `x_j`. The element `C_{ii}` is the variance of `x_i`. Args: m: A 1-D or 2-D array containing multiple variables and observations. Each row of `m` represents a variable, and each column a single observation of all those variables. rowvar: If `rowvar` is True, then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. Returns: The covariance matrix of the variables. ''' if m.dim() > 2: raise ValueError('m has more than 2 dimensions') if m.dim() < 2: m = m.view(1, -1) if not rowvar and m.size(0) != 1: m = m.t() # m = m.type(torch.double) # uncomment this line if desired fact = 1.0 / (m.size(1) - 1) m -= torch.mean(m, dim=1, keepdim=True) mt = torch.Tensor(m).t() # if complex: mt = m.t().conj() return fact * torch.Tensor(m).matmul(mt).squeeze()
def forward(self, x): """""" # Aggregate input representation x_global = torch.mean(x, dim=0) # Compute reweighting vector s s = self.encoder_decoder(x_global) return x * s
def forward(self, input): in_mean, in_var = torch.mean(input, dim=[2, 3], keepdim=True), torch.var(input, dim=[2, 3], keepdim=True) out_in = (input - in_mean) / torch.sqrt(in_var + self.eps) ln_mean, ln_var = torch.mean(input, dim=[1, 2, 3], keepdim=True), torch.var(input, dim=[1, 2, 3], keepdim=True) out_ln = (input - ln_mean) / torch.sqrt(ln_var + self.eps) out = self.rho.expand(input.shape[0], -1, -1, -1) * out_in + ( 1 - self.rho.expand(input.shape[0], -1, -1, -1)) * out_ln out = out * self.gamma.expand(input.shape[0], -1, -1, -1) + self.beta.expand( input.shape[0], -1, -1, -1) return out
def manual_bn(x, gain=None, bias=None, return_mean_var=False, eps=1e-5): # Cast x to float32 if necessary float_x = x.float() # Calculate expected value of x (m) and expected value of x**2 (m2) # Mean of x m = torch.mean(float_x, [0, 2, 3], keepdim=True) # Mean of x squared m2 = torch.mean(float_x**2, [0, 2, 3], keepdim=True) # Calculate variance as mean of squared minus mean squared. var = (m2 - m**2) # Cast back to float 16 if necessary var = var.type(x.type()) m = m.type(x.type()) # Return mean and variance for updating stored mean/var if requested if return_mean_var: return fused_bn(x, m, var, gain, bias, eps), m.squeeze(), var.squeeze() else: return fused_bn(x, m, var, gain, bias, eps)
def forward(self, x): # Normalize x x = (x + 1.) / 2.0 x = (x - self.mean) / self.std # Upsample if necessary if x.shape[2] != 299 or x.shape[3] != 299: x = F.interpolate(x, size=(299, 299), mode='bilinear', align_corners=True) # 299 x 299 x 3 x = self.net.Conv2d_1a_3x3(x) # 149 x 149 x 32 x = self.net.Conv2d_2a_3x3(x) # 147 x 147 x 32 x = self.net.Conv2d_2b_3x3(x) # 147 x 147 x 64 x = F.max_pool2d(x, kernel_size=3, stride=2) # 73 x 73 x 64 x = self.net.Conv2d_3b_1x1(x) # 73 x 73 x 80 x = self.net.Conv2d_4a_3x3(x) # 71 x 71 x 192 x = F.max_pool2d(x, kernel_size=3, stride=2) # 35 x 35 x 192 x = self.net.Mixed_5b(x) # 35 x 35 x 256 x = self.net.Mixed_5c(x) # 35 x 35 x 288 x = self.net.Mixed_5d(x) # 35 x 35 x 288 x = self.net.Mixed_6a(x) # 17 x 17 x 768 x = self.net.Mixed_6b(x) # 17 x 17 x 768 x = self.net.Mixed_6c(x) # 17 x 17 x 768 x = self.net.Mixed_6d(x) # 17 x 17 x 768 x = self.net.Mixed_6e(x) # 17 x 17 x 768 # 17 x 17 x 768 x = self.net.Mixed_7a(x) # 8 x 8 x 1280 x = self.net.Mixed_7b(x) # 8 x 8 x 2048 x = self.net.Mixed_7c(x) # 8 x 8 x 2048 pool = torch.mean(x.view(x.size(0), x.size(1), -1), 2) # 1 x 1 x 2048 logits = self.net.fc( F.dropout(pool, training=False).view(pool.size(0), -1)) # 1000 (num_classes) return pool, logits
def calculate_lpips_given_images(group_of_images): # group_of_images = [porch.randn(N, C, H, W) for _ in range(10)] device = porch.device('cuda' if porch.cuda.is_available() else 'cpu') lpips = LPIPS(pretrained_weights_fn="./metrics/LPIPS_pretrained.pdparams") lpips.eval() lpips_values = [] num_rand_outputs = len(group_of_images) # calculate the average of pairwise distances among all random outputs for i in range(num_rand_outputs - 1): for j in range(i + 1, num_rand_outputs): lpips_values.append(lpips(group_of_images[i], group_of_images[j])) lpips_value = porch.mean(porch.stack(lpips_values, dim=0)) return lpips_value.numpy()
def forward(self, x, y): x = (x - self.mu) / self.sigma y = (y - self.mu) / self.sigma x_fmaps = self.alexnet(x) y_fmaps = self.alexnet(y) lpips_value = 0 for x_fmap, y_fmap, conv1x1 in zip(x_fmaps, y_fmaps, self.lpips_weights): x_fmap = normalize(x_fmap) y_fmap = normalize(y_fmap) z=torch.pow(x_fmap - y_fmap,2) lpips_value += torch.mean(conv1x1(z)) # print("paddle alexnet mean", torch.mean(z).numpy(),lpips_value.numpy()) return lpips_value
def loss_dcgan_dis(dis_fake, dis_real): L1 = torch.mean(F.softplus(-dis_real)) L2 = torch.mean(F.softplus(dis_fake)) return L1, L2
def loss_hinge_gen(dis_fake): loss = -torch.mean(dis_fake) return loss
def loss_hinge_dis(dis_fake, dis_real): loss_real = torch.mean(F.relu(1. - dis_real)) loss_fake = torch.mean(F.relu(1. + dis_fake)) return loss_real, loss_fake
def loss_dcgan_gen(dis_fake): loss = torch.mean(F.softplus(-dis_fake)) return loss
return FAN(fname_pretrained="./wing.ckpt") if __name__ == '__main__': from paddorch.convert_pretrain_model import load_pytorch_pretrain_model import torch as pytorch import torchvision # place = fluid.CPUPlace() place = fluid.CUDAPlace(0) np.random.seed(0) x=np.random.randn(1,3,256,256).astype("float32") with fluid.dygraph.guard(place=place): model=FAN() model.eval() pytorch_model=eval_pytorch_model() pytorch_model.eval() pytorch_model. torch_output = pytorch_model(pytorch.FloatTensor(x).)[1][0] pytorch_model pytorch_state_dict=pytorch_model.state_dict() load_pytorch_pretrain_model(model, pytorch_state_dict) torch.save(model.state_dict(),"wing") paddle_output = model(torch.Tensor(x))[1][0] print("torch mean",torch_output.mean()) print("paddle mean", torch.mean(paddle_output).numpy())
def mean(self, dim=None, keepdim=False): return torch.mean(self, dim=dim, keepdim=keepdim)
if __name__ == '__main__': from paddorch.convert_pretrain_model import load_pytorch_pretrain_model import torch as pytorch import torchvision # place = fluid.CPUPlace() place = fluid.CUDAPlace(0) np.random.seed(0) x = np.random.randn(11, 3, 256, 256).astype("float32") with fluid.dygraph.guard(place=place): model = FAN() model.eval() pytorch_model = eval_pytorch_model() pytorch_model.eval() pytorch_model.cuda() torch_output = pytorch_model(pytorch.FloatTensor(x).cuda()) pytorch_state_dict = pytorch_model.state_dict() load_pytorch_pretrain_model(model, pytorch_state_dict) torch.save(model.state_dict(), "wing") paddle_output = model(torch.Tensor(x)) print("torch mean", torch_output[0][0].shape, torch_output[0][0].mean().cpu().detach().numpy()) print("paddle mean", paddle_output[0][0].shape, torch.mean(paddle_output[0][0]).numpy()) print("torch mean", torch_output[1][0].shape, torch_output[1][0].mean().cpu().detach().numpy()) print("paddle mean", paddle_output[1][0].shape, torch.mean(paddle_output[1][0]).numpy())