コード例 #1
0
            visualizer.display_current_results(i + 1,
                                               epoch_total_corrects /
                                               len(train_dataset),
                                               name='train_epoch_acc')
            # 本epoch的测试集精度
            visualizer.display_current_results(i + 1,
                                               epoch_test_acc,
                                               name='test_epoch_acc')
            visualizer.display_current_results(i + 1, eer, name='eer')
        if epoch_test_acc > best_acc:
            best_acc = epoch_test_acc
            best_model = model
            save_model(best_model, opt, iters, epoch_test_acc,
                       epoch_test_th)  # 训练中途停止的话,可以保存下来截止最好的模型
    return best_model, len(train_data_loader)


def main_train(opt):
    model, metric_fc, criterion, optimizer, scheduler = configure_model(opt)
    trained_model, iters_dataloader = train_val_model(opt, model, metric_fc,
                                                      criterion, optimizer,
                                                      scheduler)
    # save_model(trained_model, opt, opt.max_epoch*iters_dataloader)


if __name__ == "__main__":
    opt = config.opt
    if opt.display:
        visualizer = visualizer.Visualizer(opt.env)
    main_train(opt)
コード例 #2
0
    # create dataset
    dataset = ColorDataset(args)
    print('#training images=%d' % len(dataset))

    Batch_size = args.Batch_size
    shuffle = args.shuffle
    dataLoader = torch.utils.data.DataLoader(dataset,
                                             batch_size=Batch_size,
                                             shuffle=shuffle)

    #create model
    model = create_model(args)
    model.setup(args)
    model.print_networks(True)

    visualizer = visualizer.Visualizer(args)
    total_steps = 0

    # train
    iters = args.iter
    iters_decay = args.iter_decay
    for epoch in range(args.epoch_count, iters):
        epoch_start_time = time.time()
        iter_data_time = time.time()
        epoch_iter = 0

        for i, img in enumerate(dataLoader):
            image = img['img'].cuda()
            data = utils.get_colorization_data(image, args, num_points=5)
            if (data is None):
                print('no data')
