-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_lime.py
160 lines (134 loc) · 5.87 KB
/
plot_lime.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import matplotlib.pyplot as plt
from PIL import Image
import torch.nn as nn
import numpy as np
import os, json
import torch
from lime import lime_image
from skimage.segmentation import mark_boundaries
from torchvision import models, transforms
from torch.autograd import Variable
import torch.nn.functional as F
from torchvision.models import resnet34, googlenet, mobilenet_v3_small
def get_image(path):
with open(os.path.abspath(path), 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
def batch_predict(images):
model.eval()
batch = torch.stack(tuple(preprocess_transform(i) for i in images), dim=0)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
batch = batch.to(device)
logits = model(batch)
probs = F.softmax(logits, dim=1)
return probs.detach().cpu().numpy()
def get_input_transform():
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transf = transforms.Compose([
transforms.Resize((256, 256)),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize
])
return transf
def get_input_tensors(img):
transf = get_input_transform()
# unsqeeze converts single image to batch of 1
return transf(img).unsqueeze(0)
def get_pil_transform():
transf = transforms.Compose([
transforms.Resize((256, 256)),
transforms.CenterCrop(224)
])
return transf
#
def get_preprocess_transform():
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transf = transforms.Compose([
# transforms.Resize((256, 256)),
# transforms.CenterCrop(224),
transforms.ToTensor(),
# normalize
])
return transf
# resize and take the center part of image to what our model expects
if __name__ == '__main__':
import argparse
from utils import load_state_dict
parser = argparse.ArgumentParser()
parser.add_argument('--dataset',default='imagenet')
parser.add_argument('--model',default='resnet34')
args = parser.parse_args()
if args.dataset == 'imagenet':
output_feature = 100
# logging.info('dataset is imagenet.')
# train_load, val_load = loadData('dataset/', batch_size=32)
save_path = 'models/imagenet/normal' + args.model + '_best.pth'
save_path_adamin = 'models/imagenet/' + args.model + '_best.pth'
if args.dataset == 'cifar100':
# logging.info('dataset is cifar100!')
output_feature = 100
# train_load, val_load = load_cifar100('dataset/',batch_size=64)
save_path = 'models/cifar100/normal' + args.model + '_best.pth'
save_path_adamin = 'models/cifar100/' + args.model + '_best.pth'
if args.dataset == 'cifar':
# logging.info('dataset is cifar10!')
output_feature = 10
# train_load, val_load = load_cifar('dataset/', batch_size=128)
# model = resnet34(pretrained=True)
save_path = 'models/cifar/normal' + args.model + '_best.pth'
save_path_adamin = 'models/cifar/' + args.model + '_best.pth'
if args.model == 'resnet34':
model = resnet34(pretrained=True)
in_feature = model.fc.in_features
model.fc = nn.Linear(in_feature, output_feature)
# logging.info('model is resnet34!')
if args.model == 'googlenet':
model = googlenet(pretrained=True)
in_feature = model.fc.in_features
model.fc = nn.Linear(in_feature, output_feature)
# logging.info('model is googlenet!')
if args.model == 'mobilenet':
model = mobilenet_v3_small(pretrained=True)
in_feature = 1024
model.classifier.__dict__['_modules']['3'] = nn.Linear(in_feature, out_features=output_feature)
# logging.info('model is mobilenet!')
if os.path.exists(save_path):
load_state_dict(model, torch.load(save_path_adamin))
print('load weight success!')
img = get_image('./dataset/val/n01729322/ILSVRC2012_val_00002568.JPEG')
plt.imshow(img)
print(img.size)
# model = models.resnet34(pretrained=True)
# idx2label, cls2label, cls2idx = [], {}, {}
# with open(os.path.abspath('./data/imagenet_class_index.json'), 'r') as read_file:
# class_idx = json.load(read_file)
# idx2label = [class_idx[str(k)][1] for k in range(len(class_idx))]
# cls2label = {class_idx[str(k)][0]: class_idx[str(k)][1] for k in range(len(class_idx))}
# cls2idx = {class_idx[str(k)][0]: k for k in range(len(class_idx))}
# img_t = get_input_tensors(img)
# model.eval()
# logits = model(img_t)
# probs = F.softmax(logits, dim=1)
# probs5 = probs.topk(5)
# tuple((p,c, idx2label[c]) for p, c in zip(probs5[0][0].detach().numpy(), probs5[1][0].detach().numpy()))
pill_transf = get_pil_transform()
preprocess_transform = get_preprocess_transform()
# test_pred = batch_predict([pill_transf(img)])
# test_pred.squeeze().argmax()
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(np.array(pill_transf(img)),
batch_predict, # classification function
top_labels=5,
hide_color=0,
num_samples=5000) # number of images that will be sent to classification function
# temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=False)
# img_boundry1 = mark_boundaries(temp/255.0, mask)
# plt.imshow(img_boundry1)
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=20, hide_rest=True)
img_boundry2 = mark_boundaries(temp/255.0, mask)
plt.imshow(img_boundry2)
plt.show()