-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieval_vis.py
240 lines (202 loc) · 10.8 KB
/
retrieval_vis.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import re
import argparse
import random
from PIL import Image
import matplotlib.pyplot as plt
from diffusers.utils import load_image
from diffusers import StableDiffusionPipeline
from diffusers.models.unets.unet_2d_blocks import CrossAttnUpBlock2D, CrossAttnDownBlock2D
from diffusers.utils import PIL_INTERPOLATION
import torch
import torch.nn.functional as F
import numpy as np
from segment_anything import build_sam, SamAutomaticMaskGenerator
import lpips
from diffsim.hacked_modules import hacked_CrossAttnUpBlock2D_forward
from diffsim.diffsim_pipeline import DiffSimPipeline
from diffsim.diffsim import diffsim, diffsim_value, process_image
from diffsim.diffsim_xl import diffsim_xl
from metrics.clip_i import CLIPScore
from metrics.dino import Dinov2Score
from metrics.foreground_feature_averaging import ForegroundFeatureAveraging
from metrics.vgg_gram import vgg_gram
from argprocess import arg_parse
def sref_vis():
# A, B, C from the same background
# args.target_layer[2] = layer
total = 0
correct = 0 # the similarity value between A & B is smaller than A & C
correct_2x = 0 # the similarity value between A & B is twice smaller than A & C
origin_path = "/tiamat-NAS/songyiren/dataset/Sref508/"
diffsim_path = "/tiamat-NAS/songyiren/Xiaokang/data/diffsim_sref_ckpt/l0_500_retrieval/"
clip_path = "/tiamat-NAS/songyiren/Xiaokang/data/clip_sref_retrieval/"
dino_path = "/tiamat-NAS/songyiren/Xiaokang/data/dino_sref_retrieval/"
out_path = "/tiamat-NAS/songyiren/Xiaokang/data/sref_retrieval_comparison/"
prompt = "A high quality image"
for cls in os.listdir(diffsim_path):
if cls == "main.py" or cls == ".DS_Store":
continue
cls_dir_path = os.path.join(diffsim_path, cls)
cls_ckpt_path = os.path.join(out_path, cls)
if not os.path.exists(cls_ckpt_path):
print(f"Make dir {cls_ckpt_path}")
os.makedirs(cls_ckpt_path)
for retrieval_result in os.listdir(cls_dir_path):
retrieval_result_clip = os.path.join(clip_path, cls, retrieval_result)
retrieval_result_dino = os.path.join(dino_path, cls, retrieval_result)
retrieval_result_diffsim = os.path.join(diffsim_path, cls, retrieval_result)
txt_files = [retrieval_result_diffsim, retrieval_result_clip, retrieval_result_dino]
def read_image_path(dirs, file):
image_paths = []
with open(file, 'r') as file:
for line in file:
parts = line.strip().split()
if len(parts) >= 1:
cls, img_id = parts[0].split('_')
image_path = os.path.join(dirs, cls, f"{img_id[:-1]}.png")
image_paths.append(image_path)
if len(image_paths) >= 4: # Read only the first 4 image paths
break
return image_paths
origin_image_path = os.path.join(origin_path, cls, retrieval_result.replace('txt', 'png'))
image_paths_grid = []
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_diffsim))
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_clip))
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_dino))
# print(image_paths_grid)
# Display the images in a 3x5 grid and save the output
fig, axes = plt.subplots(3, 5, figsize=(15, 9))
for row, row_images in enumerate(image_paths_grid):
for col, img_path in enumerate(row_images):
# print(img_path)
img = Image.open(img_path)
axes[row, col].imshow(img)
axes[row, col].axis('off') # Turn off axis for better display
# Set padding between images
plt.subplots_adjust(wspace=0.2, hspace=0.2)
# Save the grid as an image
plt.tight_layout()
save_path = os.path.join(cls_ckpt_path, retrieval_result.replace('txt', 'png'))
plt.savefig(save_path)
print(f"Result save to {save_path}")
plt.close()
print("Finish")
def coco_vis():
# A, B, C from the same background
# args.target_layer[2] = layer
total = 0
correct = 0 # the similarity value between A & B is smaller than A & C
correct_2x = 0 # the similarity value between A & B is twice smaller than A & C
origin_path = "/tiamat-NAS/data/coco/test2017"
diffsim_path = "/tiamat-NAS/songyiren/Xiaokang/data/diffsim_coco_ckpt/l0_500_test_retrieval/"
clip_path = "/tiamat-NAS/songyiren/Xiaokang/data/clip_coco_retrieval/"
dino_path = "/tiamat-NAS/songyiren/Xiaokang/data/dino_coco_retrieval/"
out_path = "/tiamat-NAS/songyiren/Xiaokang/data/coco_retrieval_comparison/"
prompt = "A high quality image"
for retrieval_result in os.listdir(diffsim_path):
retrieval_result_clip = os.path.join(clip_path, retrieval_result)
retrieval_result_dino = os.path.join(dino_path, retrieval_result)
retrieval_result_diffsim = os.path.join(diffsim_path, retrieval_result)
txt_files = [retrieval_result_diffsim, retrieval_result_clip, retrieval_result_dino]
def read_image_path(dirs, file):
image_paths = []
with open(file, 'r') as file:
for line in file:
parts = line.strip().split()
if len(parts) >= 1:
img_id = parts[0]
image_path = os.path.join(dirs, f"{img_id[:-1]}.jpg")
image_paths.append(image_path)
if len(image_paths) >= 4: # Read only the first 4 image paths
break
return image_paths
origin_image_path = os.path.join(origin_path, retrieval_result.replace('txt', 'jpg'))
image_paths_grid = []
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_diffsim))
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_clip))
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_dino))
# print(image_paths_grid)
# Display the images in a 3x5 grid and save the output
fig, axes = plt.subplots(3, 5, figsize=(15, 9))
for row, row_images in enumerate(image_paths_grid):
for col, img_path in enumerate(row_images):
# print(img_path)
img = Image.open(img_path)
axes[row, col].imshow(img)
axes[row, col].axis('off') # Turn off axis for better display
# Set padding between images
plt.subplots_adjust(wspace=0.2, hspace=0.2)
# Save the grid as an image
plt.tight_layout()
save_path = os.path.join(out_path, retrieval_result.replace('txt', 'png'))
plt.savefig(save_path)
print(f"Result save to {save_path}")
plt.close()
def ip_vis():
# A, B, C from the same background
# args.target_layer[2] = layer
total = 0
correct = 0 # the similarity value between A & B is smaller than A & C
correct_2x = 0 # the similarity value between A & B is twice smaller than A & C
origin_path = "/tiamat-NAS/songyiren/dataset/ipref_combine/"
diffsim_path = "/tiamat-NAS/songyiren/Xiaokang/data/ipref_combine_retrieval/diffsim_retrieval"
clip_path = "/tiamat-NAS/songyiren/Xiaokang/data/ipref_combine_retrieval/clip_retrieval"
dino_path = "/tiamat-NAS/songyiren/Xiaokang/data/ipref_combine_retrieval/dino_retrieval"
out_path = "/tiamat-NAS/songyiren/Xiaokang/data/ipref_combine_retrieval/retrieval_comparison"
prompt = "A high quality image"
for cls in os.listdir(diffsim_path):
if cls == "main.py" or cls == ".DS_Store":
continue
cls_dir_path = os.path.join(diffsim_path, cls)
cls_ckpt_path = os.path.join(out_path, cls)
if not os.path.exists(cls_ckpt_path):
print(f"Make dir {cls_ckpt_path}")
os.makedirs(cls_ckpt_path)
for retrieval_result in os.listdir(cls_dir_path):
retrieval_result_clip = os.path.join(clip_path, cls, retrieval_result)
retrieval_result_dino = os.path.join(dino_path, cls, retrieval_result)
retrieval_result_diffsim = os.path.join(diffsim_path, cls, retrieval_result)
txt_files = [retrieval_result_diffsim, retrieval_result_clip, retrieval_result_dino]
def read_image_path(dirs, file):
image_paths = []
with open(file, 'r') as file:
for line in file:
parts = line.strip().split()
if len(parts) >= 1:
cls, img_id = parts[0].split('_')
if img_id == "1:":
continue
# print(line, cls, img_id)
image_path = os.path.join(dirs, cls, f"{img_id[:-1]}.png")
image_paths.append(image_path)
if len(image_paths) >= 4: # Read only the first 4 image paths
break
return image_paths
origin_image_path = os.path.join(origin_path, cls, retrieval_result.replace('txt', 'png'))
image_paths_grid = []
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_diffsim))
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_clip))
image_paths_grid.append([origin_image_path] + read_image_path(origin_path, retrieval_result_dino))
# print(image_paths_grid)
# Display the images in a 3x5 grid and save the output
fig, axes = plt.subplots(3, 5, figsize=(15, 9))
for row, row_images in enumerate(image_paths_grid):
for col, img_path in enumerate(row_images):
img = Image.open(img_path)
axes[row, col].imshow(img)
axes[row, col].axis('off') # Turn off axis for better display
# Set padding between images
plt.subplots_adjust(wspace=0.2, hspace=0.2)
# Save the grid as an image
plt.tight_layout()
save_path = os.path.join(cls_ckpt_path, retrieval_result.replace('txt', 'png'))
plt.savefig(save_path)
print(f"Result save to {save_path}")
plt.close()
print("Finish")
if __name__ == "__main__":
# save_image_descriptors()
sref_vis()
# coco_vis()
# ip_vis()