vukadinovic936 commited on
Commit
ed77274
1 Parent(s): b124c89
Files changed (1) hide show
  1. app.py +52 -15
app.py CHANGED
@@ -8,6 +8,7 @@ from dnnlib import tflib
8
  import imageio
9
  import os
10
  import subprocess
 
11
 
12
  def check_gpu():
13
  return tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
@@ -15,7 +16,23 @@ def check_gpu():
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)]
@@ -49,26 +66,46 @@ def load_initial_setup():
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  import imageio
9
  import os
10
  import subprocess
11
+ import random
12
 
13
  def check_gpu():
14
  return tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
 
16
  def generate_image_from_projected_latents(latent_vector):
17
  images = Gs.components.synthesis.run(latent_vector, **Gs_kwargs)
18
  return images
19
+ ## define video generation methods
20
+ def ED_to_ES(latent_code):
21
+ all_imgs = []
22
+ amounts_up = [i/25 for i in range(0,25)]
23
+ amounts_down = [1-i/25 for i in range(1,26)]
24
 
25
+ for amount_to_move in amounts_up:
26
+ modified_latent_code = latent_code + latent_controls["time"]*amount_to_move
27
+ images = generate_image_from_projected_latents(modified_latent_code)
28
+ all_imgs.append(np.array(images[0]))
29
+
30
+ for amount_to_move in amounts_down:
31
+ modified_latent_code = latent_code + latent_controls["time"]*amount_to_move
32
+ images = generate_image_from_projected_latents(modified_latent_code)
33
+ all_imgs.append(np.array(images[0]))
34
+
35
+ return np.array(all_imgs)
36
  def frame_to_frame(latent_code):
37
  modified_latent_code = np.copy(latent_code)
38
  full_video = [generate_image_from_projected_latents(modified_latent_code)]
 
66
  return Gs, Gs_kwargs, latent_controls, sess
67
 
68
  if __name__=="__main__":
69
+
70
+ st.header('GANcMRI DEMO')
71
+ sphericity_index = st.slider("Sphericity Index", -2., 3., 0.0)
72
+ lv_area = st.slider("LV Area", -2., 3., 0.0)
73
+ # Check if 'random_number' is already in the session state, if not, set a random number for seed
74
+ if 'random_number' not in st.session_state:
75
+ st.session_state.random_number = random.randint(0, 1000000)
76
+ cols = st.columns(2)
77
+
78
+ with cols[0]:
79
+ st.caption('ED-to-ES')
80
+ with cols[1]:
81
+ st.caption('Frame-to-Frame')
82
+
83
+ rnd = np.random.RandomState(st.session_state.random_number)
84
  Gs, Gs_kwargs, latent_controls, sess = load_initial_setup()
85
  with sess.as_default():
86
  z = rnd.randn(1, *Gs.input_shape[1:])
87
  random_img_latent_code = Gs.components.mapping.run(z,None)
88
  #make it be ED frame
89
  random_img_latent_code -= 0.7*latent_controls['time']
 
 
 
 
 
 
 
90
 
91
+ # Apply physiological adjustment
92
+ adjusted_latent_code = np.copy(random_img_latent_code)
93
+ adjusted_latent_code += sphericity_index * latent_controls['sphericity_index']
94
+ adjusted_latent_code += lv_area * latent_controls['lv_area']
95
 
96
+ ed_to_es_vid = ED_to_ES(adjusted_latent_code)
97
+ f_to_f_vid = frame_to_frame(adjusted_latent_code)
98
+ for idx,vid in enumerate([ed_to_es_vid, f_to_f_vid]):
99
+ temp_video_path=f"output{idx}.mp4"
100
+ writer=imageio.get_writer(temp_video_path, fps=20)
101
+ for i in range(vid.shape[0]):
102
+ frame = vid[i]
103
+ writer.append_data(frame)
104
+ writer.close()
105
+ out_path = f"fixed_out{idx}.mp4"
106
+ command = ["ffmpeg", "-i", temp_video_path, "-vcodec", "libx264", out_path]
107
+ subprocess.run(command)
108
+ with cols[idx]:
109
+ st.video(out_path)
110
+ os.remove(temp_video_path)
111
+ os.remove(out_path)