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

作者你好,请问文章中的这个特征图是怎么绘制的呢,文章中有相关代码吗,谢谢啦 #152

Open
lalalagogogochong opened this issue Mar 29, 2022 · 8 comments

Comments

@lalalagogogochong
Copy link

image

@csuhan
Copy link
Owner

csuhan commented Mar 29, 2022

This script may help you.

from mmdet.models import build_detector
from mmcv.runner import load_checkpoint
import numpy as np
from mmdet.datasets import to_tensor
from mmdet.datasets.transforms import ImageTransform
import mmcv
import argparse
import ipdb
import cv2
import os
import os.path as osp


def rotate_bound(image, angle):
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    return cv2.warpAffine(image, M, (nW, nH))


def img2feature(model, img, cfg):
    img_trans = ImageTransform(cfg.img_norm_cfg['mean'], cfg.img_norm_cfg['std'], size_divisor=32, to_rgb=True)
    img = to_tensor(img_trans(img, scale=1.0)[0]).unsqueeze(0).cuda()
    ys = model.extract_feat(img)
    ys_out = []
    for y in ys:
        y = y.detach().cpu().numpy().reshape(y.size(-2), y.size(-1))
        ys_out.append(y)
    return ys_out


def visualize(img_path, img_name, cfg_path, checkpoint_path, out_path, angles=[0, 90, 180, 270]):
    cfg = mmcv.Config.fromfile(cfg_path)
    model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    checkpoint = load_checkpoint(model, checkpoint_path, map_location='cpu')
    model.eval()
    model = model.cuda()

    img = mmcv.imread(img_path)
    img = mmcv.imresize(img,(2048,2048))
    for angle in angles:
        # rot image
        img = rotate_bound(img, angle)
        ys_img = img2feature(model, img, cfg)
        for i,y_img in enumerate(ys_img):
            y_img = np.uint8((y_img - y_img.min()) / (y_img.max() - y_img.min()) * 255)
            y_img = cv2.applyColorMap(y_img, cv2.COLORMAP_JET)
            img_path = osp.join(out_path, img_name + '-angle' + str(angle) + '-group' + str(i) + '.png')
            mmcv.imwrite(y_img, img_path)


def parse_arg():
    parser = argparse.ArgumentParser()
    parser.add_argument('--cfg_path',
                        default="experiments/dota15/visualize/faster_rcnn_RoITrans_er50_fpn_1x_dota1_5_ms_visualize.py")
    parser.add_argument('--checkpoint_path',
                        default="work_dirs/faster_rcnn_RoITrans_er50_fpn_1x_dota1_5_gap512_msTrainTest_rotationTrainTest/latest.pth")
    parser.add_argument('--img_dir', default='demo/vis_supp_img')
    parser.add_argument('--out_dir', default='demo_results/vis_supp')
    return parser.parse_args()


if __name__ == '__main__':
    args = parse_arg()
    img_dir = args.img_dir
    img_names = os.listdir(img_dir)
    img_paths = [osp.join(img_dir, img_name) for img_name in img_names]
    img_base_names = [osp.basename(img_name) for img_name in img_names]
    for img_path, img_name in zip(img_paths, img_base_names):
        out_dir = osp.join(args.out_dir,img_name)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)
        visualize(img_path, img_name, args.cfg_path, args.checkpoint_path, out_dir, angles=[0,90,180,270])

@lalalagogogochong
Copy link
Author

This script may help you.

from mmdet.models import build_detector
from mmcv.runner import load_checkpoint
import numpy as np
from mmdet.datasets import to_tensor
from mmdet.datasets.transforms import ImageTransform
import mmcv
import argparse
import ipdb
import cv2
import os
import os.path as osp


def rotate_bound(image, angle):
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    return cv2.warpAffine(image, M, (nW, nH))


