Bobby commited on
Commit
bff1a2e
·
1 Parent(s): 3aac0a4

Reduce ZeroGPU occupancy by moving CPU output work outside GPU tasks

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -97,6 +97,7 @@ UPSCALE = 1.0
97
 
98
  _model = count.build_pet(torch.device("cpu"))
99
  _model.load_state_dict(count.load_state_dict(count.DEFAULT_WEIGHTS), strict=True)
 
100
 
101
  _logo = Image.open("assets/logo_white.png").convert("RGBA")
102
 
@@ -126,43 +127,40 @@ def add_watermark(image):
126
  return image.convert("RGB")
127
 
128
 
 
 
 
 
 
 
 
 
 
 
129
  def _count_crowd(image_path, threshold, save_images):
130
  if image_path is None:
131
  return None, "Upload an image first."
132
  image_bgr = cv2.imread(image_path)
133
  if image_bgr is None:
134
  return None, "Could not read that image."
135
-
136
  try:
137
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
138
- model = _model.to(device)
139
- with torch.inference_mode():
140
- points = count.predict_points_tiled(
141
- model,
142
- image_bgr,
143
- device,
144
- UPSCALE,
145
- TILE,
146
- OVERLAP,
147
- float(threshold),
148
- time_budget=6.5,
149
- )
150
  total = int(points.shape[0])
151
  overlay = add_watermark(count.render_overlay(image_bgr, points, total, logo=_logo))
152
  if save_images:
153
  save_images_async(image_path, overlay, total)
154
  return overlay, f"{total:,} people"
 
 
155
  except Exception as exc:
156
  print(f"count_crowd failed: {exc}", flush=True)
157
  return None, "Crowd counter failed. Please try a smaller image or try again in a minute."
158
 
159
 
160
- @spaces.GPU(duration=7)
161
  def count_crowd(image_path, threshold):
162
  return _count_crowd(image_path, threshold, save_images=True)
163
 
164
 
165
- @spaces.GPU(duration=7)
166
  def recount_crowd(image_path, threshold):
167
  return _count_crowd(image_path, threshold, save_images=False)
168
 
 
97
 
98
  _model = count.build_pet(torch.device("cpu"))
99
  _model.load_state_dict(count.load_state_dict(count.DEFAULT_WEIGHTS), strict=True)
100
+ _model.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
101
 
102
  _logo = Image.open("assets/logo_white.png").convert("RGBA")
103
 
 
127
  return image.convert("RGB")
128
 
129
 
130
+ @spaces.GPU(duration=7)
131
+ @torch.inference_mode()
132
+ def predict_crowd_points(image_bgr, threshold):
133
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
134
+ return count.predict_points_tiled(
135
+ _model, image_bgr, device, UPSCALE, TILE, OVERLAP,
136
+ float(threshold), time_budget=6.5,
137
+ )
138
+
139
+
140
  def _count_crowd(image_path, threshold, save_images):
141
  if image_path is None:
142
  return None, "Upload an image first."
143
  image_bgr = cv2.imread(image_path)
144
  if image_bgr is None:
145
  return None, "Could not read that image."
 
146
  try:
147
+ points = predict_crowd_points(image_bgr, threshold)
 
 
 
 
 
 
 
 
 
 
 
 
148
  total = int(points.shape[0])
149
  overlay = add_watermark(count.render_overlay(image_bgr, points, total, logo=_logo))
150
  if save_images:
151
  save_images_async(image_path, overlay, total)
152
  return overlay, f"{total:,} people"
153
+ except gr.Error:
154
+ raise
155
  except Exception as exc:
156
  print(f"count_crowd failed: {exc}", flush=True)
157
  return None, "Crowd counter failed. Please try a smaller image or try again in a minute."
158
 
159
 
 
160
  def count_crowd(image_path, threshold):
161
  return _count_crowd(image_path, threshold, save_images=True)
162
 
163
 
 
164
  def recount_crowd(image_path, threshold):
165
  return _count_crowd(image_path, threshold, save_images=False)
166