radames commited on
Commit
578d7dc
·
1 Parent(s): 266dbe0

minor optimization

Browse files
server/pipelines/pix2pixTurbo.py CHANGED
@@ -7,7 +7,6 @@ from PIL import Image
7
  from pipelines.pix2pix.pix2pix_turbo import Pix2Pix_Turbo
8
  from pipelines.utils.canny_gpu import SobelOperator
9
 
10
-
11
  default_prompt = "close-up photo of the joker"
12
  page_content = """
13
  <h1 class="text-3xl font-bold">Real-Time pix2pix_turbo</h1>
@@ -94,6 +93,7 @@ class Pipeline:
94
  self.model = Pix2Pix_Turbo("edge_to_image")
95
  self.canny_torch = SobelOperator(device=device)
96
  self.device = device
 
97
 
98
  def predict(self, params: "Pipeline.InputParams") -> Image.Image:
99
  # generator = torch.manual_seed(params.seed)
@@ -123,5 +123,4 @@ class Pipeline:
123
  control_image = canny_pil.resize((w0, h0))
124
  w1, h1 = result_image.size
125
  result_image.paste(control_image, (w1 - w0, h1 - h0))
126
-
127
  return result_image
 
7
  from pipelines.pix2pix.pix2pix_turbo import Pix2Pix_Turbo
8
  from pipelines.utils.canny_gpu import SobelOperator
9
 
 
10
  default_prompt = "close-up photo of the joker"
11
  page_content = """
12
  <h1 class="text-3xl font-bold">Real-Time pix2pix_turbo</h1>
 
93
  self.model = Pix2Pix_Turbo("edge_to_image")
94
  self.canny_torch = SobelOperator(device=device)
95
  self.device = device
96
+ self.last_time = 0.0
97
 
98
  def predict(self, params: "Pipeline.InputParams") -> Image.Image:
99
  # generator = torch.manual_seed(params.seed)
 
123
  control_image = canny_pil.resize((w0, h0))
124
  w1, h1 = result_image.size
125
  result_image.paste(control_image, (w1 - w0, h1 - h0))
 
126
  return result_image
server/pipelines/utils/canny_gpu.py CHANGED
@@ -5,6 +5,13 @@ from PIL import Image
5
 
6
 
7
  class SobelOperator(nn.Module):
 
 
 
 
 
 
 
8
  def __init__(self, device="cuda"):
9
  super(SobelOperator, self).__init__()
10
  self.device = device
@@ -14,17 +21,13 @@ class SobelOperator(nn.Module):
14
  self.edge_conv_y = nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False).to(
15
  self.device
16
  )
17
-
18
- sobel_kernel_x = torch.tensor(
19
- [[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]], device=self.device
20
  )
21
- sobel_kernel_y = torch.tensor(
22
- [[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]], device=self.device
23
  )
24
 
25
- self.edge_conv_x.weight = nn.Parameter(sobel_kernel_x.view((1, 1, 3, 3)))
26
- self.edge_conv_y.weight = nn.Parameter(sobel_kernel_y.view((1, 1, 3, 3)))
27
-
28
  @torch.no_grad()
29
  def forward(
30
  self,
@@ -40,10 +43,10 @@ class SobelOperator(nn.Module):
40
  # Compute gradients
41
  edge_x = self.edge_conv_x(image_tensor)
42
  edge_y = self.edge_conv_y(image_tensor)
43
- edge = torch.sqrt(edge_x**2 + edge_y**2)
44
 
45
  # Apply thresholding
46
- edge = edge / edge.max() # Normalize to 0-1
47
  edge[edge >= high_threshold] = 1.0
48
  edge[edge <= low_threshold] = 0.0
49
 
 
5
 
6
 
7
  class SobelOperator(nn.Module):
8
+ SOBEL_KERNEL_X = torch.tensor(
9
+ [[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]]
10
+ )
11
+ SOBEL_KERNEL_Y = torch.tensor(
12
+ [[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]]
13
+ )
14
+
15
  def __init__(self, device="cuda"):
16
  super(SobelOperator, self).__init__()
17
  self.device = device
 
21
  self.edge_conv_y = nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False).to(
22
  self.device
23
  )
24
+ self.edge_conv_x.weight = nn.Parameter(
25
+ self.SOBEL_KERNEL_X.view((1, 1, 3, 3)).to(self.device)
 
26
  )
27
+ self.edge_conv_y.weight = nn.Parameter(
28
+ self.SOBEL_KERNEL_Y.view((1, 1, 3, 3)).to(self.device)
29
  )
30
 
 
 
 
31
  @torch.no_grad()
32
  def forward(
33
  self,
 
43
  # Compute gradients
44
  edge_x = self.edge_conv_x(image_tensor)
45
  edge_y = self.edge_conv_y(image_tensor)
46
+ edge = torch.sqrt(torch.square(edge_x) + torch.square(edge_y))
47
 
48
  # Apply thresholding
49
+ edge.div_(edge.max()) # Normalize to 0-1 (in-place operation)
50
  edge[edge >= high_threshold] = 1.0
51
  edge[edge <= low_threshold] = 0.0
52