hugohabicht01 commited on
Commit
ff46b3e
·
1 Parent(s): a07c49e

move all sam to cpu

Browse files
Files changed (2) hide show
  1. app.py +0 -1
  2. blurnonymize.py +8 -6
app.py CHANGED
@@ -107,7 +107,6 @@ except Exception as e:
107
  raise gr.Error(f"Failed to initialize Blurnonymizer. Check logs. Error: {e}")
108
 
109
  # --- Core Processing Function ---
110
- @spaces.GPU(duration=20) # add this so that the sam segmentation runs on the gpu
111
  def anonymise_image(input_image_np: np.ndarray, boxes: list[BoundingBox]):
112
  """Calls the blurnonymizer instance to censor the image."""
113
  if not blurnonymizer_instance:
 
107
  raise gr.Error(f"Failed to initialize Blurnonymizer. Check logs. Error: {e}")
108
 
109
  # --- Core Processing Function ---
 
110
  def anonymise_image(input_image_np: np.ndarray, boxes: list[BoundingBox]):
111
  """Calls the blurnonymizer instance to censor the image."""
112
  if not blurnonymizer_instance:
blurnonymize.py CHANGED
@@ -156,8 +156,9 @@ class ImageBlurnonymizer:
156
  if self.predictor is not None and not force:
157
  return
158
 
159
- self.device = "cuda" if torch.cuda.is_available() else "cpu"
160
- self.predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-large", device=self.device)
 
161
 
162
  @staticmethod
163
  def _smoothen_mask(mask: np.ndarray):
@@ -216,9 +217,9 @@ class ImageBlurnonymizer:
216
  if self.predictor is None:
217
  raise Exception("[-] sam has not been initialized")
218
 
219
- if torch.cuda.is_available() and self.device == "cpu":
220
- # class instance was wrongly initialized to run on cpu, but gpu is avaiable
221
- self.init_sam(force=True)
222
 
223
  x_min, y_min, x_max, y_max = bbox
224
  x_width = x_max - x_min
@@ -242,7 +243,8 @@ class ImageBlurnonymizer:
242
  # Ensure points are valid coordinates (e.g., non-negative)
243
  points = [[max(0, p[0]), max(0, p[1])] for p in points]
244
 
245
- with torch.inference_mode(), torch.autocast(self.device, dtype=torch.bfloat16):
 
246
  self.predictor.set_image(image)
247
  masks, scores, _ = self.predictor.predict(
248
  box=np.array(bbox), # Predictor might expect numpy array
 
156
  if self.predictor is not None and not force:
157
  return
158
 
159
+ # self.device = "cuda" if torch.cuda.is_available() else "cpu"
160
+ self.device = "cpu"
161
+ self.predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2.1-hiera-small", device=self.device)
162
 
163
  @staticmethod
164
  def _smoothen_mask(mask: np.ndarray):
 
217
  if self.predictor is None:
218
  raise Exception("[-] sam has not been initialized")
219
 
220
+ # if torch.cuda.is_available() and self.device == "cpu":
221
+ # # class instance was wrongly initialized to run on cpu, but gpu is avaiable
222
+ # self.init_sam(force=True)
223
 
224
  x_min, y_min, x_max, y_max = bbox
225
  x_width = x_max - x_min
 
243
  # Ensure points are valid coordinates (e.g., non-negative)
244
  points = [[max(0, p[0]), max(0, p[1])] for p in points]
245
 
246
+ # with torch.inference_mode(), torch.autocast(self.device, dtype=torch.bfloat16):
247
+ with torch.inference_mode(), torch.autocast(self.device):
248
  self.predictor.set_image(image)
249
  masks, scores, _ = self.predictor.predict(
250
  box=np.array(bbox), # Predictor might expect numpy array