目标检测之Faster-RCNN的pytorch代码详解(数据预处理篇)

首先贴上代码原作者的github:https://github.com/chenyuntc/simple-faster-rcnn-pytorch(非代码作者,博文只解释代码)

今天看完了simple-faster-rcnn-pytorch-master代码的最后一个train.py文件,是时候认真的总结一下了,我打算一共总结四篇博客用来详细的分析Faster-RCNN的代码的pytorch实现, 四篇博客的内容及目录结构如下:

1 Faster-RCNN的数据读取及预处理部分:(对应于代码的/simple-faster-rcnn-pytorch-master/data文件夹):https://www.cnblogs.com/kerwins-AC/p/9734381.html 

2 Faster-RCNN的模型准备部分:(对应于代码目录/simple-faster-rcnn-pytorch-master/model/utils/文件夹):https://www.cnblogs.com/kerwins-AC/p/9752679.html

3 Faster-RCNN的模型正式介绍:(对应于代码目录/simple-faster-rcnn-pytorch-master/model/文件夹):         尚未完成

4 Faster-RCNN的训练代码部分:(对应于代码目录/simple-faster-rcnn-pytorch-master/train.py,trainer.py代码):https://www.cnblogs.com/kerwins-AC/p/9728731.html

 本篇博客主要介绍代码的数据预处理部分的内容,对应于以下几个文件:

 首先是dataset.py文件,我们用函数流程图看一下它的结构:

 然后老规矩一个函数一个函数的分析它的内容和功能!

1 def inverse_normalize(img)函数代码如下:

1 def inverse_normalize(img):
2     if opt.caffe_pretrain:
3         img = img + (np.array([122.7717, 115.9465, 102.9801]).reshape(3, 1, 1))
4         return img[::-1, :, :]
5     # approximate un-normalize for visualize
6     return (img * 0.225 + 0.45).clip(min=0, max=1) * 255
inverse_normalize()

函数首先读取opt.caffe_pretrain判断是否使用caffe_pretrain进行预训练如果是的话,对图片进行逆正则化处理,就是将图片处理成caffe模型需要的格式

2 def pytorch_normalize(img) 函数代码如下:

 

1 def pytorch_normalze(img):
2     """
3     https://github.com/pytorch/vision/issues/223
4     return appr -1~1 RGB
5     """
6     normalize = tvtsf.Normalize(mean=[0.485, 0.456, 0.406],
7                                 std=[0.229, 0.224, 0.225])
8     img = normalize(t.from_numpy(img))
9     return img.numpy()
pytorch_normalize

 

函数首先设置归一化参数normalize=tvtsf.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225]) 然后对图片进行归一化处理img=normalize(t.from_numpy(img))

3 def caffe_normalize(img)函数代码如下:

1 def caffe_normalize(img):
2     """
3     return appr -125-125 BGR
4     """
5     img = img[[2, 1, 0], :, :]  # RGB-BGR
6     img = img * 255
7     mean = np.array([122.7717, 115.9465, 102.9801]).reshape(3, 1, 1)
8     img = (img - mean).astype(np.float32, copy=True)
9     return img
caffe_normalize(img)

caffe的图片格式是BGR,所以需要img[[2,1,0],:,:]将RGB转换成BGR的格式,然后图片img = img*255 , mean = np.array([122.7717,115.9465,102.9801]).reshape(3,1,1)设置图片均值

然后用图片减去均值完成caffe形式的归一化处理

4 def preprocess(img, min_size=600, max_size=1000)函数代码如下:

 1 def preprocess(img, min_size=600, max_size=1000):
 2     """Preprocess an image for feature extraction.
 3 
 4     The length of the shorter edge is scaled to :obj:`self.min_size`.
 5     After the scaling, if the length of the longer edge is longer than
 6     :param min_size:
 7     :obj:`self.max_size`, the image is scaled to fit the longer edge
 8     to :obj:`self.max_size`.
 9 
10     After resizing the image, the image is subtracted by a mean image value
11     :obj:`self.mean`.
12 
13     Args:
14         img (~numpy.ndarray): An image. This is in CHW and RGB format.
15             The range of its value is :math:`[0, 255]`.
16 
17     Returns:
18         ~numpy.ndarray: A preprocessed image.
19 
20     """
21     C, H, W = img.shape
22     scale1 = min_size / min(H, W)
23     scale2 = max_size / max(H, W)
24     scale = min(scale1, scale2)
25     img = img / 255.
26     img = sktsf.resize(img, (C, H * scale, W * scale), mode='reflect',anti_aliasing=False)
27     # both the longer and shorter should be less than
28     # max_size and min_size
29     if opt.caffe_pretrain:
30         normalize = caffe_normalize
31     else:
32         normalize = pytorch_normalze
33     return normalize(img)
preprocess()

