nroggendorff commited on
Commit
4251451
·
1 Parent(s): 77b40bf

add cuda support

Browse files

i hope

Update app.py

Update requirements.txt

Update requirements.txt

Files changed (2) hide show
  1. app.py +92 -32
  2. requirements.txt +5 -17
app.py CHANGED
@@ -23,15 +23,15 @@ def load_pipeline():
23
  safety_checker=None,
24
  )
25
  pipe = pipe.to(device)
 
 
26
  return pipe
27
 
28
 
29
  @spaces.GPU
30
  def refine_with_img2img(image_path, strength=0.3, steps=30, seed=42):
31
  pipeline = load_pipeline()
32
-
33
  img = Image.open(image_path).convert("RGB")
34
-
35
  generator = torch.Generator(device=device).manual_seed(seed)
36
 
37
  result = pipeline(
@@ -48,7 +48,9 @@ def refine_with_img2img(image_path, strength=0.3, steps=30, seed=42):
48
 
49
 
50
  @spaces.GPU
51
- def refine_video_with_img2img(video_path, strength=0.3, steps=30, seed=42):
 
 
52
  pipeline = load_pipeline()
53
  generator = torch.Generator(device=device).manual_seed(seed)
54
 
@@ -61,42 +63,72 @@ def refine_video_with_img2img(video_path, strength=0.3, steps=30, seed=42):
61
  temp_output = "temp_refined.mp4"
62
  out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
63
 
 
64
  frame_count = 0
 
65
  while True:
66
  ret, frame = cap.read()
67
  if not ret:
 
 
 
 
 
 
 
 
 
 
 
 
68
  break
69
 
70
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
71
  pil_frame = Image.fromarray(frame_rgb)
72
-
73
- refined = pipeline(
74
- prompt="high quality, detailed face, photorealistic, natural skin texture",
75
- image=pil_frame,
76
- strength=strength,
77
- num_inference_steps=steps,
78
- guidance_scale=7.5,
79
- generator=generator,
80
- ).images[0]
81
-
82
- refined_cv = cv2.cvtColor(np.array(refined), cv2.COLOR_RGB2BGR)
83
- out.write(refined_cv)
84
-
85
- frame_count += 1
86
- print(f"Processed frame {frame_count}")
 
 
87
 
88
  cap.release()
89
  out.release()
90
-
91
  os.replace(temp_output, video_path)
92
 
93
 
94
- def denoise_image(image_path, strength=10):
95
  img = cv2.imread(image_path)
96
- denoised = cv2.fastNlMeansDenoisingColored(img, None, strength, strength, 7, 21)
 
 
 
 
 
 
97
  cv2.imwrite(image_path, denoised)
98
 
99
 
 
 
 
 
 
 
 
 
 
100
  def denoise_video(video_path, strength=10):
101
  cap = cv2.VideoCapture(video_path)
102
  fps = cap.get(cv2.CAP_PROP_FPS)
@@ -107,18 +139,39 @@ def denoise_video(video_path, strength=10):
107
  temp_output = "temp_denoised.mp4"
108
  out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
109
 
 
 
 
 
 
 
 
110
  while True:
111
  ret, frame = cap.read()
112
  if not ret:
113
  break
114
- denoised_frame = cv2.fastNlMeansDenoisingColored(
115
- frame, None, strength, strength, 7, 21
116
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  out.write(denoised_frame)
118
 
119
  cap.release()
120
  out.release()
121
-
122
  os.replace(temp_output, video_path)
123
 
124
 
@@ -133,6 +186,7 @@ def enhance_image(image_path):
133
  cv2.imwrite(image_path, enhanced)
134
 
135
 
 
136
  def process_media(
137
  image,
138
  image_or_video,
@@ -142,6 +196,7 @@ def process_media(
142
  img2img_strength,
143
  img2img_steps,
144
  seed,
 
145
  ):
146
  if os.path.exists("output_video.mp4"):
147
  os.remove("output_video.mp4")
@@ -157,7 +212,7 @@ def process_media(
157
  (".mp4", ".avi", ".mov")
158
  ):
159
  image.save("source.png")
160
- cmd = f"python3 roop/run.py -s source.png -t '{image_or_video}' -o output_video.mp4"
161
  subprocess.run(cmd, shell=True)
162
 
163
  if os.path.exists("output_video.mp4"):
@@ -165,7 +220,6 @@ def process_media(
165
  refine_video_with_img2img(
166
  "output_video.mp4", img2img_strength, img2img_steps, seed
167
  )
168
-
169
  denoise_video("output_video.mp4", denoise_strength)
170
  video_output = gr.Video(value="output_video.mp4", visible=True)
171
 
@@ -173,7 +227,7 @@ def process_media(
173
  (".png", ".jpg", ".jpeg")
174
  ):
175
  image.save("source.png")
176
- cmd = f"python3 roop/run.py -s source.png -t '{image_or_video}' -o output_image.png"
177
  subprocess.run(cmd, shell=True)
178
 
179
  if os.path.exists("output_image.png"):
@@ -181,12 +235,9 @@ def process_media(
181
  refine_with_img2img(
182
  "output_image.png", img2img_strength, img2img_steps, seed
183
  )
184
-
185
  denoise_image("output_image.png", denoise_strength)
186
-
187
  if enhance:
188
  enhance_image("output_image.png")
189
-
190
  image_output = gr.Image(value="output_image.png", visible=True)
191
 
192
  return image_output, video_output
@@ -215,6 +266,11 @@ with gr.Blocks() as demo:
215
  )
216
  seed = gr.Number(label="Seed", value=42, precision=0)
217
 
 
 
 
 
 
218
  process_btn = gr.Button("Process")
219
 
220
  image_output = gr.Image(label="Output Image", visible=False)
@@ -231,6 +287,7 @@ with gr.Blocks() as demo:
231
  img2img_strength,
232
  img2img_steps,
233
  seed,
 
234
  ],
235
  outputs=[image_output, video_output],
236
  )
@@ -239,9 +296,12 @@ demo.queue()
239
 
240
  if __name__ == "__main__":
241
  if not os.path.exists("roop"):
242
- Repo.clone_from("https://github.com/s0md3v/roop.git", "roop")
243
  subprocess.run("pip install -r roop/requirements.txt", shell=True)
 
244
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
245
  os.environ["TF_USE_LEGACY_KERAS"] = "1"
 
 
246
 
247
  demo.launch()
 
23
  safety_checker=None,
24
  )
25
  pipe = pipe.to(device)
26
+ if hasattr(pipe, "enable_attention_slicing"):
27
+ pipe.enable_attention_slicing()
28
  return pipe
29
 
30
 
31
  @spaces.GPU
32
  def refine_with_img2img(image_path, strength=0.3, steps=30, seed=42):
33
  pipeline = load_pipeline()
 
34
  img = Image.open(image_path).convert("RGB")
 
35
  generator = torch.Generator(device=device).manual_seed(seed)
36
 
37
  result = pipeline(
 
48
 
49
 
50
  @spaces.GPU
51
+ def refine_video_with_img2img(
52
+ video_path, strength=0.3, steps=30, seed=42, batch_size=4
53
+ ):
54
  pipeline = load_pipeline()
55
  generator = torch.Generator(device=device).manual_seed(seed)
56
 
 
63
  temp_output = "temp_refined.mp4"
64
  out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
65
 
66
+ frames_batch = []
67
  frame_count = 0
68
+
69
  while True:
70
  ret, frame = cap.read()
71
  if not ret:
72
+ if frames_batch:
73
+ for pil_frame in frames_batch:
74
+ refined = pipeline(
75
+ prompt="high quality, detailed face, photorealistic, natural skin texture",
76
+ image=pil_frame,
77
+ strength=strength,
78
+ num_inference_steps=steps,
79
+ guidance_scale=7.5,
80
+ generator=generator,
81
+ ).images[0]
82
+ refined_cv = cv2.cvtColor(np.array(refined), cv2.COLOR_RGB2BGR)
83
+ out.write(refined_cv)
84
  break
85
 
86
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
87
  pil_frame = Image.fromarray(frame_rgb)
88
+ frames_batch.append(pil_frame)
89
+
90
+ if len(frames_batch) >= batch_size:
91
+ for pil_frame in frames_batch:
92
+ refined = pipeline(
93
+ prompt="high quality, detailed face, photorealistic, natural skin texture",
94
+ image=pil_frame,
95
+ strength=strength,
96
+ num_inference_steps=steps,
97
+ guidance_scale=7.5,
98
+ generator=generator,
99
+ ).images[0]
100
+ refined_cv = cv2.cvtColor(np.array(refined), cv2.COLOR_RGB2BGR)
101
+ out.write(refined_cv)
102
+ frame_count += 1
103
+ print(f"Processed frame {frame_count}")
104
+ frames_batch = []
105
 
106
  cap.release()
107
  out.release()
 
108
  os.replace(temp_output, video_path)
109
 
110
 
111
+ def denoise_image_gpu(image_path, strength=10):
112
  img = cv2.imread(image_path)
113
+ img_gpu = cv2.cuda_GpuMat()
114
+ img_gpu.upload(img)
115
+
116
+ denoised_gpu = cv2.cuda.fastNlMeansDenoisingColored(
117
+ img_gpu, strength, strength, 7, 21
118
+ )
119
+ denoised = denoised_gpu.download()
120
  cv2.imwrite(image_path, denoised)
121
 
122
 
123
+ def denoise_image(image_path, strength=10):
124
+ try:
125
+ denoise_image_gpu(image_path, strength)
126
+ except:
127
+ img = cv2.imread(image_path)
128
+ denoised = cv2.fastNlMeansDenoisingColored(img, None, strength, strength, 7, 21)
129
+ cv2.imwrite(image_path, denoised)
130
+
131
+
132
  def denoise_video(video_path, strength=10):
133
  cap = cv2.VideoCapture(video_path)
134
  fps = cap.get(cv2.CAP_PROP_FPS)
 
139
  temp_output = "temp_denoised.mp4"
140
  out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))
