forked from Yunfan-Li/Contrastive-Clustering
-
Notifications
You must be signed in to change notification settings - Fork 22
/
train.py
125 lines (119 loc) · 4.63 KB
/
train.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
import os
import numpy as np
import torch
import torchvision
import argparse
from modules import transform, resnet, network, contrastive_loss
from utils import yaml_config_hook, save_model
from torch.utils import data
def train():
loss_epoch = 0
for step, ((x_i, x_j), _) in enumerate(data_loader):
optimizer.zero_grad()
x_i = x_i.to('cuda')
x_j = x_j.to('cuda')
z_i, z_j, c_i, c_j = model(x_i, x_j)
loss_instance = criterion_instance(z_i, z_j)
loss_cluster = criterion_cluster(c_i, c_j)
loss = loss_instance + loss_cluster
loss.backward()
optimizer.step()
if step % 50 == 0:
print(
f"Step [{step}/{len(data_loader)}]\t loss_instance: {loss_instance.item()}\t loss_cluster: {loss_cluster.item()}")
loss_epoch += loss.item()
return loss_epoch
if __name__ == "__main__":
parser = argparse.ArgumentParser()
config = yaml_config_hook("config/config.yaml")
for k, v in config.items():
parser.add_argument(f"--{k}", default=v, type=type(v))
args = parser.parse_args()
if not os.path.exists(args.model_path):
os.makedirs(args.model_path)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed)
# prepare data
if args.dataset == "CIFAR-10":
train_dataset = torchvision.datasets.CIFAR10(
root=args.dataset_dir,
download=True,
train=True,
transform=transform.Transforms(size=args.image_size, s=0.5),
)
test_dataset = torchvision.datasets.CIFAR10(
root=args.dataset_dir,
download=True,
train=False,
transform=transform.Transforms(size=args.image_size, s=0.5),
)
dataset = data.ConcatDataset([train_dataset, test_dataset])
class_num = 10
elif args.dataset == "CIFAR-100":
train_dataset = torchvision.datasets.CIFAR100(
root=args.dataset_dir,
download=True,
train=True,
transform=transform.Transforms(size=args.image_size, s=0.5),
)
test_dataset = torchvision.datasets.CIFAR100(
root=args.dataset_dir,
download=True,
train=False,
transform=transform.Transforms(size=args.image_size, s=0.5),
)
dataset = data.ConcatDataset([train_dataset, test_dataset])
class_num = 20
elif args.dataset == "ImageNet-10":
dataset = torchvision.datasets.ImageFolder(
root='datasets/imagenet-10',
transform=transform.Transforms(size=args.image_size, blur=True),
)
class_num = 10
elif args.dataset == "ImageNet-dogs":
dataset = torchvision.datasets.ImageFolder(
root='datasets/imagenet-dogs',
transform=transform.Transforms(size=args.image_size, blur=True),
)
class_num = 15
elif args.dataset == "tiny-ImageNet":
dataset = torchvision.datasets.ImageFolder(
root='datasets/tiny-imagenet-200/train',
transform=transform.Transforms(s=0.5, size=args.image_size),
)
class_num = 200
else:
raise NotImplementedError
data_loader = torch.utils.data.DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=True,
drop_last=True,
num_workers=args.workers,
)
# initialize model
res = resnet.get_resnet(args.resnet)
model = network.Network(res, args.feature_dim, class_num)
model = model.to('cuda')
# optimizer / loss
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
if args.reload:
model_fp = os.path.join(args.model_path, "checkpoint_{}.tar".format(args.start_epoch))
checkpoint = torch.load(model_fp)
model.load_state_dict(checkpoint['net'])
optimizer.load_state_dict(checkpoint['optimizer'])
args.start_epoch = checkpoint['epoch'] + 1
loss_device = torch.device("cuda")
criterion_instance = contrastive_loss.InstanceLoss(args.batch_size, args.instance_temperature, loss_device).to(
loss_device)
criterion_cluster = contrastive_loss.ClusterLoss(class_num, args.cluster_temperature, loss_device).to(loss_device)
# train
for epoch in range(args.start_epoch, args.epochs):
lr = optimizer.param_groups[0]["lr"]
loss_epoch = train()
if epoch % 10 == 0:
save_model(args, model, optimizer, epoch)
print(f"Epoch [{epoch}/{args.epochs}]\t Loss: {loss_epoch / len(data_loader)}")
save_model(args, model, optimizer, args.epochs)