def __init__(self): super(UnwarpLoss, self).__init__() # self.xmx, self.xmn, self.ymx, self.ymn = 166.28639310649825, -3.792634897181367, 189.04606710275974, -18.982843029373125 # self.xmx, self.xmn, self.ymx, self.ymn = 434.8578833991327, 14.898654260467202, 435.0363953546216, 14.515746051497239 # self.xmx, self.xmn, self.ymx, self.ymn = 434.9877152088082, 14.546402972133514, 435.0591952709043, 14.489902537540008 # self.xmx, self.xmn, self.ymx, self.ymn = 435.14545757153445, 13.410177297916455, 435.3297804574046, 14.194541402379988 # self.xmx, self.xmn, self.ymx, self.ymn = 0.0, 0.0, 0.0, 0.0 self.l2_loss_fn = nn.MSELoss().to(device) self.ssim_loss_fn = ssim.SSIM().to(device)
def __init__(self, celeba_loader, rafd_loader, config): """Initialize configurations.""" # Data loader. self.celeba_loader = celeba_loader self.rafd_loader = rafd_loader # Model configurations. self.c_dim = config.c_dim self.c2_dim = config.c2_dim self.image_size = config.image_size self.g_conv_dim = config.g_conv_dim self.d_conv_dim = config.d_conv_dim self.g_repeat_num = config.g_repeat_num self.d_repeat_num = config.d_repeat_num self.lambda_cls = config.lambda_cls self.lambda_rec = config.lambda_rec self.lambda_gp = config.lambda_gp # Training configurations. self.dataset = config.dataset self.batch_size = config.batch_size self.num_iters = config.num_iters self.num_iters_decay = config.num_iters_decay self.g_lr = config.g_lr self.d_lr = config.d_lr self.n_critic = config.n_critic self.beta1 = config.beta1 self.beta2 = config.beta2 self.resume_iters = config.resume_iters self.selected_attrs = config.selected_attrs self.criterionSSIM = ssim.SSIM() # Test configurations. self.test_iters = config.test_iters # Miscellaneous. self.use_tensorboard = config.use_tensorboard self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Directories. self.log_dir = config.log_dir self.sample_dir = config.sample_dir self.model_save_dir = config.model_save_dir self.result_dir = config.result_dir # Step size. self.log_step = config.log_step self.sample_step = config.sample_step self.model_save_step = config.model_save_step self.lr_update_step = config.lr_update_step # Build the model and tensorboard. self.build_model() if self.use_tensorboard: self.build_tensorboard()
def random_attack(image, max_time=500): label = get_label(image) for loop in range(5): new_image = change_cross_random(image, r=loop + 1) i = 0 while (get_label(new_image) == label or ssim.SSIM(new_image, image) < 0.7) and i < max_time: i += 1 new_image = change_cross_random(image, r=loop + 1) if i < max_time: return new_image return None
def pyssim(base_image, comparison_image): """Starting at workdir, search each successive parent dir until we find the splat executable""" gaussian_kernel_sigma = 1.5 gaussian_kernel_width = 11 gaussian_kernel_1d = ssim.get_gaussian_kernel( gaussian_kernel_width, gaussian_kernel_sigma) base_ss = ssim.SSIM(base_image, gaussian_kernel_1d) ssim_value = base_ss.ssim_value(comparison_image) #sys.stdout.write('%.7g' % ssim_value); return ssim_value
def attack(images, debug=True): start = time.time() succ = 0 ssim_ave = 0 indicates = [] size = len(images) for loop in range(3): start = time.time() succ = 0 ssim_ave = 0 gen_images = [] for i in range(size): new_image = random_attack(images[i], max_time=300) if (new_image is not None) and (not get_label(new_image) == get_label(images[i])): # 攻击成功 succ += 1 ssim_ave += ssim.SSIM(new_image, images[i]) gen_images.append(new_image) else: # 失败 gen_images.append(all_zero_attack(images[i])) print("cost", time.time() - start) print(succ, ssim_ave / size) indicates.append((gen_images, succ, ssim_ave / size)) # 寻找最优解 res_index = 0 max_succ = 0 max_ssim = 0 for i in range(len(indicates)): (succ, ssim_ave) = indicates[i][1:] if succ > max_succ: res_index = i max_succ = succ max_ssim = ssim_ave elif succ == max_succ and ssim_ave > max_ssim: res_index = i max_ssim = ssim_ave res = indicates[res_index] print(res[1:]) return res
def initialize(self, opt): BaseModel.initialize(self, opt) nb = opt.batchSize size = opt.fineSize self.input_A = self.Tensor(nb, opt.input_nc, size, size) self.input_B = self.Tensor(nb, opt.output_nc, size, size) # load/define networks # The naming conversion is different from those used in the paper # Code (paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X) self.netG_A = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG, opt.norm, not opt.no_dropout, opt.init_type, self.gpu_ids) self.netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.which_model_netG, opt.norm, not opt.no_dropout, opt.init_type, self.gpu_ids) if self.isTrain: use_sigmoid = opt.no_lsgan self.netD_A = networks.define_D(opt.output_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, opt.norm, use_sigmoid, opt.init_type, self.gpu_ids) self.netD_B = networks.define_D(opt.input_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, opt.norm, use_sigmoid, opt.init_type, self.gpu_ids) if not self.isTrain or opt.continue_train: which_epoch = opt.which_epoch self.load_network(self.netG_A, 'G_A', which_epoch) self.load_network(self.netG_B, 'G_B', which_epoch) if self.isTrain: self.load_network(self.netD_A, 'D_A', which_epoch) self.load_network(self.netD_B, 'D_B', which_epoch) if self.isTrain: self.old_lr = opt.lr self.fake_A_pool = ImagePool(opt.pool_size) self.fake_B_pool = ImagePool(opt.pool_size) # define loss functions self.criterionGAN = networks.GANLoss(use_lsgan=not opt.no_lsgan, tensor=self.Tensor) self.criterionCycle = torch.nn.L1Loss() self.criterionIdt = torch.nn.L1Loss() if opt.with_ssim: self.criterionSSIM = ssim.SSIM() # initialize optimizers self.optimizer_G = torch.optim.Adam(itertools.chain( self.netG_A.parameters(), self.netG_B.parameters()), lr=opt.lr, betas=(opt.beta1, 0.999)) self.optimizer_D_A = torch.optim.Adam(self.netD_A.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) self.optimizer_D_B = torch.optim.Adam(self.netD_B.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) self.optimizers = [] self.schedulers = [] self.optimizers.append(self.optimizer_G) self.optimizers.append(self.optimizer_D_A) self.optimizers.append(self.optimizer_D_B) for optimizer in self.optimizers: self.schedulers.append(networks.get_scheduler(optimizer, opt)) print('---------- Networks initialized -------------') networks.print_network(self.netG_A) networks.print_network(self.netG_B) if self.isTrain: networks.print_network(self.netD_A) networks.print_network(self.netD_B) print('-----------------------------------------------')
def initSSim(): model = ssim.SSIM(window_size=10, size_average=True) #print("model",model) return model
def SSIM(im1, im2): im1 = Image.fromarray(im1.transpose(1, 2, 0)) im2 = Image.fromarray(im2.transpose(1, 2, 0)) return ssim.SSIM(im1).cw_ssim_value(im2)