-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpolation_test_mean.py
225 lines (179 loc) · 6.36 KB
/
interpolation_test_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
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
import argparse
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision.transforms.functional as TF
from PIL import Image
from model import GaussianModel, LSEPModel, Model
device_name = "cuda:0"
mode = "gray" # or "color"
interpolate = "scale" # or "brightness"
randomize = "" # "scale" # or "brightness"
static = "brightness" # or "scale"
parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=str)
parser.add_argument("--interpolate", type=str)
parser.add_argument("--randomize", type=str)
parser.add_argument("--static", type=str, default="simple")
parser.add_argument("--backbone", type=str)
parser.add_argument("--method", type=str)
parser.add_argument("--supervision", type=str)
args = parser.parse_args()
mode = args.mode
interpolate = args.interpolate
randomize = args.randomize if args.randomize != "None" else ""
static = args.static if args.static != "None" else ""
backbone = args.backbone
method = args.method
supervision = args.supervision
ranked_mnist_path = "/mnt/disk2/interpolation_test_images/%s_%s_%s_%s" % (
mode,
interpolate,
randomize,
static,
)
def read_model(path):
seq_path = os.path.join(path)
ckpt = torch.load(seq_path)
state_dict = ckpt["state_dict"]
return state_dict
colors = ["#004D40", "#D81B60", "#1E88E5", "#FFC107"]
color_map = [colors[0]] + [colors[idx] for idx in range(1, 4)] + [colors[0]] * 6
if randomize == "":
if args.method == "lsep":
path = "results/%s_small_%s_%s_%s_%s/saves/threshold_best.pth" % (
mode,
interpolate,
backbone,
method,
supervision,
)
else:
path = "results/%s_small_%s_%s_%s_%s/saves/best.pth" % (
mode,
interpolate,
backbone,
method,
supervision,
)
else:
if interpolate == "brightness":
_interpolate = "brightness"
elif interpolate == "scale":
_interpolate = "ratio"
else:
print("ERROR")
exit()
if args.method == "lsep":
path = (
"results/%s_small_brightness_scale_%s_%s_%s_%s/saves/threshold_best.pth"
% (mode, _interpolate, backbone, method, supervision)
)
else:
path = "results/%s_small_brightness_scale_%s_%s_%s_%s/saves/best.pth" % (
mode,
_interpolate,
backbone,
method,
supervision,
)
if method == "gaussian_mlr":
model = GaussianModel(10, backbone).to(device_name)
elif method == "clr":
model = Model((11 * 10) // 2, backbone).to(device_name)
elif method == "lsep":
model = LSEPModel(10, backbone).to(device_name)
model.load_state_dict(torch.load(path, map_location=device_name)["state_dict"])
model = model.eval()
for param in model.parameters():
model.requires_grad = False
all_scores = []
for dir_name in os.listdir(ranked_mnist_path):
scores = []
# Load images from directory
images = []
for file in os.listdir(os.path.join(ranked_mnist_path, dir_name)):
if file.endswith(".png"):
images.append(os.path.join(ranked_mnist_path, dir_name, file))
images = sorted(images, key=lambda x: int(x.split(".")[0].split("/")[-1]))
sel_digits = list(map(int, dir_name.split("/")[-1].split("_")[1:]))
if method == "gaussian_mlr":
for t_idx, image_path in enumerate(images):
image = (
TF.to_tensor(Image.open(image_path).convert("RGB"))
.to(device_name)
.unsqueeze(0)
- 0.5
)
mean, logvar = model(image)
mean[mean < 0] = 0.0
score = np.array(mean.detach().cpu())[0, sel_digits]
scores.append(score)
elif method == "clr":
for t_idx, image_path in enumerate(images):
image = (
TF.to_tensor(Image.open(image_path).convert("RGB"))
.to(device_name)
.unsqueeze(0)
- 0.5
)
logits = model(image)
probs = torch.sigmoid(logits)
N, _ = probs.shape
K = 11
pair_map = torch.tensor(
[(i, j) for i in range(K - 1) for j in range(i + 1, K)]
).to(device_name)
left_scores = probs >= 0.5
right_scores = probs < 0.5
score_matrix = torch.zeros((N, K)).to(device_name)
for j in range(K):
score_matrix[:, j] += torch.sum(
left_scores[:, pair_map[:, 0] == j] * probs[:, pair_map[:, 0] == j],
dim=1,
)
score_matrix[:, j] += torch.sum(
right_scores[:, pair_map[:, 1] == j]
* probs[:, pair_map[:, 1] == j],
dim=1,
)
negative_map = score_matrix < score_matrix[:, -1].unsqueeze(1).repeat(1, K)
score_matrix[negative_map] = 0
score = np.array(score_matrix.detach().cpu())[0, sel_digits]
scores.append(score)
elif method == "lsep":
for t_idx, image_path in enumerate(images):
image = (
TF.to_tensor(Image.open(image_path).convert("RGB"))
.to(device_name)
.unsqueeze(0)
- 0.5
)
score, thresholds = model(image)
score[score < thresholds] = 0.0
score = np.array(score.detach().cpu())[0, sel_digits]
scores.append(score)
scores = np.array(scores)
all_scores.append(scores)
all_scores = np.array(all_scores)
scores = np.mean(all_scores, axis=0)
scores -= np.min(scores)
scores /= np.max(scores)
t = np.linspace(0.0, 1.0, len(images))
fig, ax = plt.subplots()
ax.set_box_aspect(1)
fig.tight_layout()
fig.figsize = (4, 4)
ax.plot(t, scores[:, 0], color="#D81B60", label="1st Digit", linewidth=4)
ax.plot(t, scores[:, 1], color="#1E88E5", label="2nd Digit", linewidth=4)
ax.plot(t, scores[:, 2], color="#FFC107", label="3rd Digit", linewidth=4)
ax.set_xlabel("t", fontsize=18, fontweight="heavy")
ax.set_ylabel("Scores", fontsize=18, fontweight="heavy")
plt.xticks(fontsize=18, fontweight="heavy")
plt.yticks(fontsize=18, fontweight="heavy")
plt.savefig(
"interpolation_test_results/%s_%s_%s_%s_%s_%s_%s.pdf"
% (mode, interpolate, randomize, static, backbone, method, supervision),
bbox_inches="tight",
)