Пример #1
0
def main():
	args = parser.parse_args()
	for arg in vars(args):
		print(arg, " : ", getattr(args, arg))
	
	augment = not args.no_augment
	train_loader, val_loader = train_util.load_data(
		args.dataset, 
		args.batch_size, 
		dataset_path=args.data_dir,
		augment=augment)
	
	print("=> creating model '{}'".format(args.arch))
	model_args = {
		"num_classes": 10 if args.dataset == "cifar10" else 100	
	}
	model = models.__dict__[args.arch](**model_args)
	print("Device count", torch.cuda.device_count())
	if args.parallel:
		model = nn.DataParallel(model)

	print('Number of model parameters: {}'.format(
		sum([p.data.nelement() for p in model.parameters()])))

	model = model.cuda()

	cudnn.benchmark = True

	criterion = nn.CrossEntropyLoss().cuda()
	optim_hparams = {
		'base_lr' : args.lr, 
		'momentum' : args.momentum,
		'weight_decay' : args.weight_decay,
		'optim_type' : args.optim_type
	}

	lr_hparams = {
		'lr_sched' : args.lr_sched, 
		'use_iter': args.train_by_iters}

	lr_hparams['iters_per_epoch'] = args.iters_per_epoch if args.iters_per_epoch else 391

	inner_lr_hparams = {
		'lr_sched' : args.inner_anneal,
		'use_iter' : args.train_by_iters}

	inner_lr_hparams['iters_per_epoch'] = args.iters_per_epoch if args.iters_per_epoch else 391

	optimizer = optim_util.create_optimizer(
		model,
		optim_hparams)

	curr_iter = args.start_iter
	epoch = args.start_epoch

	best_val = 0

	inner_opt = optim_util.one_step_optim(
		model, args.inner_lr)
	while True:
		model.train()
		train_acc = train_util.AverageMeter()
		train_loss = train_util.AverageMeter()
		timer = train_util.AverageMeter()
		for i, (input_data, target) in enumerate(train_loader):
					
			lr = lr_util.adjust_lr(
				optimizer,
				epoch,
				curr_iter,
				args.lr,
				lr_hparams)

			inner_lr = lr_util.adjust_lr(
				inner_opt,
				epoch,
				curr_iter,
				args.inner_lr,
				inner_lr_hparams)

			target = target.cuda(non_blocking=True)
			input_data = input_data.cuda()

			update_hparams = {
				'update_type' : args.update_type.split('zero_switch_')[-1],
				'inner_lr' : inner_lr[0],
				'use_bn' : not args.no_bn,
				'label_noise' : 0,
				'use_norm_one' : args.use_norm_one
			}

			if args.label_noise > 0:
				label_noise = train_util.label_noise_sched(
					args.label_noise, 
					epoch, 
					curr_iter, 
					args.train_by_iters, 
					args.ln_sched, 
					iters_per_epoch=args.iters_per_epoch,
					ln_decay=args.ln_decay)
				if args.update_type != 'mean_zero_label_noise' or args.also_flip_labels:
					# if it is equal, we don't want to flip the labels
					target = train_util.apply_label_noise(
						target,
						label_noise,
						num_classes=10 if args.dataset == 'cifar10' else 100)

				update_hparams['label_noise'] = label_noise
				
			loss, output, time_taken = update_loss_util.update_step(
				criterion,
				optimizer,
				model,
				input_data,
				target,
				update_hparams)

			prec1 = accuracy(output.data, target, topk=(1,))[0]
			train_loss.update(loss, target.size(0))
			train_acc.update(prec1, target.size(0))
			timer.update(time_taken, 1)
			avg_loss = train_loss.avg
			avg_acc = train_acc.avg

			loss_str = 'Loss '
			loss_str += '{:.4f} (standard)\t'.format(avg_loss)


			if i % args.print_freq == 0:
				log_str = ('Epoch: [{0}][{1}/{2}]\t'

				  'Time {3:.3f}\t {4}'
				  'Prec@1 {5:.3f})').format(
					  epoch, i, len(train_loader), timer.avg, loss_str, avg_acc)
				print(log_str)

			curr_iter += 1

		print("Validating accuracy.")
		val_acc, val_loss = train_util.validate(
			val_loader,
			model,
			criterion,
			epoch,
			print_freq=args.print_freq)

		is_best = val_acc > best_val
		best_val = val_acc if is_best else best_val

		print('Best accuracy: ', best_val)

		epoch += 1
		if args.train_by_iters:
			if curr_iter > args.iters:
				break
		else:
			if epoch > args.epochs:
				break
