使用ipdb调试

try:
import ipdb
except:
import pdb as ipdb ipdb.set_trace()

测试inference:

# coding=utf-8

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab import requests
from io import BytesIO
from PIL import Image
import numpy as np # this makes our figures bigger
pylab.rcParams['figure.figsize'] = 20, 12 from maskrcnn_benchmark.config import cfg
from predictor import COCODemo config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"
#config_file = "../configs/e2e_mask_rcnn_R_50_FPN_1x.yaml" # update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) # only "cuda" and "cpu" are valid device types
coco_demo = COCODemo(
cfg,
min_image_size=800,
confidence_threshold=0.7,
) def load(url):
"""
Given an url of an image, downloads the image and
returns a PIL image
"""
response = requests.get(url)
pil_image = Image.open(BytesIO(response.content)).convert("RGB")
# convert to BGR format
image = np.array(pil_image)[:, :, [2, 1, 0]]
return image def imshow(img):
plt.imshow(img[:, :, [2, 1, 0]])
plt.axis("off")
plt.show() # from http://cocodataset.org/#explore?id=345434
image = load("http://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg")
# image = Image.open("474797538.jpg").convert("RGB")
# image = np.array(image)[:, :, [2, 1, 0]] #imshow(image) # compute predictions
predictions = coco_demo.run_on_opencv_image(image)
imshow(predictions)

在predictor.py文件中核心函数def compute_prediction(self, original_image):下的变量信息:

->输入original_image=[480,640,3],int整型数据;

->经过变换后image=[3,800,1066],数据torch.float32;

然后进入核心函数:predictions = self.model(image_list),跳入generalized_rcnn.py文件,中def forward(self, images, targets=None):函数;

经过features = self.backbone(images.tensors)函数,使用各种基网络(如ResNet-50_FPN)提取各个stage的特征图;然后使用feature map进行RPN及ROI pooling操作;

-> features变量信息,tuple类型,5个特征图的tensor:

ipdb> p features.size()
*** AttributeError: 'tuple' object has no attribute 'size'
ipdb> p features.shape()
*** AttributeError: 'tuple' object has no attribute 'shape'
ipdb> p features[0].shape()
*** TypeError: 'torch.Size' object is not callable
ipdb> p features[0].size()
torch.Size([1, 256, 200, 272])
ipdb> p features[1].size()
torch.Size([1, 256, 100, 136])
ipdb> p features[2].size()
torch.Size([1, 256, 50, 68])
ipdb> p features[3].size()
torch.Size([1, 256, 25, 34])
ipdb> p features[4].size()
torch.Size([1, 256, 13, 17])

->经过rpn网络得到候选框:proposals, proposal_losses = self.rpn(images, features, targets)

ipdb> targets
ipdb> p targets
None
ipdb> p images
<maskrcnn_benchmark.structures.image_list.ImageList object at 0x7f5128049f28>
ipdb> p proposal_losses
{}
ipdb> p proposals
[BoxList(num_boxes=1000, image_width=1066, image_height=800, mode=xyxy)]

-> 然后经过fast rcnn网络,x, result, detector_losses = self.roi_heads(features, proposals, targets); 这部分有在roi_heads.py文件中,由两分支组成:检测分支和分割分支组成;

->在roi_heads.py文件的forward()中:x, detections, loss_box = self.box(features, proposals, targets)得到检测结果,

    def forward(self, features, proposals, targets=None):
"""
Arguments:
features (list[Tensor]): feature-maps from possibly several levels
proposals (list[BoxList]): proposal boxes
targets (list[BoxList], optional): the ground-truth targets. Returns:
x (Tensor): the result of the feature extractor
proposals (list[BoxList]): during training, the subsampled proposals
are returned. During testing, the predicted boxlists are returned
losses (dict[Tensor]): During training, returns the losses for the
head. During testing, returns an empty dict.
""" if self.training:
# Faster R-CNN subsamples during training the proposals with a fixed
# positive / negative ratio
with torch.no_grad():
proposals = self.loss_evaluator.subsample(proposals, targets) # extract features that will be fed to the final classifier. The
# feature_extractor generally corresponds to the pooler + heads
x = self.feature_extractor(features, proposals)
# final classifier that converts the features into predictions
class_logits, box_regression = self.predictor(x) if not self.training:
result = self.post_processor((class_logits, box_regression), proposals)
return x, result, {} loss_classifier, loss_box_reg = self.loss_evaluator(
[class_logits], [box_regression]
)
return (
x,
proposals,
dict(loss_classifier=loss_classifier, loss_box_reg=loss_box_reg),
)

->x为经过池化操作及特征提取的特征用于分类回归,经过后处理,剩下有用的box返回;

->筛选出来的1000个proposals,提取1024维特征; 最终有效box剩88个;

ipdb> x.shape
torch.Size([1000, 1024])
ipdb> detections.shape
*** AttributeError: 'list' object has no attribute 'shape'
ipdb> detections.size()
*** AttributeError: 'list' object has no attribute 'size'
ipdb> len(detections)
1
ipdb> detections
[BoxList(num_boxes=88, image_width=1066, image_height=800, mode=xyxy)]

