-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ours.py
More file actions
263 lines (206 loc) · 8.8 KB
/
test_ours.py
File metadata and controls
263 lines (206 loc) · 8.8 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import os
import torch
import json
import numpy as np
import torch.nn as nn
import matplotlib.pyplot as plt
import argparse
import glob
from PIL import Image
from sklearn.decomposition import PCA
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler, DDIMScheduler
from model.unet_attn import UNet2DConditionModel
from model.visual import mask_generation
parser = argparse.ArgumentParser()
parser.add_argument("--output_dir", type=str, default="output", help="Path to the output directory")
parser.add_argument("--image_path", type=str, default="./bus.jpg", help="Path to the input image")
parser.add_argument("--prompt1", type=str, default="color the bus blue", help="First prompt for the model")
parser.add_argument("--prompt2", type=str, default="color the bus green", help="Second prompt for the model")
parser.add_argument("--object", type=str, default="bus", help="Object to be colored")
parser.add_argument("--width_ratio", type=float, default=0.5, help="width")
parser.add_argument("--height_ratio", type=float, default=0.5, help="height")
parser.add_argument("--train_sample_num", type=int, default=30, help="train sample number")
parser.add_argument("--test_num", type=int, default=10, help="test sample number")
args = parser.parse_args()
#load model
model_id = "/data/yangyuqi/models/instruct-pix2pix"
pipe = DiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16,
safety_checker=None,
custom_pipeline="./ip2p_ours.py",
)
pipe.to("cuda")
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.unet = UNet2DConditionModel.from_pretrained(
model_id,
torch_dtype=torch.float16,
subfolder="unet",
).to("cuda")
output_dir = args.output_dir
embedding_dir = "/data/yangyuqi/ip2p-result/bus/blue_green"
os.makedirs(output_dir, exist_ok=True)
#load image
image_path = args.image_path
init_image = Image.open(image_path).convert("RGB")
init_image = init_image.resize((512, 512))
#插值生成一组图片 theta
train_sample_num = args.train_sample_num
test_num = args.test_num
theta = np.linspace(0, 1 , num=train_sample_num)
height = int(init_image.size[1] * args.height_ratio)
width = int(init_image.size[0] * args.width_ratio)
prompt1 = args.prompt1
prompt2 = args.prompt2
object = args.object
mask = mask_generation(init_image, object, output_dir)
def save_embedding():
imgs = []
for theta_item in theta:
img = pipe(prompt1, prompt2, object, mask, theta_item, path=None, mode='train', output_path=output_dir, image=init_image).images[0]
img.save(output_dir + f'/{theta_item:.2f}.png')
imgs.append(img)
plt.figure(figsize=(16,12))
plt.subplot(4, 8, 1)
plt.imshow(init_image)
plt.title(f'Original Image', fontsize=10)
plt.axis('off')
for i, (alpha, img) in enumerate(zip(theta, imgs)):
plt.subplot(4, 8 , i + 2)
plt.imshow(img)
plt.title(f'alpha = {alpha:.2f}', fontsize=10)
plt.axis('off')
plt.tight_layout(pad=0.5, h_pad=0.5, w_pad=0.5)
plt.subplots_adjust(top=0.9)
plt.savefig(output_dir + '/output_inter.png')
plt.show()
def load_embedding(theta):
embedding_list = []
embedding_path_list = [f'embedding_{i:.2f}.pt' for i in theta]
for file_name in embedding_path_list:
embedding_path = os.path.join(embedding_dir, file_name)
embedding = torch.load(embedding_path)
embedding_list.append(embedding['embedding'][0]) # shape: (77, 768)
embedding_batch = torch.stack(embedding_list, dim=0) # shape: (batch_size, 77, 768)
return embedding_batch # shape: (20, 77, 768)
def load_image_rgb(theta, width, height):
image_rgb_list = []
image_path_list = [f'{i:.2f}.png' for i in theta]
for file_name in image_path_list:
image_path = os.path.join(embedding_dir, file_name)
image = Image.open(image_path)
image = image.resize((512, 512))
r, g ,b = image.getpixel((width, height))
image_rgb = torch.tensor([r,g,b])
image_rgb_list.append(image_rgb) # shape: (3,)
first_pixel = image_rgb_list[4]
last_pixel = image_rgb_list[12]
image_rgb_batch = torch.stack(image_rgb_list, dim=0) # shape: (20, 3)
return image_rgb_batch, first_pixel, last_pixel # shape: (20, 3)
class Vec3ToPCAEmbedding(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Linear(3, 128),
nn.ReLU(),
nn.Linear(128, 15)
)
def forward(self, x): # x: [batch, 3]
return self.model(x)
def predict_embedding(embedding_batch, image_rgb_batch, first_pixel, last_pixel, n_components):
batch_size = embedding_batch.shape[0]
embedding = embedding_batch.cpu().reshape(batch_size, -1) # [100, 77, 768]
image_rgb = image_rgb_batch.cpu().reshape(batch_size, -1)
pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(embedding) # [100, 512]
model = Vec3ToPCAEmbedding()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.MSELoss()
X_target = torch.tensor(X_pca, dtype=torch.float32) # [100, 512]
Y_input = torch.tensor(image_rgb, dtype=torch.float32) # [100, 3]
for epoch in range(500):
pred = model(Y_input) # [100, 512]
loss = criterion(pred, X_target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 50 == 0:
print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")
pred_embeddings = []
pixel_rgb = []
with torch.no_grad():
for i in range(test_num):
alpha = i / (test_num-1)
current_pixel = (1 - alpha) * first_pixel + alpha * last_pixel
Y_input = torch.tensor(current_pixel,dtype=torch.float32)
# Y_input = torch.tensor([0, 140 + i * 4 ,200 - 5 * i],dtype=torch.float32)
pixel_str = '_'.join(str(int(x.item())) for x in current_pixel)
pred_pca = model(Y_input).numpy() # [100, 512]
pred_flat = pca.inverse_transform(pred_pca) # [100, 59136]
pred_embedding = pred_flat.reshape(77, 768) # [100, 77, 768]
pred_embeddings.append((pred_embedding, pixel_str)) # [100, 77, 768]
torch.save({'pred_embedding':pred_embedding}, output_dir + f'/pred_embedding_{i}_' + pixel_str + '.pt' )
return pred_embeddings
def generate_image(pred_embeddings):
imgs_output = []
pixel_values = []
i = 0
for (pred_embedding, pixel_str) in pred_embeddings:
img = pipe(prompt1, prompt2, object, mask, theta=0, pred_embedding=pred_embedding, mode='test',output_path=None, image=init_image).images[0]
img.save(output_dir + f'/new_{i}_{pixel_str}.png')
i += 1
imgs_output.append(img)
pixel_values.append(pixel_str)
plt.figure(figsize=(16,14))
plt.subplot(7, 8, 1)
plt.imshow(init_image)
plt.title(f'Original Image', fontsize=10)
plt.axis('off')
for i, img in enumerate(imgs_output):
plt.subplot(7, 8 , i + 2)
plt.imshow(img)
plt.title(f'{i}_{pixel_values[i]}', fontsize=10)
plt.axis('off')
plt.tight_layout(pad=0.5, h_pad=0.5, w_pad=0.5)
plt.subplots_adjust(top=0.9)
plt.savefig(output_dir + '/output_ours.png')
plt.show()
def generate_image(pred_embeddings):
imgs_output = []
pixel_values = []
for i in range(test_num):
pattern = os.path.join(output_dir, f'pred_embedding_{i}*.pt')
paths = glob.glob(pattern)
if len(paths) == 0:
print(f"[Warning] No file found for: {pattern}")
continue
path = paths[0]
filename = os.path.basename(path)
parts = filename.replace(".pt", "").split("_")
result = parts[-3:]
img = pipe(prompt1, prompt2, object, mask, theta=0, path=path, mode='test',output_path=None, image=init_image).images[0]
img.save(output_dir + f'/new_{i}_{result}.png')
imgs_output.append(img)
pixel_values.append(result)
plt.figure(figsize=(16,14))
plt.subplot(7, 8, 1)
plt.imshow(init_image)
plt.title(f'Original Image', fontsize=10)
plt.axis('off')
for i, img in enumerate(imgs_output):
plt.subplot(7, 8 , i + 2)
plt.imshow(img)
plt.title(f'{i}_{pixel_values[i]}', fontsize=10)
plt.axis('off')
plt.tight_layout(pad=0.5, h_pad=0.5, w_pad=0.5)
plt.subplots_adjust(top=0.9)
plt.savefig(output_dir + '/output_ours.png')
plt.show()
def main():
save_embedding()
embedding_batch = load_embedding(theta)
image_rgb_batch, first_pixel, last_pixel = load_image_rgb(theta, width, height) # 256, 341取RGB值的像素点坐标
predict_embeddings = predict_embedding(embedding_batch, image_rgb_batch, first_pixel, last_pixel, n_components=15)
generate_image(predict_embeddings)
if __name__ == "__main__":
main()