141
 
142
+ use_gpu = False
143
+ try:
144
+ test_gpu = cv2.cuda_GpuMat()
145
+ use_gpu = True
146
+ except:
147
+ pass
148
+
149
  while True:
150
  ret, frame = cap.read()
151
  if not ret:
152
  break
153
+
154
+ if use_gpu:
155
+ try:
156
+ frame_gpu = cv2.cuda_GpuMat()
157
+ frame_gpu.upload(frame)
158
+ denoised_gpu = cv2.cuda.fastNlMeansDenoisingColored(
159
+ frame_gpu, strength, strength, 7, 21
160
+ )
161
+ denoised_frame = denoised_gpu.download()
162
+ except:
163
+ denoised_frame = cv2.fastNlMeansDenoisingColored(
164
+ frame, None, strength, strength, 7, 21
165
+ )
166
+ else:
167
+ denoised_frame = cv2.fastNlMeansDenoisingColored(
168
+ frame, None, strength, strength, 7, 21
169
+ )
170
+
171
  out.write(denoised_frame)
172
 
173
  cap.release()
174
  out.release()
 
175
  os.replace(temp_output, video_path)
176
 
177
 
 
186
  cv2.imwrite(image_path, enhanced)
187
 
188
 
189
+ @spaces.GPU
190
  def process_media(
191
  image,
192
  image_or_video,
 
196
  img2img_strength,
197
  img2img_steps,
198
  seed,
199
+ execution_provider,
200
  ):
