Beispiel #1
0
def vis_grad(model, class_index, layer, image_path, size=[224, 224]):
    original_image = cv2.imread(image_path, 1)
    #plt.imshow(original_image)
    #plt.show()
    prep_img = preprocess_image(original_image, size)
    file_name_to_export = 'model' + '_classindex_' + str(
        class_index) + '-layer_' + str(layer)

    # Grad cam
    gcv2 = GradCam(model, target_layer=layer)
    # Generate cam mask
    cam = gcv2.generate_cam(prep_img, class_index, size)
    print('Grad cam completed')

    # Guided backprop
    GBP = GuidedBackprop(model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, class_index)
    print('Guided backpropagation completed')

    # Guided Grad cam
    cam_gb = guided_grad_cam(cam, guided_grads)
    #save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam')
    grayscale_cam_gb = convert_to_grayscale(cam_gb)
    #save_gradient_images(grayscale_cam_gb, file_name_to_export + '_GGrad_Cam_gray')
    print('Guided grad cam completed')
    cam_gb = trans(cam_gb)
    grayscale_cam_gb = trans(grayscale_cam_gb)

    return cam_gb, grayscale_cam_gb
    def show_misclassifications(self, k=25, gradcam=False):

        if gradcam:
            fig = plt.figure(figsize=(20, 6 * k))

            for idx in np.arange(k):
                img = self.incorrect_examples[idx][0]
                gm = GradCam(model=self.model,
                             img_tensor=img,
                             correct_class=self.correct_labels[idx][0],
                             classes=self.classes,
                             feature_module=self.model.layer4,
                             target_layer_names=['1'])
                input_img, cam_img = gm.get()
                ax = fig.add_subplot(3 * k, 5, idx + 1)
                ax.set_title(
                    f'pred: {self.classes[self.incorrect_labels[idx]]}'
                    f' / correct: {self.classes[self.correct_labels[idx]]}')
                plt.imshow(cam_img)

            fig.tight_layout()
        else:
            fig = plt.figure(figsize=(20, 6 * k))

            for idx in np.arange(k):
                ax = fig.add_subplot(3 * k, 5, idx + 1)
                ax.set_title(
                    f'pred: {self.classes[self.incorrect_labels[idx]]}'
                    f' / correct: {self.classes[self.correct_labels[idx]]}')
                img = self.incorrect_examples[idx][0]
                imshow(img)
            fig.tight_layout()
Beispiel #3
0
    def gradcam_data(self, test_loader, hmp_dims=(512,512), ens_flg=False, cams_ens=None, prob_ens=None):
        # threshold to draw a heatmap
        out_dim = self.env.out_dim

        CxrDataset.eval()
        self.env.model.eval()
        #with torch.no_grad():
        gradcam_res_list  = []
        gradcam_path_list = []

        cams = np.zeros((len(test_loader), len(self.cls_gradcam), 16, 16))

        for batch_idx, (data, target, info) in enumerate(test_loader):
            #data, target = data.to(self.env.device), target.to(self.env.device)
            data, target, info = data.to(self.env.device), target.to(self.env.device), info.to(self.env.device)
            # Grad CAM
            grad_cam = GradCam(self.env.model, self.env.type)
            if self.fl_ensemble:
                cam = self.gradcam_save_argcls_ens(grad_cam, data, test_loader, batch_idx, hmp_dims, info, ens_flg=ens_flg, cams_ens=cams_ens, prob_ens=prob_ens)
            else:
                gradcam_res, gradcam_path = self.gradcam_save_argcls(grad_cam, data, test_loader, batch_idx, hmp_dims, info)

            try:
                if self.fl_ensemble:
                    cams[batch_idx, :, :, :] = cam
                else:
                    gradcam_res_list.append(gradcam_res)
                    gradcam_path_list.append(gradcam_path)

            except AttributeError as e:
                print("No GradCam result?")

        return gradcam_res_list, gradcam_path_list, cams
Beispiel #4
0
    def __init__(self, model, classes, target_layers):
        super(VisualizeCam, self).__init__()
        self.model = model
        self.classes = classes
        self.target_layers = target_layers
        self.device = next(model.parameters()).device

        self.gcam = GradCam(model, target_layers, len(classes))
