def main(args):
    # 输入
    img = io.imread(args.image_path)
    img = np.float32(cv2.resize(img, (224, 224))) / 255
    inputs = prepare_input(img)
    # 输出图像
    image_dict = {}
    # 网络
    net = get_net(args.network, args.weight_path)
    # Grad-CAM
    layer_name = get_last_conv_name(
        net) if args.layer_name is None else args.layer_name
    grad_cam = GradCAM(net, layer_name)
    mask = grad_cam(inputs, args.class_id)  # cam mask
    image_dict['cam'], image_dict['heatmap'] = gen_cam(img, mask)
    grad_cam.remove_handlers()
    # Grad-CAM++
    grad_cam_plus_plus = GradCamPlusPlus(net, layer_name)
    mask_plus_plus = grad_cam_plus_plus(inputs, args.class_id)  # cam mask
    image_dict['cam++'], image_dict['heatmap++'] = gen_cam(img, mask_plus_plus)
    grad_cam_plus_plus.remove_handlers()

    # GuidedBackPropagation
    gbp = GuidedBackPropagation(net)
    inputs.grad.zero_()  # 梯度置零
    grad = gbp(inputs)

    gb = gen_gb(grad)
    image_dict['gb'] = gb
    # 生成Guided Grad-CAM
    cam_gb = gb * mask[..., np.newaxis]
    image_dict['cam_gb'] = norm_image(cam_gb)

    save_image(image_dict, os.path.basename(args.image_path), args.network,
               args.output_dir)
Esempio n. 2
0
def heat_map(image,
             tensors,
             model,
             class_id=None,
             layer_name=None,
             save_path='./heatmap/'):
    """
    生成热值图
    :param image: 图片原始numpy对象
    :param tensors: 图像tensor对象(之所以单独读取,是因为数据增强方式会变化)
    :param model: 模型对象
    :param class_id: 识别类别索引,为None时去概率最大的类别
    :param layer_name: 可视化的卷积层名称,请参考get_conv_name读取,默认最后一层
    :param save_path: 结果保存路径,默认'./heatmap/'
    :return:
    """
    image_dict = {}
    if layer_name is None:
        layer_name = get_last_conv_name(model)
    grad_cam = GradCAM(model, layer_name)
    mask = grad_cam(tensors, class_id)  # cam mask
    image_dict['cam'], image_dict['heatmap'] = gen_cam(image, mask)
    grad_cam.remove_handlers()
    # Grad-CAM++
    grad_cam_plus_plus = GradCamPlusPlus(model, layer_name)
    mask_plus_plus = grad_cam_plus_plus(tensors, class_id)  # cam mask
    image_dict['cam++'], image_dict['heatmap++'] = gen_cam(
        image, mask_plus_plus)
    grad_cam_plus_plus.remove_handlers()

    # GuidedBackPropagation
    gbp = GuidedBackPropagation(model)
    # image_wakan.zero_grad()  # 梯度置零
    grad = gbp(tensors)

    gb = gen_gb(grad)
    image_dict['gb'] = norm_image(gb)
    # 生成Guided Grad-CAM
    cam_gb = gb * mask[..., np.newaxis]
    image_dict['cam_gb'] = norm_image(cam_gb)

    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_image(image_dict, 'heatmap', save_path)
Esempio n. 3
0
def main(args):

    # 输入图像预处理
    img = io.imread(args.image_path)
    img = np.float32(cv2.resize(img, (224, 224))) / 255
    inputs = preprocess_image(img)

    image_dict = {}  # 将输出CAM图像存为字典
    net = get_net(args.network, args.weight_path)  # 获取网络
    layer_name = get_last_conv_name(net) if args.layer_name is None else args.layer_name

    # Grad-CAM
    grad_cam = GradCAM(net, layer_name)  # 类实例化
    mask = grad_cam(inputs, args.class_id)  # 调用该对象得到 mask
    image_dict['cam'], image_dict['heatmap'] = show_cam_on_image(img, mask)
    # cam, heatmap = show_cam_on_image(img, mask)
    grad_cam.remove_handlers()

    # Grad-CAM++
    # grad_cam_plus_plus = GradCamPlusPlus(net, layer_name)
    # mask_plus_plus = grad_cam_plus_plus(inputs, args.class_id)  # cam mask
    # image_dict['cam++'], image_dict['heatmap++'] = show_cam_on_image(img, mask_plus_plus)
    # grad_cam_plus_plus.remove_handlers()

    # Guided BP
    gbp = GuidedBackPropagation(net)
    inputs.grad.zero_()  # 梯度置零
    grad = gbp(inputs)
    gb = get_gb(grad)
    image_dict['gb'] = gb

    # 生成Guided Grad-CAM
    cam_gb = gb * mask[..., np.newaxis]  # CAM
    # cam_gb = gb * mask_plus_plus[..., np.newaxis]  # CAM

    cam_gb = cam_gb.copy()
    cam_gb = cam_gb - np.max(np.min(cam_gb), 0)
    cam_gb = cam_gb / np.max(cam_gb)
    cam_gb = np.uint8(255 * cam_gb)
    image_dict['cam_gb'] = cam_gb
    # cv2.imwrite("./results/GBP_beat.jpg", cam_gb)

    save_image(image_dict, os.path.basename(args.image_path), args.network, args.output_dir)