def img2feature(model, img, cfg):
    img_trans = ImageTransform(cfg.img_norm_cfg['mean'], cfg.img_norm_cfg['std'], size_divisor=32, to_rgb=True)
    img = to_tensor(img_trans(img, scale=1.0)[0]).unsqueeze(0).cuda()
    ys = model.extract_feat(img)
    ys_out = []
    for y in ys:
        y = y.detach().cpu().numpy().reshape(y.size(-2), y.size(-1))
        ys_out.append(y)
    return ys_out


def visualize(img_path, img_name, cfg_path, checkpoint_path, out_path, angles=[0, 90, 180, 270]):
    cfg = mmcv.Config.fromfile(cfg_path)
    model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    checkpoint = load_checkpoint(model, checkpoint_path, map_location='cpu')
    model.eval()
    model = model.cuda()

    img = mmcv.imread(img_path)
    img = mmcv.imresize(img,(2048,2048))
    for angle in angles:
        # rot image
        img = rotate_bound(img, angle)
        ys_img = img2feature(model, img, cfg)
        for i,y_img in enumerate(ys_img):
            y_img = np.uint8((y_img - y_img.min()) / (y_img.max() - y_img.min()) * 255)
            y_img = cv2.applyColorMap(y_img, cv2.COLORMAP_JET)
            img_path = osp.join(out_path, img_name + '-angle' + str(angle) + '-group' + str(i) + '.png')
            mmcv.imwrite(y_img, img_path)


def parse_arg():
    parser = argparse.ArgumentParser()
    parser.add_argument('--cfg_path',
                        default="experiments/dota15/visualize/faster_rcnn_RoITrans_er50_fpn_1x_dota1_5_ms_visualize.py")
    parser.add_argument('--checkpoint_path',
                        default="work_dirs/faster_rcnn_RoITrans_er50_fpn_1x_dota1_5_gap512_msTrainTest_rotationTrainTest/latest.pth")
    parser.add_argument('--img_dir', default='demo/vis_supp_img')
    parser.add_argument('--out_dir', default='demo_results/vis_supp')
    return parser.parse_args()


if __name__ == '__main__':
    args = parse_arg()
    img_dir = args.img_dir
    img_names = os.listdir(img_dir)
    img_paths = [osp.join(img_dir, img_name) for img_name in img_names]
    img_base_names = [osp.basename(img_name) for img_name in img_names]
    for img_path, img_name in zip(img_paths, img_base_names):
        out_dir = osp.join(args.out_dir,img_name)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)
        visualize(img_path, img_name, args.cfg_path, args.checkpoint_path, out_dir, angles=[0,90,180,270])

谢谢你,请问你文章中的特征图是哪个layer提取出来的呢,如果提取fpn那部分的特征图,是直接对各个通道相加吗,还是需要对通道怎么操作呢,第一张图是提取的layer1的,第二张是fpn的一张,第一张看起来应该可以,我是直接将各个通道相加的,第二张fpn看起来好香有点问题,不知道正不正常,麻烦你帮忙解答一下吧,谢谢啦
image
image

@csuhan
Copy link
Owner

csuhan commented Mar 31, 2022

缩放至相同尺寸后相加

@lalalagogogochong
Copy link
Author

lalalagogogochong commented Apr 1, 2022

缩放至相同尺寸后相加

你好,这个缩放至相同尺寸是指什么呢,同一层特征图各个通道的尺寸都是一样的啊,还有下面这个提取来的特征如果直接resize会报错误
image
image
麻烦你帮忙看一下吧

@csuhan
Copy link
Owner

csuhan commented Apr 2, 2022

不同fpn层的feature map resize到一样大

@runqi-zhao
Copy link

缩放至相同尺寸后相加

你好,这个缩放至相同尺寸是指什么呢,同一层特征图各个通道的尺寸都是一样的啊,还有下面这个提取来的特征如果直接resize会报错误 image image 麻烦你帮忙看一下吧

你好,请问您问题解决没 我遇到了跟您一样的问题

@hu-wei-carrzy
Copy link

from mmdet.datasets import to_tensor
from mmdet.datasets.transforms import ImageTransform

importError

@hu-wei-carrzy
Copy link

请问这个mmdet是哪个版本呀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants