vukadinovic936 commited on
Commit
a47a354
1 Parent(s): 83fd9be

added generated file

Browse files
Files changed (2) hide show
  1. Dockerfile +7 -1
  2. app.py +80 -5
Dockerfile CHANGED
@@ -5,7 +5,13 @@ WORKDIR /data
5
  COPY requirements.txt ./
6
  RUN pip install --no-cache-dir -r requirements.txt
7
 
8
- EXPOSE 7860
 
 
 
 
 
9
 
 
10
  COPY . .
11
  CMD ["streamlit", "run", "app.py", "--server.port", "7860"]
 
5
  COPY requirements.txt ./
6
  RUN pip install --no-cache-dir -r requirements.txt
7
 
8
+ RUN apt-get update && \
9
+ apt-get install -y libglib2.0-0 && \
10
+ apt-get install libgl1-mesa-glx && \
11
+ apt-get install -y ffmpeg && \
12
+ apt-get clean && \
13
+ rm -rf /var/lib/apt/lists/*
14
 
15
+ EXPOSE 7860
16
  COPY . .
17
  CMD ["streamlit", "run", "app.py", "--server.port", "7860"]
app.py CHANGED
@@ -1,12 +1,87 @@
1
  import streamlit as st
2
  import tensorflow as tf
 
 
 
 
 
 
 
 
3
 
4
  def check_gpu():
5
  return tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
6
 
7
- st.title('GPU Availability Checker')
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- if check_gpu():
10
- st.success('GPU is available!')
11
- else:
12
- st.warning('GPU is not available.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import tensorflow as tf
3
+ import pickle
4
+ import numpy as np
5
+ from pathlib import Path
6
+ import dnnlib
7
+ from dnnlib import tflib
8
+ import cv2
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)
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)]
53
+ for i in range(49):
54
+ modified_latent_code = modified_latent_code + latent_controls[f'{i}{i+1}']
55
+ ims = generate_image_from_projected_latents(modified_latent_code)
56
+ full_video.append(ims)
57
+ return np.array(full_video).squeeze()
58
+
59
+ #load the model
60
+ Gs, noise_vars, Gs_kwargs = load_model()
61
+ #select a random latent code
62
+ rnd = np.random.RandomState(3)
63
+ z = rnd.randn(1, *Gs.input_shape[1:])
64
+ noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
65
+ tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars})
66
+ random_img_latent_code = Gs.components.mapping.run(z,None)
67
+
68
+ #make it be ED frame
69
+ random_img_latent_code -= 0.7*latent_controls['time']
70
+ vid = frame_to_frame(random_img_latent_code)
71
+
72
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
73
+ temp_video_path="output.mp4"
74
+ out = cv2.VideoWriter(temp_video_path, fourcc, 20.0, (256, 256), isColor=False)
75
+
76
+ for i in range(vid.shape[0]):
77
+ frame = vid[i]
78
+ out.write(frame)
79
+ out.release()
80
+ out_path = "fixed_out.mp4"
81
+ command = ["ffmpeg", "-i", temp_video_path, "-vcodec", "libx264", out_path]
82
+ subprocess.run(command)
83
+
84
+ st.video(out_path)
85
+
86
+ os.remove(temp_video_path)
87
+ os.remove(out_path)