首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何禁用Darkflow YOLOv2上的图像翻转?

如何禁用Darkflow YOLOv2上的图像翻转?
EN

Stack Overflow用户
提问于 2020-03-14 23:17:02
回答 1查看 215关注 0票数 0

我目前正在训练一个基于方向的模型。我希望能够训练一个模型,它可以告诉一个物体的估计方向。目前,我在1000纪元左右,精度不是很好。我的理论是,翻转操作似乎会导致模型不准确,因为90度的方向可能会翻转到-90度。因此,这两个单独的类会相互混淆。

代码语言:javascript
复制
def imcv2_affine_trans(im):
    # Scale and translate
    h, w, c = im.shape
    scale = np.random.uniform() / 10. + 1.
    max_offx = (scale-1.) * w
    max_offy = (scale-1.) * h
    offx = int(np.random.uniform() * max_offx)
    offy = int(np.random.uniform() * max_offy)

    im = cv2.resize(im, (0,0), fx = scale, fy = scale)
    im = im[offy : (offy + h), offx : (offx + w)]
    flip = np.random.binomial(1, .5)
    if flip: im = cv2.flip(im, 1)
    return im, [w, h, c], [scale, [offx, offy], flip]

def preprocess(self, im, allobj = None):
    """
    Takes an image, return it as a numpy tensor that is readily
    to be fed into tfnet. If there is an accompanied annotation (allobj),
    meaning this preprocessing is serving the train process, then this
    image will be transformed with random noise to augment training data,
    using scale, translation, flipping and recolor. The accompanied
    parsed annotation (allobj) will also be modified accordingly.
    """
    if type(im) is not np.ndarray:
        im = cv2.imread(im)

    if allobj is not None: # in training mode
        result = imcv2_affine_trans(im)
        im, dims, trans_param = result
        scale, offs, flip = trans_param
        for obj in allobj:
            _fix(obj, dims, scale, offs)
            if not flip: continue
            obj_1_ =  obj[1]
            obj[1] = dims[0] - obj[3]
            obj[3] = dims[0] - obj_1_
        im = imcv2_recolor(im)

    im = self.resize_input(im)
    if allobj is None: return im
    return im#, np.array(im) # for unit testing

这些是与训练过程中的数据增强相关的代码。如果我的理论正确的话,我想请教一下你的意见。如果是这样的话,我如何禁用翻转操作,而保留其余的数据增强?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-06-27 15:34:53

我也有同样的问题,但我找到了答案。我们可以在darkflow\darkflow\net\yolo\predict.py上看到"from ...utils.im_transform import imcv2_recolor,imcv2_affine_trans“

在darkflow\darkflow\utils\im_transform.py中定义了函数imcv2_affine_trans

因此,我们可以禁用翻转,如下所示。

代码语言:javascript
复制
def imcv2_affine_trans(im):
   # Scale and translate
   h, w, c = im.shape
   scale = np.random.uniform() / 10. + 1.
   max_offx = (scale-1.) * w
   max_offy = (scale-1.) * h
   offx = int(np.random.uniform() * max_offx)
   offy = int(np.random.uniform() * max_offy)

   im = cv2.resize(im, (0,0), fx = scale, fy = scale)
   im = im[offy : (offy + h), offx : (offx + w)]
   flip = 0#np.random.binomial(1, .5)
   #if flip: im = cv2.flip(im, 1)
   return im, [w, h, c], [scale, [offx, offy], flip]

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60684230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档