コード例 #3
0
def train():
    """
    train function
    :return:
    """

    vis = visualizer.Visualizer(config.visdom_env)

    # step1: configure model
    model = torchvision.models.densenet121(pretrained=False, num_classes=2)
    if config.use_gpu:
        model = torch.nn.DataParallel(model).cuda()

    # step2: data
    train_data = DogCat(config.train_data_root, train=True)
    val_data = DogCat(config.train_data_root, train=False)
    train_dataloader = DataLoader(train_data,
                                  config.batch_size,
                                  shuffle=True,
                                  num_workers=config.num_workers)
    val_dataloader = DataLoader(val_data,
                                config.batch_size,
                                shuffle=False,
                                num_workers=config.num_workers)

    # step3: criterion and optimizer
    criterion = torch.nn.CrossEntropyLoss()
    lr = config.lr
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=lr,
                                 weight_decay=config.weight_decay)

    # step4: meters
    loss_meter = meter.AverageValueMeter()
    confusion_matrix = meter.ConfusionMeter(2)
    previous_loss = 1e100

    # train
    model.train()
    best_acc = -1.0
    start_epoch = -1

    # optionally resume from a checkpoint
    state = dict()
    if config.load_model_path:
        logging.info('Loading checkpoint from {path}'.format(
            path=config.load_model_path))
        state = model_util.load(config.load_model_path)
        start_epoch = state['epoch']
        best_acc = state['accuracy']
        model.load_state_dict(state['state_dic'])
        optimizer.load_state_dict(state['optimizer'])
        logging.info('Loaded checkpoint from {path}'.format(
            path=config.load_model_path))

    for epoch in range(start_epoch + 1, config.max_epoch):

        logging.info('epoch = %d' % epoch)

        loss_meter.reset()
        confusion_matrix.reset()

        for ii, (data, label) in tqdm(enumerate(train_dataloader),
                                      total=len(train_data)):

            # train model
            input_var = Variable(data)
            target_var = Variable(label)
            if config.use_gpu:
                input_var = input_var.cuda()
                target_var = target_var.cuda()

            optimizer.zero_grad()
            score = model(input_var)
            loss = criterion(score, target_var)
            loss.backward()
            optimizer.step()

            # meters update and visualize
            loss_meter.add(loss.data[0])
            confusion_matrix.add(score.data, target_var.data)

            if ii % config.print_freq == config.print_freq - 1:
                vis.plot('loss', loss_meter.value()[0])

                # 进入debug模式
                # if os.path.exists(config.debug_file):
                #     import ipdb;
                #     ipdb.set_trace()

        # validate and visualize
        val_cm, val_accuracy = val(model, val_dataloader)

        vis.plot('val_accuracy', val_accuracy)
        vis.log(
            "epoch:{epoch},lr:{lr},loss:{loss},train_cm:{train_cm},val_cm:{val_cm}"
            .format(epoch=epoch,
                    loss=loss_meter.value()[0],
                    val_cm=str(val_cm.value()),
                    train_cm=str(confusion_matrix.value()),
                    lr=lr))

        is_best = val_accuracy > best_acc
        best_acc = max(val_accuracy, best_acc)

        logging.info(
            "epoch:{epoch},lr:{lr},loss:{loss},acc:{acc} train_cm:{train_cm},val_cm:{val_cm}"
            .format(epoch=epoch,
                    loss=loss_meter.value()[0],
                    acc=val_accuracy,
                    val_cm=str(val_cm.value()),
                    train_cm=str(confusion_matrix.value()),
                    lr=lr))

        state['epoch'] = epoch
        state['model'] = config.model
        state['state_dic'] = model.state_dict()
        state['accuracy'] = val_accuracy
        state['optimizer'] = optimizer.state_dict()
        model_util.save(state, config.checkpoint_dir, is_best)

        # update learning rate
        if loss_meter.value()[0] > previous_loss:
            lr = lr * config.lr_decay
            # 第二种降低学习率的方法:不会有moment等信息的丢失
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr

        previous_loss = loss_meter.value()[0]
コード例 #4
0
                           lr=settings.LEARNING_RATE,
                           betas=settings.BETAS)
opt_E = torch.optim.Adamax(E.parameters(),
                           lr=settings.LEARNING_RATE,
                           betas=settings.BETAS)
opt_toRGB = torch.optim.Adamax(toRGB.parameters(),
                               lr=settings.LEARNING_RATE,
                               betas=settings.BETAS)
opt_fromRGB = torch.optim.Adamax(toRGB.parameters(),
                                 lr=settings.LEARNING_RATE,
                                 betas=settings.BETAS)

reconstruction_loss = nn.L1Loss()
adversarial_loss = nn.MSELoss()

visualizer = vis.Visualizer()
state = json.load(open("working_model/state.json", "r"))
pred_real_history += state["pred_real"]
pred_fake_history += state["pred_fake"]
visualizer.point = state["point"]

if settings.WORKING_MODEL:
    print("Using model parameters in ./working_model")
    G.load_state_dict(torch.load("working_model/G.params"))
    D.load_state_dict(torch.load("working_model/D.params"))
    E.load_state_dict(torch.load("working_model/E.params"))

    toRGB.load_state_dict(torch.load("working_model/toRGB6.params"))
    fromRGB.load_state_dict(torch.load("working_model/fromRGB6.params"))
    print("Loaded RGB layers too")
コード例 #5
0
    def __init__(self, opt):
        super(DDPModel, self).__init__()

        self.gpu_ids = opt.gpu_ids
        self.device = torch.device(
            'cuda:{}'.format(opt.gpu_ids[0])) if opt.gpu_ids else torch.device(
                'cpu')  # get device name: CPU or GPU
        self.save_dir = os.path.join(
            opt.checkpoints_dir,
            opt.name)  # save all the checkpoints to save_dir
        if not os.path.exists(self.save_dir):
            os.makedirs(self.save_dir)
            print('Directory created: %s' % self.save_dir)

        # define schedule
        if opt.beta_schedule == 'cosine':
            """ Cosine Schedule
                @inproceedings{
                    anonymous2021improved,
                    title={Improved Denoising Diffusion Probabilistic Models},
                    author={Anonymous},
                    booktitle={Submitted to International Conference on Learning Representations},
                    year={2021},
                    url={https://openreview.net/forum?id=-NEXDKk8gZ},
                    note={under review}
                }
            """
            s = 0.008
            x = np.linspace(0, opt.num_timesteps - 1, opt.num_timesteps - 1)
            alphas_cumprod = np.cos(
                ((x / opt.num_timesteps) + s) / (1 + s) * np.pi * 0.5)**2
            alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
            alphas_cumprod[alphas_cumprod > 0.999999] = 0.999999
            alphas_cumprod_prev = np.append(1., alphas_cumprod[:-1])
            betas = np.clip(1 - (alphas_cumprod / alphas_cumprod_prev),
                            a_min=0,
                            a_max=0.999)
            alphas = 1. - betas
        else:
            betas = self.get_beta_schedule(opt.beta_schedule, opt.beta_start,
                                           opt.beta_end, opt.num_timesteps)
            alphas = 1. - betas
            alphas_cumprod = np.cumprod(alphas, axis=0)
            alphas_cumprod_prev = np.append(1., alphas_cumprod[:-1])
        assert alphas_cumprod_prev.shape == betas.shape

        assert isinstance(
            betas, np.ndarray) and (betas >= 0).all() and (betas <= 1).all()
        timesteps, = betas.shape
        self.num_timesteps = int(timesteps)

        self.betas = torch.tensor(betas)
        self.alphas_cumprod = torch.tensor(alphas_cumprod)
        self.alphas_cumprod_prev = torch.tensor(alphas_cumprod_prev)

        # calculations for diffusion q(x_t | x_{t-1}) and others
        self.sqrt_alphas_cumprod = torch.tensor(np.sqrt(alphas_cumprod),
                                                dtype=torch.float32,
                                                device=self.device)
        self.sqrt_one_minus_alphas_cumprod = torch.tensor(
            np.sqrt(1. - alphas_cumprod),
            dtype=torch.float32,
            device=self.device)
        self.log_one_minus_alphas_cumprod = torch.tensor(
            np.log(1. - alphas_cumprod),
            dtype=torch.float32,
            device=self.device)
        self.sqrt_recip_alphas_cumprod = torch.tensor(np.sqrt(1. /
                                                              alphas_cumprod),
                                                      dtype=torch.float32,
                                                      device=self.device)
        self.sqrt_recipm1_alphas_cumprod = torch.tensor(
            np.sqrt(1. / alphas_cumprod - 1),
            dtype=torch.float32,
            device=self.device)

        # calculations for posterior q(x_{t-1} | x_t, x_0)
        posterior_variance = betas * (1. - alphas_cumprod_prev) / (
            1. - alphas_cumprod)
        # above: equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t)
        self.posterior_variance = torch.tensor(posterior_variance,
                                               dtype=torch.float32,
                                               device=self.device)
        # below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain
        self.posterior_log_variance_clipped = torch.tensor(np.log(
            np.maximum(posterior_variance, 1e-20)),
                                                           dtype=torch.float32,
                                                           device=self.device)
        self.posterior_mean_coef1 = torch.tensor(
            betas * np.sqrt(alphas_cumprod_prev) / (1. - alphas_cumprod),
            dtype=torch.float32,
            device=self.device)
        self.posterior_mean_coef2 = torch.tensor(
            (1. - alphas_cumprod_prev) * np.sqrt(alphas) /
            (1. - alphas_cumprod),
            dtype=torch.float32,
            device=self.device)

        # setup denoise model
        model = []
        if opt.block_size != 1:
            model += [utils.SpaceToDepth(opt.block_size)]
        model += [
            unet.Unet(opt.input_nc,
                      opt.input_nc,
                      num_middles=1,
                      ngf=opt.ngf,
                      norm=opt.norm,
                      activation=opt.activation,
                      use_dropout=opt.dropout,
                      use_attention=opt.attention,
                      device=self.device)
        ]
        if opt.block_size != 1:
            model += [utils.SpaceToDepth(opt.block_size)]
        self.denoise_model = utils.init_net(nn.Sequential(*model),
                                            opt.init_type, opt.init_gain,
                                            opt.gpu_ids)

        if opt.phase == 'train':
            # setup optimizer, visualizer, and learning rate scheduler
            self.optimizer = torch.optim.Adam(self.denoise_model.parameters(),
                                              lr=opt.lr,
                                              betas=(opt.beta1, 0.999))
            if 'mse' in opt.loss_type:
                self.loss_criteria = nn.MSELoss()
            elif 'l1' in opt.loss_type:
                self.loss_criteria = nn.L1Loss()
            else:
                raise NotImplementedError(opt.loss_type)
            # set prediction function
            if 'noisepred' in opt.loss_type:
                self.pred_fn = DDPModel._noisepred
            else:
                raise NotImplementedError(opt.loss_type)
            self.loss_type = opt.loss_type
            self.visualizer = visualizer.Visualizer(opt)
            self.scheduler = utils.get_scheduler(self.optimizer, opt)
            self.lr_policy = opt.lr_policy
        else:
            self.image_size = (opt.batch_size, opt.input_nc, opt.load_size,
                               opt.load_size)
            self.denoise_model.train(False)
            # set prediction function
            if 'noisepred' in opt.loss_type:
                self.pred_fn = self.predict_start_from_noise
            else:
                raise NotImplementedError(opt.loss_type)

        if opt.phase == 'interpolate':
            self.mix_rate = opt.mix_rate
