Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualizations #26

Open
caoyunkang opened this issue Dec 25, 2024 · 0 comments
Open

Visualizations #26

caoyunkang opened this issue Dec 25, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@caoyunkang
Copy link
Owner

I observed issues with numerical precision across different GPUs, which can result in poor visualizations in the original code. This is primarily due to the anomaly scores being exceedingly small, around 1e-6 . To address this, it is recommended to use the following revised code for tools/visualization.py to improve the visualizations.

def plot_sample_cv2(names, imgs, scores_: dict, gts, save_folder=None):
    os.makedirs(save_folder, exist_ok=True)

    # get subplot number
    total_number = len(imgs)

    scores = copy.deepcopy(scores_)
    # normarlisze anomalies
    for k, v in scores.items():
        max_value = np.max(v)
        min_value = np.min(v)

        # scores[k] = (scores[k] - min_value) / max_value * 255
        # scores[k] = scores[k].astype(np.uint8)

        # scale_factor = 255 / (max_value - min_value)
        # scores[k] = ((scores[k] - min_value) * scale_factor).round().clip(0, 255).astype(np.uint8)

        for i in range(len(scores[k])):
            cur_max_value = np.percentile(scores[k][i], 98)
            cur_min_value = np.percentile(scores[k][i], 2)

            scale_factor = 255 / (cur_max_value - cur_min_value)
            scores[k][i] = ((scores[k][i] - cur_min_value) * scale_factor).round().clip(0, 255).astype(np.uint8)

    # draw gts
    mask_imgs = []
    for idx in range(total_number):
        gts_ = gts[idx]
        mask_imgs_ = imgs[idx].copy()
        mask_imgs_[gts_ > 0.5] = (255, 255, 255)
        mask_imgs.append(mask_imgs_)

    # save imgs
    for idx in range(total_number):

        cv2.imwrite(os.path.join(save_folder, f'{names[idx]}_img.png'), imgs[idx])

        mask_img = cv2.addWeighted(mask_imgs[idx], 0.5, imgs[idx], 0.5, 0)
        cv2.imwrite(os.path.join(save_folder, f'{names[idx]}_mask.png'), mask_img)
        # cv2.imwrite(os.path.join(save_folder, f'{names[idx]}_mask.jpg'), mask_img)

        for key in scores:
            heat_map = cv2.applyColorMap(scores[key][idx], cv2.COLORMAP_JET)
            visz_map = cv2.addWeighted(heat_map, 0.5, imgs[idx], 0.5, 0)
            cv2.imwrite(os.path.join(save_folder, f'{names[idx]}_{key}.png'),
                        visz_map)
@caoyunkang caoyunkang added the enhancement New feature or request label Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant