forked from haochenglouis/SelfSup_NoisyLabel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CE.py
167 lines (134 loc) · 5.43 KB
/
CE.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
158
159
160
161
162
163
164
#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import argparse
import sys
import builtins
import os
import random
import shutil
import time
import warnings
from PIL import Image
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.multiprocessing as mp
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.models as models
import torch.optim as optim
from torchvision.datasets import CIFAR10
from cifar_noisy import CIFAR10_noisy
from sklearn import manifold
import numpy as np
from sklearn import manifold
from model import Model
np.random.seed(0)
parser = argparse.ArgumentParser(description='Cross Entropy')
parser.add_argument('--model_path', type=str, default='results/128_0.5_200_512_1000_model.pth',
help='The pretrained model path')
parser.add_argument('--batch_size', type=int, default=256, help='Number of images in each mini-batch')
parser.add_argument('--lr', type = float, default = 0.001)
parser.add_argument('--epochs', type=int, default=150, help='Number of sweeps over the dataset to train')
parser.add_argument('--num_classes', type=int, default=10, help='Number of classes')
parser.add_argument('--noise_rate', type = float, help = 'corruption rate, should be less than 1', default = 0.6)
parser.add_argument('--noise_type', type = str, help='[pairflip, symmetric,instance]', default='symmetric')
parser.add_argument('--simclr_pretrain', action='store_true')
parser.add_argument('--finetune_fc_only', action='store_true')
parser.add_argument('--down_sample', action='store_true')
args = parser.parse_args()
class Net(nn.Module):
def __init__(self, num_class, pretrained_path):
super(Net, self).__init__()
# encoder
self.f = Model().f
# classifier
self.fc = nn.Linear(512, num_class, bias=True)
if pretrained_path is not None:
self.load_state_dict(torch.load(pretrained_path, map_location='cpu'), strict=False)
def forward(self, x):
x = self.f(x)
feature = torch.flatten(x, start_dim=1)
out = self.fc(feature)
return out
if args.simclr_pretrain:
model = Net(num_class=args.num_classes, pretrained_path=args.model_path).cuda()
else:
model = Net(num_class=args.num_classes, pretrained_path=None).cuda()
if args.finetune_fc_only:
for param in model.f.parameters():
param.requires_grad = False
train_cifar10_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
test_cifar10_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
train_dataset = CIFAR10_noisy(root='./data/',indexes = None,
train=True, down_sample = args.down_sample,
transform = train_cifar10_transform,
noise_type= args.noise_type,noise_rate=args.noise_rate, random_state=0)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size = args.batch_size,
num_workers=32,
shuffle=True,pin_memory=True)
test_dataset = CIFAR10(root='data', train=False, transform=test_cifar10_transform, download=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size = args.batch_size,
num_workers=32,
shuffle=False,pin_memory=True)
model.cuda()
criterion = nn.CrossEntropyLoss().cuda()
if args.simclr_pretrain:
alpha_plan = [0.001] * args.epochs
optimizer = optim.Adam(model.parameters(), lr=0.001)
else:
alpha_plan = [0.1] * 50 + [0.01] * (args.epochs - 50)
optimizer = optim.SGD(model.parameters(), lr=0.1)
def adjust_learning_rate(optimizer, epoch,alpha_plan):
for param_group in optimizer.param_groups:
param_group['lr']=alpha_plan[epoch]
best_acc = [0]
def validate(val_loader, model, criterion):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for i, (images, labels) in enumerate(val_loader):
images = Variable(images).cuda()
# compute output
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
for epoch in range(args.epochs):
if args.finetune_fc_only:
model.eval()
else:
model.train()
adjust_learning_rate(optimizer, epoch, alpha_plan)
for i, (images, labels,true_labels,indexes) in enumerate(train_loader):
images = Variable(images).cuda()
labels = Variable(labels).cuda()
output = model(images)
loss = criterion(output, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
acc1 = validate(test_loader, model, criterion)
if acc1>best_acc[0]:
best_acc[0] = acc1
print('current epoch',epoch)
print('best acc',best_acc[0])
print('last acc', acc1)