HAL-42 / FMA-WSSS

This repo is a implementation of the Foundation Model Assisted Weakly Supervised Semantic Segmentation. The code is developed based on the Pytorch framework.
Apache License 2.0
38 stars 4 forks source link

quasi-superpixel method #34

Open lk0429 opened 1 month ago

lk0429 commented 1 month ago

Thanks for the great work, can you tell me which part of the code is about the quasi-superpixel method in the SAM-Based Seeding Module? Sorry, I really can't find it.

HAL-42 commented 1 month ago

In src/tasks/anns_seed/run.py, we load CAM, quasi-superpixel and run SAMS:

        if cfg.cam.loader:
            cam, fg_cls = cfg.cam.loader.cal(cfg.cam.dir, img_id)
            cam = torch.as_tensor(cam, dtype=torch.float32, device=device)  # PHW
            fg_cls = torch.as_tensor(fg_cls, dtype=torch.uint8, device=device)  # P
        else:
            loaded = np.load(osp.join(cfg.cam.dir, f'{img_id}.npz'))
            cam = torch.as_tensor(loaded['cam'], dtype=torch.float32, device=device)  # PHW
            fg_cls = torch.as_tensor(loaded['fg_cls'], dtype=torch.uint8, device=device)  # P

        # ** CAM插值到原图大小。
        cam = resize_cam_cuda(cam, (ori_h, ori_w))

        # * 读取SAM标注,并计算种子点。
        if osp.isfile(anns_file := osp.join(cfg.sam_anns.dir, f'{img_id}.pkl')):
            with open(anns_file, 'rb') as f:
                anns = SamAnns(pickle.load(f))

            # * 提前准备好二进制掩码segmentation。
            for ann in anns:
                SamAuto.decode_segmentation(ann, replace=True)

            # * stack好数据。
            anns.stack_data(masks=True, areas=True, device=device)

            seed = cfg.seed.cal(anns, cam, fg_cls, norm_first=norm_first, bg_method=bg_method).cpu().numpy()

where cfg.seed.cal is gather_norm_bg_argmax in src/libs/seeding/seed_anns/score_cw.py