Spaces:
Running
Running
testing cuda usage
Browse files
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 |
-
|
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 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
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)
|