def attack_all(self, models, samples=500, pixels=(1,3,5), targeted=False, 
                maxiter=75, popsize=400, verbose=False):
        results = []
        for model in models:
            model_results = []
            valid_imgs = self.correct_imgs[self.correct_imgs.name == model.name].img
            img_samples = np.random.choice(valid_imgs, samples)

            for pixel_count in pixels:
                for i,img in enumerate(img_samples):
                    print(model.name, '- image', img, '-', i+1, '/', len(img_samples))
                    targets = [None] if not targeted else range(10)
                    
                    for target in targets:
                        if (targeted):
                            print('Attacking with target', class_names[target])
                            if (target == self.y_test[img,0]):
                                continue
                        result = self.attack(img, model, target, pixel_count, 
                                        maxiter=maxiter, popsize=popsize, 
                                        verbose=verbose)
                        model_results.append(result)
                        
            results += model_results
            helper.checkpoint(results, targeted)
        return results
    def attack_all(self, models, samples=10000, pixels=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), targeted=False,
                   maxiter=75, popsize=400, verbose=False):
        results = []
        for model in models:
            model_results = []
            valid_imgs = self.correct_imgs[self.correct_imgs.name == model.name].img

            img_samples = np.random.choice(valid_imgs, samples)
            #img_samples = valid_imgs
            for pixel_count in pixels:
                for i, img in enumerate(img_samples):
                    print(model.name, '- image', img, '-', i + 1, '/', len(img_samples))
                    targets = [None] if not targeted else range(10)

                    for target in targets:
                        if (targeted):
                            # print('Attacking with target', class_names[target])
                            if (target == self.y_test[img, 0]):
                                continue
                        result = self.attack(img, model, target, pixel_count,
                                             maxiter=maxiter, popsize=popsize,
                                             verbose=verbose)
                        # print('------------------------------------qzh')
                        # print(result)
                        # print('------------------------------------qzh')
                        model_results.append(result)

            results += model_results
            helper.checkpoint(results, targeted)
        return results
Esempio n. 3
0
    def attack_all(self,
                   models,
                   samples=500,
                   pixels=(1, 3, 5),
                   targeted=False,
                   maxiter=75,
                   popsize=400,
                   info='',
                   verbose=False):
        """
        @models: list of models to evaluate
        @samples: how many random samples to take from provided data
        @pixels: iterable controlling what attack sizes to iterate through
        @targeted: boolean, whether you want to test targeted attacks or not
        @maxiter: maximum iterations on the optimization
        @popsize: population size at each iteration of the optimization
        @info: string to attach to results pickle filename
        @verbose: bool, controls printing
        """
        results = []
        for model in models:
            model_results = []
            valid_imgs = self.correct_imgs[self.correct_imgs.name ==
                                           model.name].img
            img_samples = np.random.choice(valid_imgs, samples)

            for pixel_count in pixels:
                for i, img in enumerate(img_samples):
                    print(model.name, '- image', img, '-', i + 1, '/',
                          len(img_samples))
                    targets = [None] if not targeted else range(10)

                    for target in targets:
                        if (targeted):
                            print('Attacking with target', class_names[target])
                            if (target == self.y_test[img, 0]):
                                continue
                        result = self.attack(img,
                                             model,
                                             target,
                                             pixel_count,
                                             maxiter=maxiter,
                                             popsize=popsize,
                                             verbose=verbose)
                        model_results.append(result)

            results += model_results
            helper.checkpoint(results, targeted, info)
        return results