def main(args):
    # mkdir the output dir
    mkdir(args.output_dir)
    mkdir(os.path.join(args.output_dir, "class1"))
    mkdir(os.path.join(args.output_dir, "class2"))

    # MTCNN Detector
    with tf.device('gpu:0'):
        detector = MTCNN()
        # Input
        img1, box1 = load_img_and_box(args.image_path_1, detector)
        img2, box2 = load_img_and_box(args.image_path_2, detector)
        img1_crop = box_crop(img1, box1)
        img2_crop = box_crop(img2, box2)

    # Clean the GPU memory of tensorflow
    cuda_.select_device(0)
    cuda_.close()

    # Network
    net = get_net(args.network, args.weight_path)

    # Grad-CAM
    layer_name = get_last_conv_name(
        net) if args.layer_name is None else args.layer_name
    attr_maps = get_attribution_maps_methods(net, layer_name,
                                             args.attribution_maps)

    # GuidedBackPropagation
    gbp = GuidedBackPropagation(net)

    # Prepare image
    inputs1, h_start1, w_start1 = prepare_input(img1_crop, args.network)
    inputs2, h_start2, w_start2 = prepare_input(img2_crop, args.network)

    # Output image
    image1_dict = {}
    image2_dict = {}

    # CAM
    mask1 = attr_maps(inputs1.cuda(), args.class_id_1)  # cam mask
    attr_maps.remove_handlers()
    mask1_c = attr_maps(inputs1.cuda(), args.class_id_2)  # cam mask
    attr_maps.remove_handlers()

    mask2 = attr_maps(inputs2.cuda(), args.class_id_2)  # cam mask
    attr_maps.remove_handlers()
    mask2_c = attr_maps(inputs2.cuda(), args.class_id_1)  # cam mask
    attr_maps.remove_handlers()

    # Descriminant explanations
    discriminant1 = Descriminant_explanations(mask1, mask1_c, mask1)
    discriminant2 = Descriminant_explanations(mask2, mask1_c, mask2)

    # Generate Guided Grad-CAM
    inputs1.grad.zero_()  # Zero gradient
    inputs2.grad.zero_()  # Zero gradient

    grad1 = gbp(inputs1.cpu())
    grad2 = gbp(inputs2.cpu())

    gb1 = gen_gb(grad1)
    gb2 = gen_gb(grad2)

    cam_gb1 = gb1 * mask1[..., np.newaxis]
    cam_gb2 = gb2 * mask1[..., np.newaxis]

    # Save image
    image1_dict[args.attribution_maps], image1_dict['heatmap'] = gen_cam(
        img1, img1_crop, mask1, box1, h_start1, w_start1)
    image1_dict[args.attribution_maps +
                '-id2'], image1_dict['heatmap-id2'] = gen_cam(
                    img1, img1_crop, mask1_c, box1, h_start1, w_start1)
    image1_dict[
        args.attribution_maps +
        '-discriminant'], image1_dict['heatmap-discriminant'] = gen_cam(
            img1, img1_crop, discriminant1, box1, h_start1, w_start1)
    image1_dict['gb'] = norm_image(gb1)
    image1_dict['cam_gb'] = norm_image(cam_gb1)

    save_image(image1_dict[args.attribution_maps],
               os.path.basename(args.image_path_1), args.network,
               args.attribution_maps, os.path.join(args.output_dir, "class1"))
    save_image(image1_dict[args.attribution_maps + '-id2'],
               os.path.basename(args.image_path_1), args.network,
               args.attribution_maps + '-id2',
               os.path.join(args.output_dir, "class1"))
    save_image(image1_dict['heatmap'],
               os.path.basename(args.image_path_1), args.network, 'heatmap',
               os.path.join(args.output_dir, "class1"))
    save_image(image1_dict['heatmap-id2'], os.path.basename(args.image_path_1),
               args.network, 'heatmap-id2',
               os.path.join(args.output_dir, "class1"))
    save_image(image1_dict[args.attribution_maps + '-discriminant'],
               os.path.basename(args.image_path_1), args.network,
               args.attribution_maps + '-discriminant',
               os.path.join(args.output_dir, "class1"))
    save_image(image1_dict['heatmap-discriminant'],
               os.path.basename(args.image_path_1), args.network,
               'heatmap-discriminant', os.path.join(args.output_dir, "class1"))
    save_image(image1_dict['gb'], os.path.basename(args.image_path_1),
               args.network, 'gb', os.path.join(args.output_dir, "class1"))
    save_image(image1_dict['cam_gb'], os.path.basename(args.image_path_1),
               args.network, 'cam_gb', os.path.join(args.output_dir, "class1"))

    image2_dict[args.attribution_maps], image2_dict['heatmap'] = gen_cam(
        img2, img2_crop, mask2, box2, h_start2, w_start2)
    image2_dict[args.attribution_maps +
                '-id2'], image2_dict['heatmap-id2'] = gen_cam(
                    img2, img2_crop, mask2_c, box2, h_start2, w_start2)
    image2_dict[
        args.attribution_maps +
        '-discriminant'], image2_dict['heatmap-discriminant'] = gen_cam(
            img2, img2_crop, discriminant2, box2, h_start2, w_start2)
    image2_dict['gb'] = norm_image(gb2)
    image2_dict['cam_gb'] = norm_image(cam_gb2)

    save_image(image2_dict[args.attribution_maps],
               os.path.basename(args.image_path_2), args.network,
               args.attribution_maps, os.path.join(args.output_dir, "class2"))
    save_image(image2_dict[args.attribution_maps + '-id2'],
               os.path.basename(args.image_path_2), args.network,
               args.attribution_maps + '-id2',
               os.path.join(args.output_dir, "class2"))
    save_image(image2_dict['heatmap'],
               os.path.basename(args.image_path_2), args.network, 'heatmap',
               os.path.join(args.output_dir, "class2"))
    save_image(image2_dict['heatmap-id2'], os.path.basename(args.image_path_2),
               args.network, 'heatmap-id2',
               os.path.join(args.output_dir, "class2"))
    save_image(image2_dict[args.attribution_maps + '-discriminant'],
               os.path.basename(args.image_path_2), args.network,
               args.attribution_maps + '-discriminant',
               os.path.join(args.output_dir, "class2"))
    save_image(image2_dict['heatmap-discriminant'],
               os.path.basename(args.image_path_2), args.network,
               'heatmap-discriminant', os.path.join(args.output_dir, "class2"))
    save_image(image2_dict['gb'], os.path.basename(args.image_path_2),
               args.network, 'gb', os.path.join(args.output_dir, "class2"))
    save_image(image2_dict['cam_gb'], os.path.basename(args.image_path_2),
               args.network, 'cam_gb', os.path.join(args.output_dir, "class2"))
