首页 > 解决方案 > 如何禁用 Darkflow YOLOv2 上的图像翻转?

问题描述

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

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

这些是与训练期间的数据增强相关的代码。如果我的理论是正确的,我想咨询你的意见?如果是这样,我如何禁用翻转操作但保留其余的数据扩充?谢谢!

标签: pythonyolodata-augmentationdarkflow

解决方案


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

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

所以我们可以像下面那样禁用翻转。

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]

希望这可以帮助!


推荐阅读