def training_loop(dataloader_RGB,
                  dataloader_Depth,
                  test_dataloader_RGB,
                  test_dataloader_Depth,
                  n_epochs=1000):

    print_every = 10
    losses = []
    test_iter_RGB = iter(test_dataloader_RGB)
    test_iter_Depth = iter(test_dataloader_Depth)
    fixed_RGB = test_iter_RGB.next()[0]
    fixed_Depth = test_iter_Depth.next()[0]
    fixed_RGB = scale(fixed_RGB)
    fixed_Depth = scale(fixed_Depth)
    iter_RGB = iter(dataloader_RGB)
    iter_Depth = iter(dataloader_Depth)
    batches_per_epoch = min(len(iter_RGB), len(iter_Depth))
    for epoch in range(1, n_epochs + 1):

        if epoch % batches_per_epoch == 0:
            iter_RGB = iter(dataloader_RGB)
            iter_Depth = iter(dataloader_Depth)

        images_RGB, _ = iter_RGB.next()
        images_RGB = scale(images_RGB)

        images_Depth, _ = iter_Depth.next()
        images_Depth = scale(images_Depth)

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        images_RGB = images_RGB.to(device)
        images_Depth = images_Depth.to(device)
        #-------------------------------------Discriminator Training-----------------------------------------------------#

        # optimizers are initialized to zero so that they don't carry forward the errors form previous epochs. Basically to
        # to avoid any accumulation
        optimizer_disc_A.zero_grad()

        # Discriminator-A is initially checked for its performance with real image
        out_RGB = disc_A(images_RGB)
        disc_A_real_loss = real_mse_loss(out_RGB)

        # A fake image is generated by the generator to check the Discriminator's performance with fake image
        fake_RGB = gen_B2A(images_Depth)

        out_RGB = disc_A(fake_RGB)
        disc_A_fake_loss = fake_mse_loss(out_RGB)

        # Losses produced while the above operations are added which is the total loss of the Discriminator-A. It is backpropogated
        disc_A_loss = disc_A_real_loss + disc_A_fake_loss
        disc_A_loss.backward()
        optimizer_disc_A.step()

        # Same operations as mentioned above are now performed on Discriminator-B
        optimizer_disc_B.zero_grad()

        out_Depth = disc_B(images_Depth)
        disc_B_real_loss = real_mse_loss(out_Depth)

        fake_Depth = gen_A2B(images_RGB)

        out_Depth = disc_B(fake_Depth)
        disc_B_fake_loss = fake_mse_loss(out_Depth)

        disc_B_loss = disc_B_real_loss + disc_B_fake_loss
        disc_B_loss.backward()
        optimizer_disc_B.step()

        #------------------------------------------Generator Training---------------------------------------------------#

        optimizer_gen.zero_grad()

        # An image is produced from the generator (in this case generator-B2A is being trained) is produced to check how well it can
        fake_RGB = gen_B2A(images_Depth)

        # The generated image is then fed to discrminator to check the fakeness and the MSE loss is received
        out_RGB = disc_A(fake_RGB)
        gen_B2A_loss = real_mse_loss(out_RGB)

        # To check the consistency, the generated image is fed to other generator(A2B in this case) and reconstructed image is received
        reconstructed_Depth = gen_A2B(fake_RGB)
        #  Reconstruction loss is received by comparing two images as mentioned in step(4) of training generators
        reconstructed_Depth_loss = cycle_consistency_loss(images_Depth,
                                                          reconstructed_Depth,
                                                          lambda_weight=10)

        # Same operations are done, but this time to train gen_A2B
        fake_Depth = gen_A2B(images_RGB)

        out_Depth = disc_B(fake_Depth)
        gen_A2B_loss = real_mse_loss(out_Depth)

        reconstructed_RGB = gen_B2A(fake_Depth)
        reconstructed_RGB_loss = cycle_consistency_loss(images_RGB,
                                                        reconstructed_RGB,
                                                        lambda_weight=10)

        # Losses from both the generators are received and propogated backwards
        g_total_loss = gen_B2A_loss + gen_A2B_loss + reconstructed_Depth_loss + reconstructed_RGB_loss
        g_total_loss.backward()
        optimizer_gen.step()

        # Weight decay as mentioned earlier
        learningRate_scheduler_gen.step()
        learningRate_scheduler_disc_A.step()
        learningRate_scheduler_disc_B.step()

        if epoch % print_every == 0:

            losses.append(
                (disc_A_loss.item(), disc_B_loss.item(), g_total_loss.item()))
            print(
                'Epoch [{:5d}/{:5d}] | d_X_loss: {:6.4f} | d_Y_loss: {:6.4f} | g_total_loss: {:6.4f}'
                .format(epoch, n_epochs, disc_A_loss.item(),
                        disc_B_loss.item(), g_total_loss.item()))

    # A training sample is saved every given time to check the performance of our network
        sample_every = 100

        if epoch % sample_every == 0:
            gen_B2A.eval()
            gen_A2B.eval()
            save_samples(epoch,
                         fixed_Depth,
                         fixed_RGB,
                         gen_B2A,
                         gen_A2B,
                         batch_size=16)
            gen_B2A.train()
            gen_A2B.train()

        # Models are saved after every given number of epochs
        checkpoint_every = 1000
        # Save the model parameters
        if epoch % checkpoint_every == 0:
            checkpoint(epoch, gen_A2B, gen_B2A, disc_A, disc_B)

    return losses
Esempio n. 5
0
                    torchvision.utils.save_image(grid_image, path)
                    print('Saved {}'.format(path))

                    Y_fake = G_YtoX(images_Y)
                    grid_image_real = torchvision.utils.make_grid(
                        images_Y.cpu())
                    grid_image_fake = torchvision.utils.make_grid(Y_fake.cpu())
                    grid_image = torch.cat((grid_image_real, grid_image_fake),
                                           1)
                    path = filename + '_' + 'YtoX.jpg'
                    torchvision.utils.save_image(grid_image, path)
                    print('Saved {}'.format(path))

                    # save models after certein epoch
                    if epoch > 2:
                        helper.checkpoint(epoch, idx, G_XtoY, G_YtoX, D_X, D_Y,
                                          save_path)

        # Print the log info
        if epoch % print_every == 0:
            losses.append((epoch_d_x_loss, epoch_d_y_loss, epoch_g_total_loss))
            print(
                'Epoch [{:5d}/{:5d}] | d_X_loss: {:6.4f} | d_Y_loss: {:6.4f} | g_total_loss: {:6.4f}'
                .format(epoch, n_epochs, epoch_d_x_loss, epoch_d_y_loss,
                        epoch_g_total_loss))

        # update scheduler
        if is_scheduler:
            g_lr_scheduler.step()
            d_x_lr_scheduler.step()
            d_y_lr_scheduler.step()