Upload folder using huggingface_hub
Browse files
demo.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
os.system("pip install datasets einops
|
| 3 |
|
| 4 |
import os, cv2, time, math
|
| 5 |
print("=> Loading libraries...")
|
|
@@ -15,6 +15,8 @@ from pytorch_grad_cam import GradCAM
|
|
| 15 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
| 18 |
print(f"=> Libraries loaded in {time.time()- start:.2f} sec(s).")
|
| 19 |
print("=> Loading model...")
|
| 20 |
start = time.time()
|
|
@@ -25,7 +27,7 @@ crop_pct = 0.9
|
|
| 25 |
IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
|
| 26 |
IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
|
| 27 |
|
| 28 |
-
model = create_model(f"tpmlp_{size}").
|
| 29 |
load_checkpoint(model, f"../tpmlp_{size}.pth.tar", True)
|
| 30 |
model.eval()
|
| 31 |
|
|
@@ -56,7 +58,7 @@ def transform(img):
|
|
| 56 |
def predict(inp):
|
| 57 |
img, inp = transform(inp)
|
| 58 |
inp = inp.unsqueeze(0)
|
| 59 |
-
with GradCAM(model=model, target_layers=[model.layers[3]], use_cuda=
|
| 60 |
grayscale_cam, probs = cam(input_tensor=inp, aug_smooth=False, eigen_smooth=False, return_probs=True)
|
| 61 |
|
| 62 |
# Here grayscale_cam has only one image in the batch
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.system("pip install datasets einops tabulate opencv-python ttach -U")
|
| 3 |
|
| 4 |
import os, cv2, time, math
|
| 5 |
print("=> Loading libraries...")
|
|
|
|
| 15 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 16 |
|
| 17 |
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
|
| 20 |
print(f"=> Libraries loaded in {time.time()- start:.2f} sec(s).")
|
| 21 |
print("=> Loading model...")
|
| 22 |
start = time.time()
|
|
|
|
| 27 |
IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
|
| 28 |
IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
|
| 29 |
|
| 30 |
+
model = create_model(f"tpmlp_{size}").to(device)
|
| 31 |
load_checkpoint(model, f"../tpmlp_{size}.pth.tar", True)
|
| 32 |
model.eval()
|
| 33 |
|
|
|
|
| 58 |
def predict(inp):
|
| 59 |
img, inp = transform(inp)
|
| 60 |
inp = inp.unsqueeze(0)
|
| 61 |
+
with GradCAM(model=model, target_layers=[model.layers[3]], use_cuda=device=="cuda") as cam:
|
| 62 |
grayscale_cam, probs = cam(input_tensor=inp, aug_smooth=False, eigen_smooth=False, return_probs=True)
|
| 63 |
|
| 64 |
# Here grayscale_cam has only one image in the batch
|