深度学习图片预处理:crop | 您所在的位置:网站首页 › 数字3192什么意思 › 深度学习图片预处理:crop |
内容
1.前言 2.带注释代码 3.原理分析 前言在深度学习中,经常需要将输入图片进行裁剪,转换为网络输入的图片height和width。如果直接将图片进行resize,可能使得图片中的bbox变形。因此需要对原图进行扩展之后,以原图的bbox中心作为输出图的中心,按照[height,width] × \times × c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size的大小进行裁剪,然后再resize到[height,width]进行输出。这样裁剪的好处是可以使得bbox不会变形。 带注释代码原图(img),扩展后的图片(bimg),裁剪后的图片(crop_img)效果如下: 1.首先为了确保最后resize的时候bbox能全部裁剪进去,所以要先对bbox进行适当扩展。 2.由于最终resize之后的[height,width]确定,所以需要确定的就是放缩的系数 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size。 先来理解这一段代码。 # crop_size 取比例较大的边,min_shape取边对应的输出边 if crop_height / height > crop_width / width: crop_size = crop_height min_shape = height else: crop_size = crop_width min_shape = width可以看到相当于是粗略的选择了一下放缩系数 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size。那么为什么要选择比例较大的边呢,我个人认为可以这么理解,假设一个情况。假若crop_height很大,crop_width很小,而height很小,width很大,那么如果我们选择比例较小的边,那就是crop_width / width作为放缩系数 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size,根据假设可以得出这个放缩系数小于1。 由下面代码可以看出,在bimg上裁剪的时候,是以[height,width] × \times × c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size的大小进行裁剪的,如果放缩系数小于1,height本身很小,crop_height很大,就会使得height × \times × c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size比crop_height小很多。这样将导致裁剪的时候bbox会丢失很多在height方向上的图片信息。 min_x = int(objcenter[0] - crop_size / 2. / min_shape * width) max_x = int(objcenter[0] + crop_size / 2. / min_shape * width) min_y = int(objcenter[1] - crop_size / 2. / min_shape * height) max_y = int(objcenter[1] + crop_size / 2. / min_shape * height)因此粗略的确定 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size这个系数需要选择比例较大的边。 3.后面部分就是关于 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size的一个调整了。 关于调整主要需要关注三个方面的内容,第一个, c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size需要保证区域大小完全在bimg之内,这个很显然,如果超出了bimg区域,怎么来裁剪呢;第二个 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size需要保证完全包括了bbox的所有区域;第三个, c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size需要尽量小,因为在完全包括bbox的前提下,放缩系数越大,resize之后的bbox区域就会越模糊。 由于是同左右上下进行比较,所以我只取同左边进行比较相关的代码,其余可以以此类推。下面一个一个来看代码是怎么来满足这三个方面的。 crop_size = min(crop_size, objcenter[0] / width * min_shape * 2. - 1.) min_x = int(objcenter[0] - crop_size / 2. / min_shape * width)第一个: 假如crop_size crop_width / width时。此时H=crop_size,满足H>bbox_height,所以在height上满足第二个要求,同时有W=width × \times × c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size>width × \times × c r o p _ w i d t h w i d t h \frac{crop\_width}{width} widthcrop_width=crop_width,所以在width上满足第二个要求。 当min(crop_size, objcenter[0] / width * min_shape * 2. - 1.)=objcenter[0] / width * min_shape * 2. - 1时, 有W=width × \times × c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size=width × \times × ( o b j c e n t e r [ 0 ] ∗ 2 / w i d t h − 1 m i n _ s h a p e ) (objcenter[0] * 2/ width-\frac{1}{min\_shape}) (objcenter[0]∗2/width−min_shape1)。 现在比较 ( o b j c e n t e r [ 0 ] ∗ 2 / w i d t h − 1 m i n _ s h a p e ) (objcenter[0] * 2/ width-\frac{1}{min\_shape}) (objcenter[0]∗2/width−min_shape1)和 c r o p _ w i d t h w i d t h \frac{crop\_width}{width} widthcrop_width的 大小关系。经过变换,等于比较 ( o b j c e n t e r [ 0 ] ∗ 2 − w i d t h m i n _ s h a p e ) (objcenter[0] * 2-\frac{width}{min\_shape}) (objcenter[0]∗2−min_shapewidth)和crop_width的关系。因为objcenter[0] * 2等价于bimg中bbox中心以左部分长度的两倍。add = max(img.shape[0], img.shape[1]),add>=bbox_width,而可以从下面的代码可以看出crop_widthcrop_width bbox_extend_factor = (0.1, 0.15) crop_width = (bbox[2] - bbox[0]) * (1 + bbox_extend_factor[0] * 2)而 w i d t h m i n _ s h a p e \frac{width}{min\_shape} min_shapewidth是一个很小的量,所以可以不太严谨地证明出 ( o b j c e n t e r [ 0 ] ∗ 2 − w i d t h m i n _ s h a p e ) (objcenter[0] * 2-\frac{width}{min\_shape}) (objcenter[0]∗2−min_shapewidth)大于crop_width。所以有W=width × \times × ( o b j c e n t e r [ 0 ] ∗ 2 / w i d t h − 1 m i n _ s h a p e ) (objcenter[0] * 2/ width-\frac{1}{min\_shape}) (objcenter[0]∗2/width−min_shape1)>width × \times × c r o p _ w i d t h w i d t h \frac{crop\_width}{width} widthcrop_width=crop_width,所以在width上满足第二个要求。 第三个: 由前面的证明得到了crop_size及左右上下的部分,都满足第一个和第二个要求。那么下面这一段代码其实就是在寻找满足第三个要求的过程,即找到尽量小的 c r o p _ s i z e m i n _ s h a p e \frac{crop\_size}{min\_shape} min_shapecrop_size。 crop_size = min(crop_size, objcenter[0] / width * min_shape * 2. - 1.) crop_size = min(crop_size, (bimg.shape[1] - objcenter[0]) / width * min_shape * 2. - 1) crop_size = min(crop_size, objcenter[1] / height * min_shape * 2. - 1.) crop_size = min(crop_size, (bimg.shape[0] - objcenter[1]) / height * min_shape * 2. - 1)到这里为止,我们对代码进行了分析,理解了代码为什么这么写的原理,数据预处理里面crop的部分算是大功告成啦(撒花)。 以上都是个人的理解和推导过程,转载请注明出处,要是有错误或考虑不周的地方欢迎指出。 |
CopyRight 2018-2019 实验室设备网 版权所有 |