developerskyebrowse commited on
Commit
6dbb9c3
·
1 Parent(s): 8e103f0

testing new preprocess

Browse files
Files changed (4) hide show
  1. app.py +10 -11
  2. local_app.py +1 -1
  3. local_preprocess.py +2 -37
  4. preprocess.py +5 -37
app.py CHANGED
@@ -21,7 +21,6 @@ from diffusers import (
21
  AutoencoderKL,
22
  )
23
  from diffusers.models.attention_processor import AttnProcessor2_0
24
- from preprocess import Preprocessor
25
  MAX_SEED = np.iinfo(np.int32).max
26
  API_KEY = os.environ.get("API_KEY", None)
27
 
@@ -31,9 +30,6 @@ compiled = False
31
  # api = HfApi()
32
 
33
  import spaces
34
-
35
- preprocessor = Preprocessor()
36
- preprocessor.load("NormalBae")
37
 
38
  if gr.NO_RELOAD:
39
  torch.cuda.max_memory_allocated(device="cuda")
@@ -98,13 +94,16 @@ if gr.NO_RELOAD:
98
  # gc.collect()
99
  print("---------------Loaded controlnet pipeline---------------")
100
 
101
- # @spaces.GPU(duration=12)
102
- # def init(pipe):
103
- # pipe.enable_xformers_memory_efficient_attention()
104
- # pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
105
- # pipe.unet.set_attn_processor(AttnProcessor2_0())
106
- # print("Model Compiled!")
107
- # init(pipe)
 
 
 
108
 
109
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
110
  if randomize_seed:
 
21
  AutoencoderKL,
22
  )
23
  from diffusers.models.attention_processor import AttnProcessor2_0
 
24
  MAX_SEED = np.iinfo(np.int32).max
25
  API_KEY = os.environ.get("API_KEY", None)
26
 
 
30
  # api = HfApi()
31
 
32
  import spaces
 
 
 
33
 
34
  if gr.NO_RELOAD:
35
  torch.cuda.max_memory_allocated(device="cuda")
 
94
  # gc.collect()
95
  print("---------------Loaded controlnet pipeline---------------")
96
 
97
+ @spaces.GPU(duration=12)
98
+ def init(pipe):
99
+ pipe.enable_xformers_memory_efficient_attention()
100
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
101
+ pipe.unet.set_attn_processor(AttnProcessor2_0())
102
+ from preprocess import Preprocessor
103
+ preprocessor = Preprocessor()
104
+ preprocessor.load("NormalBae")
105
+ print("Model Compiled!")
106
+ init(pipe)
107
 
108
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
109
  if randomize_seed:
local_app.py CHANGED
@@ -21,7 +21,6 @@ from diffusers import (
21
  AutoencoderKL,
22
  )
23
  from diffusers.models.attention_processor import AttnProcessor2_0
24
- from local_preprocess import Preprocessor
25
  MAX_SEED = np.iinfo(np.int32).max
26
  API_KEY = os.environ.get("API_KEY", None)
27
 
@@ -30,6 +29,7 @@ print("loading pipe")
30
  compiled = False
31
 
32
  if gr.NO_RELOAD:
 
33
  preprocessor = Preprocessor()
34
  preprocessor.load("NormalBae")
35
  torch.cuda.max_memory_allocated(device="cuda")
 
21
  AutoencoderKL,
22
  )
23
  from diffusers.models.attention_processor import AttnProcessor2_0
 
24
  MAX_SEED = np.iinfo(np.int32).max
25
  API_KEY = os.environ.get("API_KEY", None)
26
 
 
29
  compiled = False
30
 
31
  if gr.NO_RELOAD:
32
+ from local_preprocess import Preprocessor
33
  preprocessor = Preprocessor()
34
  preprocessor.load("NormalBae")
35
  torch.cuda.max_memory_allocated(device="cuda")
local_preprocess.py CHANGED
@@ -10,21 +10,6 @@ from controlnet_aux import NormalBaeDetector
10
 
11
  class Preprocessor:
12
  MODEL_ID = "lllyasviel/Annotators"
13
-
14
- # def resize_image(input_image, resolution, interpolation=None):
15
- # H, W, C = input_image.shape
16
- # H = float(H)
17
- # W = float(W)
18
- # k = float(resolution) / max(H, W)
19
- # H *= k
20
- # W *= k
21
- # H = int(np.round(H / 64.0)) * 64
22
- # W = int(np.round(W / 64.0)) * 64
23
- # if interpolation is None:
24
- # interpolation = cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA
25
- # img = cv2.resize(input_image, (W, H), interpolation=interpolation)
26
- # return img
27
-
28
 
29
  def __init__(self):
30
  self.model = None
@@ -36,33 +21,13 @@ class Preprocessor:
36
  elif name == "NormalBae":
37
  print("Loading NormalBae")
38
  self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
 
 
39
  # elif name == "Canny":
40
  # self.model = CannyDetector()
41
  else:
42
  raise ValueError
43
- torch.cuda.empty_cache()
44
- gc.collect()
45
-
46
  self.name = name
47
 
48
  def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
49
- # if self.name == "Canny":
50
- # if "detect_resolution" in kwargs:
51
- # detect_resolution = kwargs.pop("detect_resolution")
52
- # image = np.array(image)
53
- # image = HWC3(image)
54
- # image = resize_image(image, resolution=detect_resolution)
55
- # image = self.model(image, **kwargs)
56
- # return PIL.Image.fromarray(image)
57
- # elif self.name == "Midas":
58
- # detect_resolution = kwargs.pop("detect_resolution", 512)
59
- # image_resolution = kwargs.pop("image_resolution", 512)
60
- # image = np.array(image)
61
- # image = HWC3(image)
62
- # image = resize_image(image, resolution=detect_resolution)
63
- # image = self.model(image, **kwargs)
64
- # image = HWC3(image)
65
- # image = resize_image(image, resolution=image_resolution)
66
- # return PIL.Image.fromarray(image)
67
- # else:
68
  return self.model(image, **kwargs)
 
10
 
11
  class Preprocessor:
12
  MODEL_ID = "lllyasviel/Annotators"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def __init__(self):
15
  self.model = None
 
21
  elif name == "NormalBae":
22
  print("Loading NormalBae")
23
  self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
24
+ torch.cuda.empty_cache()
25
+ gc.collect()
26
  # elif name == "Canny":
27
  # self.model = CannyDetector()
28
  else:
29
  raise ValueError
 
 
 
30
  self.name = name
31
 
32
  def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  return self.model(image, **kwargs)
preprocess.py CHANGED
@@ -1,7 +1,8 @@
1
  # import numpy as np
2
  import PIL.Image
3
- # import torch
4
  from controlnet_aux_local import NormalBaeDetector#, CannyDetector
 
5
 
6
  # from controlnet_aux.util import HWC3
7
  # import cv2
@@ -9,21 +10,6 @@ from controlnet_aux_local import NormalBaeDetector#, CannyDetector
9
 
10
  class Preprocessor:
11
  MODEL_ID = "lllyasviel/Annotators"
12
-
13
- # def resize_image(input_image, resolution, interpolation=None):
14
- # H, W, C = input_image.shape
15
- # H = float(H)
16
- # W = float(W)
17
- # k = float(resolution) / max(H, W)
18
- # H *= k
19
- # W *= k
20
- # H = int(np.round(H / 64.0)) * 64
21
- # W = int(np.round(W / 64.0)) * 64
22
- # if interpolation is None:
23
- # interpolation = cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA
24
- # img = cv2.resize(input_image, (W, H), interpolation=interpolation)
25
- # return img
26
-
27
 
28
  def __init__(self):
29
  self.model = None
@@ -35,33 +21,15 @@ class Preprocessor:
35
  elif name == "NormalBae":
36
  print("Loading NormalBae")
37
  self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
 
 
38
  # elif name == "Canny":
39
  # self.model = CannyDetector()
40
  else:
41
  raise ValueError
42
- # torch.cuda.empty_cache()
43
- # gc.collect()
44
 
45
  self.name = name
46
 
47
  def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
48
- # if self.name == "Canny":
49
- # if "detect_resolution" in kwargs:
50
- # detect_resolution = kwargs.pop("detect_resolution")
51
- # image = np.array(image)
52
- # image = HWC3(image)
53
- # image = resize_image(image, resolution=detect_resolution)
54
- # image = self.model(image, **kwargs)
55
- # return PIL.Image.fromarray(image)
56
- # elif self.name == "Midas":
57
- # detect_resolution = kwargs.pop("detect_resolution", 512)
58
- # image_resolution = kwargs.pop("image_resolution", 512)
59
- # image = np.array(image)
60
- # image = HWC3(image)
61
- # image = resize_image(image, resolution=detect_resolution)
62
- # image = self.model(image, **kwargs)
63
- # image = HWC3(image)
64
- # image = resize_image(image, resolution=image_resolution)
65
- # return PIL.Image.fromarray(image)
66
- # else:
67
  return self.model(image, **kwargs)
 
1
  # import numpy as np
2
  import PIL.Image
3
+ import torch, gc
4
  from controlnet_aux_local import NormalBaeDetector#, CannyDetector
5
+ import spaces
6
 
7
  # from controlnet_aux.util import HWC3
8
  # import cv2
 
10
 
11
  class Preprocessor:
12
  MODEL_ID = "lllyasviel/Annotators"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def __init__(self):
15
  self.model = None
 
21
  elif name == "NormalBae":
22
  print("Loading NormalBae")
23
  self.model = NormalBaeDetector.from_pretrained(self.MODEL_ID).to("cuda")
24
+ torch.cuda.empty_cache()
25
+ gc.collect()
26
  # elif name == "Canny":
27
  # self.model = CannyDetector()
28
  else:
29
  raise ValueError
30
+
 
31
 
32
  self.name = name
33
 
34
  def __call__(self, image: PIL.Image.Image, **kwargs) -> PIL.Image.Image:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  return self.model(image, **kwargs)