def main():
    iter_path = os.path.join(opt.checkpoints_dir, opt.name, 'iter.txt')
    data_loader = CreateDataLoader(opt)
    dataset = data_loader.load_data()
    dataset_size = len(data_loader)
    print(('#training images = %d' % dataset_size))
    (start_epoch, epoch_iter) = (1, 0)
    total_steps = (((start_epoch - 1) * dataset_size) + epoch_iter)
    display_delta = (total_steps % opt.display_freq)
    print_delta = (total_steps % opt.print_freq)
    save_delta = (total_steps % opt.save_latest_freq)
    model = create_model(opt)
    model = model.cuda()
    visualizer = Visualizer(opt)
    for epoch in range(start_epoch, ((opt.niter + opt.niter_decay) + 1)):
        epoch_start_time = time.time()
        if (epoch != start_epoch):
            epoch_iter = (epoch_iter % dataset_size)
        for (i, data) in enumerate(dataset, start=epoch_iter):
            iter_start_time = time.time()
            total_steps += opt.batchSize
            epoch_iter += opt.batchSize
            save_fake = ((total_steps % opt.display_freq) == display_delta)
            (losses, generated) = model(Variable(data['label']), Variable(data['inst']), Variable(data['image']), Variable(data['feat']), infer=save_fake)
            losses = [(torch.mean(x) if (not isinstance(x, int)) else x) for x in losses]
            loss_dict = dict(zip(model.loss_names, losses))
            loss_D = ((loss_dict['D_fake'] + loss_dict['D_real']) * 0.5)
            loss_G = ((loss_dict['G_GAN'] + loss_dict.get('G_GAN_Feat', 0)) + loss_dict.get('G_VGG', 0))
            model.optimizer_G.zero_grad()
            loss_G.backward()
            model.optimizer_G.step()
            model.optimizer_D.zero_grad()
            loss_D.backward()
            model.optimizer_D.step()
            if ((total_steps % opt.print_freq) == print_delta):
                errors = {k: (v.data if (not isinstance(v, int)) else v) for (k, v) in loss_dict.items()}
                t = ((time.time() - iter_start_time) / opt.batchSize)
                visualizer.print_current_errors(epoch, epoch_iter, errors, t)
                visualizer.plot_current_errors(errors, total_steps)
            if save_fake:
                visuals = OrderedDict([('input_label', util.tensor2label(data['label'][0], opt.label_nc)), ('synthesized_image', util.tensor2im(generated.data[0])), ('real_image', util.tensor2im(data['image'][0]))])
                visualizer.display_current_results(visuals, epoch, total_steps)
            if ((total_steps % opt.save_latest_freq) == save_delta):
                print(('saving the latest model (epoch %d, total_steps %d)' % (epoch, total_steps)))
                model.save('latest')
                np.savetxt(iter_path, (epoch, epoch_iter), delimiter=',', fmt='%d')
            if (epoch_iter >= dataset_size):
                break
        print(('End of epoch %d / %d \t Time Taken: %d sec' % (epoch, (opt.niter + opt.niter_decay), (time.time() - epoch_start_time))))
        if ((epoch % opt.save_epoch_freq) == 0):
            print(('saving the model at the end of epoch %d, iters %d' % (epoch, total_steps)))
            model.save('latest')
            model.save(epoch)
            np.savetxt(iter_path, ((epoch + 1), 0), delimiter=',', fmt='%d')
        if ((opt.niter_fix_global != 0) and (epoch == opt.niter_fix_global)):
            model.update_fixed_params()
        if (epoch > opt.niter):
            model.update_learning_rate()
    torch.cuda.empty_cache()
def main():
    iter_path = os.path.join(opt.checkpoints_dir, opt.name, 'iter.txt')
    data_loader = CreateDataLoader(opt)
    dataset = data_loader.load_data()
    dataset_size = len(data_loader)
    print(('#training images = %d' % dataset_size))
    (start_epoch, epoch_iter) = (1, 0)
    total_steps = (((start_epoch - 1) * dataset_size) + epoch_iter)
    display_delta = (total_steps % opt.display_freq)
    print_delta = (total_steps % opt.print_freq)
    save_delta = (total_steps % opt.save_latest_freq)
    model = create_model(opt)
    model = model.cuda()
    visualizer = Visualizer(opt)
    for epoch in range(start_epoch, ((opt.niter + opt.niter_decay) + 1)):
        epoch_start_time = time.time()
        if (epoch != start_epoch):
            epoch_iter = (epoch_iter % dataset_size)
        for (i, data) in enumerate(dataset, start=epoch_iter):
            iter_start_time = time.time()
            total_steps += opt.batchSize
            epoch_iter += opt.batchSize
            save_fake = ((total_steps % opt.display_freq) == display_delta)
            (losses, generated) = model(Variable(data['label']), Variable(data['inst']), Variable(data['image']), Variable(data['feat']), infer=save_fake)
            losses = [(torch.mean(x) if (not isinstance(x, int)) else x) for x in losses]
            loss_dict = dict(zip(model.loss_names, losses))
            loss_D = ((loss_dict['D_fake'] + loss_dict['D_real']) * 0.5)
            loss_G = ((loss_dict['G_GAN'] + loss_dict.get('G_GAN_Feat', 0)) + loss_dict.get('G_VGG', 0))
            model.optimizer_G.zero_grad()
            loss_G.backward()
            model.optimizer_G.step()
            model.optimizer_D.zero_grad()
            loss_D.backward()
            model.optimizer_D.step()
            if ((total_steps % opt.print_freq) == print_delta):
                errors = {k: (v.data if (not isinstance(v, int)) else v) for (k, v) in loss_dict.items()}
                t = ((time.time() - iter_start_time) / opt.batchSize)
                visualizer.print_current_errors(epoch, epoch_iter, errors, t)
                visualizer.plot_current_errors(errors, total_steps)
            if save_fake:
                visuals = OrderedDict([('input_label', util.tensor2label(data['label'][0], opt.label_nc)), ('synthesized_image', util.tensor2im(generated.data[0])), ('real_image', util.tensor2im(data['image'][0]))])
                visualizer.display_current_results(visuals, epoch, total_steps)
            if ((total_steps % opt.save_latest_freq) == save_delta):
                print(('saving the latest model (epoch %d, total_steps %d)' % (epoch, total_steps)))
                model.save('latest')
                np.savetxt(iter_path, (epoch, epoch_iter), delimiter=',', fmt='%d')
            if (epoch_iter >= dataset_size):
                break
        print(('End of epoch %d / %d \t Time Taken: %d sec' % (epoch, (opt.niter + opt.niter_decay), (time.time() - epoch_start_time))))
        if ((epoch % opt.save_epoch_freq) == 0):
            print(('saving the model at the end of epoch %d, iters %d' % (epoch, total_steps)))
            model.save('latest')
            model.save(epoch)
            np.savetxt(iter_path, ((epoch + 1), 0), delimiter=',', fmt='%d')
        if ((opt.niter_fix_global != 0) and (epoch == opt.niter_fix_global)):
            model.update_fixed_params()
        if (epoch > opt.niter):
            model.update_learning_rate()
    torch.cuda.empty_cache()