-> 利用检测的结果,经过mask分支:x, detections, loss_mask = self.mask(mask_features, detections, targets); mask分支:

    def forward(self, features, proposals, targets=None):
"""
Arguments:
features (list[Tensor]): feature-maps from possibly several levels
proposals (list[BoxList]): proposal boxes
targets (list[BoxList], optional): the ground-truth targets. Returns:
x (Tensor): the result of the feature extractor
proposals (list[BoxList]): during training, the original proposals
are returned. During testing, the predicted boxlists are returned
with the `mask` field set
losses (dict[Tensor]): During training, returns the losses for the
head. During testing, returns an empty dict.
""" if self.training:
# during training, only focus on positive boxes
all_proposals = proposals
proposals, positive_inds = keep_only_positive_boxes(proposals)
if self.training and self.cfg.MODEL.ROI_MASK_HEAD.SHARE_BOX_FEATURE_EXTRACTOR:
x = features
x = x[torch.cat(positive_inds, dim=0)]
else:
x = self.feature_extractor(features, proposals)
mask_logits = self.predictor(x) if not self.training:
result = self.post_processor(mask_logits, proposals)
return x, result, {} loss_mask = self.loss_evaluator(proposals, mask_logits, targets) return x, all_proposals, dict(loss_mask=loss_mask)

->x为maks分支特征的tensor,变成[88, 256, 14, 14],返回的detections就是box+mask的内容

ipdb> x.shape
torch.Size([88, 256, 14, 14])
ipdb> detections
[BoxList(num_boxes=88, image_width=1066, image_height=800, mode=xyxy)]
ipdb> loss_mask
{}

->做完后,返回generalized_rcnn.py文件,返回predictor.py进行一些后处理,可视化结果即可!

maskrcnn_benchmark代码分析(1)的更多相关文章

  1. maskrcnn_benchmark代码分析(2)

    maskrcnn_benchmark训练过程 ->训练命令: python tools/train_net.py --config-file "configs/e2e_mask_rcn ...

  2. maskrcnn_benchmark代码分析(3)

    数据结构 数据加载 数据后处理

  3. Android代码分析工具lint学习

    1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...

  4. pmd静态代码分析

    在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可 ...

  5. [Asp.net 5] DependencyInjection项目代码分析-目录

    微软DI文章系列如下所示: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Autofac [ ...

  6. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)

    Asp.net 5的依赖注入注入系列可以参考链接: [Asp.net 5] DependencyInjection项目代码分析-目录 我们在之前讲微软的实现时,对于OpenIEnumerableSer ...

  7. 完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...

  8. STM32启动代码分析 IAR 比较好

    stm32启动代码分析 (2012-06-12 09:43:31) 转载▼     最近开始使用ST的stm32w108芯片(也是一款zigbee芯片).开始看他的启动代码看的晕晕呼呼呼的. 还好在c ...

  9. 常用 Java 静态代码分析工具的分析与比较

    常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基 本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBu ...

随机推荐

  1. android 的安全问题

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 1,webView的js脚本引发的安全 2,代码的混淆加密.还可采用第三方apk加固程序 ...

  2. 【转载】C语言 构建参数个数不固定函数

    深入浅出可变参数函数的使用技巧本文主要介绍可变参数的函数使用,然后分析它的原理,程序员自己如何对它们实现和封装,最后是可能会出现的问题和避免措施. VA函数(variable argument fun ...

  3. Javascript 严格模式use strict详解

    1.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...

  4. 使用 IntraWeb (25) - 基本控件之 TIWRegion

    这应该是 IW 中最重要的容器了, 和它同父的还有 TIWTabControl TIWRegion 所在单元及继承链: IWRegion.TIWRegion 主要成员: property Align: ...

  5. git 拉取和获取 pull 和 fetch 区别

    使用Git  直接提交的话   直接 push 获取最新版本  有两种  拉取 和 获取 pull 和 fetch git  pull     从远程拉取最新版本 到本地  自动合并 merge   ...

  6. rcp(插件开发)插件B需要引用插件A中的jar包-如何处理依赖关系

    如果插件B需要引用插件A中的jar 通常需要以下几步: 1.插件B要依赖插件A 2.在插件B的build path中添加插件A的jar包 3.插件A的runtime导出插件B中使用jar的packag ...

  7. 导出文件在IE和火狐中文件名乱码问题的解决

    $ua = $_SERVER["HTTP_USER_AGENT"]; $filename = "客户数据.xls"; $encoded_filename = u ...

  8. 树莓派创始人访谈:我们是怎么让大家都成为DIY黑客的

    原文出处: Linux.CN 请记住它是为喜欢折腾的人准备的只要35美元的计算机 我永远不会忘记我第一次看到树莓派的情形.那个小巧的,信用卡大小的计算机,性能却足够强劲,可以作为一般家用PC,媒体中心 ...

  9. 没有可用的复制构造函数或复制构造函数声明为“explicit”

           c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810) : error C2558: str ...

  10. 加快Qemu Aarch32虚拟开发板的启动速度

    软件版本 Qemu: 2.8.0 虚拟开发板: vexpress-ca9 概述 之前的博文介绍了将Python移植到开发板上, 根文件系统采用的是ramdisk, 这个文件系统的缺点是修改的内容重启会 ...