-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapply_reversible.py
193 lines (158 loc) · 8.92 KB
/
apply_reversible.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
import math
import os
import os.path
from PIL import Image
import numpy as np
import torch
from myutils import utils
from adjustable_upscaleNst import AdjustableNetwork
from adaptiveStrokeNet import JohnsonAdaptiveStroke
from reversibleEdit.reversible_rotation import ReversibleRotation
from reversibleEdit.reversible_warping import ReversibleWarping
from reversibleEdit.reversible_swirl import ReversibleSwirl
from reversibleEdit.reversible_warping import ReversibleWarpingSine
from reversibleEdit.vtk_tps import create_vtk_thin_spline_warp
def forward_backward_warp(warp_op, content_image, method):
if method == 'optimize' or method == 'naive_reverse':
content_image_warped = utils.tensor_load_rgbimage(content_image, size=1024, keep_asp=True, np_array=True, reversible_edit=warp_op.forward)
content_image_original = utils.tensor_load_rgbimage(content_image, size=1024, keep_asp=True, np_array=True)
warp_img = content_image_warped.transpose(1,2,0).astype('uint8')
warp_img = Image.fromarray(warp_img)
warp_img.save(f"output/warp_forward.jpg")
if method == 'optimize':
out_iters, grid = warp_op.backward_optimize(torch.from_numpy(content_image_warped).float().unsqueeze(0),
torch.from_numpy(content_image_original).float().unsqueeze(0), use_theta=True, use_src_as_dst=True)
out_iters = out_iters.cpu().detach()[0].numpy().transpose(1,2,0).astype('uint8')
out_iters = Image.fromarray(out_iters)
out_iters.save(f"output/warp_iterative_backwards.jpg")
else:
out = warp_op.backward(warp_img)
out.save(f"output/warp_naive_backwards.jpg")
elif method == "vtk":
img = np.array(Image.open(content_image)).astype('uint8')
warped_output = create_vtk_thin_spline_warp(img, warp_op.src_pts, warp_op.dst_pts , show_grid=False)
warped_output = warped_output.transpose(2,0,1)
Image.fromarray(warped_output).save(f"output/warp_vtk_forward.jpg" )
warped_back = create_vtk_thin_spline_warp(warped_output, warp_op.src_pts,
warp_op.dst_pts, use_inverse=False, show_grid=False)
warped_back = warped_back.transpose(2,0,1)
Image.fromarray(warped_back).save(f"output/warp_vtk_backwards.jpg" )
def create_nst_adj_rot():
CONTENT_IMAGE = "images/content/ferry.jpg"
#STYLE = "sketch_girl"
# STYLE = "abstractOrangePattern_2"
STYLE = "mondrian"
dir = "models/adjustable"
REVERSIBLE_EDIT = ReversibleRotation()# ReversibleSwirl()
warping = True
device = torch.device("cuda")
model_path = [f.path for f in os.scandir(dir + "/" + STYLE) if f.name.endswith(".model")][0]
def remove_IN_params(state_dict):
new_dict = {}
for (k,v) in state_dict.items():
if not ".in1." in k:
new_dict[k] = state_dict[k]
return new_dict
with torch.no_grad():
style_model = AdjustableNetwork(fusion_krnsize=9)
state_dict = remove_IN_params(torch.load(model_path))
style_model.eval()
style_model.load_state_dict(state_dict)
style_model.to(device)
for i in range(0,360,1):
print (i)
REVERSIBLE_EDIT.rotation = i
content_size = 1024
upscale_fact = 1
warped_image = utils.tensor_load_rgbimage(CONTENT_IMAGE, size=1024, keep_asp=True, np_array=True, reversible_edit=REVERSIBLE_EDIT.forward)
warped_image_small = utils.tensor_load_rgbimage(CONTENT_IMAGE, size=int(content_size/upscale_fact), keep_asp=True, np_array=True, reversible_edit=REVERSIBLE_EDIT.forward)
warped_image = torch.from_numpy(warped_image).float()
warped_image = warped_image.unsqueeze(0).to(device)
warped_image = utils.preprocess_batch(warped_image)
warped_image_small = torch.from_numpy(warped_image_small).float()
warped_image_small = warped_image_small.unsqueeze(0).to(device)
warped_image_small = utils.preprocess_batch(warped_image_small)
stroke_factor_input = torch.tensor([1.0]).to(device)
output = style_model(warped_image_small, warped_image, stroke_factor_input).float().cpu().squeeze(0)
img = output.clamp(0,255).numpy()
img = img.transpose(1,2,0).astype('uint8')
img = img[:,:,::-1]
#output_warped_back = create_vtk_thin_spline_warp(img, REVERSIBLE_EDIT.src_pts, REVERSIBLE_EDIT.dst_pts, use_inverse=False, show_grid=False)
out = REVERSIBLE_EDIT.backward(img)
out.save(f"output/animation/rot_output_{i:04d}.jpg" )
os.system(f"ffmpeg -i output/animation/rot_output_%04d.jpg -vcodec libx265 -crf 28 output/rot_output_-{STYLE}.mp4")
def create_nst_swirl():
CONTENT_IMAGE = "images/content/ferry.jpg"
OUT = "out.jpg"
STYLE = "abstractOrangePattern_2"
REVERSIBLE_EDIT = ReversibleSwirl()
warping = True
device = torch.device("cuda")
model_path = [f.path for f in os.scandir("models/adaptiveStroke/" + STYLE) if f.name.endswith(".model")][0]
with torch.no_grad():
style_model = JohnsonAdaptiveStroke()
state_dict = torch.load(model_path)
style_model.eval()
style_model.load_state_dict(state_dict)
style_model.to(device)
for i in range(80):
print (i)
REVERSIBLE_EDIT.strength = int(i / 4) if i <= 40 else 40 - int(i / 4)
REVERSIBLE_EDIT.center = (560, 420)
warped_image = utils.tensor_load_rgbimage(CONTENT_IMAGE, size=1024, keep_asp=True, np_array=True, reversible_edit=REVERSIBLE_EDIT.forward)
warped_image = torch.from_numpy(warped_image).float()
warped_image = warped_image.unsqueeze(0).to(device)
warped_image = utils.preprocess_batch(warped_image)
stroke_factor_input = torch.tensor([1.0]).to(device)
output = style_model(warped_image, stroke_factor_input).float().cpu().squeeze(0)
img = output.clamp(0,255).numpy()
img = img.transpose(1,2,0).astype('uint8')
img = img[:,:,::-1]
out = REVERSIBLE_EDIT.backward(img)
# output_warped_back = Image.fromarray(output_warped_back)
out.save(f"output/animation/swirl_output_{i:04d}.jpg" )
os.system(f"ffmpeg -i output/animation/swirl_output_%04d.jpg -vcodec libx265 -crf 28 output/swirl_output_-{STYLE}.mp4")
def create_nst_warping_animation():
CONTENT_IMAGE = "images/content/ferry.jpg"
STYLE = "delaunay"
REVERSIBLE_EDIT = ReversibleWarping()
device = torch.device("cuda")
model_path = [f.path for f in os.scandir("models/adaptiveStroke/" + STYLE) if f.name.endswith(".model")][0]
with torch.no_grad():
style_model = JohnsonAdaptiveStroke()
state_dict = torch.load(model_path)
style_model.eval()
style_model.load_state_dict(state_dict)
style_model.to(device)
for i in range(80):
print (i)
REVERSIBLE_EDIT.dst_pts = torch.FloatTensor((
# (0.45 - i * 0.001,0.5),(0.55 + i * 0.001,0.5),(0.5,0.45 - i * 0.001),(0.5,0.55 + i * 0.001)
(0.45 ,0.5),(0.55 ,0.5),(0.5,0.45),(0.5,0.55 )
))
REVERSIBLE_EDIT.src_pts = torch.FloatTensor((
(0.46- i * 0.001,0.5),(0.58 + i * 0.001,0.5),(0.5,0.43 - i * 0.001),(0.5,0.49 + i * 0.001)
))
REVERSIBLE_EDIT.add_border_points(4)
img = np.array(Image.open(CONTENT_IMAGE)).astype('uint8')
warped_image = create_vtk_thin_spline_warp(img, REVERSIBLE_EDIT.src_pts, REVERSIBLE_EDIT.dst_pts, show_grid=False)
#warped_image = utils.tensor_load_rgbimage("output/animation/cur_forward.jpg", size=512, keep_asp=True)
warped_image = warped_image.transpose(2,0,1)
warped_image = torch.from_numpy(warped_image).float()
warped_image = warped_image.unsqueeze(0).to(device)
warped_image = utils.preprocess_batch(warped_image)
stroke_factor_input = torch.tensor([1.0]).to(device)
output = style_model(warped_image, stroke_factor_input).float().cpu().squeeze(0)
img = output.clamp(0,255).numpy()
img = img.transpose(1,2,0).astype('uint8')
img = img[:,:,::-1]
output_warped_back = create_vtk_thin_spline_warp(img, REVERSIBLE_EDIT.src_pts, REVERSIBLE_EDIT.dst_pts, use_inverse=False, show_grid=False)
output_warped_back = Image.fromarray(output_warped_back)
output_warped_back.save(f"output/animation/output_{i:04d}.jpg" )
os.system(f"ffmpeg -i output/animation/warped_nst_%04d.jpg -vcodec libx265 -crf 28 output/out-nst_warps-{STYLE}.mp4")
os.system(f"ffmpeg -i output/animation/output_%04d.jpg -vcodec libx265 -crf 28 output/out-{STYLE}.mp4")
if __name__ == "__main__":
#create_nst_warping_animation()
create_nst_swirl()
#nbb_warp_nst()
#create_nst_warping_animation()