-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
60 lines (48 loc) · 1.74 KB
/
serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
import torch.nn as nn
import torchvision.models as models
from torchvision import transforms
from VisualizeCNN.gradCam import GradCam
# ===== MODEL ==================================================================
def get_model():
return models.vgg16(pretrained=True)
def get_conv_layers(model):
all_modules = dict(model.named_modules())
conv_layers = []
for name, module in all_modules.items():
if isinstance(module, nn.Conv2d):
conv_layers.append(name)
return conv_layers
def get_model_classes():
return [("0", "0"), ("1", "1")]
def inv_model_classes(model_classes):
inv_classes = {}
for name, val in model_classes:
inv_classes[val] = name
return inv_classes
def get_resize_transform():
return transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224))
])
def get_transforms():
transform = transforms.Compose([
get_resize_transform(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
return transform
# ===== TOOLS ==================================================================
def serve_gradcam(model, transform, chosen_layers):
def run_gradcam(image, idx, device):
grad_cam = GradCam(model)
grad_cam.set_target_layers(chosen_layers)
grad_cam.run_model(transform(image).to(device).unsqueeze(0))
cam = grad_cam(idx, device)
grad_cam.reset_gradcam()
return cam
return run_gradcam
# ===== MISC ==================================================================
def get_device():
return torch.device("cuda:0" if torch.cuda.is_available() else "cpu")