-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_k_mean.py
58 lines (39 loc) · 2 KB
/
run_k_mean.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
import json
import os
from src.models.k_mean import read_vector, reduce_dim_combine, k_mean
from src.utils.analyze_label import symlink_cluster
from src.utils.config import get_config_from_json
IMAGE_EXTENSION = ('jpg', 'jpeg', 'bmp', 'png')
if __name__ == "__main__":
# Get config
config, _ = get_config_from_json("configs/configs.json")
object_name = config.model.object_name
dim = config.model.reduced_dimension
img_dir = os.path.join(config.paths.image_dir, object_name)
vector_dir = os.path.join(config.paths.vector_dir, object_name)
save_plot_dir = os.path.join(config.paths.plot_dir, object_name)
cluster_label_path = os.path.join(config.paths.cluster_label_dir, object_name + ".json")
if not os.path.isdir(vector_dir):
raise Exception("Please run feature extraction for all images first")
# Read feature vector from vector dir
vector_array, vector_files = read_vector(vector_dir)
if len(vector_files) == 0:
raise Exception("Please run feature extraction for all images first")
# Apply dimensional reducing approach
vector_array = reduce_dim_combine(vector_array, dim=dim)
labels = k_mean(vector_array, config.model.k).tolist()
assert len(labels) == len(vector_files), "Not equal length"
label_dict = [{"img_file": vector_files[i].replace(".npz", ""), "label": str(labels[i]), "prob": "1.0"} for i in
range(len(labels))]
# Save to disk
os.makedirs(os.path.dirname(cluster_label_path), exist_ok=True)
with open(cluster_label_path, 'w') as fp:
json.dump({"data": label_dict}, fp)
print("Cluster label for each image are saved at results/cluster_label/example.")
# Symlink
link_base_dir = config.paths.link_dir
os.makedirs(link_base_dir, exist_ok=True)
symlink_cluster(label_path=cluster_label_path,
dest_dir=os.path.join(link_base_dir, object_name),
src_dir=img_dir)
print("Go to results/link/example to see images in each cluster")