def test_l1_loss(self): jt_loss=jnn.L1Loss() tc_loss=tnn.L1Loss() output=np.random.randn(10,100).astype(np.float32) target=np.random.randn(10,100).astype(np.float32) jt_y=jt_loss(jt.array(output), jt.array(target)) tc_y=tc_loss(torch.from_numpy(output), torch.from_numpy(target)) assert np.allclose(jt_y.numpy(), tc_y.numpy())
img = img[0] min_ = -1 max_ = 1 img = (img - min_) / (max_ - min_) * 255 img = img.transpose((1, 2, 0)) if C == 3: img = img[:, :, ::-1] cv2.imwrite(path, img) os.makedirs("images/%s" % opt.dataset_name, exist_ok=True) os.makedirs("checkpoints/%s" % opt.dataset_name, exist_ok=True) # Loss functions criterion_GAN = nn.BCELoss() # no lsgan criterion_pixelwise = nn.L1Loss() # Calculate output of image discriminator (PatchGAN) patch = (1, opt.img_height // 2**4, opt.img_width // 2**4) # Initialize generator and discriminator G_global = GeneratorResNet(in_channels=opt.in_channels, out_channels=opt.out_channels) G_l_eyel = GeneratorResNet(in_channels=opt.in_channels, out_channels=opt.out_channels, num_res_blocks=3) G_l_eyer = GeneratorResNet(in_channels=opt.in_channels, out_channels=opt.out_channels, num_res_blocks=3) G_l_nose = GeneratorResNet(in_channels=opt.in_channels, out_channels=opt.out_channels,
min_ = img.min() max_ = img.max() img = (img - min_) / (max_ - min_) * 255 img = img.transpose((1, 2, 0)) if C == 3: img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imwrite(path, img) # Calculate output of image discriminator (PatchGAN) patch_h, patch_w = int(opt.mask_size / 2**3), int(opt.mask_size / 2**3) patch = (1, patch_h, patch_w) # Loss function adversarial_loss = nn.MSELoss() pixelwise_loss = nn.L1Loss() # Initialize generator and discriminator generator = Generator(channels=opt.channels) discriminator = Discriminator(channels=opt.channels) # Dataset loader transforms_ = [ transform.Resize((opt.img_size, opt.img_size), mode=Image.BICUBIC), transform.ImageNormalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ] dataloader = ImageDataset("../../data/%s" % opt.dataset_name, transforms_=transforms_).set_attrs( batch_size=opt.batch_size, shuffle=True, num_workers=opt.n_cpu,
[img, img2[:, W * ncol * i:W * ncol * (i + 1), :]], axis=2) min_ = img.min() max_ = img.max() img = (img - min_) / (max_ - min_) * 255 img = img.transpose((1, 2, 0)) if C == 3: # img = img[:,:,::-1] img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imwrite(path, img) c_dim = len(opt.selected_attrs) img_shape = (opt.channels, opt.img_height, opt.img_width) # Loss functions criterion_cycle = nn.L1Loss() bce_with_logits_loss = nn.BCEWithLogitsLoss(size_average=False) def criterion_cls(logit, target): return bce_with_logits_loss(logit, target) / logit.size(0) # Loss weights lambda_cls = 1 lambda_rec = 10 lambda_gp = 10 # Initialize generator and discriminator generator = GeneratorResNet(img_shape=img_shape, res_blocks=opt.residual_blocks,