-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.py
157 lines (126 loc) · 5.37 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding:utf-8 -*-
import os
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from data.datasets import input_dataset
from models import *
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--lr', type = float, default = 0.1)
parser.add_argument('--noise_type', type = str, help='clean, aggre, worst, rand1, rand2, rand3, clean100, noisy100', default='clean')
parser.add_argument('--noise_path', type = str, help='path of CIFAR-10_human.pt', default=None)
parser.add_argument('--dataset', type = str, help = ' cifar10 or cifar100', default = 'cifar10')
parser.add_argument('--n_epoch', type=int, default=100)
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--print_freq', type=int, default=50)
parser.add_argument('--num_workers', type=int, default=4, help='how many subprocesses to use for data loading')
parser.add_argument('--is_human', action='store_true', default=False)
# Adjust learning rate and for SGD Optimizer
def adjust_learning_rate(optimizer, epoch,alpha_plan):
for param_group in optimizer.param_groups:
param_group['lr']=alpha_plan[epoch]
def accuracy(logit, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
output = F.softmax(logit, dim=1)
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
# Train the Model
def train(epoch, train_loader, model, optimizer):
train_total=0
train_correct=0
for i, (images, labels, indexes) in enumerate(train_loader):
ind=indexes.cpu().numpy().transpose()
batch_size = len(ind)
images = Variable(images).cuda()
labels = Variable(labels).cuda()
# Forward + Backward + Optimize
logits = model(images)
prec, _ = accuracy(logits, labels, topk=(1, 5))
# prec = 0.0
train_total+=1
train_correct+=prec
loss = F.cross_entropy(logits, labels, reduce = True)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % args.print_freq == 0:
print ('Epoch [%d/%d], Iter [%d/%d] Training Accuracy: %.4F, Loss: %.4f'
%(epoch+1, args.n_epoch, i+1, len(train_dataset)//batch_size, prec, loss.data))
train_acc=float(train_correct)/float(train_total)
return train_acc
# Evaluate the Model
def evaluate(test_loader, model):
model.eval() # Change model to 'eval' mode.
print('previous_best', best_acc_)
correct = 0
total = 0
for images, labels, _ in test_loader:
images = Variable(images).cuda()
logits = model(images)
outputs = F.softmax(logits, dim=1)
_, pred = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (pred.cpu() == labels).sum()
acc = 100*float(correct)/float(total)
return acc
#####################################main code ################################################
args = parser.parse_args()
# Seed
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
# Hyper Parameters
batch_size = 128
learning_rate = args.lr
noise_type_map = {'clean':'clean_label', 'worst': 'worse_label', 'aggre': 'aggre_label', 'rand1': 'random_label1', 'rand2': 'random_label2', 'rand3': 'random_label3', 'clean100': 'clean_label', 'noisy100': 'noisy_label'}
args.noise_type = noise_type_map[args.noise_type]
# load dataset
if args.noise_path is None:
if args.dataset == 'cifar10':
args.noise_path = './data/CIFAR-10_human.pt'
elif args.dataset == 'cifar100':
args.noise_path = './data/CIFAR-100_human.pt'
else:
raise NameError(f'Undefined dataset {args.dataset}')
train_dataset,test_dataset,num_classes,num_training_samples = input_dataset(args.dataset,args.noise_type, args.noise_path, args.is_human)
noise_prior = train_dataset.noise_prior
noise_or_not = train_dataset.noise_or_not
print('train_labels:', len(train_dataset.train_labels), train_dataset.train_labels[:10])
# load model
print('building model...')
model = ResNet34(num_classes)
print('building model done')
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, weight_decay=0.0005, momentum=0.9)
train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
batch_size = 128,
num_workers=args.num_workers,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size = 64,
num_workers=args.num_workers,
shuffle=False)
alpha_plan = [0.1] * 60 + [0.01] * 40
model.cuda()
epoch=0
train_acc = 0
# training
noise_prior_cur = noise_prior
for epoch in range(args.n_epoch):
# train models
print(f'epoch {epoch}')
adjust_learning_rate(optimizer, epoch, alpha_plan)
model.train()
train_acc = train(epoch, train_loader, model, optimizer)
# evaluate models
test_acc = evaluate(test_loader=test_loader, model=model)
# save results
print('train acc on train images is ', train_acc)
print('test acc on test images is ', test_acc)