201
  if os.path.exists("output_video.mp4"):
202
  os.remove("output_video.mp4")
 
212
  (".mp4", ".avi", ".mov")
213
  ):
214
  image.save("source.png")
215
+ cmd = f"python3 roop/run.py -s source.png -t '{image_or_video}' -o output_video.mp4 --execution-provider {execution_provider}"
216
  subprocess.run(cmd, shell=True)
217
 
218
  if os.path.exists("output_video.mp4"):
 
220
  refine_video_with_img2img(
221
  "output_video.mp4", img2img_strength, img2img_steps, seed
222
  )
 
223
  denoise_video("output_video.mp4", denoise_strength)
224
  video_output = gr.Video(value="output_video.mp4", visible=True)
225
 
 
227
  (".png", ".jpg", ".jpeg")
228
  ):
229
  image.save("source.png")
230
+ cmd = f"python3 roop/run.py -s source.png -t '{image_or_video}' -o output_image.png --execution-provider {execution_provider}"
231
  subprocess.run(cmd, shell=True)
232
 
233
  if os.path.exists("output_image.png"):
 
235
  refine_with_img2img(
236
  "output_image.png", img2img_strength, img2img_steps, seed
237
  )
 
238
  denoise_image("output_image.png", denoise_strength)
 
239
  if enhance:
240
  enhance_image("output_image.png")
 
241
  image_output = gr.Image(value="output_image.png", visible=True)
242
 
243
  return image_output, video_output
 
266
  )
267
  seed = gr.Number(label="Seed", value=42, precision=0)
268
 
269
+ with gr.Row():
270
+ execution_provider = gr.Radio(
271
+ choices=["cuda", "tensorrt"], value="cuda", label="Roop Execution Provider"
272
+ )
273
+
274
  process_btn = gr.Button("Process")
275
 
276
  image_output = gr.Image(label="Output Image", visible=False)
 
287
  img2img_strength,
288
  img2img_steps,
289
  seed,
290
+ execution_provider,
291
  ],
292
  outputs=[image_output, video_output],
293
  )
 
296
 
297
  if __name__ == "__main__":
298
  if not os.path.exists("roop"):
299
+ Repo.clone_from("https://github.com/nroggendorff/roop.git", "roop")
300
  subprocess.run("pip install -r roop/requirements.txt", shell=True)
301
+
302
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
303
  os.environ["TF_USE_LEGACY_KERAS"] = "1"
304
+ os.environ["OMP_NUM_THREADS"] = "8"
305
+ os.environ["MKL_NUM_THREADS"] = "8"
306
 
307
  demo.launch()
requirements.txt CHANGED
@@ -1,19 +1,7 @@
1
- numpy==1.24.3
2
- opencv-python==4.7.0.72
3
- onnx==1.14.0
4
- gitpython==3.1.30
5
- pillow==9.5.0
6
- insightface==0.7.3
7
- psutil==5.9.5
8
- tk==0.1.0
9
- customtkinter==5.2.0
10
- typing-extensions>=4.8.0
11
- tkinterdnd2==0.3.0
12
- onnxruntime
13
- tensorflow>=2.14.0
14
- opennsfw2==0.10.2
15
- tqdm==4.65.0
16
  diffusers
17
  accelerate
18
- transformers
19
- torch==2.2
 
 
1
+ # torch==2.2.0
2
+ transformers
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  diffusers
4
  accelerate
5
+ gitpython
6
+ opencv-python
7
+ pillow