Esempio n. 1
0
                     },
                     loss_2={
                         "image": lambda img1, img2: Loss(l1_loss(img1, img2))
                     },
                     lr=lr)

iters = 0

print("Starting Training Loop...")
t0 = time.time()

for epoch in range(5):
    for i, data in enumerate(dataloader, 0):

        imgs = data[0].to(device)
        z = noise.sample(batch_size)

        loss = gan_model.train(imgs, z)
        loss_back = gan_model_back.train(z, imgs)

        cycle_gan.train({"noise": z}, {"image": imgs})

        # Output training stats
        if i % 10 == 0:
            print(time.time() - t0)
            t0 = time.time()
            print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f' %
                  (epoch, 5, i, len(dataloader), loss[0], loss[1]))

        if iters % 50 == 0:
            with torch.no_grad():
def optimization_step():
    noise = NormalNoise(n_noise, device)
    measure2image = ResMeasureToImage(args.measure_size * 3 + noise.size(),
                                      args.image_size, ngf).cuda()

    netD = DCDiscriminator(ndf=ndf).cuda()
    gan_model = GANModel(measure2image,
                         HingeLoss(netD).add_generator_loss(nn.L1Loss(), L1),
                         lr=0.0004)

    fabric = ProbabilityMeasureFabric(args.image_size)
    barycenter = fabric.load("barycenter").cuda().padding(args.measure_size)
    print(barycenter.coord.shape)
    barycenter = fabric.cat([barycenter for b in range(args.batch_size)])
    print(barycenter.coord.shape)

    image2measure = ResImageToMeasure(args.measure_size).cuda()
    image2measure_opt = optim.Adam(image2measure.parameters(), lr=0.0002)

    def test():
        dataloader_test = torch.utils.data.DataLoader(dataset_test,
                                                      batch_size=40,
                                                      num_workers=20)

        sum_loss = 0

        for i, (imgs, masks) in enumerate(dataloader_test, 0):
            imgs = imgs.cuda().type(torch.float32)
            pred_measures: ProbabilityMeasure = image2measure(imgs)
            ref_measures: ProbabilityMeasure = fabric.from_mask(
                masks).cuda().padding(args.measure_size)
            ref_loss = Samples_Loss()(pred_measures, ref_measures)
            sum_loss += ref_loss.item()

        return sum_loss

    for epoch in range(20):

        ot_iters = 100
        print("epoch", epoch)
        test_imgs = None

        for i, imgs in enumerate(dataloader, 0):

            imgs = imgs.cuda().type(torch.float32)
            test_imgs = imgs
            pred_measures: ProbabilityMeasure = image2measure(imgs)
            cond = pred_measures.toChannels()
            n = cond.shape[0]
            barycenter_batch = barycenter.slice(0, n)

            z = noise.sample(n)
            cond = torch.cat((cond, z), dim=1)
            gan_model.train(imgs, cond.detach())

            with torch.no_grad():
                A, T = LinearTransformOT.forward(pred_measures,
                                                 barycenter_batch, ot_iters)

            bc_loss_T = Samples_Loss()(pred_measures,
                                       pred_measures.detach() + T)
            bc_loss_A = Samples_Loss()(
                pred_measures.centered(),
                pred_measures.centered().multiply(A).detach())
            bc_loss_W = Samples_Loss()(pred_measures.centered().multiply(A),
                                       barycenter_batch.centered())
            bc_loss = bc_loss_W * cw + bc_loss_A * ca + bc_loss_T * ct

            fake = measure2image(cond)
            g_loss = gan_model.generator_loss(imgs, fake)
            (g_loss + bc_loss).minimize_step(image2measure_opt)

    return test()