File size: 7,169 Bytes
019483f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0630af
4da0e3e
019483f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0630af
019483f
 
 
 
 
 
 
a0630af
019483f
 
 
 
 
 
 
 
 
 
 
a0630af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
019483f
a0630af
 
019483f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4da0e3e
 
 
 
 
 
 
 
019483f
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import random
import time
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import matplotlib.pyplot as plt

from glob import glob
from PIL import Image
from model.load_model import get_model
from torchvision import transforms

from pytorch_grad_cam import GradCAM, GuidedBackpropReLUModel
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image, deprocess_image

from ultralytics import YOLO

# from rembg import remove
import uuid


# Static variables
model_path = "efficientnet-b0-best.pth"
model_name = "efficientnet_b0"
YOLO_MODEL_WEIGHTS = "yolo-v11-best.pt"
classes = ["Healthy", "Resistant", "Susceptible"]
resizing_transforms = transforms.Compose([transforms.CenterCrop(224)])


# Function definitions
def reproduce(seed=42):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    cudnn.deterministic = True
    cudnn.benchmark = False


def get_grad_cam_results(image, transformed_image, class_index=0):
    with GradCAM(model=model, target_layers=target_layers) as cam:
        targets = [ClassifierOutputTarget(class_index)]
        grayscale_cam = cam(
            input_tensor=transformed_image.unsqueeze(0), targets=targets
        )
        grayscale_cam = grayscale_cam[0, :]

        visualization = show_cam_on_image(
            np.array(image) / 255.0, grayscale_cam, use_rgb=True
        )
    return visualization, grayscale_cam


def get_backpropagation_results(transformed_image, class_index=0):
    transformed_image = transformed_image.unsqueeze(0)
    backpropagation = gbp_model(transformed_image, target_category=class_index)
    bp_deprocessed = deprocess_image(backpropagation)
    return backpropagation, bp_deprocessed


def get_guided_gradcam(image, cam_grayscale, bp):
    cam_mask = np.expand_dims(cam_grayscale, axis=-1)
    cam_mask = np.repeat(cam_mask, 3, axis=-1)
    img = show_cam_on_image(
        np.array(image) / 255.0, deprocess_image(cam_mask * bp), use_rgb=False
    )
    return img


def explain_results(image, class_index=0):
    transformed_image = image_transform(image)
    image = resizing_transforms(image)

    visualization, cam_mask = get_grad_cam_results(
        image, transformed_image, class_index
    )
    backpropagation, bp_deprocessed = get_backpropagation_results(
        transformed_image, class_index
    )
    guided_gradcam = get_guided_gradcam(image, cam_mask, backpropagation)

    return visualization, bp_deprocessed, guided_gradcam


def make_prediction_and_explain(image):
    transformed_image = image_transform(image)
    transformed_image = transformed_image.unsqueeze(0)
    model.eval()
    with torch.no_grad():
        output = model(transformed_image)
        output = torch.nn.functional.softmax(output, dim=1)

    predictions = [round(x, 4) * 100 for x in output[0].tolist()]
    results = {}

    for i, k in enumerate(classes):
        gradcam, bp_deprocessed, guided_gradcam = explain_results(image, class_index=i)

        results[k] = {
            "original_image": image,
            "prediction": f"{k} ({predictions[i]}%)",
            "gradcam": gradcam,
            "backpropagation": bp_deprocessed,
            "guided_gradcam": guided_gradcam,
        }

    return results


def save_explanation_results(res, path):
    fig, ax = plt.subplots(3, 4, figsize=(15, 15))
    for i, (k, v) in enumerate(res.items()):
        ax[i, 0].imshow(v["original_image"])
        ax[i, 0].set_title(f"Original Image (class: {v['prediction']}")
        ax[i, 0].axis("off")

        ax[i, 1].imshow(v["gradcam"])
        ax[i, 1].set_title("GradCAM")
        ax[i, 1].axis("off")

        ax[i, 2].imshow(v["backpropagation"])
        ax[i, 2].set_title("Backpropagation")
        ax[i, 2].axis("off")

        ax[i, 3].imshow(v["guided_gradcam"])
        ax[i, 3].set_title("Guided GradCAM")
        ax[i, 3].axis("off")

    plt.tight_layout()
    plt.savefig(path, bbox_inches="tight")
    plt.close(fig)


model, image_transform = get_model(model_name)
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
model.train()
target_layers = [model.conv_head]
gbp_model = GuidedBackpropReLUModel(model=model, device="cpu")

yolo_model = YOLO(YOLO_MODEL_WEIGHTS)


def get_results(img_path=None, img_for_testing=None, od=False):
    if img_path is None and img_for_testing is None:
        raise ValueError("Either img_path or img_for_testing should be provided.")

    if img_path is not None:
        image = Image.open(img_path)

    if img_for_testing is not None:
        image = Image.fromarray(img_for_testing)

    result_paths = []

    if od:
        results = yolo_model(img_path if img_path else img_for_testing)
        for i, result in enumerate(results):
            unique_id = uuid.uuid4().hex
            save_path = f"/tmp/with-bg-result-{unique_id}.png"
            bbox = result.boxes.xyxy[0].cpu().numpy().astype(int)
            bbox_image = image.crop((bbox[0], bbox[1], bbox[2], bbox[3]))

            # bbox_image = remove(bbox_image).convert("RGB")
            # bbox_image = Image.fromarray(
            #     np.where(
            #         np.array(bbox_image) == [0, 0, 0],
            #         [255, 255, 255],
            #         np.array(bbox_image),
            #     ).astype(np.uint8)
            # )

            res = make_prediction_and_explain(bbox_image)
            save_explanation_results(res, save_path)

            result_paths.append(save_path)
    else:
        unique_id = uuid.uuid4().hex
        save_path = f"/tmp/with-bg-result-{unique_id}.png"
        res = make_prediction_and_explain(image)
        save_explanation_results(res, save_path)
        result_paths.append(save_path)

    return result_paths


if __name__ == "__main__":
    # Actual logic
    reproduce()

    model, image_transform = get_model(model_name)
    model.load_state_dict(torch.load(model_path))
    model.train()
    target_layers = [model.conv_head]
    gbp_model = GuidedBackpropReLUModel(model=model, device="cpu")

    yolo_model = YOLO(YOLO_MODEL_WEIGHTS)

    for IMAGE_PATH in glob("samples/*"):
        start = time.perf_counter()

        results = yolo_model(IMAGE_PATH)
        image = Image.open(IMAGE_PATH)

        for i, result in enumerate(results):
            save_path = IMAGE_PATH.replace(
                "samples/", f"sample-results/with-white-bg-result-{i:02d}-"
            )
            bbox = result.boxes.xyxy[0].cpu().numpy().astype(int)
            bbox_image = image.crop((bbox[0], bbox[1], bbox[2], bbox[3]))

            # bbox_image = remove(bbox_image).convert("RGB")
            # bbox_image = Image.fromarray(
            #     np.where(
            #         np.array(bbox_image) == [0, 0, 0],
            #         [255, 255, 255],
            #         np.array(bbox_image),
            #     ).astype(np.uint8)
            # )

            res = make_prediction_and_explain(bbox_image)
            save_explanation_results(res, save_path)

        end = time.perf_counter() - start
        print(f"Completed in {end}s")