huntrezz commited on
Commit
7bc8ed0
·
verified ·
1 Parent(s): 9aa4818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -7,7 +7,7 @@ import torch.nn.utils.prune as prune
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
 
10
- model = DPTForDepthEstimation.from_pretrained("Intel/dpt-swinv2-tiny-256", torch_dtype=torch.float32)
11
  model.eval()
12
 
13
  # Apply global unstructured pruning
@@ -27,20 +27,21 @@ model = torch.quantization.quantize_dynamic(
27
  model, {torch.nn.Linear, torch.nn.Conv2d}, dtype=torch.qint8
28
  )
29
 
30
- model = model.to(device)
31
 
32
- processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
33
 
34
  color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)
 
35
 
36
- input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float32, device=device)
37
 
38
  def preprocess_image(image):
39
- image = torch.from_numpy(image).to(device)
40
  image = torch.nn.functional.interpolate(image.permute(2, 0, 1).unsqueeze(0), size=(128, 128), mode='bilinear', align_corners=False)
41
  return (image.squeeze(0) / 255.0)
42
 
43
- static_input = torch.zeros((1, 3, 128, 128), device=device, dtype=torch.float16) # Use float16 if using FP16
44
  g = torch.cuda.CUDAGraph()
45
  with torch.cuda.graph(g):
46
  static_output = model(static_input)
@@ -52,13 +53,12 @@ def process_frame(image):
52
  preprocessed = preprocess_image(image)
53
  static_input.copy_(preprocessed)
54
  g.replay()
55
- depth_map = static_output.predicted_depth.squeeze().cpu().numpy()
56
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
57
- depth_map = (depth_map * 255).astype(np.uint8)
58
- depth_map_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
59
-
60
- return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
61
-
62
  interface = gr.Interface(
63
  fn=process_frame,
64
  inputs=gr.Image(sources="webcam", streaming=True),
 
7
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
 
10
+ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-swinv2-tiny-128", torch_dtype=torch.float16)
11
  model.eval()
12
 
13
  # Apply global unstructured pruning
 
27
  model, {torch.nn.Linear, torch.nn.Conv2d}, dtype=torch.qint8
28
  )
29
 
30
+ model = model.half().to(device)
31
 
32
+ processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-128")
33
 
34
  color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)
35
+ color_map = torch.from_numpy(color_map).to(device)
36
 
37
+ input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float16, device=device)
38
 
39
  def preprocess_image(image):
40
+ image = torch.from_numpy(image).to(device, dtype=torch.float16)
41
  image = torch.nn.functional.interpolate(image.permute(2, 0, 1).unsqueeze(0), size=(128, 128), mode='bilinear', align_corners=False)
42
  return (image.squeeze(0) / 255.0)
43
 
44
+ static_input = torch.zeros((1, 3, 128, 128), device=device, dtype=torch.float16)
45
  g = torch.cuda.CUDAGraph()
46
  with torch.cuda.graph(g):
47
  static_output = model(static_input)
 
53
  preprocessed = preprocess_image(image)
54
  static_input.copy_(preprocessed)
55
  g.replay()
56
+ depth_map = static_output.predicted_depth.squeeze()
57
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
58
+ depth_map = (depth_map * 255).to(torch.uint8)
59
+ depth_map_colored = color_map[depth_map]
60
+ return depth_map_colored.cpu().numpy()
61
+
 
62
  interface = gr.Interface(
63
  fn=process_frame,
64
  inputs=gr.Image(sources="webcam", streaming=True),