Пример #1
0
            val_accs_raw.append(
                (y_pred_raw.argmax(1) == y).float().mean().item())
            val_accs_crop.append(
                (y_pred_crop.argmax(1) == y).float().mean().item())
    if CALLBACK:
        CALLBACK.scalar('Val_Loss', epoch + 1, val_losses.last_avg)
        CALLBACK.scalar('Val_Acc', epoch + 1, val_accs.last_avg)
        CALLBACK.scalar('Val_Acc_Raw', epoch + 1, val_accs_raw.last_avg)
        CALLBACK.scalar('Val_Acc_Crop', epoch + 1, val_accs_crop.last_avg)

    print(
        '\nEpoch {} | Validation | Loss: {:.4f}, Accs: {:.4f}, Accs_raw: {:.4f}, Accs_crop: {:.4f}'
        .format(epoch, val_losses.avg, val_accs.avg, val_accs_raw.avg,
                val_accs_crop.avg))

    valacc_final = val_accs.avg

    if epoch % 5 == 0 or batch_loss.item() < 0.01:
        save_checkpoint(model.state_dict(),
                        os.path.join(
                            args.save_folder, args.save_name + '_valacc' +
                            str(valacc_final) + '_' + str(epoch) + '.pth'),
                        cpu=True)
print('\nSaving model...')
save_checkpoint(model.state_dict(),
                os.path.join(
                    args.save_folder,
                    args.save_name + '_valacc' + str(valacc_final) + '.pth'),
                cpu=True)
CALLBACK.save([args.visdom_env])
send_email("cifar10_wsdan 0.2实验完成\nval acc = " + str(valacc_final))
Пример #2
0
            val_accs.append((logits.argmax(1) == labels).float().mean().item())
            val_losses.append(loss.item())

    if CALLBACK:
        CALLBACK.scalar('Val_Loss', epoch + 1, val_losses.last_avg)
        CALLBACK.scalar('Val_Acc', epoch + 1, val_accs.last_avg)

    print('Epoch {} | Validation | Loss: {:.4f}, Accs: {:.4f}'.format(
        epoch, val_losses.avg, val_accs.avg))

    save_path = 'weights/best/'
    if val_accs.avg >= best_acc:  # args.adv is None and
        best_acc = val_accs.avg
        best_epoch = epoch
        best_dict = deepcopy(model.state_dict())
        files2remove = glob.glob(os.path.join(save_path, 'shape_ALP_*'))
        for _i in files2remove:
            os.remove(_i)
        strsave = "shape_ALP_cifar10_ep_%d_val_acc%.4f.pth" % (epoch, best_acc)
        torch.save(model.cpu().state_dict(), os.path.join(save_path, strsave))
        model.to(DEVICE)

    if args.adv is None and val_accs.avg >= best_acc:
        best_acc = val_accs.avg
        best_epoch = epoch
        best_dict = deepcopy(model.state_dict())

    if not (epoch + 1) % args.save_freq:
        save_checkpoint(model.state_dict(),
                        os.path.join(
            logits = model(images)
            loss = F.cross_entropy(logits, labels)

            val_accs.append((logits.argmax(1) == labels).float().mean().item())
            val_losses.append(loss.item())

    if CALLBACK:
        CALLBACK.scalar('Val_Loss', epoch + 1, val_losses.last_avg)
        CALLBACK.scalar('Val_Acc', epoch + 1, val_accs.last_avg)

    print('Epoch {} | Validation | Loss: {:.4f}, Accs: {:.4f}'.format(epoch, val_losses.avg, val_accs.avg))

    if args.adv is None and val_accs.avg >= best_acc:
        best_acc = val_accs.avg
        best_epoch = epoch
        best_dict = deepcopy(model.state_dict())

    if not (epoch + 1) % args.save_freq:
        save_checkpoint(
            model.state_dict(), os.path.join(args.save_folder, args.save_name + '_{}.pth'.format(epoch + 1)), cpu=True)

if args.adv is None:
    model.load_state_dict(best_dict)

test_accs = AverageMeter()
test_losses = AverageMeter()

with torch.no_grad():
    for i, (images, labels) in enumerate(tqdm.tqdm(test_loader, ncols=80)):
        images, labels = images.to(DEVICE), labels.to(DEVICE)