atesgoral commited on
Commit
76d24ba
1 Parent(s): 8b60672

Check for CUDA

Browse files
Files changed (3) hide show
  1. depth_estimator.py +7 -4
  2. pipeline.py +7 -4
  3. upscaler.py +4 -1
depth_estimator.py CHANGED
@@ -3,17 +3,20 @@ import numpy as np
3
  from PIL import Image
4
  from transformers import DPTFeatureExtractor, DPTForDepthEstimation
5
 
 
6
  depth_estimator = None
7
  feature_extractor = None
8
 
9
 
10
  def init():
11
- global depth_estimator, feature_extractor
 
 
12
 
13
  print("Initializing depth estimator...")
14
 
15
  depth_estimator = DPTForDepthEstimation.from_pretrained(
16
- "Intel/dpt-hybrid-midas").to("cuda")
17
  feature_extractor = DPTFeatureExtractor.from_pretrained(
18
  "Intel/dpt-hybrid-midas")
19
 
@@ -22,9 +25,9 @@ def get_depth_map(image):
22
  original_size = image.size
23
 
24
  image = feature_extractor(
25
- images=image, return_tensors="pt").pixel_values.to("cuda")
26
 
27
- with torch.no_grad(), torch.autocast("cuda"):
28
  depth_map = depth_estimator(image).predicted_depth
29
 
30
  depth_map = torch.nn.functional.interpolate(
 
3
  from PIL import Image
4
  from transformers import DPTFeatureExtractor, DPTForDepthEstimation
5
 
6
+ device = None
7
  depth_estimator = None
8
  feature_extractor = None
9
 
10
 
11
  def init():
12
+ global device, depth_estimator, feature_extractor
13
+
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
  print("Initializing depth estimator...")
17
 
18
  depth_estimator = DPTForDepthEstimation.from_pretrained(
19
+ "Intel/dpt-hybrid-midas").to(device)
20
  feature_extractor = DPTFeatureExtractor.from_pretrained(
21
  "Intel/dpt-hybrid-midas")
22
 
 
25
  original_size = image.size
26
 
27
  image = feature_extractor(
28
+ images=image, return_tensors="pt").pixel_values.to(device)
29
 
30
+ with torch.no_grad(), torch.autocast(device):
31
  depth_map = depth_estimator(image).predicted_depth
32
 
33
  depth_map = torch.nn.functional.interpolate(
pipeline.py CHANGED
@@ -1,11 +1,14 @@
1
  import torch
2
  from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel, AutoencoderKL, UniPCMultistepScheduler
3
 
 
4
  pipe = None
5
 
6
 
7
  def init():
8
- global pipe
 
 
9
 
10
  print("Initializing depth ControlNet...")
11
 
@@ -13,14 +16,14 @@ def init():
13
  "diffusers/controlnet-depth-sdxl-1.0",
14
  use_safetensors=True,
15
  torch_dtype=torch.float16
16
- ).to("cuda")
17
 
18
  print("Initializing autoencoder...")
19
 
20
  vae = AutoencoderKL.from_pretrained(
21
  "madebyollin/sdxl-vae-fp16-fix",
22
  torch_dtype=torch.float16,
23
- ).to("cuda")
24
 
25
  print("Initializing SDXL pipeline...")
26
 
@@ -32,7 +35,7 @@ def init():
32
  use_safetensors=True,
33
  torch_dtype=torch.float16
34
  # low_cpu_mem_usage=True
35
- ).to("cuda")
36
 
37
  pipe.enable_model_cpu_offload()
38
  # speed up diffusion process with faster scheduler and memory optimization
 
1
  import torch
2
  from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel, AutoencoderKL, UniPCMultistepScheduler
3
 
4
+ device = None
5
  pipe = None
6
 
7
 
8
  def init():
9
+ global device, pipe
10
+
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
 
13
  print("Initializing depth ControlNet...")
14
 
 
16
  "diffusers/controlnet-depth-sdxl-1.0",
17
  use_safetensors=True,
18
  torch_dtype=torch.float16
19
+ ).to(device)
20
 
21
  print("Initializing autoencoder...")
22
 
23
  vae = AutoencoderKL.from_pretrained(
24
  "madebyollin/sdxl-vae-fp16-fix",
25
  torch_dtype=torch.float16,
26
+ ).to(device)
27
 
28
  print("Initializing SDXL pipeline...")
29
 
 
35
  use_safetensors=True,
36
  torch_dtype=torch.float16
37
  # low_cpu_mem_usage=True
38
+ ).to(device)
39
 
40
  pipe.enable_model_cpu_offload()
41
  # speed up diffusion process with faster scheduler and memory optimization
upscaler.py CHANGED
@@ -2,6 +2,7 @@
2
  import os
3
  import requests
4
 
 
5
  import cv2
6
  import numpy as np
7
  from PIL import Image
@@ -14,6 +15,8 @@ upsampler = None
14
  def init():
15
  global upsampler
16
 
 
 
17
  print("Initializing upscaler...")
18
 
19
  if not os.path.exists("weights"):
@@ -25,7 +28,7 @@ def init():
25
  model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
26
  num_block=23, num_grow_ch=32, scale=2)
27
  upsampler = RealESRGANer(
28
- scale=2, model_path="weights/RealESRGAN_x2plus.pth", model=model, device="cuda")
29
 
30
 
31
  def upscale(image):
 
2
  import os
3
  import requests
4
 
5
+ import torch
6
  import cv2
7
  import numpy as np
8
  from PIL import Image
 
15
  def init():
16
  global upsampler
17
 
18
+ device = "cuda" if torch.cuda.is_available() else "cpu"
19
+
20
  print("Initializing upscaler...")
21
 
22
  if not os.path.exists("weights"):
 
28
  model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
29
  num_block=23, num_grow_ch=32, scale=2)
30
  upsampler = RealESRGANer(
31
+ scale=2, model_path="weights/RealESRGAN_x2plus.pth", model=model, device=device)
32
 
33
 
34
  def upscale(image):