Esempio n. 5
0
def CAM(net, img_list, date, input_type):
    # img_list = glob.glob("./../eye_data/dataset/test/*")
    correct = 0
    for img_path in img_list:
        file_name = img_path.split("/")[-1]
        class_id = 1 if file_name[0] == 'n' else 2
        class_id -= 1
        img = Image.open(img_path)
        transform_test = transforms.Compose([
            transforms.Resize(size=(224, 224), interpolation=2),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])
        # img = transform_test(img)
        # img = img[np.newaxis, ...]  # 增加batch维
        # img = Variable(img).cuda()
        # output = net(img)  # size(1, 2)

        inputs = transform_test(img)
        inputs = inputs[np.newaxis, ...]  # 增加batch维
        inputs = torch.tensor(inputs, requires_grad=True)
        inputs = Variable(inputs, requires_grad=True).cuda()
        inputs.retain_grad()
        output = net(inputs)

        img = io.imread(img_path)
        img = np.float32(cv2.resize(img, (224, 224))) / 255

        # print(output.data, class_id-1)

        _, predicted = output.max(1)
        # print(predicted[0].item())
        if (predicted[0].item() == class_id - 1):
            correct += 1

        image_dict = {}

        # Grad-CAM
        layer_name = get_last_conv_name(net)
        grad_cam = GradCAM(net, layer_name)
        mask = grad_cam(inputs, class_id)  # cam mask
        image_dict['cam'], image_dict['heatmap'] = gen_cam(img, mask)
        grad_cam.remove_handlers()

        # Grad-CAM++
        grad_cam_plus_plus = GradCamPlusPlus(net, layer_name)
        mask_plus_plus = grad_cam_plus_plus(inputs, class_id)  # cam mask
        image_dict['cam++'], image_dict['heatmap++'] = gen_cam(
            img, mask_plus_plus)
        grad_cam_plus_plus.remove_handlers()

        # GuidedBackPropagation
        gbp = GuidedBackPropagation(net)
        inputs.grad.zero_()  # 梯度置零
        grad = gbp(inputs)

        gb = gen_gb(grad)
        image_dict['gb'] = norm_image(gb)
        # 生成Guided Grad-CAM
        cam_gb = gb * mask[..., np.newaxis]
        image_dict['cam_gb'] = norm_image(cam_gb)

        save_image(image_dict, file_name, "EfficientNet-B0",
                   f"./grad_cam_result/{input_type}_{date}")

    print("Test the accuracy on Grad-CAM:", correct / len(img_list))