i am trying to generate heatmap but always have corrupted images…i am not sure about the logic of generating CAM but i will explain the steps i do so that if you can please help me…
1-) the first thing i load my trained model then remove the last classification layer from it to get the last conv layer.
2-) then i convert the image to tensor so i can feed it to the last convolutional layer and get the output feature map.
3-) i loop through the weights of classification layer of my trained model and then multiply the classification weights by output feature map.
that was the logic i do…i am not pretty sure it is right because i am new to deep learning and don’t have a lot of experience with it.
i would be very thankful for you help.
here is the code :
import os
import numpy as np
import time
import sys
from PIL import Image

import heatmap_test as ht

import cv2

import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms

from densenet import densenet121
from densenet import densenet169
from densenet import densenet201
from densenet import DenseNet

from torchvision.transforms import ToTensor



class densenet_last_layer(torch.nn.Module):
    def __init__(self, model):
        super(densenet_last_layer, self).__init__()
        # remove last classification layer
        self.features = torch.nn.Sequential(
            *list(model.children())[:-1]
        )

    def forward(self, x):
        x = self.features(x)
        x = torch.nn.functional.relu(x, inplace=True)
        return x


class DrawHeatmap():
    def __init__(self, pathModel, img, pathOutputFile):
        model = densenet121(False)

        modelCheckpoint = torch.load(pathModel, map_location=torch.device('cpu'))
        model.load_state_dict(modelCheckpoint['state_dict'], strict=False)

        named_layers = dict(model.named_modules())

        model.eval()
        weights = None
        bias = None
        for name, params in model.named_parameters():
            print(name)
            if name == 'classifier.weight':
                weights = params
            if name == 'classifier.bias':
                bias = params

        conv_model = densenet_last_layer(model)

        # ---- Initialize the image transform - resize + normalize
        normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        transformList = []
        # transformList.append(transforms.Resize(transCrop))
        transformList.append(transforms.ToTensor())
        # transformList.append(normalize)

        transformSequence = transforms.Compose(transformList)

        imageData = Image.open(img).convert('RGB')
        imageData = transformSequence(imageData)
        imageData = imageData.unsqueeze(0)

        input_img = torch.autograd.Variable(imageData)
        output = conv_model(input_img)
        # print(output.shape)
        #
        # output = output.cpu().data.numpy()
        # output = np.squeeze(output)
        print(output.shape)

        heatmap = None
        for i in range(0, len(weights)):
            map = output[0, i, :, :]
            if i == 0:
                heatmap = weights[i] * map
            else:
                heatmap += weights[i] * map


        npHeatmap = heatmap.cpu().data.numpy()

        imgOriginal = cv2.imread(img, 1)
        imgOriginal = cv2.resize(imgOriginal, (224, 224))

        cam = npHeatmap / np.max(npHeatmap)
        cam = cv2.resize(cam, (224, 224))
        heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)

        img = heatmap * 0.5 + imgOriginal

        cv2.imwrite(pathOutputFile, img)




pathInputImage = 'C:/Users/mahmo/Desktop/best projects/Stanford Code/cxr-code/00000001_000.png'
pathModel = 'C:/Users/mahmo/Desktop/best projects/Stanford Code/cxr-code/m-25012018-123527.pth.tar'
pathOutputImage = 'C:/Users/mahmo/Desktop/best projects/Stanford Code/cxr-code/heatmap_test.png'
h = DrawHeatmap(pathModel, pathInputImage, pathOutputImage)



# ht.draw_CAM(pathModel, pathInputImage, True)