Beispiel #5
0
def test_everything(snapshot):
    filename = "examples/cat_dog.png"
    layer_name = "block5_conv3"
    model_filename = "model.h5"

    gc = GradCam.from_hdf(model_filename)
    cam, heatmap = gc.compute_cam(filename, layer_name)
    snapshot.assert_match(cam)
    snapshot.assert_match(heatmap)
Beispiel #6
0
def vis_gradcam(model, class_index, layer, image_path, size=[224, 224]):

    original_image = cv2.imread(image_path, 1)
    #plt.imshow(original_image)
    #plt.show()
    prep_img = preprocess_image(original_image, size)
    file_name_to_export = 'model' + '_classindex_' + str(
        class_index) + '-layer_' + str(layer)

    # Grad cam
    gcv2 = GradCam(model, target_layer=layer)
    # Generate cam mask
    cam = gcv2.generate_cam(prep_img, class_index, size)
    print('Grad cam completed')

    #save_class_activation_on_image(original_image, cam, file_name_to_export)
    img_with_heatmap, activation_heatmap = act_on_img(original_image, cam,
                                                      size)

    return cam, activation_heatmap, img_with_heatmap
if freeze:
    ct = 0
    for child in model.children():
        ct += 1
        if ct < 10:  # Freeze 1-9 layers
            for param in child.parameters():
                param.requires_grad = False
    print('Freezed layers!')

    for param in model.layer4[1].conv2.parameters():
        param.requires_grad = True
model.to(DEVICE)

optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)

grad_cam= GradCam(model=model, feature_module=model.layer4, \
                  target_layer_names=["1"], use_cuda=True)

sigmoid = nn.Sigmoid()
bce = nn.BCELoss()


def compute_accuracy(model, data_loader, device):
    correct_pred, num_examples = 0, 0
    for i, (features, targets) in enumerate(data_loader):

        features = features.to(device)
        targets = targets.to(device)

        logits, probas = model(features)
        _, predicted_labels = torch.max(probas, 1)
        num_examples += targets.size(0)
