Пример #1
0
        if (epoch + 1) % args.print_freq == 0:
            for j in range(len(best_acc)):
                print(
                    "epoch:{} model:{} train loss:{:.2f} acc:{:.2f}  val loss{:.2f} acc:{:.2f}"
                    .format(epoch + 1, args.model_names[j], train_losses[j],
                            train_acces[j], val_losses[j], val_acces[j]))

    for k in range(len(best_acc)):
        print("model:{} best acc:{:.2f}".format(args.model_names[k],
                                                best_acc[k]))


if __name__ == '__main__':
    train_loader, val_loader = get_cifar100_dataloaders(
        root=args.root,
        batch_size=args.batch_size,
        num_workers=args.num_workers,
        is_instance=False)
    model_list = []
    optimizer_list = []
    scheduler_list = []
    for name in args.model_names:
        lr = 0.01 if name in ['MobileNetV2', 'ShuffleV1', 'ShuffleV2'
                              ] else args.lr
        model = model_dict[name](num_classes=100)
        if torch.cuda.is_available(): model = model.cuda()

        optimizer = optim.SGD(model.parameters(),
                              lr=lr,
                              momentum=args.momentum,
                              weight_decay=args.weight_decay)
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)

if args.s_arch in ['MobileNetV2', 'ShuffleV1', 'ShuffleV2']:
    args.lr = 0.01

exp_name = f'CAM/nodistill_mag_S_{args.s_arch}'
exp_path = './experiments/{}'.format(exp_name)
os.makedirs(exp_path, exist_ok=True)

print(f"CAM no distillation MAG Student:{args.s_arch} [{exp_path}]")

logger = SummaryWriter(osp.join(exp_path, 'events'), flush_secs=10)

train_loader, val_loader, n_data = get_cifar100_dataloaders(
    root=args.root,
    batch_size=args.batch_size,
    num_workers=4,
    is_instance=True)
args.n_data = n_data

# student model definition
s_model = model_dict[args.s_arch](num_classes=100)
s_model = wrapper(module=s_model, cfg=args).cuda()

# ----------------  start distillation ! -------------------
print("-------------start distillation ! -------------")

# construct kd loss and optimizer

s_optimizer = optim.SGD(chain(s_model.parameters()),
                        lr=args.lr,