Esempio n. 1
0
def test_memory_usage_in_loop(numpy_image, batch_size, width, height,
                              cnn_model, target_layer_names, cam_method,
                              target_category, aug_smooth, eigen_smooth):
    img = cv2.resize(numpy_image, (width, height))
    input_tensor = preprocess_image(img)
    input_tensor = input_tensor.repeat(batch_size, 1, 1, 1)
    model = cnn_model(pretrained=True)
    target_layers = []
    for layer in target_layer_names:
        target_layers.append(eval(f"model.{layer}"))
    targets = [
        ClassifierOutputTarget(target_category) for _ in range(batch_size)
    ]
    initial_memory = 0
    for i in range(100):
        with cam_method(model=model,
                        target_layers=target_layers,
                        use_cuda=False) as cam:
            grayscale_cam = cam(input_tensor=input_tensor,
                                targets=targets,
                                aug_smooth=aug_smooth,
                                eigen_smooth=eigen_smooth)

            if i == 0:
                initial_memory = psutil.virtual_memory()[2]
        assert (psutil.virtual_memory()[2] <= initial_memory * 1.05)
def test_all_cam_models_can_run(numpy_image, batch_size, width, height,
                                cnn_model, target_layer_names, cam_method,
                                target_category, aug_smooth, eigen_smooth):
    img = cv2.resize(numpy_image, (width, height))
    input_tensor = preprocess_image(img)
    input_tensor = input_tensor.repeat(batch_size, 1, 1, 1)

    model = cnn_model(pretrained=True)
    target_layers = []
    for layer in target_layer_names:
        target_layers.append(eval(f"model.{layer}"))

    cam = cam_method(model=model, target_layers=target_layers, use_cuda=False)
    cam.batch_size = 4
    if target_category is None:
        targets = None
    else:
        targets = [
            ClassifierOutputTarget(target_category) for _ in range(batch_size)
        ]

    grayscale_cam = cam(input_tensor=input_tensor,
                        targets=targets,
                        aug_smooth=aug_smooth,
                        eigen_smooth=eigen_smooth)
    assert (grayscale_cam.shape[0] == input_tensor.shape[0])
    assert (grayscale_cam.shape[1:] == input_tensor.shape[2:])
Esempio n. 3
0
def image2cam(image_path, cam, outdir):
    rgb_img = cv2.imread(image_path, 1)[:, :, ::-1]
    rgb_img = cv2.resize(rgb_img, (image_size, image_size))
    rgb_img = np.float32(rgb_img) / 255
    input_tensor = preprocess_image(rgb_img,
                                    mean=[0.5, 0.5, 0.5],
                                    std=[0.5, 0.5, 0.5])

    # If None, returns the map for the highest scoring category.
    # Otherwise, targets the requested category.
    target_category = None

    # AblationCAM and ScoreCAM have batched implementations.
    # You can override the internal batch size for faster computation.
    cam.batch_size = 32

    grayscale_cam = cam(input_tensor=input_tensor,
                        target_category=target_category,
                        eigen_smooth=args.eigen_smooth,
                        aug_smooth=args.aug_smooth)

    # Here grayscale_cam has only one image in the batch
    grayscale_cam = grayscale_cam[0, :]

    cam_image = show_cam_on_image(rgb_img,
                                  grayscale_cam,
                                  use_rgb=False,
                                  colormap=cv2.COLORMAP_JET)
    # cv2.imshow('image', cam_image)
    # cv2.waitKey(0)

    img_name = image_path.split('/')[-1].split('.')[0]
    if '\\' in img_name:
        img_name = img_name.split('\\')[-1]
    cv2.imwrite(os.path.join(out_dir, f'{img_name}_{args.method}.jpg'),
                cam_image)
Esempio n. 4
0
        model = model.cuda()

    target_layer = model.blocks[-1].norm1

    if args.method not in methods:
        raise Exception(f"Method {args.method} not implemented")

    cam = methods[args.method](model=model, 
                               target_layer=target_layer,
                               use_cuda=args.use_cuda,
                               reshape_transform=reshape_transform)

    rgb_img = cv2.imread(args.image_path, 1)[:, :, ::-1]
    rgb_img = cv2.resize(rgb_img, (224, 224))
    rgb_img = np.float32(rgb_img) / 255
    input_tensor = preprocess_image(rgb_img, mean=[0.5, 0.5, 0.5], 
                                             std=[0.5, 0.5, 0.5])

    # If None, returns the map for the highest scoring category.
    # Otherwise, targets the requested category.
    target_category = None

    # AblationCAM and ScoreCAM have batched implementations.
    # You can override the internal batch size for faster computation.
    cam.batch_size = 32

    grayscale_cam = cam(input_tensor=input_tensor,
                        target_category=target_category)

    cam_image = show_cam_on_image(rgb_img, grayscale_cam)
    cv2.imwrite(f'{args.method}_cam.jpg', cam_image)
Esempio n. 5
0
    try:
        model.load_state_dict(torch.load(args.ckpt)['state_dict'], strict=True)
    except:
        model.load_state_dict(torch.load(args.ckpt), strict=True)
    # print(model)
    output_dir = Path(args.output) if args.output else Path('./')
    target_category = args.target
    target_layer = getattr(model, args.target_layer)
    cam = methods[args.method](model=model,
                               target_layer=target_layer,
                               use_cuda=True)
    gb_model = GuidedBackpropReLUModel(model=copy.deepcopy(model), use_cuda=True)
    cam.batch_size = 32
    rgb_img = cv2.imread(str(args.image), cv2.IMREAD_COLOR)[:, :, ::-1]
    # rgb_img = cv2.resize(rgb_img, (224, 224))
    rgb_img = np.float32(rgb_img) / 255.0
    input_tensor = preprocess_image(rgb_img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    grayscale_cam = cam(input_tensor=input_tensor,
                        target_category=None,
                        aug_smooth=args.aug_smooth,
                        eigen_smooth=args.eigen_smooth)
    grayscale_cam = grayscale_cam[0, :]
    cam_image = show_cam_on_image(rgb_img, grayscale_cam)
    gb = gb_model(input_tensor, target_category=args.target)
    cam_mask = cv2.merge([grayscale_cam, grayscale_cam, grayscale_cam])
    cam_gb = deprocess_image(cam_mask * gb)
    gb = deprocess_image(gb)
    cv2.imwrite(str(output_dir / (args.method + '_cam_' + image_path.name)), cam_image)
    cv2.imwrite(str(output_dir / ('gb_' + image_path.name)), gb)
    cv2.imwrite(str(output_dir / (args.method + '_cam_gb_' + image_path.name)), cam_gb)