Пример #2
0
def train(
    conf,
    step,
    epoch,
    loader,
    model,
    model_ema,
    criterion,
    optimizer,
    scheduler,
    scaler,
    grad_accum,
):
    device = "cuda"

    batch_time = Meter()
    data_time = Meter()
    losses = Meter()
    top1 = Meter()
    top5 = Meter()

    model.train()

    agc_params = [
        p[1] for p in model.named_parameters() if "linear" not in p[0]
    ]
    params = list(model.parameters())

    logger = get_logger(mode=conf.logger)

    start = perf_counter()
    for i, (input, label1, label2, ratio) in enumerate(loader):
        # measure data loading time
        input = input.to(device)
        label1 = label1.to(device)
        label2 = label2.to(device)
        ratio = ratio.to(device=device, dtype=torch.float32)
        data_time.update(perf_counter() - start)

        with amp.autocast(enabled=conf.fp16):
            out = model(input)
            loss = criterion(out, label1, label2, ratio) / grad_accum

        prec1, prec5 = accuracy(out, label1, topk=(1, 5))
        batch = input.shape[0]
        losses.update(loss.item() * grad_accum, batch)
        top1.update(prec1.item(), batch)
        top5.update(prec5.item(), batch)

        scaler.scale(loss).backward()

        if ((i + 1) % grad_accum == 0) or (i + 1) == len(loader):
            if conf.training.agc > 0 or conf.training.clip_grad_norm > 0:
                if conf.fp16:
                    scaler.unscale_(optimizer)

                if conf.training.agc > 0:
                    adaptive_grad_clip(agc_params, conf.training.agc)

                if conf.training.clip_grad_norm > 0:
                    nn.utils.clip_grad_norm_(params,
                                             conf.training.clip_grad_norm)

            scheduler.step()
            scaler.step(optimizer)
            scaler.update()
            optimizer.zero_grad(set_to_none=True)

        # optimizer.step()
        t = step + i

        if conf.training.ema > 0:
            if conf.distributed:
                model_module = model.module

            else:
                model_module = model

            accumulate(
                model_ema,
                model_module,
                min(conf.training.ema, (1 + t) / (10 + t)),
                ema_bn=conf.training.ema_bn,
            )

        batch_time.update(perf_counter() - start)
        start = perf_counter()

        if dist.is_primary() and i % conf.log_freq == 0:
            lr = optimizer.param_groups[0]["lr"]

            logger.info(
                f"epoch: {epoch} ({i}/{len(loader)}); time: {batch_time.val:.3f} ({batch_time.avg:.2f}); "
                f"data: {data_time.val:.3f} ({data_time.avg:.2f}); "
                f"loss: {losses.val:.3f} ({losses.avg:.3f}); "
                f"prec@1: {top1.val:.2f} ({top1.avg:.2f}); "
                f"prec@5: {top5.val:.2f} ({top5.avg:.2f})")

    return losses
Пример #3
0
time.sleep(1.5)
train_util.display_prediction(random_test_image(),
                              model,
                              topk=5,
                              model_name=model_choice)
time.sleep(1.5)
train_util.display_prediction(random_test_image(),
                              model,
                              topk=5,
                              model_name=model_choice)

testiter = iter(dataloaders['test'])
# Get a batch of testing images and labels
features, targets = next(testiter)

acc_res = train_util.accuracy(model(features.to('cuda')), targets, topk=(1, 5))
print(f'Top1 정확도 : {acc_res[0]}%')
print(f'Top5 정확도 : {acc_res[1]}%')

criterion = nn.NLLLoss()
# Evaluate the model on all the training data
results = train_util.evaluate(model, dataloaders['test'], criterion, n_classes)
print("\n전체 카테고리별 결과")
print(results)

results.sort_values('top1', ascending=False, inplace=True)
print("\nTop1 정확도가 가장 높은 카테고리 5개")
print(results.head())

print("\nTop1 정확도가 가장 낮은 카테고리 5개")
print(results.tail())