forked from chrisgoringe/Custom-classifier-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_extraction_comparison.py
43 lines (30 loc) · 1.26 KB
/
feature_extraction_comparison.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
from src.ap.feature_extractor import FeatureExtractor
import os
import torch
import matplotlib.pyplot as plt
class Args:
directory = ""
feature_extractor_model = ""
mse = torch.nn.MSELoss()
def compare_images(imgfilenames:list[str]):
image_directory = Args.directory
feature_extractor = FeatureExtractor.get_feature_extractor(pretrained=Args.feature_extractor_model, image_directory=image_directory, device="cuda")
features = [feature_extractor.get_features_from_file(os.path.join(image_directory,f), caching=True) for f in imgfilenames]
typical_delta =torch.abs(features[1]-features[0])
edited_delta = torch.abs(features[2]-features[0])
td = list(float(x) for x in typical_delta)
ed = list(float(x) for x in edited_delta)
td.sort(reverse=True)
ed.sort(reverse=True)
hwfm = lambda a, b : sum( x > a*b[0] for x in b )
for f in (0.5, 0.6, 0.7, 0.8, 0.9):
print("Values greater than {:>4.2}*maximum: unrelated images {:>4}, edited pair {:>4}".format(f, hwfm(f, td), hwfm(f, ed)))
plt.plot(ed)
plt.plot(td)
plt.show()
feature_extractor._save_cache()
def main():
imgfilenames = [ "img1.png", "img2.png", "edited_img1.png", ]
compare_images(imgfilenames)
if __name__ == "__main__":
main()