コード例 #6
0
opt = options.GatherOptions().parse()
cagan_dataset = cagan_dataset.CAGAN_Dataset(opt)

if opt.mode == "train":

    train_dataloader = data.DataLoader(cagan_dataset,
                                       opt.batchsize,
                                       shuffle=True,
                                       num_workers=opt.num_workers,
                                       pin_memory=True)
    # calculate num of steps for decaying learning to zero at the end of training
    if (opt.lr_policy == 'lambda') and (opt.niter_decay == -1):
        opt.niter_decay = opt.epoch * len(train_dataloader) - opt.niter
    model = trainer.CAGAN_Trainer(opt)

    vis_custom = visualizer.Visualizer(opt.mode)
    vis_custom.reset_env()
    loss_save_path = os.path.join(opt.save_dir, 'loss.dict')
    step = 0
    if opt.resume:
        step = opt.step
        model.load_networks(step=step, load_netD=True)
        try:
            vis_custom.recover_loss(loss_save_path)
        except:
            print("Loss dict can not be found in %s" % opt.save_dir)

    for epoch in range(opt.epoch):
        if opt.lr_policy not in ['lambda']:
            model.update_learning_rate()
        for real in train_dataloader:
コード例 #7
0
import torch

import utils.datasets as data
from utils import visualizer

dataset = data.SyntheticFullyAnnotated("~/Data/DeepGeneration1")
visualizer = visualizer.Visualizer()

data_point = dataset[182]
visualizer.update_image(data_point[0], "real_img")
visualizer.update_image(data_point[1], "real_map")
visualizer.update_image(data_point.mean(0), "real_cat")

a = torch.randn(1)
b = torch.randn(1)
for i in range(100):
    a += torch.randn(1)
    b += torch.randn(1)
    visualizer.update_loss(a, b)

コード例 #8
0
from options import test_options
from dataloader import data_loader
from model import create_model
from utils import visualizer
from itertools import islice

if __name__ == '__main__':
    # get testing options
    opt = test_options.TestOptions().parse()
    # creat a dataset
    dataset = data_loader.dataloader(opt)
    dataset_size = len(dataset) * opt.batchSize
    print('testing images = %d' % dataset_size)
    # create a model
    model = create_model(opt)
    model.eval()
    # create a visualizer
    visualizer = visualizer.Visualizer(opt)

    for i, data in enumerate(islice(dataset, opt.how_many)):
        model.set_input(data)
        model.test()