-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain_color_mnist_feature.py
131 lines (100 loc) · 3.58 KB
/
train_color_mnist_feature.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
import argparse
import os
from pathlib import Path
import torch
import torch.nn.functional as F
from diagan.datasets.predefined import get_predefined_dataset
from diagan.models.convnets import SimpleConvNet
from diagan.utils.settings import set_seed
from diagan.utils.trainer import accuracy, AverageMeter
from torch.utils import data
from torch.utils.data.dataloader import DataLoader
from torchvision import datasets
from torchvision.transforms import transforms
from tqdm import tqdm
def get_color_mnist_transform():
img_size = 32
transform = transforms.Compose([
transforms.Resize(img_size),
transforms.CenterCrop(img_size),
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5),
std=(0.5, 0.5, 0.5))])
return transform
def get_mnist_fmnist_transform():
img_size = 32
transform = transforms.Compose([
transforms.Resize(img_size),
transforms.CenterCrop(img_size),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
return transform
def validate(val_loader, model):
model.eval()
top1 = AverageMeter()
with torch.no_grad():
for idx, (images, labels) in enumerate(val_loader):
images = images.float().cuda()
labels = labels.cuda()
bsz = labels.shape[0]
output, _ = model(images)
acc1, = accuracy(output, labels, topk=(1,))
top1.update(acc1[0], bsz)
return top1.avg
def train(model, tr_loader, optimizer):
model.train()
top1 = AverageMeter()
tr_iter = tqdm(iter(tr_loader))
for x, y, _, _ in tr_iter:
x, y = x.cuda(), y.cuda()
N = x.size(0)
pred, _ = model(x)
loss = F.cross_entropy(pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
prec1, = accuracy(pred, y, topk=(1,))
top1.update(prec1.item(), N)
return top1.avg
def parse_option():
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--bs', type=int, default=64, help='batch_size')
parser.add_argument('--epochs', type=int, default=80)
parser.add_argument('--num_data', type = int, default = 10000)
opt = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = str(opt.gpu)
return opt
def main():
opt = parse_option()
set_seed(opt.seed)
transform = get_color_mnist_transform()
ds_train = get_predefined_dataset(
dataset_name='color_mnist',
root='./dataset/colour_mnist',
weights=None,
major_ratio=0.5,
num_data=opt.num_data
)
dataloader = data.DataLoader(
dataset=ds_train,
batch_size=128,
shuffle=False,
num_workers=8,
pin_memory=True)
model = SimpleConvNet(num_labels = 20).cuda()
model.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, [opt.epochs * 3 // 7, opt.epochs * 6 // 7], gamma=0.1)
print(f'train_biased_model - opt: {optimizer}, sched: {scheduler}')
ckpt_path = Path(f'./exp_results/color-mnist-convnet-{opt.num_data}-seed{opt.seed}')
ckpt_path.mkdir(exist_ok=True, parents=True)
for n in range(1, opt.epochs + 1):
train_acc = train(model, dataloader, optimizer)
print(f'[{n} / {opt.epochs}] train_acc: {train_acc}')
if n % 10 == 0:
torch.save(model.state_dict(), ckpt_path / f'ckpt_{n}.pt')
if __name__ == '__main__':
main()