text stringlengths 1 93.6k |
|---|
return gb
|
def save_image(image_dicts, input_image_name, index, network='AirShot-101', output_dir='./vis/grad_rpn/'):
|
prefix = os.path.splitext(input_image_name)[0]
|
for key, image in image_dicts.items():
|
io.imsave(os.path.join(output_dir, '{}-{}-{}-RPN_{}_vis.jpg'.format(prefix, network, key, index)), image)
|
def get_parser():
|
parser = argparse.ArgumentParser(description="Detectron2 demo for builtin models")
|
parser.add_argument(
|
"--config-file",
|
default="configs/fsod/R101/test_R_101_C4_1x_coco5.yaml",
|
metavar="FILE",
|
help="path to config file",
|
)
|
parser.add_argument("--input", help="A list of space separated input images")
|
parser.add_argument(
|
"--output",
|
help="A file or directory to save output visualizations. "
|
"If not given, will show output in an OpenCV window.",
|
)
|
parser.add_argument(
|
"--confidence-threshold",
|
type=float,
|
default=0.8,
|
help="Minimum score for instance predictions to be shown",
|
)
|
parser.add_argument(
|
"--opts",
|
help="Modify config options using the command-line 'KEY VALUE' pairs",
|
default=[],
|
nargs=argparse.REMAINDER,
|
)
|
parser.add_argument(
|
"--index",
|
type=int,
|
)
|
return parser
|
def main(args):
|
setup_logger(name="fvcore")
|
logger = setup_logger()
|
logger.info("Arguments: " + str(args))
|
cfg = setup_cfg(args)
|
print(cfg)
|
# 构建模型
|
model = build_model(cfg)
|
metadata = MetadataCatalog.get(cfg.DATASETS.TEST[0])
|
# 加载权重
|
checkpointer = DetectionCheckpointer(model)
|
checkpointer.load(cfg.MODEL.WEIGHTS)
|
# 加载图像
|
path = os.path.expanduser(args.input)
|
original_image = read_image(path, format="BGR")
|
height, width = original_image.shape[:2]
|
transform_gen = T.ResizeShortestEdge(
|
[cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST
|
)
|
image = transform_gen.get_transform(original_image).apply_image(original_image)
|
image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1)).requires_grad_(True)
|
inputs = {"image": image, "height": height, "width": width}
|
# Grad-CAM
|
layer_name = 'proposal_generator.rpn_head.conv'
|
# layer_name = 'apn.r3.conv.0'
|
grad_cam = GradCAM(model, layer_name, (height, width))
|
mask, box = grad_cam(inputs,args.index) # cam mask
|
grad_cam.remove_handlers()
|
#
|
image_dict = {}
|
img = original_image[..., ::-1]
|
x1, y1, x2, y2 = box
|
# image_dict['predict_box'] = img[y1:y2, x1:x2]
|
image_dict['image_cam'], _ = gen_cam(img, mask)
|
# 获取类别名称
|
# meta = MetadataCatalog.get(
|
# cfg.DATASETS.TEST[0] if len(cfg.DATASETS.TEST) else "__unused"
|
# )
|
# label = meta.thing_classes[class_id]
|
# print("label:{}".format(label))
|
# # GuidedBackPropagation
|
# gbp = GuidedBackPropagation(model)
|
# inputs['image'].grad.zero_() # 梯度置零
|
# grad = gbp(inputs)
|
# print("grad.shape:{}".format(grad.shape))
|
# gb = gen_gb(grad)
|
# gb = gb[y1:y2, x1:x2]
|
# image_dict['gb'] = gb
|
# # 生成Guided Grad-CAM
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.