-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_cc_duplicates.py
149 lines (126 loc) · 5.26 KB
/
process_cc_duplicates.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
import pandas as pd
from copy import deepcopy
from pathlib import Path
from definitions import *
from utils import *
from config import *
def do_process_cc_duplicates(path_map: Dict[str, str]) -> Set[str]:
assert Path(FASTDUP_ALL_CC).exists()
assert Path(FASTDUP_SELECTED_CC).exists()
assert Path(FASTDUP_95_PLUS_A1).exists()
assert Path(FASTDUP_90_95_A1).exists()
# Working with connected components
all_cc_df = pd.read_csv(
FASTDUP_ALL_CC,
header="infer",
converters={"files": pd.eval, "files_ids": pd.eval},
)
selected_cc_df = pd.read_csv(FASTDUP_SELECTED_CC, header="infer")
selected_cc_df_detailed = pd.merge(
selected_cc_df,
all_cc_df[["component_id", "files", "files_ids"]],
on="component_id",
how="left",
)
selected_cc_df_detailed["filenames_with_dir"] = selected_cc_df_detailed.apply(
lambda row: [get_filename_with_dir(filepath) for filepath in row["files"]],
axis=1,
)
selected_cc_df_detailed["filenames"] = selected_cc_df_detailed.apply(
lambda row: [get_filenames(filepath) for filepath in row["files"]], axis=1
)
selected_cc_filelist = selected_cc_df_detailed["filenames"].to_list()
selected_cc_filelist_flat = [
item for cluster in selected_cc_filelist for item in cluster
]
print(f"Number of selected CCs: {len(selected_cc_df)}")
print(f"Number of images in selected CCs: {len(selected_cc_filelist_flat)}")
# Working with duplicates
if USE_A2_ANNOTATIONS:
duplicate_annotations_df = pd.concat(
[
pd.read_csv(
FASTDUP_95_PLUS_A1, header=None, names=["pair_name", "is_duplicate"]
),
pd.read_csv(
FASTDUP_95_PLUS_A2, header=None, names=["pair_name", "is_duplicate"]
),
pd.read_csv(
FASTDUP_90_95_A1, header=None, names=["pair_name", "is_duplicate"]
),
]
)
else:
duplicate_annotations_df = pd.concat(
[
pd.read_csv(
FASTDUP_95_PLUS_A1, header=None, names=["pair_name", "is_duplicate"]
),
pd.read_csv(
FASTDUP_90_95_A1, header=None, names=["pair_name", "is_duplicate"]
),
]
)
duplicate_annotations_df = duplicate_annotations_df[
duplicate_annotations_df["is_duplicate"] == "Duplicate"
]
duplicate_annotations_pairs = [
[
"_".join(item.split("_")[:4]),
"_".join(item.split("_")[4:]).replace(".jpg", ""),
]
for item in duplicate_annotations_df["pair_name"].to_list()
]
print(f"Number of duplicate pairs: {len(duplicate_annotations_pairs)}")
print(
f"Number of unique images in duplicate pairs: {len(set([item for pair in duplicate_annotations_pairs for item in pair]))}"
)
# Working with cleanlab duplicates
cleanlab_duplicate_pairs = (
pd.read_csv(CLEANLAB_DUPLICATE_PAIRS, header="infer").to_numpy().tolist()
)
cleanlab_duplicate_pairs = [
[get_filenames(img) for img in pair] for pair in cleanlab_duplicate_pairs
]
print(f"Number of cleanlab duplicate pairs: {len(cleanlab_duplicate_pairs)}")
print(
f"Number of unique images in cleanlab duplicate pairs: {len(set([item for pair in cleanlab_duplicate_pairs for item in pair]))}"
)
incomplete_clusters: List[List[str]] = deepcopy(selected_cc_filelist)
incomplete_clusters.extend(duplicate_annotations_pairs)
incomplete_clusters.extend(cleanlab_duplicate_pairs)
complete_clusters: List[List[str]] = make_complete_clusters(incomplete_clusters)
print(f"Number of complete clusters: {len(complete_clusters)}")
print(
f"Number of unique images in complete clusters: {len(set([item for cluster in complete_clusters for item in cluster]))}"
)
# Save the clusters to a file.
with open(ANANLYSES_OUTPUT_PATH / "complete_clusters.txt", "w") as f:
for cluster in complete_clusters:
f.write(f"{cluster}\n")
# Determine which, if any, images to keep from homogeneous clusters.
# A homogeneous cluster is a cluster that contains images with the same "diag"
# and "fst" labels.
candidate_images_to_keep_from_homogeneous_clusters = (
find_candidate_images_from_homogenous_clusters(
list_of_clusters=complete_clusters, path_map=path_map
)
)
# Save the candidate images to keep to a file.
with open(
ANANLYSES_OUTPUT_PATH
/ "candidate_images_to_keep_from_homogeneous_clusters.txt",
"w",
) as f:
for item in candidate_images_to_keep_from_homogeneous_clusters:
f.write(f"{item}\n")
if REMOVE_CC_DUPLICATES_LEVEL == "All":
return set([item for cluster in complete_clusters for item in cluster])
elif REMOVE_CC_DUPLICATES_LEVEL == "KeepOne":
return set(
[item for cluster in complete_clusters for item in cluster]
).difference(set(candidate_images_to_keep_from_homogeneous_clusters))
else:
raise ValueError(
f"REMOVE_CC_DUPLICATES_LEVEL must be either 'KeepOne' (default) or 'All'. Got '{REMOVE_CC_DUPLICATES_LEVEL}'."
)