forked from sophiajw/HistAuGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_transform.py
171 lines (157 loc) · 6.03 KB
/
test_transform.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
import torch
import cv2
import numpy as np
from augmentations import generate_hist_augs, opts, mean_domains, std_domains
from histaugan.model import MD_multi
import imageio
from torchvision import transforms
import time
from utils import PPlot
def main():
trnsfrms_val = transforms.Compose(
[
transforms.ToTensor()
]
)
model = MD_multi(opts)
model.resume(opts.resume, train=False)
model.to('cuda:0')
model.eval()
# img = imageio.v2.imread(r'D:\jassorRepository\OCELOT_Dataset\what.jpg')
img = cv2.imread(r'D:\jassorRepository\OCELOT_Dataset\how.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (512, 512))
pplt = PPlot()
# pplt.add(img)
img = trnsfrms_val(img).cuda() # torch.Size([3, 512, 512])
print(img.shape)
img1 = img.cpu().numpy()
print(np.max(img1), np.min(img1))
z_content = model.enc_c(img.sub(0.5).mul(2).unsqueeze(0))
# 实验
for d in range(5):
for seed in [
torch.tensor([[10, 0, 0, 0, 0, 0, 0, 0]], dtype=torch.float32),
torch.tensor([[0, 1, 0, 0, 0, 0, 0, 0]], dtype=torch.float32),
torch.tensor([[0, 0, 0, 4, 0, 0, 0, 0]], dtype=torch.float32),
torch.tensor([[0, 0, 0, 0, 3, 0, 3, 0]], dtype=torch.float32),
torch.tensor([[0, 0, 0, 0, 0, 2, 0, 0]], dtype=torch.float32),
torch.tensor([[0, 0, 0, 0, 0, 0, 0, 10]], dtype=torch.float32),
]:
print(seed)
z_attribute = (seed * std_domains[0] + mean_domains[0]).to('cuda:0')
domain = torch.eye(5)[d].unsqueeze(0).to('cuda:0')
out = model.gen(z_content, z_attribute, domain).detach().squeeze(0)
out = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# pplt.title(int(seed.sum()))
pplt.add(out)
pplt.show()
# # 实验 -- 确定参数如下所示
# d = 4
# for i, j in [(i, j) for i in range(10) for j in range(10)]:
# if i >= 6:
# pplt.add(np.zeros((10, 10), dtype=np.uint8))
# continue
# seed = torch.zeros(1, 8, dtype=torch.float32)
# if i == 0:
# seed[0, 0] = 1 * j
# elif i == 1:
# seed[0, 1] = 0.1 * j
# elif i == 2:
# seed[0, 3] = 0.4 * j
# elif i == 3:
# seed[0, (4, 6)] = 0.3 * j
# elif i == 4:
# seed[0, 5] = 0.2 * j
# elif i == 5:
# seed[0, 7] = 1 * j
# print(i, j, seed)
# z_attribute = (seed * std_domains[0] + mean_domains[0]).to('cuda:0')
# domain = torch.eye(5)[d].unsqueeze(0).to('cuda:0')
# out = model.gen(z_content, z_attribute, domain).detach().squeeze(0)
# out = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# # pplt.title(int(seed.sum()))
# pplt.add(out)
# pplt.show()
# 实验
# d = 4
# for i, j in [(i, j) for i in range(10) for j in range(10)]:
# if i >= 3:
# pplt.add(np.zeros((10, 10), dtype=np.uint8))
# continue
# seed = torch.zeros(1, 8, dtype=torch.float32)
# if i == 0:
# seed[0, 7] = 1 * j
# elif i == 1:
# seed[0, 0] = 1 * j
# elif i == 2:
# seed[0, (7, 0)] = 1 * j
# print(i, j, seed)
# z_attribute = (seed * std_domains[0] + mean_domains[0]).to('cuda:0')
# domain = torch.eye(5)[d].unsqueeze(0).to('cuda:0')
# out = model.gen(z_content, z_attribute, domain).detach().squeeze(0)
# out = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# # pplt.title(int(seed.sum()))
# pplt.add(out)
# pplt.show()
# # 实验
# d = 3
# for i, j in [(i, j) for i in range(8) for j in range(8)]:
# seed = torch.zeros(1, 8, dtype=torch.float32)
# seed[0, i] = 1 * j
# print(i, j, seed)
# z_attribute = (seed * std_domains[0] + mean_domains[0]).to('cuda:0')
# domain = torch.eye(5)[d].unsqueeze(0).to('cuda:0')
# out = model.gen(z_content, z_attribute, domain).detach().squeeze(0)
# out = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# # pplt.title(int(seed.sum()))
# pplt.add(out)
# pplt.show()
# # 控制变量法观察:固定 domain,改变 z_attribute
# # github code
# for d in range(5):
# for _ in range(5):
# seed = torch.randn((1, 8,))
# z_attribute = (seed * std_domains[d] + mean_domains[d]).to('cuda:0')
# domain = torch.eye(5)[d].unsqueeze(0).to('cuda:0')
# out = model.gen(z_content, z_attribute, domain).detach().squeeze(0)
# out = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# print(out.shape, out.dtype, out.mean())
# pplt.add(out)
# pplt.show()
# # 控制变量法观察:固定 z_attribute,改变 domain
# seed = torch.randn((1, 8,))
# # github code
# for _ in range(5):
# for d in range(5):
# z_attribute = (seed * std_domains[d] + mean_domains[d]).to('cuda:0')
# domain = torch.eye(5)[d].unsqueeze(0).to('cuda:0')
# out = model.gen(z_content, z_attribute, domain).detach().squeeze(0)
# out = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# print(out.shape, out.dtype, out.mean())
# pplt.add(out)
# pplt.show()
# 炳豆方法
# for iiii in range(1):
# aa1 = time.time()
# for i in range(5):
# out = generate_hist_augs(
# img,
# domain,
# model,
# z_content,
# same_attribute=False,
# new_domain=i,
# stats=(mean_domains, std_domains),
# device='cuda:0'
# )
# out_img = out.add(1).div(2).permute(1, 2, 0).cpu().numpy()
# # print(np.max(out_img),np.min(out_img))
# # imageio.imwrite(rf'D:\jassorRepository\OCELOT_Dataset\{i}.jpg', out_img)
# aa2 = time.time()
#
# print(7777, aa2 - aa1)
biao = [
[],
]
main()