简体   繁体   中英

How do i disable the flipping of images on Darkflow YOLOv2?

I am currently training a model based on orientation. I want to be able to train a model that can tell the estimated orientation of an object. Currently, i am at around 1000 epoch and the accuracy is not good. My theory is that the flip operation seems to lead to an inaccurate model as an orientation of 90 degrees may be flipped to -90 degrees. Hence, the 2 separate classes would be confused with one another.

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

These are the codes related to the data augmentation during training. I would like to consult your advice if my theory is right? And if so, how can i disable the flip operation but keep the rest of the data augmentation? Thank you!

I had the same problem, but I found the answer. We can see "from ...utils.im_transform import imcv2_recolor, imcv2_affine_trans" on the darkflow\\darkflow\\net\\yolo\\predict.py

The function, imcv2_affine_trans is defined in darkflow\\darkflow\\utils\\im_transform.py

So we can disable flip like below.

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]

hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM