-
Notifications
You must be signed in to change notification settings - Fork 9
/
cluster.py
177 lines (150 loc) · 6.35 KB
/
cluster.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
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import torch
import torch.nn as nn
import argparse
from tqdm import tqdm
from torchvision import transforms
import numpy as np
from cluster.kmeans import Kmeans
from sklearn.metrics.cluster import normalized_mutual_info_score
from cluster.hungarian import reAssignSingle, reAssignMultiply
import json
import src.resnet as resnet_model
from src.singlecropdataset import ClusterImageFolder
parser = argparse.ArgumentParser(description="Argument For Eval")
parser.add_argument("--num_workers", type=int, default=32, help="num of workers to use")
parser.add_argument("-a", "--arch", default="resnet50", type=str, help="convnet architecture")
parser.add_argument("-b", "--batch_size", default=256, type=int, metavar="N", help="batch size")
parser.add_argument("-c", "--num_classes", default=50, type=int, help="the number of classes")
parser.add_argument("-s", "--seed", default=None, type=int, help="the seed for clustering")
parser.add_argument("--pretrained", type=str, default=None, help="the model checkpoint")
parser.add_argument("--data_path", type=str, default=None, help="path to data")
parser.add_argument("--dump_path", type=str, default=None, help="path to save clustering results")
args = parser.parse_args()
def main():
model = resnet_model.__dict__[args.arch](hidden_mlp=0, output_dim=0, nmb_prototypes=0, train_mode='pixelattn')
# loading pretrained weights
checkpoint = torch.load(args.pretrained, map_location="cpu")["state_dict"]
state_dict = {}
for k in checkpoint.keys():
if k.startswith("module") and not k.startswith("module.prototypes") and not k.startswith("module.projection"):
state_dict[k[len("module.") :]] = checkpoint[k]
msg = model.load_state_dict(state_dict, strict=False)
print("=> loaded model '{}'".format(args.pretrained))
assert len(msg.missing_keys) == 0, msg.missing_keys
model = nn.DataParallel(model)
model.cuda()
model.eval()
# build datasets
train_folder = os.path.join(args.data_path, "train")
val_folder = os.path.join(args.data_path, "validation")
mask_folder = os.path.join(args.data_path, "validation-segmentation")
dump_path = os.path.join(args.dump_path, "cluster")
if not os.path.exists(dump_path):
os.makedirs(dump_path)
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
train_dataset = ClusterImageFolder(
train_folder,
transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]
),
)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
pin_memory=True,
)
val_dataset = ClusterImageFolder(
val_folder,
transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]
)
)
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=1, num_workers=args.num_workers, pin_memory=True
)
# extracting features
print("Extracting features ...")
_, train_targets, train_embeddings, train_paths = getEmb(train_loader, model, len(train_dataset))
_, _, val_embeddings, val_paths = getEmb(val_loader, model.module, len(val_dataset))
train_targets = train_targets.tolist()
# clustering
print("Clustering features ...")
deepcluster = Kmeans(args.num_classes, nredo=30)
deepcluster.cluster(train_embeddings.copy(), npdata2=val_embeddings.copy(), save_centroids=True)
train_labels = deepcluster.labels[:len(train_dataset)]
val_labels = [[x] for x in deepcluster.labels[len(train_dataset):]]
# clustering metric
nmi_train = normalized_mutual_info_score(train_targets, train_labels)
acc_train, _ = reAssignSingle(np.array(train_targets), np.array(train_labels), num_classes=args.num_classes,)
print("train nmi {:.4f}".format(nmi_train))
print("train acc {:.4f}".format(acc_train))
result = dict(
nmi_train=nmi_train,
acc_train=acc_train,
train_labels=train_labels,
val_labels=val_labels,
centroids=deepcluster.centroids,
)
save(train_paths, val_paths, dump_path, result)
def save(train_paths, val_paths, dump_path, result):
# save centroids o clustering
centroids = result["centroids"].reshape(args.num_classes, -1)
np.save(os.path.join(dump_path, "centroids.npy"), centroids)
# save generated labels
train_labeled = []
val_labeled = []
for img, label in zip(train_paths, result['train_labels']):
train_labeled.append("{0}/{1} {2}".format(img.split('/')[-2], img.split('/')[-1], label))
for img, label in zip(val_paths, result['val_labels']):
val_labeled.append("{0}/{1} {2}".format(img.split('/')[-2], img.split('/')[-1], label[0]))
with open(os.path.join(dump_path, "train_labeled.txt"), "w") as f:
f.write("\n".join(train_labeled))
with open(os.path.join(dump_path, "val_labeled.txt"), "w") as f:
f.write("\n".join(val_labeled))
def getEmb(dataloader, model, size):
targets = torch.zeros(size).long().cuda()
embeddings = None
indexes = torch.zeros(size).long().cuda()
paths = []
start_idx = 0
with torch.no_grad():
for idx, path, inputs, target in tqdm(dataloader):
nmb_unique_idx = inputs.size(0)
# get embeddings
inputs = inputs.cuda(non_blocking=True)
emb = model(inputs, mode="cluster")
if start_idx == 0:
embeddings = torch.zeros(size, emb.shape[1]).cuda()
# fill the memory bank
targets[start_idx : start_idx + nmb_unique_idx] = target
indexes[start_idx : start_idx + nmb_unique_idx] = idx
embeddings[start_idx : start_idx + nmb_unique_idx] = emb
paths += path
start_idx += nmb_unique_idx
return indexes.cpu().numpy(), targets.cpu().numpy(), embeddings.cpu().numpy(), paths
def fix_random_seeds():
"""
Fix random seeds.
"""
if args.seed is None:
return
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
if __name__ == "__main__":
fix_random_seeds()
main()