forked from VITA-Group/Alleviate-Robust-Overfitting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_std.py
137 lines (110 loc) · 5.54 KB
/
main_std.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'''
Adversarial Training
'''
import os
import sys
import torch
import pickle
import argparse
import torch.optim
import torch.nn as nn
import torch.utils.data
import matplotlib.pyplot as plt
import torchvision.models as models
from utils import *
parser = argparse.ArgumentParser(description='PyTorch Standard Training')
########################## data setting ##########################
parser.add_argument('--data', type=str, default='data/cifar10', help='location of the data corpus', required=True)
parser.add_argument('--dataset', type=str, default='cifar10', help='dataset [cifar10, cifar100, tinyimagenet]', required=True)
########################## model setting ##########################
parser.add_argument('--arch', type=str, default='resnet18', help='model architecture [resnet18, wideresnet, vgg16]', required=True)
parser.add_argument('--depth_factor', default=34, type=int, help='depth-factor of wideresnet')
parser.add_argument('--width_factor', default=10, type=int, help='width-factor of wideresnet')
########################## basic setting ##########################
parser.add_argument('--seed', default=None, type=int, help='random seed')
parser.add_argument('--gpu', type=int, default=0, help='gpu device id')
parser.add_argument('--resume', action="store_true", help="resume from checkpoint")
parser.add_argument('--pretrained', default=None, type=str, help='pretrained model')
parser.add_argument('--eval', action="store_true", help="evaluation pretrained model")
parser.add_argument('--print_freq', default=50, type=int, help='logging frequency during training')
parser.add_argument('--save_dir', help='The directory used to save the trained models', default=None, type=str)
########################## training setting ##########################
parser.add_argument('--batch_size', type=int, default=128, help='batch size')
parser.add_argument('--lr', default=0.1, type=float, help='initial learning rate')
parser.add_argument('--decreasing_lr', default='50,150', help='decreasing strategy')
parser.add_argument('--momentum', default=0.9, type=float, help='momentum')
parser.add_argument('--weight_decay', default=5e-4, type=float, help='weight decay')
parser.add_argument('--epochs', default=200, type=int, help='number of total epochs to run')
def main():
args = parser.parse_args()
print(args)
torch.cuda.set_device(int(args.gpu))
if args.seed:
print('set random seed = ', args.seed)
setup_seed(args.seed)
train_loader, val_loader, test_loader, model = setup_dataset_models_standard(args)
model.cuda()
########################## optimizer and scheduler ##########################
decreasing_lr = list(map(int, args.decreasing_lr.split(',')))
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=decreasing_lr, gamma=0.1)
######################### only evaluation ###################################
if args.eval:
assert args.pretrained
pretrained_model = torch.load(args.pretrained, map_location = torch.device('cuda:'+str(args.gpu)))
if 'state_dict' in pretrained_model.keys():
pretrained_model = pretrained_model['state_dict']
model.load_state_dict(pretrained_model)
test(test_loader, model, criterion, args)
return
os.makedirs(args.save_dir, exist_ok=True)
########################## resume ##########################
start_epoch = 0
if args.resume:
print('resume from checkpoint.pth.tar')
checkpoint = torch.load(os.path.join(args.save_dir, 'checkpoint.pth.tar'), map_location = torch.device('cuda:'+str(args.gpu)))
best_sa = checkpoint['best_sa']
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
all_result = checkpoint['result']
else:
all_result = {}
all_result['train_acc'] = []
all_result['val_sa'] = []
all_result['test_sa'] = []
best_sa = 0
########################## training process ##########################
for epoch in range(start_epoch, args.epochs):
print(optimizer.state_dict()['param_groups'][0]['lr'])
train_acc = train_epoch(train_loader, model, criterion, optimizer, epoch, args)
all_result['train_acc'].append(train_acc)
scheduler.step()
###validation###
val_sa = test(val_loader, model, criterion, args)
test_sa = test(test_loader, model, criterion, args)
all_result['val_sa'].append(val_sa)
all_result['test_sa'].append(test_sa)
is_sa_best = val_sa > best_sa
best_sa = max(val_sa, best_sa)
checkpoint_state = {
'best_sa': best_sa,
'epoch': epoch+1,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'result': all_result
}
save_checkpoint(checkpoint_state, is_sa_best, False, False, False, args.save_dir)
plt.plot(all_result['train_acc'], label='train_acc')
plt.plot(all_result['test_sa'], label='test_SA')
plt.plot(all_result['val_sa'], label='val_SA')
plt.legend()
plt.savefig(os.path.join(args.save_dir, 'net_train.png'))
plt.close()
if __name__ == '__main__':
main()