图片处理函数,C,H,W = img.shape 读取图片格式通道,高度,宽度 

Scale1 = min_size/min(H,W)

Scale2 = max_size / max(H,W)

Scale = min(scale1,scale2)设置放缩比,这个过程很直觉,选小的方便大的和小的都能够放缩到合适的位置

img  = img/ 255

img = sktsf.resize(img,(C,H*scale,W*scale),model='reflecct')将图片调整到合适的大小位于(min_size,max_size)之间、

然后根据opt.caffe_pretrain是否存在选择调用前面的pytorch正则化还是caffe_pretrain正则化

 

5 class Transform(object):代码如下

 

 1 class Transform(object):
 2 
 3     def __init__(self, min_size=600, max_size=1000):
 4         self.min_size = min_size
 5         self.max_size = max_size
 6 
 7     def __call__(self, in_data):
 8         img, bbox, label = in_data
 9         _, H, W = img.shape
10         img = preprocess(img, self.min_size, self.max_size)
11         _, o_H, o_W = img.shape
12         scale = o_H / H
13         bbox = util.resize_bbox(bbox, (H, W), (o_H, o_W))
14 
15         # horizontally flip
16         img, params = util.random_flip(
17             img, x_random=True, return_param=True)
18         bbox = util.flip_bbox(
19             bbox, (o_H, o_W), x_flip=params['x_flip'])
20 
21         return img, bbox, label, scale
Transform

 

__init__函数设置了图片的最小最大尺寸,本pytorch代码中min_size=600,max_size=1000

__call__函数中 从in_data中读取 img,bbox,label 图片,bboxes的框框和label

然后从_,H,W = img.shape读取出图片的长和宽

img = preposses(img,self.min_size,self.max_size)将图片进行最小最大化放缩然后进行归一化

_,o_H,o_W = img.shape 读取放缩后图片的shape 

scale = o_H/H 放缩前后相除,得出放缩比因子

bbox = util.reszie_bbox(bbox,(H,W),(o_H,o_W)) 重新调整bboxes框的大小

img,params = utils.random_flip(img.x_random =True,return_param=True)进行图片的随机反转,图片旋转不变性,增强网络的鲁棒性!

同样的对bboxes进行随机反转,最后返回img,bbox,label,scale

6 class Dataset 代码如下

 

 1 class Dataset:
 2     def __init__(self, opt):
 3         self.opt = opt
 4         self.db = VOCBboxDataset(opt.voc_data_dir)
 5         self.tsf = Transform(opt.min_size, opt.max_size)
 6 
 7     def __getitem__(self, idx):
 8         ori_img, bbox, label, difficult = self.db.get_example(idx)
 9 
10         img, bbox, label, scale = self.tsf((ori_img, bbox, label))
11         # TODO: check whose stride is negative to fix this instead copy all
12         # some of the strides of a given numpy array are negative.
13         return img.copy(), bbox.copy(), label.copy(), scale
14 
15     def __len__(self):
16         return len(self.db)
class Dataset

 

__init__初始化设置self.opt =opt ,self.db = VOCBboxDataset(opt.voc_data_dir)以及self.tsf = Transform(opt.min_size,opt.max_size) 

—getitem__可以简单的理解为从数据集存储路径中将例子一个个的获取出来,然后调用前面的Transform函数将图片,label进行最小值最大值放缩归一化,重新调整bboxes的大小,然后随机反转,最后将数据集返回!

7 class TestDataset 代码如下

 

 1 class TestDataset:
 2     def __init__(self, opt, split='test', use_difficult=True):
 3         self.opt = opt
 4         self.db = VOCBboxDataset(opt.voc_data_dir, split=split, use_difficult=use_difficult)
 5 
 6     def __getitem__(self, idx):
 7         ori_img, bbox, label, difficult = self.db.get_example(idx)
 8         img = preprocess(ori_img)
 9         return img, ori_img.shape[1:], bbox, label, difficult
10 
11     def __len__(self):
12         return len(self.db)
TestDataset

 

 

TestData完成的功能和前面类似,但是获取调用的数据集是不同的,因为def __init__(self,opt,split='test',use_difficult=True)可以看到它在从Voc_data_dir中获取数据的时候使用了split='test'也就是从test往后分割的部分数据送入到TestDataset的self.db中,然后在进行图片处理的时候,并没有调用transform函数,因为测试图片集没有bboxes需要考虑,同时测试图片集也不需要随机反转,反转无疑为测试准确率设置了阻碍!所以直接调用preposses()函数进行最大值最小值裁剪然后归一化就完成了测试数据集的处理!最后将整个self.db返回,至此,dataset.py介绍完毕

 

转载于:https://www.cnblogs.com/kerwins-AC/p/9734381.html

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值