ReDet icon indicating copy to clipboard operation
ReDet copied to clipboard

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

Open lalalagogogochong opened this issue 3 years ago • 8 comments

image

lalalagogogochong avatar Mar 29 '22 01:03 lalalagogogochong

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])

csuhan avatar Mar 29 '22 04:03 csuhan

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

lalalagogogochong avatar Mar 30 '22 08:03 lalalagogogochong

缩放至相同尺寸后相加

csuhan avatar Mar 31 '22 06:03 csuhan

缩放至相同尺寸后相加

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

lalalagogogochong avatar Apr 01 '22 08:04 lalalagogogochong

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

csuhan avatar Apr 02 '22 07:04 csuhan

缩放至相同尺寸后相加

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

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

runqi-zhao avatar Nov 28 '22 07:11 runqi-zhao

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

importError

hu-wei-carrzy avatar Mar 25 '23 06:03 hu-wei-carrzy

请问这个mmdet是哪个版本呀

hu-wei-carrzy avatar Mar 26 '23 08:03 hu-wei-carrzy