ahsanMah commited on
Commit
69da633
·
1 Parent(s): b3c4783

testing cuda usage

Browse files
Files changed (1) hide show
  1. hfapp.py +12 -13
hfapp.py CHANGED
@@ -14,7 +14,10 @@ from app import (
14
 
15
  @spaces.GPU
16
  def run_inference(model, img):
 
 
17
  print("model on cuda:", next(model.scorenet.net.parameters()).is_cuda)
 
18
  img = torch.nn.functional.interpolate(img, size=64, mode="bilinear")
19
  score_norms = model.scorenet(img)
20
  score_norms = score_norms.square().sum(dim=(2, 3, 4)) ** 0.5
@@ -24,20 +27,16 @@ def run_inference(model, img):
24
 
25
 
26
  def localize_anomalies(input_img, preset="edm2-img64-s-fid", load_from_hub=False):
27
- print("NEW LOCALIZE")
28
- device = "cuda" if torch.cuda.is_available() else "cpu"
29
- # img = center_crop_imagenet(64, img)
30
  input_img = input_img.resize(size=(64, 64), resample=Image.Resampling.LANCZOS)
31
-
32
- with torch.inference_mode():
33
- img = np.array(input_img)
34
- img = torch.from_numpy(img).permute(2, 0, 1).unsqueeze(0)
35
- img = img.float().to(device)
36
- model = load_model_from_hub(preset=preset, device=device)
37
- img_likelihood, score_norms = run_inference(model, img)
38
- nll, pct, ref_nll = compute_gmm_likelihood(
39
- score_norms, model_dir=f"models/{preset}"
40
- )
41
 
42
  outstr = f"Anomaly score: {nll:.3f} / {pct:.2f} percentile"
43
  histplot = plot_against_reference(nll, ref_nll)
 
14
 
15
  @spaces.GPU
16
  def run_inference(model, img):
17
+ model = model.to('cuda')
18
+ img = img.to('cuda')
19
  print("model on cuda:", next(model.scorenet.net.parameters()).is_cuda)
20
+ print("img on cuda:", img.is_cuda)
21
  img = torch.nn.functional.interpolate(img, size=64, mode="bilinear")
22
  score_norms = model.scorenet(img)
23
  score_norms = score_norms.square().sum(dim=(2, 3, 4)) ** 0.5
 
27
 
28
 
29
  def localize_anomalies(input_img, preset="edm2-img64-s-fid", load_from_hub=False):
30
+ device = "cuda"
 
 
31
  input_img = input_img.resize(size=(64, 64), resample=Image.Resampling.LANCZOS)
32
+ img = np.array(input_img)
33
+ img = torch.from_numpy(img).permute(2, 0, 1).unsqueeze(0)
34
+ img = img.float().to(device)
35
+ model = load_model_from_hub(preset=preset, device=device)
36
+ img_likelihood, score_norms = run_inference(model, img)
37
+ nll, pct, ref_nll = compute_gmm_likelihood(
38
+ score_norms, model_dir=f"models/{preset}"
39
+ )
 
 
40
 
41
  outstr = f"Anomaly score: {nll:.3f} / {pct:.2f} percentile"
42
  histplot = plot_against_reference(nll, ref_nll)