Spaces:
Sleeping
Sleeping
vukadinovic936
commited on
Commit
•
b124c89
1
Parent(s):
eba7f71
fasterr
Browse files
app.py
CHANGED
@@ -12,41 +12,10 @@ import subprocess
|
|
12 |
def check_gpu():
|
13 |
return tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
|
14 |
|
15 |
-
model_path = 'best_net.pkl'
|
16 |
-
#define load model functions
|
17 |
-
_cached_networks = dict()
|
18 |
-
def load_networks(path):
|
19 |
-
if path in _cached_networks:
|
20 |
-
return _cached_networks[path]
|
21 |
-
stream = open(path, 'rb')
|
22 |
-
tflib.init_tf()
|
23 |
-
with stream:
|
24 |
-
G, D, Gs = pickle.load(stream, encoding='latin1')
|
25 |
-
_cached_networks[path] = G, D, Gs
|
26 |
-
return G, D, Gs
|
27 |
-
|
28 |
-
# Code to load the StyleGAN2 Model
|
29 |
-
def load_model():
|
30 |
-
_G, _D, Gs = load_networks(model_path)
|
31 |
-
noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
|
32 |
-
Gs_kwargs = dnnlib.EasyDict()
|
33 |
-
Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
|
34 |
-
Gs_kwargs.randomize_noise = False
|
35 |
-
return Gs, noise_vars, Gs_kwargs
|
36 |
-
#define helper functions
|
37 |
-
def get_control_latent_vectors(path):
|
38 |
-
files = [x for x in Path(path).iterdir() if str(x).endswith('.npy')]
|
39 |
-
latent_vectors = {f.name[:-4]:np.load(f) for f in files}
|
40 |
-
return latent_vectors
|
41 |
-
|
42 |
-
#load latent directions
|
43 |
-
latent_controls = get_control_latent_vectors('trajectories/')
|
44 |
-
|
45 |
def generate_image_from_projected_latents(latent_vector):
|
46 |
images = Gs.components.synthesis.run(latent_vector, **Gs_kwargs)
|
47 |
return images
|
48 |
|
49 |
-
|
50 |
def frame_to_frame(latent_code):
|
51 |
modified_latent_code = np.copy(latent_code)
|
52 |
full_video = [generate_image_from_projected_latents(modified_latent_code)]
|
@@ -56,32 +25,50 @@ def frame_to_frame(latent_code):
|
|
56 |
full_video.append(ims)
|
57 |
return np.array(full_video).squeeze()
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
random_img_latent_code -= 0.7*latent_controls['time']
|
70 |
-
vid = frame_to_frame(random_img_latent_code)
|
71 |
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
|
84 |
-
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
os.remove(
|
|
|
|
12 |
def check_gpu():
|
13 |
return tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def generate_image_from_projected_latents(latent_vector):
|
16 |
images = Gs.components.synthesis.run(latent_vector, **Gs_kwargs)
|
17 |
return images
|
18 |
|
|
|
19 |
def frame_to_frame(latent_code):
|
20 |
modified_latent_code = np.copy(latent_code)
|
21 |
full_video = [generate_image_from_projected_latents(modified_latent_code)]
|
|
|
25 |
full_video.append(ims)
|
26 |
return np.array(full_video).squeeze()
|
27 |
|
28 |
+
@st.cache(allow_output_mutation=True) # Cache to avoid reloading the model every time
|
29 |
+
def load_initial_setup():
|
30 |
+
stream = open('best_net.pkl', 'rb')
|
31 |
+
tflib.init_tf()
|
32 |
+
sess=tf.get_default_session()
|
33 |
+
|
34 |
+
with stream:
|
35 |
+
G, D, Gs = pickle.load(stream, encoding='latin1')
|
36 |
+
noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
|
37 |
+
Gs_kwargs = dnnlib.EasyDict()
|
38 |
+
Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
|
39 |
+
Gs_kwargs.randomize_noise = False
|
40 |
+
#load latent directions
|
41 |
+
files = [x for x in Path('trajectories/').iterdir() if str(x).endswith('.npy')]
|
42 |
+
latent_controls = {f.name[:-4]:np.load(f) for f in files}
|
43 |
+
#select a random latent code
|
44 |
+
noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
|
45 |
+
rnd = np.random.RandomState()
|
46 |
|
47 |
+
tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars})
|
|
|
|
|
48 |
|
49 |
+
return Gs, Gs_kwargs, latent_controls, sess
|
50 |
|
51 |
+
if __name__=="__main__":
|
52 |
+
rnd = np.random.RandomState()
|
53 |
+
Gs, Gs_kwargs, latent_controls, sess = load_initial_setup()
|
54 |
+
with sess.as_default():
|
55 |
+
z = rnd.randn(1, *Gs.input_shape[1:])
|
56 |
+
random_img_latent_code = Gs.components.mapping.run(z,None)
|
57 |
+
#make it be ED frame
|
58 |
+
random_img_latent_code -= 0.7*latent_controls['time']
|
59 |
+
vid = frame_to_frame(random_img_latent_code)
|
60 |
|
61 |
+
temp_video_path="output.mp4"
|
62 |
+
writer=imageio.get_writer(temp_video_path, fps=20)
|
63 |
+
for i in range(vid.shape[0]):
|
64 |
+
frame = vid[i]
|
65 |
+
writer.append_data(frame)
|
66 |
|
67 |
+
writer.close()
|
68 |
+
out_path = "fixed_out.mp4"
|
69 |
+
command = ["ffmpeg", "-i", temp_video_path, "-vcodec", "libx264", out_path]
|
70 |
+
subprocess.run(command)
|
71 |
|
72 |
+
st.video(out_path)
|
73 |
+
os.remove(temp_video_path)
|
74 |
+
os.remove(out_path)
|