Beispiel #8
0
        grad_cam_mask (np_arr): Class activation map mask
        guided_backprop_mask (np_arr):Guided backprop mask
    """
    cam_gb = np.multiply(grad_cam_mask, guided_backprop_mask)
    return cam_gb


if __name__ == '__main__':
    # Get params
    target_example = 0  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_example_params(target_example)

    # Grad cam
    # gcv2 = GradCam(pretrained_model, target_layer=11)
    gcv2 = GradCam(pretrained_model, target_layer=7)
    # Generate cam mask
    cam = gcv2.generate_cam(prep_img, target_class)
    print('Grad cam completed')

    # Guided backprop
    GBP = GuidedBackprop(pretrained_model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, target_class)
    print('Guided backpropagation completed')

    # Guided Grad cam
    cam_gb = guided_grad_cam(cam, guided_grads)
    save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam')
    grayscale_cam_gb = convert_to_grayscale(cam_gb)
    save_gradient_images(grayscale_cam_gb,
Beispiel #9
0
        if NET_MODE == 'Capsule_ps':
            model = MixNet(data_type=DATA_TYPE,
                           capsule_type='ps',
                           num_iterations=NUM_ITERATIONS,
                           return_prob=True)
            AM_method = ProbAM(model)
        elif NET_MODE == 'Capsule_fc':
            model = MixNet(data_type=DATA_TYPE,
                           capsule_type='fc',
                           routing_type='dynamic',
                           num_iterations=NUM_ITERATIONS,
                           return_prob=True)
            AM_method = ProbAM(model)
        else:
            model = MixNet(data_type=DATA_TYPE, net_mode='CNN')
            AM_method = GradCam(model)
        if torch.cuda.is_available():
            model = model.to('cuda')
            model.load_state_dict(
                torch.load('epochs/' + DATA_TYPE + '_' + NET_MODE + '.pth'))
        else:
            model.load_state_dict(
                torch.load('epochs/' + DATA_TYPE + '_' + NET_MODE + '.pth',
                           map_location='cpu'))

        if torch.cuda.is_available():
            images = images.to('cuda')

        conv1_heat_maps, features_heat_maps = AM_method(images)

        save_image(conv1_heat_maps,
Beispiel #10
0
from torchvision import transforms
from torchvision.models import vgg19

from gradcam import GradCam

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Test Grad-CAM')
    parser.add_argument('--image_name',
                        default='both.png',
                        type=str,
                        help='the tested image name')
    parser.add_argument('--save_name',
                        default='grad_cam.png',
                        type=str,
                        help='saved image name')

    opt = parser.parse_args()

    IMAGE_NAME = opt.image_name
    SAVE_NAME = opt.save_name
    test_image = (transforms.ToTensor()(
        Image.open(IMAGE_NAME))).unsqueeze(dim=0)
    model = vgg19(pretrained=True)
    if torch.cuda.is_available():
        test_image = test_image.cuda()
        model.cuda()
    grad_cam = GradCam(model)
    feature_image = grad_cam(test_image).squeeze(dim=0)
    feature_image = transforms.ToPILImage()(feature_image)
    feature_image.save(SAVE_NAME)
Beispiel #11
0
import wget
from PIL import Image
import tensorflow.keras as keras
import io
import cv2
import numpy as np
from gradcam import GradCam
import io
st.markdown(
    "<h1 style='text-align: center; color: black;'>Phneumonia detection website</h1>",
    unsafe_allow_html=True)
st.text("Plase upload your lungs x-ray here")
uploaded_file = st.file_uploader("Upload Files", type=['png', 'jpeg', 'jpg'])
model = load_model('model.hdf5')

grad = GradCam(model)


def predict(matrix):

    overlay = grad.get_gradcam_overlay(matrix, 0)
    prediction = int(np.round(grad.score.numpy()))
    return (overlay, prediction)


def load_img(bytes):
    nparr = np.fromstring(bytes, np.uint8)
    img_np = cv2.imdecode(nparr,
                          cv2.IMREAD_COLOR)  # cv2.IMREAD_COLOR in OpenCV 3.1
    img_np = cv2.resize(img_np, (224, 224))
Beispiel #12
0
    # print(pretrained_model)

    probs, label = predict2(image_path, pretrained_model.to(device))
    print(os.path.basename(image_path))
    print(probs)
    # print(*label, sep=", ")
    print([idx_to_class[idx] for idx in label], sep=", ")
    print("-" * 60 + "\n")

    # gradcam
    original_image = PIL.Image.open(image_path).convert('RGB')
    prep_img = process_image(original_image).unsqueeze(0)
    crop_image = only_crop_image(original_image)

    target_class = label[0]
    file_name_to_export = "butterfly_gradcam"

    # Grad cam
    grad_cam = GradCam(pretrained_model, target_layer=target_layer)
    # Generate cam mask
    cam = grad_cam.generate_cam(prep_img, target_class)

    # Save mask
    save_class_activation_images(
        crop_image, cam,
        os.path.join('heatmaps',
                     os.path.splitext(os.path.basename(model_dir))[0]),
        "{}_{}_{:.2f}".format(
            os.path.splitext(os.path.basename(image_path))[0],
            idx_to_class[label[0]], probs[0]))
    Args:
        grad_cam_mask (np_arr): Class activation map mask
        guided_backprop_mask (np_arr):Guided backprop mask
    """
    cam_gb = np.multiply(grad_cam_mask, guided_backprop_mask)
    return cam_gb


if __name__ == '__main__':
    # Get params
    target_example = 0  # Snake
    (original_image, prep_img, target_class, file_name_to_export, pretrained_model) =\
        get_params(target_example)

    # Grad cam
    gcv2 = GradCam(pretrained_model, target_layer=11)
    # Generate cam mask
    cam = gcv2.generate_cam(prep_img, target_class)
    print('Grad cam completed')

    # Guided backprop
    GBP = GuidedBackprop(pretrained_model)
    # Get gradients
    guided_grads = GBP.generate_gradients(prep_img, target_class)
    print('Guided backpropagation completed')

    # Guided Grad cam
    cam_gb = guided_grad_cam(cam, guided_grads)
    save_gradient_images(cam_gb, file_name_to_export + '_GGrad_Cam')
    grayscale_cam_gb = convert_to_grayscale(cam_gb)
    save_gradient_images(grayscale_cam_gb, file_name_to_export + '_GGrad_Cam_gray')